glenn-jocher commited on
Commit
d3dad42
1 Parent(s): 19c8b2c

Move generated *.json annotations into runs/test (#1209)

Browse files

* Move generated *.json annotations into runs/test

* list and string robust

Files changed (1) hide show
  1. test.py +6 -6
test.py CHANGED
@@ -222,11 +222,11 @@ def test(data,
222
 
223
  # Save JSON
224
  if save_json and len(jdict):
225
- f = 'detections_val2017_%s_results.json' % \
226
- (weights.split(os.sep)[-1].replace('.pt', '') if isinstance(weights, str) else '') # filename
227
- print('\nCOCO mAP with pycocotools... saving %s...' % f)
228
- with open(f, 'w') as file:
229
- json.dump(jdict, file)
230
 
231
  try: # https://github.com/cocodataset/cocoapi/blob/master/PythonAPI/pycocoEvalDemo.ipynb
232
  from pycocotools.coco import COCO
@@ -234,7 +234,7 @@ def test(data,
234
 
235
  imgIds = [int(Path(x).stem) for x in dataloader.dataset.img_files]
236
  cocoGt = COCO(glob.glob('../coco/annotations/instances_val*.json')[0]) # initialize COCO ground truth api
237
- cocoDt = cocoGt.loadRes(f) # initialize COCO pred api
238
  cocoEval = COCOeval(cocoGt, cocoDt, 'bbox')
239
  cocoEval.params.imgIds = imgIds # image IDs to evaluate
240
  cocoEval.evaluate()
 
222
 
223
  # Save JSON
224
  if save_json and len(jdict):
225
+ w = Path(weights[0] if isinstance(weights, list) else weights).stem if weights is not None else '' # weights
226
+ file = save_dir / f"detections_val2017_{w}_results.json" # predicted annotations file
227
+ print('\nCOCO mAP with pycocotools... saving %s...' % file)
228
+ with open(file, 'w') as f:
229
+ json.dump(jdict, f)
230
 
231
  try: # https://github.com/cocodataset/cocoapi/blob/master/PythonAPI/pycocoEvalDemo.ipynb
232
  from pycocotools.coco import COCO
 
234
 
235
  imgIds = [int(Path(x).stem) for x in dataloader.dataset.img_files]
236
  cocoGt = COCO(glob.glob('../coco/annotations/instances_val*.json')[0]) # initialize COCO ground truth api
237
+ cocoDt = cocoGt.loadRes(str(file)) # initialize COCO pred api
238
  cocoEval = COCOeval(cocoGt, cocoDt, 'bbox')
239
  cocoEval.params.imgIds = imgIds # image IDs to evaluate
240
  cocoEval.evaluate()