glenn-jocher
commited on
Increase pycocotools robustness (#1396)
Browse files
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 |
-
|
244 |
-
|
245 |
-
|
|
|
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 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
map, map50 =
|
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 |
|