@@ -38,7 +38,7 @@ def test(data,
3838 plots = True ,
3939 wandb_logger = None ,
4040 compute_loss = None ,
41- half_precision = True ,
41+ half = True ,
4242 opt = None ):
4343 # Initialize/load model and set device
4444 training = model is not None
@@ -63,7 +63,7 @@ def test(data,
6363 # model = nn.DataParallel(model)
6464
6565 # Half
66- half = device .type != 'cpu' and half_precision # half precision only supported on CUDA
66+ half & = device .type != 'cpu' # half precision only supported on CUDA
6767 if half :
6868 model .half ()
6969
@@ -95,20 +95,22 @@ def test(data,
9595 names = {k : v for k , v in enumerate (model .names if hasattr (model , 'names' ) else model .module .names )}
9696 coco91class = coco80_to_coco91_class ()
9797 s = (
'%20s' + '%11s' * 6 )
% (
'Class' ,
'Images' ,
'Labels' ,
'P' ,
'R' ,
'[email protected] ' ,
'[email protected] :.95' )
98- p , r , f1 , mp , mr , map50 , map , t0 , t1 = 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.
98+ p , r , f1 , mp , mr , map50 , map , t0 , t1 , t2 = 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.
9999 loss = torch .zeros (3 , device = device )
100100 jdict , stats , ap , ap_class , wandb_images = [], [], [], [], []
101101 for batch_i , (img , targets , paths , shapes ) in enumerate (tqdm (dataloader , desc = s )):
102+ t_ = time_synchronized ()
102103 img = img .to (device , non_blocking = True )
103104 img = img .half () if half else img .float () # uint8 to fp16/32
104105 img /= 255.0 # 0 - 255 to 0.0 - 1.0
105106 targets = targets .to (device )
106107 nb , _ , height , width = img .shape # batch size, channels, height, width
108+ t = time_synchronized ()
109+ t0 += t - t_
107110
108111 # Run model
109- t = time_synchronized ()
110112 out , train_out = model (img , augment = augment ) # inference and training outputs
111- t0 += time_synchronized () - t
113+ t1 += time_synchronized () - t
112114
113115 # Compute loss
114116 if compute_loss :
@@ -119,7 +121,7 @@ def test(data,
119121 lb = [targets [targets [:, 0 ] == i , 1 :] for i in range (nb )] if save_hybrid else [] # for autolabelling
120122 t = time_synchronized ()
121123 out = non_max_suppression (out , conf_thres , iou_thres , labels = lb , multi_label = True , agnostic = single_cls )
122- t1 += time_synchronized () - t
124+ t2 += time_synchronized () - t
123125
124126 # Statistics per image
125127 for si , pred in enumerate (out ):
@@ -236,9 +238,10 @@ def test(data,
236238 print (pf % (names [c ], seen , nt [c ], p [i ], r [i ], ap50 [i ], ap [i ]))
237239
238240 # Print speeds
239- t = tuple (x / seen * 1E3 for x in (t0 , t1 , t0 + t1 )) + ( imgsz , imgsz , batch_size ) # tuple
241+ t = tuple (x / seen * 1E3 for x in (t0 , t1 , t2 )) # speeds per image
240242 if not training :
241- print ('Speed: %.1f/%.1f/%.1f ms inference/NMS/total per %gx%g image at batch-size %g' % t )
243+ shape = (batch_size , 3 , imgsz , imgsz )
244+ print (f'Speed: %.1fms pre-process, %.1fms inference, %.1fms NMS per image at shape { shape } ' % t )
242245
243246 # Plots
244247 if plots :
@@ -327,24 +330,25 @@ def test(data,
327330 save_txt = opt .save_txt | opt .save_hybrid ,
328331 save_hybrid = opt .save_hybrid ,
329332 save_conf = opt .save_conf ,
330- half_precision = opt .half ,
333+ half = opt .half ,
331334 opt = opt
332335 )
333336
334337 elif opt .task == 'speed' : # speed benchmarks
335- for w in opt .weights :
336- test (opt .data , w , opt .batch_size , opt .img_size , 0.25 , 0.45 , save_json = False , plots = False , opt = opt )
338+ for w in opt .weights if isinstance (opt .weights , list ) else [opt .weights ]:
339+ test (opt .data , w , opt .batch_size , opt .img_size , 0.25 , 0.45 , save_json = False , plots = False , half = True ,
340+ opt = opt )
337341
338342 elif opt .task == 'study' : # run over a range of settings and save/plot
339343 # python test.py --task study --data coco.yaml --iou 0.7 --weights yolov5s.pt yolov5m.pt yolov5l.pt yolov5x.pt
340344 x = list (range (256 , 1536 + 128 , 128 )) # x axis (image sizes)
341- for w in opt .weights :
345+ for w in opt .weights if isinstance ( opt . weights , list ) else [ opt . weights ] :
342346 f = f'study_{ Path (opt .data ).stem } _{ Path (w ).stem } .txt' # filename to save to
343347 y = [] # y axis
344348 for i in x : # img-size
345349 print (f'\n Running { f } point { i } ...' )
346350 r , _ , t = test (opt .data , w , opt .batch_size , i , opt .conf_thres , opt .iou_thres , opt .save_json ,
347- plots = False , opt = opt )
351+ plots = False , half = True , opt = opt )
348352 y .append (r + t ) # results and times
349353 np .savetxt (f , y , fmt = '%10.4g' ) # save
350354 os .system ('zip -r study.zip study_*.txt' )
0 commit comments