glenn-jocher commited on
Commit
b40852d
1 Parent(s): 6c01a5b

update test.py

Browse files
Files changed (1) hide show
  1. test.py +5 -6
test.py CHANGED
@@ -126,13 +126,13 @@ def test(data,
126
  # Append to pycocotools JSON dictionary
127
  if save_json:
128
  # [{"image_id": 42, "category_id": 18, "bbox": [258.15, 41.29, 348.26, 243.78], "score": 0.236}, ...
129
- image_id = int(Path(paths[si]).stem.split('_')[-1])
130
  box = pred[:, :4].clone() # xyxy
131
  scale_coords(img[si].shape[1:], box, shapes[si][0], shapes[si][1]) # to original shape
132
  box = xyxy2xywh(box) # xywh
133
  box[:, :2] -= box[:, 2:] / 2 # xy center to top-left corner
134
  for p, b in zip(pred.tolist(), box.tolist()):
135
- jdict.append({'image_id': image_id,
136
  'category_id': coco91class[int(p[5])],
137
  'bbox': [round(x, 3) for x in b],
138
  'score': round(p[4], 5)})
@@ -200,8 +200,7 @@ def test(data,
200
  print('Speed: %.1f/%.1f/%.1f ms inference/NMS/total per %gx%g image at batch-size %g' % t)
201
 
202
  # Save JSON
203
- if save_json and map50 and len(jdict):
204
- imgIds = [int(Path(x).stem.split('_')[-1]) for x in dataloader.dataset.img_files]
205
  f = 'detections_val2017_%s_results.json' % \
206
  (weights.split(os.sep)[-1].replace('.pt', '') if isinstance(weights, str) else '') # filename
207
  print('\nCOCO mAP with pycocotools... saving %s...' % f)
@@ -212,6 +211,7 @@ def test(data,
212
  from pycocotools.coco import COCO
213
  from pycocotools.cocoeval import COCOeval
214
 
 
215
  cocoGt = COCO(glob.glob('../coco/annotations/instances_val*.json')[0]) # initialize COCO ground truth api
216
  cocoDt = cocoGt.loadRes(f) # initialize COCO pred api
217
  cocoEval = COCOeval(cocoGt, cocoDt, 'bbox')
@@ -221,8 +221,7 @@ def test(data,
221
  cocoEval.summarize()
222
  map, map50 = cocoEval.stats[:2] # update results ([email protected]:0.95, [email protected])
223
  except:
224
- print('WARNING: pycocotools must be installed with numpy==1.17 to run correctly. '
225
- 'See https://github.com/cocodataset/cocoapi/issues/356')
226
 
227
  # Return results
228
  model.float() # for training
 
126
  # Append to pycocotools JSON dictionary
127
  if save_json:
128
  # [{"image_id": 42, "category_id": 18, "bbox": [258.15, 41.29, 348.26, 243.78], "score": 0.236}, ...
129
+ image_id = Path(paths[si]).stem
130
  box = pred[:, :4].clone() # xyxy
131
  scale_coords(img[si].shape[1:], box, shapes[si][0], shapes[si][1]) # to original shape
132
  box = xyxy2xywh(box) # xywh
133
  box[:, :2] -= box[:, 2:] / 2 # xy center to top-left corner
134
  for p, b in zip(pred.tolist(), box.tolist()):
135
+ jdict.append({'image_id': int(image_id) if image_id.isnumeric() else image_id,
136
  'category_id': coco91class[int(p[5])],
137
  'bbox': [round(x, 3) for x in b],
138
  'score': round(p[4], 5)})
 
200
  print('Speed: %.1f/%.1f/%.1f ms inference/NMS/total per %gx%g image at batch-size %g' % t)
201
 
202
  # Save JSON
203
+ if save_json and len(jdict):
 
204
  f = 'detections_val2017_%s_results.json' % \
205
  (weights.split(os.sep)[-1].replace('.pt', '') if isinstance(weights, str) else '') # filename
206
  print('\nCOCO mAP with pycocotools... saving %s...' % f)
 
211
  from pycocotools.coco import COCO
212
  from pycocotools.cocoeval import COCOeval
213
 
214
+ imgIds = [int(Path(x).stem) for x in dataloader.dataset.img_files]
215
  cocoGt = COCO(glob.glob('../coco/annotations/instances_val*.json')[0]) # initialize COCO ground truth api
216
  cocoDt = cocoGt.loadRes(f) # initialize COCO pred api
217
  cocoEval = COCOeval(cocoGt, cocoDt, 'bbox')
 
221
  cocoEval.summarize()
222
  map, map50 = cocoEval.stats[:2] # update results ([email protected]:0.95, [email protected])
223
  except:
224
+ print('pycocotools not evaluated')
 
225
 
226
  # Return results
227
  model.float() # for training