@@ -86,7 +86,7 @@ def test(data,
86
86
p , r , f1 , mp , mr , map50 , map , t0 , t1 = 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.
87
87
loss = torch .zeros (3 , device = device )
88
88
jdict , stats , ap , ap_class = [], [], [], []
89
- for batch_i , (img , targets , paths , shapes ) in enumerate (tqdm ( dataloader , desc = s ) ):
89
+ for batch_i , (img , targets , paths , shapes ) in enumerate (dataloader ):
90
90
img = img .to (device , non_blocking = True )
91
91
img = img .half () if half else img .float () # uint8 to fp16/32
92
92
img /= 255.0 # 0 - 255 to 0.0 - 1.0
@@ -135,20 +135,6 @@ def test(data,
135
135
# Clip boxes to image bounds
136
136
clip_coords (pred , (height , width ))
137
137
138
- # Append to pycocotools JSON dictionary
139
- if save_json :
140
- # [{"image_id": 42, "category_id": 18, "bbox": [258.15, 41.29, 348.26, 243.78], "score": 0.236}, ...
141
- image_id = Path (paths [si ]).stem
142
- box = pred [:, :4 ].clone () # xyxy
143
- scale_coords (img [si ].shape [1 :], box , shapes [si ][0 ], shapes [si ][1 ]) # to original shape
144
- box = xyxy2xywh (box ) # xywh
145
- box [:, :2 ] -= box [:, 2 :] / 2 # xy center to top-left corner
146
- for p , b in zip (pred .tolist (), box .tolist ()):
147
- jdict .append ({'image_id' : int (image_id ) if image_id .isnumeric () else image_id ,
148
- 'category_id' : coco91class [int (p [5 ])],
149
- 'bbox' : [round (x , 3 ) for x in b ],
150
- 'score' : round (p [4 ], 5 )})
151
-
152
138
# Assign all predictions as incorrect
153
139
correct = torch .zeros (pred .shape [0 ], niou , dtype = torch .bool , device = device )
154
140
if nl :
@@ -211,31 +197,7 @@ def test(data,
211
197
# Print speeds
212
198
t = tuple (x / seen * 1E3 for x in (t0 , t1 , t0 + t1 )) + (imgsz , imgsz , batch_size ) # tuple
213
199
if not training :
214
- print ('Speed: %.1f/%.1f/%.1f ms inference/NMS/total per %gx%g image at batch-size %g' % t )
215
-
216
- # Save JSON
217
- if save_json and len (jdict ):
218
- f = 'detections_val2017_%s_results.json' % \
219
- (weights .split (os .sep )[- 1 ].replace ('.pt' , '' ) if isinstance (weights , str ) else '' ) # filename
220
- print ('\n COCO mAP with pycocotools... saving %s...' % f )
221
- with open (f , 'w' ) as file :
222
- json .dump (jdict , file )
223
-
224
- try : # https://github.com/cocodataset/cocoapi/blob/master/PythonAPI/pycocoEvalDemo.ipynb
225
- from pycocotools .coco import COCO
226
- from pycocotools .cocoeval import COCOeval
227
-
228
- imgIds = [int (Path (x ).stem ) for x in dataloader .dataset .img_files ]
229
- cocoGt = COCO (glob .glob ('../coco/annotations/instances_val*.json' )[0 ]) # initialize COCO ground truth api
230
- cocoDt = cocoGt .loadRes (f ) # initialize COCO pred api
231
- cocoEval = COCOeval (cocoGt , cocoDt , 'bbox' )
232
- cocoEval .params .imgIds = imgIds # image IDs to evaluate
233
- cocoEval .evaluate ()
234
- cocoEval .accumulate ()
235
- cocoEval .summarize ()
236
- map ,
map50 = cocoEval .
stats [:
2 ]
# update results ([email protected] :0.95, [email protected] )
237
- except Exception as e :
238
- print ('ERROR: pycocotools unable to run: %s' % e )
200
+ print ('Speed: %.1f/%.1f/%.1f ms inference/NMS/total per %gx%g image at batch-size %g' % t )
239
201
240
202
# Return results
241
203
model .float () # for training
0 commit comments