-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.py
128 lines (96 loc) · 4.06 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/python3
# coding=utf-8
import os
import sys
sys.path.insert(0, '../')
sys.dont_write_bytecode = True
import cv2
import numpy as np
import matplotlib.pyplot as plt
plt.ion()
import torch
from skimage import io
from PIL import Image
from torch.utils.data import DataLoader
import dataset
from MEUNet import MEUNet
def normPRED(d):
ma = torch.max(d+1)
mi = torch.min(d+1)
dn = (d+1 - mi) / (ma - mi)
return dn
def save_output(image_name, pred, d_dir):
predict = pred
predict = predict.squeeze()
predict_np = predict.cpu().data.numpy()
im = Image.fromarray(predict_np * 255).convert('RGB')
img_name = image_name.split(os.sep)[-1]
image = io.imread(image_name)
imo = im.resize((image.shape[1], image.shape[0]), resample=Image.BILINEAR)
pb_np = np.array(imo)
aaa = img_name.split(".")
bbb = aaa[0:-1]
imidx = bbb[0]
for i in range(1, len(bbb)):
imidx = imidx + "." + bbb[i]
imo.save(d_dir + imidx + '.png')
class Test(object):
def __init__(self, Dataset, Network, Path,image_file_name):
## dataset
self.model_name = 'U3Net_epoch_596_itr_394020_train_0.090393'
self.cfg = Dataset.Config(datapath=Path,image_file_name=image_file_name, snapshot='./out/'+self.model_name+'.pth', mode='test')
self.net = Network(self.cfg)
self.data = Dataset.Data(self.cfg)
self.loader = DataLoader(self.data, batch_size=1, shuffle=False, num_workers=8)
## network
self.net.train(False)
self.net.cuda()
def save_prediction(self,dataset_name):
with torch.no_grad():
for image, (H, W), name in self.loader:
image, shape = image.cuda().float(), (H, W)
print("inferencing:%s"%name)
d_edge, d_united, d1, d2, d3, d4 = self.net(image)
# add
# pred = torch.sigmoid(d_united[0,0])
pred = torch.sigmoid(d_united[0,0])
#can be canceled
pred = normPRED(pred)
pred = pred.cpu().numpy() * 255
#
head = './save_datas/' + self.model_name+'/'+dataset_name+ os.sep
if not os.path.exists(head):
os.makedirs(head, exist_ok=True)
cv2.imwrite(head + name[0] + '.png', np.round(pred))
# edge prediction
# pred = torch.sigmoid(d_edge[0, 0]).cpu().numpy() * 255
# head = './save_datas/' +self.model_name+'/edge/'+dataset_name+ os.sep
# if not os.path.exists(head):
# os.makedirs(head, exist_ok=True)
# cv2.imwrite(head + '/' + name[0] + '.png', np.round(pred))
if __name__ == '__main__':
for dataset_name in ['DUTS-TE', 'DUT-OMROM', 'ECSSD', 'HKU-IS', 'PASCAL-S', 'SOD']:
image_root_dir = "/home/bianyetong/datasets"
# image_dir = os.path.join(os.getcwd(), 'test_data', 'test_images')
# prediction_dir = os.path.join(os.getcwd(), 'test_data', model_name + '_results' + os.sep)
# model_dir = os.path.join(os.getcwd(), 'saved_models', model_name, model_name + '.pth')
if dataset_name == 'DUTS-TE':
image_dir = os.path.join(image_root_dir, 'DUTS/DUTS-TE')
image_file_name = '/Image'
elif dataset_name == 'DUT-OMROM':
image_dir = os.path.join(image_root_dir, 'DUT-OMROM')
image_file_name = '/Image'
elif dataset_name == 'ECSSD':
image_dir = os.path.join(image_root_dir, 'ECSSD')
image_file_name = '/Image'
elif dataset_name == 'HKU-IS':
image_dir = os.path.join(image_root_dir, 'HKU-IS')
image_file_name = '/Img'
elif dataset_name == 'PASCAL-S':
image_dir = os.path.join(image_root_dir, 'PASCAL-S')
image_file_name = '/Imgs'
elif dataset_name == 'SOD':
image_dir = os.path.join(image_root_dir, 'SOD')
image_file_name = '/Imgs/Imgs'
t = Test(dataset, MEUNet, image_dir, image_file_name)
t.save_prediction(dataset_name)