-
Notifications
You must be signed in to change notification settings - Fork 8
/
inference.py
542 lines (445 loc) · 20.8 KB
/
inference.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
from numpy.lib import tile
from options import CustomOptions
from models.eval_network import EvalKpSFR
from models.inference_core import InferenceCore
from worldcup_test_loader import WorldcupTestDataset
from ts_worldcup_test_loader import MainTestDataset
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
import numpy as np
import os
import os.path as osp
import time
from PIL import Image
from tqdm import tqdm
import shutil
import cv2
import matplotlib.pyplot as plt
import utils
import metrics
import skimage.segmentation as ss
# Get input arguments
opt = CustomOptions(train=False)
opt = opt.parse()
# Log on tensorboard
# writer = SummaryWriter('runs/' + opt.name)
# Setup GPU
os.environ['CUDA_VISIBLE_DEVICES'] = opt.gpu_ids
print('CUDA Visible Devices: %s' % opt.gpu_ids)
device = torch.device('cuda:0')
print('device: %s' % device)
def calc_euclidean_distance(a, b, _norm=np.linalg.norm, axis=None):
return _norm(a - b, axis=axis)
def my_mseloss(gt, pred):
return torch.mean(torch.square(pred - gt))
def postprocessing(scores, pred, target, num_classes, nms_thres):
# TODO: decode the heatmaps into keypoint sets using non-maximum suppression
pred_cls_dict = {k: [] for k in range(1, num_classes)}
for cls in range(1, num_classes):
pred_inds = pred == cls
# implies the current class does not appear in this heatmaps
if not np.any(pred_inds):
continue
values = scores[pred_inds]
max_score = values.max()
max_index = values.argmax()
val_inds = np.where(values == max_score)[0]
indices = np.where(pred_inds)
coords = list(zip(indices[0], indices[1]))
l = []
for idx in range(val_inds.shape[0]):
l.append(coords[val_inds[idx]])
l = np.array(l).mean(axis=0).astype(np.int64)
# the only keypoint with max confidence is greater than threshold or not
if max_score >= nms_thres:
pred_cls_dict[cls].append(max_score)
pred_cls_dict[cls].append(l)
gt_cls_dict = {k: [] for k in range(1, num_classes)}
for cls in range(1, num_classes):
gt_inds = target == cls
# implies the current class does not appear in this heatmaps
if not np.any(gt_inds):
continue
coords = np.argwhere(gt_inds)[0]
# coordinate order is (y, x)
gt_cls_dict[cls].append((coords[0], coords[1]))
return gt_cls_dict, pred_cls_dict
def calc_keypts_metrics(gt_cls_dict, pred_cls_dict, pr_thres):
num_gt_pos = 0
num_pred_pos = 0
num_both_keypts_appear = 0
tp = 0
mse_loss = 0.0
for (gk, gv), (pk, pv) in zip(gt_cls_dict.items(), pred_cls_dict.items()):
if gv:
num_gt_pos += 1
if pv:
num_pred_pos += 1
if gv and pv:
num_both_keypts_appear += 1
mse_loss += my_mseloss(torch.FloatTensor(gv[0]),
torch.FloatTensor(pv[1]))
if calc_euclidean_distance(np.array(gv[0]), np.array(pv[1])) <= pr_thres:
tp += 1
if num_both_keypts_appear == 0:
return 0.0, 0.0, 0.0
return tp / num_pred_pos, tp / num_gt_pos, mse_loss / num_both_keypts_appear
def class_mapping(rgb):
# TODO: class mapping
template = utils.gen_template_grid() # grid shape (91, 3), (x, y, label)
src_pts = rgb.copy()
cls_map_pts = []
for ind, elem in enumerate(src_pts):
coords = np.where(elem[2] == template[:, 2])[0] # find correspondence
cls_map_pts.append(template[coords[0]])
dst_pts = np.array(cls_map_pts, dtype=np.float32)
return src_pts[:, :2], dst_pts[:, :2]
def test():
num_classes = 92
# num_objects = opt.num_objects
num_objects = 91
non_local = bool(opt.use_non_local)
model_archi = opt.model_archi
# Initialize models
eval_model = EvalKpSFR(model_archi=model_archi,
num_objects=num_objects, non_local=non_local).to(device)
if opt.train_stage == 0:
# Load testing data
print('Loading public worldcup testing data...')
test_dataset = WorldcupTestDataset(
root=opt.public_worldcup_root,
data_type=opt.testset,
mode='test',
num_objects=num_objects,
target_image=opt.target_image
)
test_loader = DataLoader(
dataset=test_dataset,
batch_size=1,
shuffle=False,
num_workers=4
)
elif opt.train_stage == 1:
# Load testing data
print('Loading time sequence worldcup testing data...')
test_dataset = MainTestDataset(
root=opt.custom_worldcup_root,
data_type=opt.testset, # test
mode='test',
num_objects=num_objects,
target_video=opt.target_video
)
test_loader = DataLoader(
dataset=test_dataset,
batch_size=1,
shuffle=False,
num_workers=4
)
total_epoch = opt.train_epochs
# Set data path
denorm = utils.UnNormalize(
mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]
)
exp_name_path = osp.join(opt.checkpoints_dir, opt.name)
test_visual_dir = osp.join(exp_name_path, 'imgs', 'test_visual')
if osp.exists(test_visual_dir):
print(f'Remove directory: {test_visual_dir}')
shutil.rmtree(test_visual_dir)
print(f'Create directory: {test_visual_dir}')
os.makedirs(test_visual_dir, exist_ok=True)
iou_visual_dir = osp.join(test_visual_dir, 'iou')
os.makedirs(iou_visual_dir, exist_ok=True)
homo_visual_dir = osp.join(exp_name_path, 'homography')
os.makedirs(homo_visual_dir, exist_ok=True)
field_model = Image.open(
osp.join(opt.template_path, 'worldcup_field_model.png'))
# TODO: Load pretrained model or resume training
if len(opt.ckpt_path) > 0:
load_weights_path = opt.ckpt_path
print('Loading weights: ', load_weights_path)
assert osp.isfile(load_weights_path), 'Error: no checkpoints found'
checkpoint = torch.load(load_weights_path, map_location=device)
eval_model.load_state_dict(checkpoint['model_state_dict'])
epoch = checkpoint['epoch']
print('Checkpoint Epoch:', epoch)
print("Testing...")
eval_model.eval()
avg_batch_l2loss = 0.0
avg_precision_list = []
avg_recall_list = []
avg_iou_part_list = []
avg_iou_whole_list = []
avg_proj_error_list = []
avg_reproj_error_list = []
test_progress_bar = tqdm(
enumerate(test_loader), total=len(test_loader), leave=False)
test_progress_bar.set_description(
f'Epoch: {epoch}/{total_epoch}')
total_process_time = 0
total_frames = 0
with torch.no_grad():
for step, data in test_progress_bar:
image = data['rgb'].to(device) # b*t*c*h*w
target_dilated_hm = data['target_dilated_hm'][0].to(
device) # k*t*1*h*w
cls_gt = data['cls_gt'][0] # t*h*w
gt_homo = data['gt_homo'][0]
selector = data['selector'][0].to(device) # k:91 or t*k
lookup = data['lookup'][0].to(device) # k:91 or t*k
info = data['info']
k = info['num_objects'][0]
sfp_path = info['single_frame_path'][0]
vid_name = info['name'][0]
torch.cuda.synchronize()
process_begin = time.time()
processor = InferenceCore(eval_model, image, device, k, lookup)
# selector does not use
processor.interact(0, image.shape[1], selector)
size = target_dilated_hm.shape[-2:]
out_masks = torch.zeros((processor.t, 1, *size), device=device)
out_scores = torch.zeros_like(out_masks)
for ti in range(processor.t):
prob = processor.prob[:, ti]
out_scores[ti], out_masks[ti] = torch.max(
prob, dim=0) # 1*h*w
out_masks = out_masks.detach().cpu().numpy()[:, 0] # t*h*w
out_scores = out_scores.detach().cpu().numpy()[:, 0] # t*h*w
image = np.transpose(denorm(image[0]).detach(
).cpu().numpy(), (0, 2, 3, 1)) # t*h*w*c
cls_gt = cls_gt.cpu().numpy() # t*h*w
gt_homo = gt_homo.cpu().numpy()
torch.cuda.synchronize()
total_process_time += time.time() - process_begin
total_frames += out_masks.shape[0]
print(f'Video {step + 1} start processing...')
if opt.train_stage == 0 and opt.target_image:
tmp_step = step
step = int(opt.target_image.pop())
for ti in range(processor.t):
print(f'Current frame is {ti}')
print('scores: ',
out_scores[ti].min(), out_scores[ti].max())
gt_cls_dict, pred_cls_dict = postprocessing(
out_scores[ti], out_masks[ti], cls_gt[ti], num_classes, opt.nms_thres)
# No any point after postprocessing
if not any(pred_cls_dict.values()):
print(f'not keypts at {ti}')
plt.imsave(osp.join(exp_name_path, 'imgs', 'test_%05d_%05d_pred_not_keypts.png' % (
epoch, step)), out_masks[ti], vmin=0, vmax=processor.k)
continue
p, r, loss2 = calc_keypts_metrics(
gt_cls_dict, pred_cls_dict, opt.pr_thres)
if p == 0 and r == 0 and loss2 == 0: # No common point appeared
print(f'diff location at {ti}')
plt.imsave(osp.join(exp_name_path, 'imgs', 'test_%05d_%05d_pred_diff_location.png' % (
epoch, step)), out_masks[ti], vmin=0, vmax=processor.k)
continue
avg_precision_list.append(p)
avg_recall_list.append(r)
avg_batch_l2loss += loss2.detach()
# TODO: show keypoints visual result after postprocessing
pred_keypoints = np.zeros_like(out_masks[0])
pred_rgb = []
for ind, (pk, pv) in enumerate(pred_cls_dict.items()):
if pv:
pred_keypoints[pv[1][0],
pv[1][1]] = pk # (H, W)
# camera view point sets (x, y, label) in rgb domain not heatmap domain
pred_rgb.append(
[pv[1][1] * 4, pv[1][0] * 4, pk])
pred_rgb = np.asarray(
pred_rgb, dtype=np.float32) # (?, 3)
pred_homo = None
if pred_rgb.shape[0] >= 4: # at least four points
src_pts, dst_pts = class_mapping(pred_rgb)
pred_homo, _ = cv2.findHomography(
src_pts.reshape(-1, 1, 2), dst_pts.reshape(-1, 1, 2), cv2.RANSAC, 10)
if pred_homo is not None:
iou_part, gt_part_mask, pred_part_mask, part_merge_result = metrics.calc_iou_part(
pred_homo, gt_homo[ti], image[ti], field_model)
avg_iou_part_list.append(iou_part)
# Bugs still existing
iou_whole, whole_line_merge_result, whole_fill_merge_result = metrics.calc_iou_whole_with_poly(
pred_homo, gt_homo[ti], image[ti], field_model)
avg_iou_whole_list.append(iou_whole)
proj_error = metrics.calc_proj_error(
pred_homo, gt_homo[ti], image[ti], field_model)
avg_proj_error_list.append(proj_error)
reproj_error = metrics.calc_reproj_error(
pred_homo, gt_homo[ti], image[ti], field_model)
avg_reproj_error_list.append(reproj_error)
else:
print(f'pred homo is None at {ti}')
avg_iou_part_list.append(float('nan'))
avg_iou_whole_list.append(float('nan'))
avg_proj_error_list.append(float('nan'))
avg_reproj_error_list.append(float('nan'))
else:
print(f'less than four points at {ti}')
avg_iou_part_list.append(float('nan'))
avg_iou_whole_list.append(float('nan'))
avg_proj_error_list.append(float('nan'))
avg_reproj_error_list.append(float('nan'))
# TODO: save undilated heatmap for each testing video
if opt.train_stage == 0:
vid_path = osp.join(homo_visual_dir, 'worldcup_2014')
os.makedirs(vid_path, exist_ok=True)
vid_path_m = osp.join(
exp_name_path, model_archi, 'worldcup_2014') # for evaluate worldcup test set
elif opt.train_stage == 1:
vid_path = osp.join(
homo_visual_dir, vid_name)
os.makedirs(vid_path, exist_ok=True)
vid_path_m = osp.join(
exp_name_path,
model_archi,
osp.join('80_95', vid_name),
)
os.makedirs(vid_path_m, exist_ok=True)
cv2.imwrite(osp.join(vid_path_m, '%05d.png' %
ti), np.uint8(pred_keypoints))
cv2.imwrite(osp.join(vid_path_m, '%05d_gt.png' %
ti), np.uint8(cls_gt[ti]))
# TODO: save heatmap for visual result
if False:
# if True:
gt_keypoints = ss.expand_labels(
cls_gt[ti], distance=5)
plt.imsave(osp.join(test_visual_dir, 'test_%05d_%05d_gt_seg%02d.png' % (
epoch, step, ti)), gt_keypoints, vmin=0, vmax=processor.k)
plt.imsave(osp.join(test_visual_dir, 'test_%05d_%05d_pred_seg%02d.png' % (
epoch, step, ti)), out_masks[ti], vmin=0, vmax=processor.k)
pred_keypoints = ss.expand_labels(
pred_keypoints, distance=5)
plt.imsave(osp.join(test_visual_dir, 'test_%05d_%05d_pred_keypts%02d.png' % (
epoch, step, ti)), pred_keypoints, vmin=0, vmax=processor.k)
# TODO: save homography
# if False:
if True:
if pred_rgb.shape[0] >= 4 and pred_homo is not None:
# plt.imsave(osp.join(iou_visual_dir, 'test_%05d_%05d_gt_iou_part%02d.png' % (
# epoch, step, ti)), gt_part_mask)
# plt.imsave(osp.join(iou_visual_dir, 'test_%05d_%05d_pred_iou_part%02d.png' % (
# epoch, step, ti)), pred_part_mask)
# plt.imsave(osp.join(iou_visual_dir, 'test_%05d_%05d_merge_iou_part%02d.png' % (
# epoch, step, ti)), part_merge_result)
# plt.imsave(osp.join(iou_visual_dir, 'test_%05d_%05d_line_iou_whole%02d.png' % (
# epoch, step, ti)), whole_line_merge_result)
# plt.imsave(osp.join(iou_visual_dir, 'test_%05d_%05d_fill_iou_whole%02d.png' % (
# epoch, step, ti)), whole_fill_merge_result)
homo_vid_path = osp.join(
homo_visual_dir,
vid_name,
)
np.save(
osp.join(
homo_vid_path, f'test_{epoch:05d}_{step:05d}_gt_homography{ti:02d}.npy'),
gt_homo[ti]
)
np.save(
osp.join(
homo_vid_path, f'test_{epoch:05d}_{step:05d}_pred_homography{ti:02d}.npy'),
pred_homo
)
print(f'Video {step + 1} is done...')
if opt.train_stage == 0 and opt.target_image:
step = tmp_step
del image
del target_dilated_hm
del selector
del lookup
del processor
avg_batch_l2loss /= len(avg_precision_list)
# TODO: log loss
print(f'Testing MSE Loss: {avg_batch_l2loss:.4f}')
# writer.add_scalar('Loss/MSE', avg_batch_l2loss, epoch)
average_precision = np.array(avg_precision_list).mean()
average_recall = np.array(avg_recall_list).mean()
# average_precision = 0
# average_recall = 0
print(
f'Average Precision: {average_precision:.2f}, Recall: {average_recall:.2f}')
# writer.add_scalar(
# 'Metrics/average keypoints precision', average_precision, epoch)
# writer.add_scalar(
# 'Metrics/average keypoints recall', average_recall, epoch)
iou_part_list = np.array(avg_iou_part_list)
iou_whole_list = np.array(avg_iou_whole_list)
# print('IoU part length:', len(iou_part_list),
# 'exclude frame 0:', len(iou_part_list)-10)
mean_iou_part = np.nanmean(iou_part_list)
mean_iou_whole = np.nanmean(iou_whole_list)
# mean_iou_whole = 0
print(
f'Mean IOU part: {mean_iou_part * 100.:.1f}, IOU whole: {mean_iou_whole * 100.:.1f}')
# writer.add_scalar('Metrics/mean IOU part',
# mean_iou_part * 100., epoch)
# writer.add_scalar('Metrics/mean IOU whole',
# mean_iou_whole * 100., epoch)
median_iou_part = np.nanmedian(iou_part_list)
median_iou_whole = np.nanmedian(iou_whole_list)
# median_iou_whole = 0
print(
f'Median IOU part: {median_iou_part * 100.:.1f}, IOU whole: {median_iou_whole * 100.:.1f}')
# writer.add_scalar('Metrics/median IOU part',
# median_iou_part * 100., epoch)
# writer.add_scalar('Metrics/median IOU whole',
# median_iou_whole * 100., epoch)
proj_error_list = np.array(avg_proj_error_list)
reproj_error_list = np.array(avg_reproj_error_list)
mean_proj_error = np.nanmean(proj_error_list)
mean_reproj_error = np.nanmean(reproj_error_list)
print(
f'Mean Projection Error: {mean_proj_error:.2f}, Reprojection Error: {mean_reproj_error:.3f}')
# writer.add_scalar('Metrics/mean Projection Error',
# mean_proj_error, epoch)
# writer.add_scalar(
# 'Metrics/mean Reprojection Error', mean_reproj_error, epoch)
median_proj_error = np.nanmedian(proj_error_list)
median_reproj_error = np.nanmedian(reproj_error_list)
print(
f'Median Projection Error: {median_proj_error:.2f}, Reprojection Error: {median_reproj_error:.3f}')
# writer.add_scalar(
# 'Metrics/median Projection Error', median_proj_error, epoch)
# writer.add_scalar(
# 'Metrics/median Reprojection Error', median_reproj_error, epoch)
with open(osp.join(exp_name_path, 'metrics_%05d.txt' % epoch), 'w') as out_file:
out_file.write(
f'Loading weights: {load_weights_path}')
out_file.write('\n')
out_file.write(
f'Path of single frame prediction: {sfp_path}')
out_file.write('\n')
out_file.write(f'Model architecture: {model_archi}')
out_file.write('\n')
out_file.write(
f'Average Precision: {average_precision:.2f}, Recall: {average_recall:.2f}')
out_file.write('\n')
out_file.write(
f'Mean IOU part: {mean_iou_part * 100.:.1f}, IOU whole: {mean_iou_whole * 100.:.1f}')
out_file.write('\n')
out_file.write(
f'Median IOU part: {median_iou_part * 100.:.1f}, IOU whole: {median_iou_whole * 100.:.1f}')
out_file.write('\n')
out_file.write(
f'Mean Projection Error: {mean_proj_error:.2f}, Reprojection Error: {mean_reproj_error:.3f}')
out_file.write('\n')
out_file.write(
f'Median Projection Error: {median_proj_error:.2f}, Reprojection Error: {median_reproj_error:.3f}')
out_file.write('\n')
print('Total processing time: ', total_process_time)
print('Total processed frames: ', total_frames)
print(f'FPS: {(total_frames / total_process_time):.3f}')
def main():
test()
# writer.flush()
# writer.close()
if __name__ == '__main__':
start_time = time.time()
main()
print(f'Done...Take {(time.time() - start_time):.4f} (sec)')