-
Notifications
You must be signed in to change notification settings - Fork 55
/
test.py
394 lines (343 loc) · 14.9 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
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
import argparse
import json
from torch.utils.data import DataLoader
import cv2
from models import *
from utils.datasets import *
from utils.utils import *
from tiny_classifier.model import TinyModel
def apply_tiny_classifier(x, model, img, paths):
# applies a second stage classifier to yolo outputs
for i, d in enumerate(x): # per image
# print(paths[i])
im0 = cv2.imread(paths[i])
if d is not None and len(d):
d = d.clone()
# Reshape and pad cutouts
b = xyxy2xywh(d[:, :4]) # boxes
b[:, 2:] = b[:, 2:].max(1)[0].unsqueeze(1) # rectangle to square
b[:, 2:] = b[:, 2:] * 1.3 + 30 # pad
d[:, :4] = xywh2xyxy(b).long()
# Rescale boxes from img_size to im0 size
scale_coords(img.shape[2:], d[:, :4], im0.shape) # ori: im0[i].shape
# Classes
pred_cls1 = d[:, 5].long()
ims = []
for j, a in enumerate(d): # per item
# print(j,'===',a)
cutout = im0[int(a[1]):int(a[3]), int(a[0]):int(a[2])]
im = cv2.resize(cutout, (21, 21)) # BGR 224 to 21
# cv2.imwrite('test%i.jpg' % j, cutout)
im = im[:, :, ::-1].transpose(2, 0,
1) # BGR to RGB, to 3x416x416
im = np.ascontiguousarray(im,
dtype=np.float32) # uint8 to float32
im /= 255.0 # 0 - 255 to 0.0 - 1.0
ims.append(im)
pred_cls2 = model(torch.Tensor(ims).to(d.device)).argmax(
1) # classifier prediction
# print(pred_cls1, pred_cls2)
x[i] = x[i][pred_cls1 ==
pred_cls2] # retain matching class detections
return x
def test(
cfg,
data,
weights=None,
batch_size=16,
img_size=416,
conf_thres=0.001,
iou_thres=0.5, # for nms
save_json=False,
model=None,
dataloader=None,
classify=False):
# Initialize/load model and set device
if model is None:
device = torch_utils.select_device(opt.device, batch_size=batch_size)
verbose = opt.task == 'test'
# Remove previous
for f in glob.glob('test_batch*.jpg'):
os.remove(f)
# Initialize model
model = Darknet(cfg, img_size).to(device)
# Load weights
attempt_download(weights)
if weights.endswith('.pt'): # pytorch format
model.load_state_dict(
torch.load(weights, map_location=device)['model'])
else: # darknet format
print(weights)
_ = load_darknet_weights(model, weights)
if torch.cuda.device_count() > 1:
model = nn.DataParallel(model)
else: # called by train.py
device = next(model.parameters()).device # get model device
verbose = False
# Configure run
data = parse_data_cfg(data)
nc = int(data['classes']) # number of classes
path = data['valid'] # path to test images
names = load_classes(data['names']) # class names
iouv = torch.linspace(0.5, 0.95,
10).to(device) # iou vector for [email protected]:0.95
iouv = iouv[0].view(1) # comment for [email protected]:0.95
niou = iouv.numel()
if classify:
modelc = TinyModel(num_classes=2)
modelc.load_state_dict(
torch.load("tiny_classifier/checkpoints/epoch_90_0.955.pt",
map_location='cpu'))
modelc.to(device).eval()
# Dataloader
if dataloader is None:
dataset = LoadImagesAndLabels(path, img_size, batch_size, rect=True)
batch_size = min(batch_size, len(dataset))
dataloader = DataLoader(dataset,
batch_size=batch_size,
num_workers=min([
os.cpu_count(),
batch_size if batch_size > 1 else 0, 8
]),
pin_memory=True,
collate_fn=dataset.collate_fn)
seen = 0
model.eval()
coco91class = coco80_to_coco91_class()
s = ('%20s' + '%10s' * 6) % ('Class', 'Images', 'Targets', 'P', 'R',
'[email protected]', 'F1')
p, r, f1, mp, mr, map, mf1 = 0., 0., 0., 0., 0., 0., 0.
loss = torch.zeros(3)
jdict, stats, ap, ap_class = [], [], [], []
for batch_i, (imgs, targets, paths,
shapes) in enumerate(tqdm(dataloader, desc=s)):
# print("batch:%d, batch img num:%d"%(batch_i,len(imgs)))
imgs = imgs.to(
device).float() / 255.0 # uint8 to float32, 0 - 255 to 0.0 - 1.0
targets = targets.to(device)
_, _, height, width = imgs.shape # batch size, channels, height, width
# Plot images with bounding boxes
if batch_i == 0 and not os.path.exists('test_batch0.png'):
plot_images(imgs=imgs,
targets=targets,
paths=paths,
fname='test_batch0.png')
# Disable gradients
with torch.no_grad():
# Run model
inf_out, train_out = model(imgs) # inference and training outputs
# Compute loss
if hasattr(model, 'hyp'): # if model has loss hyperparameters
loss += compute_loss(train_out, targets,
model)[1][:3].cpu() # GIoU, obj, cls
# Run NMS
output = non_max_suppression(inf_out,
conf_thres=conf_thres,
iou_thres=iou_thres,
multi_cls=False)
if classify:
output = apply_tiny_classifier(
output,
modelc,
imgs,
paths,
)
# Statistics per image
'''
output: (bs, x1, y1, x2, y2, object_conf, conf, class)
'''
for si, pred in enumerate(output):
labels = targets[targets[:, 0] == si, 1:]
nl = len(labels)
tcls = labels[:, 0].tolist() if nl else [] # target class
seen += 1
if pred is None:
if nl:
stats.append((torch.zeros(0, niou, dtype=torch.uint8),
torch.Tensor(), torch.Tensor(), tcls))
continue
# Append to text file
# with open('test.txt', 'a') as file:
# [file.write('%11.5g' * 7 % tuple(x) + '\n') for x in pred]
# Clip boxes to image bounds
clip_coords(pred, (height, width))
# Append to pycocotools JSON dictionary
if save_json:
# [{"image_id": 42, "category_id": 18, "bbox": [258.15, 41.29, 348.26, 243.78], "score": 0.236}, ...
image_id = int(Path(paths[si]).stem.split('_')[-1])
box = pred[:, :4].clone() # xyxy
scale_coords(imgs[si].shape[1:], box, shapes[si][0],
shapes[si][1]) # to original shape
box = xyxy2xywh(box) # xywh
box[:, :2] -= box[:, 2:] / 2 # xy center to top-left corner
for di, d in enumerate(pred):
jdict.append({
'image_id': image_id,
'category_id': coco91class[int(d[5])],
'bbox': [floatn(x, 3) for x in box[di]],
'score': floatn(d[4], 5)
})
# Assign all predictions as incorrect
correct = torch.zeros(len(pred), niou, dtype=torch.uint8)
if nl:
detected = [] # target indices
tcls_tensor = labels[:, 0]
# target boxes
tbox = xywh2xyxy(labels[:, 1:5]) * torch.Tensor([width, height, width, height]).to(device)
# Per target class
for cls in torch.unique(tcls_tensor):
ti = (cls == tcls_tensor).nonzero().view(
-1) # prediction indices
pi = (cls == pred[:, 5]).nonzero().view(
-1) # target indices
# Search for detections
if len(pi):
# Prediction to target ious
ious, i = box_iou(pred[pi, :4], tbox[ti]).max(
1) # best ious, indices
# Append detections
for j in (ious > iouv[0]).nonzero():
d = ti[i[j]] # detected target
if d not in detected:
detected.append(d)
correct[pi[j]] = (
ious[j] > iouv).cpu() # iou_thres is 1xn
if len(
detected
) == nl: # all targets already located in image
break
# Append statistics (correct, conf, pcls, tcls)
stats.append((correct, pred[:, 4].cpu(), pred[:, 5].cpu(), tcls))
# Compute statistics
stats = [np.concatenate(x, 0) for x in zip(*stats)] # to numpy
if len(stats):
p, r, ap, f1, ap_class = ap_per_class(*stats)
if niou > 1:
p, r, ap, f1 = p[:, 0], r[:, 0], ap.mean(
1), ap[:, 0] # [P, R, [email protected]:0.95, [email protected]]
mp, mr, map, mf1 = p.mean(), r.mean(), ap.mean(), f1.mean()
nt = np.bincount(stats[3].astype(np.int64),
minlength=nc) # number of targets per class
else:
nt = torch.zeros(1)
# Print results
pf = '%20s' + '%10.3g' * 6 # print format
print(pf % ('all', seen, nt.sum(), mp, mr, map, mf1))
# Print results per class
if verbose and nc > 1 and len(stats):
for i, c in enumerate(ap_class):
print(pf % (names[c], seen, nt[c], p[i], r[i], ap[i], f1[i]))
# Save JSON
if save_json and map and len(jdict):
imgIds = [
int(Path(x).stem.split('_')[-1])
for x in dataloader.dataset.img_files
]
with open('results.json', 'w') as file:
json.dump(jdict, file)
try:
from pycocotools.coco import COCO
from pycocotools.cocoeval import COCOeval
except:
print(
'WARNING: missing pycocotools package, can not compute official COCO mAP. See requirements.txt.'
)
# https://github.com/cocodataset/cocoapi/blob/master/PythonAPI/pycocoEvalDemo.ipynb
cocoGt = COCO(glob.glob('../coco/annotations/instances_val*.json')
[0]) # initialize COCO ground truth api
cocoDt = cocoGt.loadRes('results.json') # initialize COCO pred api
cocoEval = COCOeval(cocoGt, cocoDt, 'bbox')
cocoEval.params.imgIds = imgIds # [:32] # only evaluate these images
cocoEval.evaluate()
cocoEval.accumulate()
cocoEval.summarize()
mf1, map = cocoEval.stats[:
2] # update to pycocotools results ([email protected]:0.95, [email protected])
# Return results
maps = np.zeros(nc) + map
for i, c in enumerate(ap_class):
maps[c] = ap[i]
return (mp, mr, map, mf1, *(loss / len(dataloader)).tolist()), maps
if __name__ == '__main__':
parser = argparse.ArgumentParser(prog='test.py')
parser.add_argument('--cfg',
type=str,
default='dt-6a-spp.cfg',
help='*.cfg path')
parser.add_argument('--data',
type=str,
default='data/voc.data',
help='*.data path')
parser.add_argument('--weights',
type=str,
default='./weights/best.pt',
help='path to weights file')
parser.add_argument('--batch-size',
type=int,
default=2,
help='size of each image batch')
parser.add_argument('--img-size',
type=int,
default=416,
help='inference size (pixels)')
parser.add_argument('--conf-thres',
type=float,
default=0.1,
help='object confidence threshold')
parser.add_argument('--iou-thres',
type=float,
default=0.3,
help='IOU threshold for NMS')
parser.add_argument('--save-json',
action='store_true',
help='save a cocoapi-compatible JSON results file')
parser.add_argument('--task',
default='test',
help="'test', 'study', 'benchmark'")
parser.add_argument('--device',
default='',
help='device id (i.e. 0 or 0,1) or cpu')
opt = parser.parse_args()
opt.save_json = opt.save_json or any([
x in opt.data for x in ['coco.data', 'coco2014.data', 'coco2017.data']
])
print(opt)
if opt.task == 'test': # task = 'test', 'study', 'benchmark'
# Test
test(opt.cfg, opt.data, opt.weights, opt.batch_size, opt.img_size,
opt.conf_thres, opt.iou_thres, opt.save_json)
elif opt.task == 'benchmark':
# mAPs at 320-608 at conf 0.5 and 0.7
y = []
for i in [320, 416, 512, 608]:
for j in [0.5, 0.7]:
t = time.time()
r = test(opt.cfg, opt.data, opt.weights, opt.batch_size, i,
opt.conf_thres, j, opt.save_json)[0]
y.append(r + (time.time() - t, ))
np.savetxt('benchmark.txt', y,
fmt='%10.4g') # y = np.loadtxt('study.txt')
elif opt.task == 'study':
# Parameter study
y = []
x = np.arange(0.4, 0.9, 0.05)
for i in x:
t = time.time()
r = test(opt.cfg, opt.data, opt.weights, opt.batch_size,
opt.img_size, opt.conf_thres, i, opt.save_json)[0]
y.append(r + (time.time() - t, ))
np.savetxt('study.txt', y, fmt='%10.4g') # y = np.loadtxt('study.txt')
# Plot
fig, ax = plt.subplots(3, 1, figsize=(6, 6))
y = np.stack(y, 0)
ax[0].plot(x, y[:, 2], marker='.', label='[email protected]')
ax[0].set_ylabel('mAP')
ax[1].plot(x, y[:, 3], marker='.', label='[email protected]:0.95')
ax[1].set_ylabel('mAP')
ax[2].plot(x, y[:, -1], marker='.', label='time')
ax[2].set_ylabel('time (s)')
for i in range(3):
ax[i].legend()
ax[i].set_xlabel('iou_thr')
fig.tight_layout()
plt.savefig('study.jpg', dpi=200)