-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtest_from_file.py
380 lines (321 loc) · 14.5 KB
/
test_from_file.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
"""
Serve as a convenient wrapper for validate.py.
"""
import gc
import os
import timeit
import IPython
import torch
import numpy as np
import scipy.misc as misc
import yaml
from torch.utils import data
from ptsemseg.loader import get_loader
from ptsemseg.utils import get_logger
from utils import test_parser
from validate import validate, load_model_and_preprocess
from validate_from_file import load_complete_info_from_dir, final_run_dirs
try:
import pydensecrf.densecrf as dcrf
except:
print(
"Failed to import pydensecrf,\
CRF post-processing will not work"
)
ROOT = '/cvlabdata2/home/kaicheng/pycharm/pytorch-semseg'
M_ROOT = "runs/"
os.chdir(ROOT)
def _save_output(img_path_target, outputs, resized_img, loader, cfg):
"""
Better code reusing.
:param outputs:
:param resized_img:
:return:
"""
if args.dcrf:
unary = outputs.data.cpu().numpy()
unary = np.squeeze(unary, 0)
unary = -np.log(unary)
unary = unary.transpose(2, 1, 0)
w, h, c = unary.shape
unary = unary.transpose(2, 0, 1).reshape(loader.n_classes, -1)
unary = np.ascontiguousarray(unary)
resized_img = np.ascontiguousarray(resized_img)
d = dcrf.DenseCRF2D(w, h, loader.n_classes)
d.setUnaryEnergy(unary)
d.addPairwiseBilateral(sxy=5, srgb=3, rgbim=resized_img, compat=1)
q = d.inference(50)
mask = np.argmax(q, axis=0).reshape(w, h).transpose(1, 0)
decoded_crf = loader.decode_segmap(np.array(mask, dtype=np.uint8))
dcrf_path = os.path.splitext(img_path_target)[0] + "_drf.png"
misc.imsave(dcrf_path, decoded_crf)
print("Dense CRF Processed Mask Saved at: {}".format(dcrf_path))
if isinstance(outputs, torch.Tensor):
pred = np.squeeze(outputs.data.max(1)[1].cpu().numpy(), axis=0)
else:
pred = np.squeeze(outputs, axis=0)
if cfg['model']['arch'] in ["pspnet", "icnet", "icnetBN"]:
pred = pred.astype(np.float32)
# float32 with F mode, resize back to orig_size
pred = misc.imresize(pred, cfg['data']['orig_size'], "nearest", mode="F")
if cfg['data']['dataset'] == 'drive':
pred = misc.imresize(pred, (584, 565), "nearest", mode="F")
decoded = pred
print("Classes found: ", np.unique(pred))
misc.imsave(img_path_target, decoded)
print("Segmentation Mask Saved at: {}".format(img_path_target))
def output_masks_to_files(outputs, loader, resized_img, img_name, output_dir, args, cfg):
"""
Save the prediction in ORIGINAL image size. to output_dir
:param outputs:
:param loader:
:param resized_img:
:param img_name: img_name
:param args:
:param cfg:
:return:
"""
# img_name = os.path.splitext(os.path.basename(img_path))[0]
img_name = img_name.replace('/', '-')
out_path = output_dir
if args.is_recurrent:
for step, output in enumerate(outputs):
img_path_target = os.path.join(out_path, img_name + '_step{}.png'.format(step + 1))
_save_output(img_path_target, output, resized_img, loader, cfg)
else:
img_path_target = os.path.join(out_path, img_name + '.png')
_save_output(img_path_target, outputs, resized_img, loader, cfg)
def _evaluate_from_model(model, images, args, cfg, n_classes, device):
# device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
if args.eval_flip:
# Flip images in numpy (not support in tensor)
flipped_images = np.copy(images.data.cpu().numpy()[:, :, :, ::-1])
flipped_images = torch.from_numpy(flipped_images).float().to(device)
if cfg['model']['arch'] in ['reclast']:
h0 = torch.ones([images.shape[0], args.hidden_size, images.shape[2], images.shape[3]],
dtype=torch.float32, device=device)
outputs = model(images, h0)
outputs_flipped = model(flipped_images, h0)
elif cfg['model']['arch'] in ['recmid']:
W, H = images.shape[2], images.shape[3]
w = int(np.floor(np.floor(np.floor(W / 2) / 2) / 2) / 2)
h = int(np.floor(np.floor(np.floor(H / 2) / 2) / 2) / 2)
h0 = torch.ones([images.shape[0], args.hidden_size, w, h],
dtype=torch.float32, device=device)
outputs = model(images, h0)
outputs_flipped = model(flipped_images, h0)
elif cfg['model']['arch'] in ['dru']:
W, H = images.shape[2], images.shape[3]
w = int(np.floor(np.floor(np.floor(W / 2) / 2) / 2) / 2)
h = int(np.floor(np.floor(np.floor(H / 2) / 2) / 2) / 2)
h0 = torch.ones([images.shape[0], args.hidden_size, w, h],
dtype=torch.float32, device=device)
s0 = torch.ones([images.shape[0], n_classes, W, H],
dtype=torch.float32, device=device)
outputs = model(images, h0, s0)
outputs_flipped = model(flipped_images, h0, s0)
elif cfg['model']['arch'] in ['druvgg16', 'druresnet50']:
W, H = images.shape[2], images.shape[3]
w, h = int(W / 2 ** 4), int(H / 2 ** 4)
if cfg['model']['arch'] in ['druresnet50']:
w, h = int(W / 2 ** 5), int(H / 2 ** 5)
h0 = torch.ones([images.shape[0], args.hidden_size, w, h],
dtype=torch.float32, device=device)
s0 = torch.zeros([images.shape[0], n_classes, W, H],
dtype=torch.float32, device=device)
outputs = model(images, h0, s0)
outputs_flipped = model(flipped_images, h0, s0)
else:
outputs = model(images)
outputs_flipped = model(flipped_images)
if type(outputs) is list:
outputs_list = [output.data.cpu().numpy() for output in outputs]
outputs_flipped_list = [output_flipped.data.cpu().numpy() for output_flipped in outputs_flipped]
outputs_list = [(outputs + outputs_flipped[:, :, :, ::-1]) / 2.0 for
outputs, outputs_flipped in zip(outputs_list, outputs_flipped_list)]
pred = [np.argmax(outputs, axis=1) for outputs in outputs_list]
else:
outputs = outputs.data.cpu().numpy()
outputs_flipped = outputs_flipped.data.cpu().numpy()
outputs = (outputs + outputs_flipped[:, :, :, ::-1]) / 2.0
pred = np.argmax(outputs, axis=1)
else:
if cfg['model']['arch'] in ['reclast']:
h0 = torch.ones([images.shape[0], args.hidden_size, images.shape[2], images.shape[3]],
dtype=torch.float32, device=device)
outputs = model(images, h0)
elif cfg['model']['arch'] in ['recmid']:
W, H = images.shape[2], images.shape[3]
w = int(np.floor(np.floor(np.floor(W / 2) / 2) / 2) / 2)
h = int(np.floor(np.floor(np.floor(H / 2) / 2) / 2) / 2)
h0 = torch.ones([images.shape[0], args.hidden_size, w, h],
dtype=torch.float32, device=device)
outputs = model(images, h0)
elif cfg['model']['arch'] in ['dru']:
W, H = images.shape[2], images.shape[3]
w = int(np.floor(np.floor(np.floor(W / 2) / 2) / 2) / 2)
h = int(np.floor(np.floor(np.floor(H / 2) / 2) / 2) / 2)
h0 = torch.ones([images.shape[0], args.hidden_size, w, h],
dtype=torch.float32, device=device)
s0 = torch.ones([images.shape[0], n_classes, W, H],
dtype=torch.float32, device=device)
outputs = model(images, h0, s0)
elif cfg['model']['arch'] in ['druvgg16', 'druresnet50']:
W, H = images.shape[2], images.shape[3]
w, h = int(W / 2 ** 4), int(H / 2 ** 4)
if cfg['model']['arch'] in ['druresnet50']:
w, h = int(W / 2 ** 5), int(H / 2 ** 5)
h0 = torch.ones([images.shape[0], args.hidden_size, w, h],
dtype=torch.float32, device=device)
s0 = torch.zeros([images.shape[0], n_classes, W, H],
dtype=torch.float32, device=device)
outputs = model(images, h0, s0)
else:
outputs = model(images)
outputs_list = [output.data.cpu().numpy() for output in outputs]
if len(outputs_list)>1:
outputs_list = [output.data.cpu().numpy() for output in outputs]
pred = [np.argmax(outputs, axis=1) for outputs in outputs_list]
else:
outputs = outputs.data.cpu().numpy()
pred = np.argmax(outputs, axis=1)
# if args.eval_flip:
# outputs = model(images)
# # Flip images in numpy (not support in tensor)
# flipped_images = np.copy(images.data.cpu().numpy()[:, :, :, ::-1])
# flipped_images = torch.from_numpy(flipped_images).float().to(args.device)
# outputs_flipped = model(flipped_images)
#
# if args.is_recurrent:
# outputs_list = [output.data.cpu().numpy() for output in outputs]
# outputs_flipped_list = [output_flipped.data.cpu().numpy() for output_flipped in outputs_flipped]
# outputs_list = [(outputs + outputs_flipped[:, :, :, ::-1]) / 2.0 for
# outputs, outputs_flipped in zip(outputs_list, outputs_flipped_list)]
# pred = [np.argmax(outputs, axis=1) for outputs in outputs_list]
# else:
# outputs = outputs.data.cpu().numpy()
# outputs_flipped = outputs_flipped.data.cpu().numpy()
# outputs = (outputs + outputs_flipped[:, :, :, ::-1]) / 2.0
# pred = np.argmax(outputs, axis=1)
# else:
# outputs = model(images)
# if args.is_recurrent:
# pred = [output.data.max(1)[1].cpu().numpy() for output in outputs]
# else:
# pred = outputs.data.max(1)[1].cpu().numpy()
return pred
def test_with_cfg(cfg, args):
logger = get_logger(cfg['logdir'], 'test')
device = torch.device(args.device)
out_path = cfg['eval_out_path']
if not os.path.exists(out_path):
os.makedirs(out_path)
# Setup image
valid_images = [".jpg", ".gif", ".png", ".tga", ".tif", ".tiff"]
data_loader = get_loader(cfg['data']['dataset'])
data_path = cfg['data']['path']
print("Read Input Image from : {}".format(data_path))
print(f"Save the output to : {cfg['eval_out_path']}")
loader = data_loader(
data_path,
is_transform=True, # Return the original image without any augmentation.
split=cfg['data']['test_split'],
img_size=(cfg['data']['img_rows'],
cfg['data']['img_cols']),
)
im_loader = data_loader(
data_path,
is_transform=False, # Return the original image without any augmentation.
split=cfg['data']['test_split'],
img_size=(cfg['data']['img_rows'],
cfg['data']['img_cols']),
)
testloader = data.DataLoader(
loader,
batch_size=1,
num_workers=2
)
roi_only = 'roi' in cfg['data']['dataset']
n_classes = loader.n_classes
# Setup Model
model, model_path = load_model_and_preprocess(cfg, args, n_classes, device)
logger.info(f"Loading model {cfg['model']['arch']} from {model_path}")
# model_file_name = os.path.split(args.model_path)[1]
# model_name = cfg['model']['arch']
# flag_subf = False
# Replace the entire loader, like doing the validation.
# IPython.embed()
with torch.no_grad():
# For all the images in this loader.
for i, (images, labels) in enumerate(testloader):
# TODO DEBUGING here.
# if i > 2:
# break
(orig_img, org_lab) = im_loader[i]
img_name = loader.files[loader.split][i]
if type(img_name) is list:
img_name = img_name[0]
start_time = timeit.default_timer()
images = images.to(device)
n_classes = loader.n_classes
pred = _evaluate_from_model(model, images, args, cfg, n_classes, device)
gt = labels.numpy()
# CHeck the org_lab == labels
# IPython.embed()
if roi_only:
""" Process for ROI, basically, mask the Pred based on GT"""
# IPython.embed()
# if args.is_recurrent:
if type(pred) is list:
for k in range(len(pred)):
pred[k] = np.where(gt == loader.void_classes, loader.void_classes, pred[k])
if cfg['data']['dataset'] == 'drive':
pred[k] = pred[k] + 1
pred[k][gt == 250] = 2
pred[k][pred[k] == 2] = 0
else:
pred = np.where(gt == loader.void_classes, loader.void_classes, pred)
if cfg['data']['dataset'] == 'drive':
pred = pred + 1
pred[gt == 250] = 2
pred[pred == 2] = 0
if type(pred) is list:
for k in range(len(pred)):
pred[k] = np.where(gt == loader.void_classes, loader.void_classes, pred[k])
if cfg['data']['dataset'] == 'drive':
pred[k] = pred[k] + 1
pred[k][gt == 250] = 2
pred[k][pred[k] == 2] = 0
else:
pred = np.where(gt == loader.void_classes, loader.void_classes, pred)
if cfg['data']['dataset'] == 'drive':
pred = pred + 1
pred[gt == 250] = 2
pred[pred == 2] = 0
output_masks_to_files(pred, loader, orig_img, img_name, out_path, args, cfg)
# Other unrelated stuff
if args.measure_time:
elapsed_time = timeit.default_timer() - start_time
if (i + 1) % 50 == 0:
print(
"Inference time \
(iter {0:5d}): {1:3.5f} fps".format(
i + 1, pred[-1].shape[0] / elapsed_time
)
)
def run_test(args, run_dirs):
"""
run multiple validation here.
"""
for i, r_dir in enumerate(run_dirs):
cfg, args = load_complete_info_from_dir(r_dir, args)
# IPython.embed()
test_with_cfg(cfg, args)
cfg = None
gc.collect()
if __name__ == '__main__':
parser = test_parser()
args = parser.parse_args()
run_dirs = final_run_dirs(args)
del args.dataset
run_test(args, run_dirs)