-
Notifications
You must be signed in to change notification settings - Fork 11
/
test.py
212 lines (193 loc) · 6.87 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
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import argparse
import yaml
import math
import numpy as np
import cv2
import random
import torch
import torch.backends.cudnn as cudnn
from data import preproc_for_test
from models.detector import Detector
from utils import (
Timer,
SeqBoxMatcher,
post_process,
get_prior_box,
get_model_complexity_info,
)
from data import COCODetection, VOCDetection, XMLDetection
cudnn.benchmark = True
### For Reproducibility ###
# import random
# SEED = 0
# random.seed(SEED)
# np.random.seed(SEED)
# torch.manual_seed(SEED)
# torch.cuda.manual_seed_all(SEED)
# torch.cuda.empty_cache()
# cudnn.benchmark = False
# cudnn.deterministic = True
# cudnn.enabled = True
### For Reproducibility ###
parser = argparse.ArgumentParser(description="Model Evluation")
parser.add_argument("--config", type=str)
parser.add_argument("--dataset", default="COCO", type=str)
parser.add_argument("--trained_model", default=None, type=str)
args = parser.parse_args()
if __name__ == "__main__":
print("Extracting params...")
with open(args.config, "r") as f:
configs = yaml.safe_load(f)
for config in configs.values():
for key, value in config.items():
setattr(args, key, value)
print(args)
print("Loading dataset...")
if args.dataset == "COCO":
testset = COCODetection([("2017", "val")], args.image_size)
elif args.dataset == "VOC":
testset = VOCDetection([("2007", "test")], args.image_size)
elif args.dataset == "XML":
testset = XMLDetection("val", args.image_size)
else:
raise NotImplementedError("Unkown dataset {}!".format(args.dataset))
print("Loading network...")
model = Detector(
args.image_size,
testset.num_classes,
args.backbone,
args.neck,
mode="normal",
).cuda()
print("Loading weights from trained model...")
if args.trained_model is None:
args.trained_model = os.path.join(
args.save_folder,
"{}_{}_{}_size{}_anchor{}_{}_Final.pth".format(
args.dataset,
args.neck,
args.backbone,
args.image_size,
args.anchor_size,
"MG" if args.mutual_guide else "Retina",
),
)
state_dict = torch.load(args.trained_model)
model.load_state_dict(state_dict["model"], strict=False)
model.deploy()
print("Evaluating model complexity...")
flops, params = get_model_complexity_info(
model, (3, args.image_size, args.image_size)
)
print("{:<30} {:<8}".format("Computational complexity: ", flops))
print("{:<30} {:<8}".format("Number of parameters: ", params))
print("Preparing anchor boxes...")
priors = get_prior_box(args.anchor_size, args.image_size).cuda()
print("Start evaluation...")
num_images = len(testset)
all_boxes = [[None for _ in range(num_images)] for _ in range(testset.num_classes)]
if args.seq_matcher:
box_matcher = SeqBoxMatcher()
if args.vis:
rgbs = dict()
os.makedirs("vis/", exist_ok=True)
os.makedirs("vis/{}/".format(args.dataset), exist_ok=True)
_t = {"im_detect": Timer(), "im_nms": Timer()}
for i in range(num_images):
# prepare image to detect
img = testset.pull_image(i)
scale = torch.Tensor(
[img.shape[1], img.shape[0], img.shape[1], img.shape[0]]
).cuda()
x = torch.from_numpy(preproc_for_test(img, args.image_size)).unsqueeze(0).cuda()
# model inference
torch.cuda.current_stream().synchronize()
_t["im_detect"].tic()
with torch.no_grad():
out = model(x)
torch.cuda.current_stream().synchronize()
detect_time = _t["im_detect"].toc()
# post processing
_t["im_nms"].tic()
(boxes, scores) = post_process(
out,
priors,
scale,
eval_thresh=args.eval_thresh,
nms_thresh=args.nms_thresh,
)
if args.seq_matcher:
boxes, scores = box_matcher.update(boxes, scores)
for j in range(testset.num_classes):
inds = np.where(scores[:, j] > args.eval_thresh)[0]
if len(inds) == 0:
all_boxes[j][i] = np.empty([0, 5])
else:
all_boxes[j][i] = np.hstack((boxes[inds], scores[inds, j : j + 1]))
nms_time = _t["im_nms"].toc()
# vis bounding boxes on images
if args.vis:
for j in range(testset.num_classes):
c_dets = all_boxes[j][i]
for line in c_dets[::-1]:
x1, y1, x2, y2, score = (
int(line[0]),
int(line[1]),
int(line[2]),
int(line[3]),
float(line[4]),
)
if score > 0.25:
if j not in rgbs:
r = random.randint(0, 255)
g = random.randint(0, 255)
b = random.randint(0, 255)
rgbs[j] = [r, g, b]
label = "{}{:.2f}".format(testset.pull_classes()[j], score)
cv2.rectangle(img, (x1, y1), (x2, y2), rgbs[j], 2)
cv2.rectangle(
img, (x1, y1 - 15), (x1 + len(label) * 9, y1), rgbs[j], -1
)
cv2.putText(
img,
label,
(x1, y1 - 5),
cv2.FONT_HERSHEY_SIMPLEX,
0.5,
(255, 255, 255),
1,
cv2.LINE_AA,
)
label = "MutualGuide ({}x{}) : {:.2f}ms on {}".format(
args.image_size,
args.image_size,
detect_time * 1000,
torch.cuda.get_device_name(0),
)
cv2.rectangle(img, (0, 0), (0 + len(label) * 9, 20), [0, 0, 0], -1)
cv2.putText(
img,
label,
(0, 15),
cv2.FONT_HERSHEY_SIMPLEX,
0.5,
(0, 255, 255),
1,
cv2.LINE_AA,
)
filename = "vis/{}/{}.jpg".format(args.dataset, i)
cv2.imwrite(filename, img)
# logging
if i % math.floor(num_images / 10) == 0 and i > 0:
print(
"[{}/{}] model inference = {:.2f}ms, post process = {:.2f}ms,".format(
i, num_images, detect_time * 1000, nms_time * 1000
)
)
_t["im_detect"].clear()
_t["im_nms"].clear()
# evaluation
testset.evaluate_detections(all_boxes)