-
Notifications
You must be signed in to change notification settings - Fork 11
/
test.py
53 lines (48 loc) · 1.65 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import os
import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.utils.data as data
import numpy as np
from PIL import Image
from adamp import AdamP
# my import
from model import AIMnet
from dataset_all import TestData
os.environ["CUDA_VISIBLE_DEVICES"] = "0,1"
bz = 1
model_root = 'pretrained/model.pth'
input_root = 'data/test/benchmarkA/'
save_path = 'result/benchmarkA'
if not os.path.isdir(save_path):
os.makedirs(save_path)
checkpoint = torch.load(model_root)
Mydata_ = TestData(input_root)
data_load = data.DataLoader(Mydata_, batch_size=bz)
model = AIMnet().cuda()
model = nn.DataParallel(model, device_ids=[0, 1])
optimizer = AdamP(model.parameters(), lr=2e-4, betas=(0.9, 0.999), weight_decay=1e-4)
model.load_state_dict(checkpoint['state_dict'])
optimizer.load_state_dict(checkpoint['optimizer_dict'])
epoch = checkpoint['epoch']
model.eval()
print('START!')
if 1:
print('Load model successfully!')
for data_idx, data_ in enumerate(data_load):
data_input, data_la = data_
data_input = Variable(data_input).cuda()
data_la = Variable(data_la).cuda()
print(data_idx)
with torch.no_grad():
result, _ = model(data_input, data_la)
name = Mydata_.A_paths[data_idx].split('/')[5]
print(name)
temp_res = np.transpose(result[0, :].cpu().detach().numpy(), (1, 2, 0))
temp_res[temp_res > 1] = 1
temp_res[temp_res < 0] = 0
temp_res = (temp_res*255).astype(np.uint8)
temp_res = Image.fromarray(temp_res)
temp_res.save('%s/%s' % (save_path, name))
print('result saved!')
print('finished!')