glenn-jocher commited on
Commit
9b0f6e3
·
unverified ·
1 Parent(s): cf58130

Increase pycocotools robustness (#1396)

Browse files
Files changed (1) hide show
  1. test.py +14 -12
test.py CHANGED
@@ -66,6 +66,7 @@ def test(data,
66
 
67
  # Configure
68
  model.eval()
 
69
  with open(data) as f:
70
  data = yaml.load(f, Loader=yaml.FullLoader) # model dict
71
  check_dataset(data) # check
@@ -240,24 +241,25 @@ def test(data,
240
  # Save JSON
241
  if save_json and len(jdict):
242
  w = Path(weights[0] if isinstance(weights, list) else weights).stem if weights is not None else '' # weights
243
- file = save_dir / f"detections_val2017_{w}_results.json" # predicted annotations file
244
- print('\nCOCO mAP with pycocotools... saving %s...' % file)
245
- with open(file, 'w') as f:
 
246
  json.dump(jdict, f)
247
 
248
  try: # https://github.com/cocodataset/cocoapi/blob/master/PythonAPI/pycocoEvalDemo.ipynb
249
  from pycocotools.coco import COCO
250
  from pycocotools.cocoeval import COCOeval
251
 
252
- imgIds = [int(Path(x).stem) for x in dataloader.dataset.img_files]
253
- cocoAnno = COCO(glob.glob('../coco/annotations/instances_val*.json')[0]) # initialize COCO annotations api
254
- cocoPred = cocoAnno.loadRes(str(file)) # initialize COCO pred api
255
- cocoEval = COCOeval(cocoAnno, cocoPred, 'bbox')
256
- cocoEval.params.imgIds = imgIds # image IDs to evaluate
257
- cocoEval.evaluate()
258
- cocoEval.accumulate()
259
- cocoEval.summarize()
260
- map, map50 = cocoEval.stats[:2] # update results (mAP@0.5:0.95, mAP@0.5)
261
  except Exception as e:
262
  print('ERROR: pycocotools unable to run: %s' % e)
263
 
 
66
 
67
  # Configure
68
  model.eval()
69
+ is_coco = data.endswith('coco.yaml') # is COCO dataset
70
  with open(data) as f:
71
  data = yaml.load(f, Loader=yaml.FullLoader) # model dict
72
  check_dataset(data) # check
 
241
  # Save JSON
242
  if save_json and len(jdict):
243
  w = Path(weights[0] if isinstance(weights, list) else weights).stem if weights is not None else '' # weights
244
+ anno_json = glob.glob('../coco/annotations/instances_val*.json')[0] # annotations json
245
+ pred_json = str(save_dir / f"{w}_predictions.json") # predictions json
246
+ print('\nEvaluating pycocotools mAP... saving %s...' % pred_json)
247
+ with open(pred_json, 'w') as f:
248
  json.dump(jdict, f)
249
 
250
  try: # https://github.com/cocodataset/cocoapi/blob/master/PythonAPI/pycocoEvalDemo.ipynb
251
  from pycocotools.coco import COCO
252
  from pycocotools.cocoeval import COCOeval
253
 
254
+ anno = COCO(anno_json) # init annotations api
255
+ pred = anno.loadRes(pred_json) # init predictions api
256
+ eval = COCOeval(anno, pred, 'bbox')
257
+ if is_coco:
258
+ eval.params.imgIds = [int(Path(x).stem) for x in dataloader.dataset.img_files] # image IDs to evaluate
259
+ eval.evaluate()
260
+ eval.accumulate()
261
+ eval.summarize()
262
+ map, map50 = eval.stats[:2] # update results (mAP@0.5:0.95, mAP@0.5)
263
  except Exception as e:
264
  print('ERROR: pycocotools unable to run: %s' % e)
265