forked from ultralytics/yolov5
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Increase pycocotools robustness (ultralytics#1396)
- Loading branch information
1 parent
cf58130
commit 9b0f6e3
Showing
1 changed file
with
14 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,6 +66,7 @@ def test(data, | |
|
||
# Configure | ||
model.eval() | ||
is_coco = data.endswith('coco.yaml') # is COCO dataset | ||
with open(data) as f: | ||
data = yaml.load(f, Loader=yaml.FullLoader) # model dict | ||
check_dataset(data) # check | ||
|
@@ -240,24 +241,25 @@ def test(data, | |
# Save JSON | ||
if save_json and len(jdict): | ||
w = Path(weights[0] if isinstance(weights, list) else weights).stem if weights is not None else '' # weights | ||
file = save_dir / f"detections_val2017_{w}_results.json" # predicted annotations file | ||
print('\nCOCO mAP with pycocotools... saving %s...' % file) | ||
with open(file, 'w') as f: | ||
anno_json = glob.glob('../coco/annotations/instances_val*.json')[0] # annotations json | ||
pred_json = str(save_dir / f"{w}_predictions.json") # predictions json | ||
print('\nEvaluating pycocotools mAP... saving %s...' % pred_json) | ||
with open(pred_json, 'w') as f: | ||
json.dump(jdict, f) | ||
|
||
try: # https://github.com/cocodataset/cocoapi/blob/master/PythonAPI/pycocoEvalDemo.ipynb | ||
from pycocotools.coco import COCO | ||
from pycocotools.cocoeval import COCOeval | ||
|
||
imgIds = [int(Path(x).stem) for x in dataloader.dataset.img_files] | ||
cocoAnno = COCO(glob.glob('../coco/annotations/instances_val*.json')[0]) # initialize COCO annotations api | ||
cocoPred = cocoAnno.loadRes(str(file)) # initialize COCO pred api | ||
cocoEval = COCOeval(cocoAnno, cocoPred, 'bbox') | ||
cocoEval.params.imgIds = imgIds # image IDs to evaluate | ||
cocoEval.evaluate() | ||
cocoEval.accumulate() | ||
cocoEval.summarize() | ||
map, map50 = cocoEval.stats[:2] # update results ([email protected]:0.95, [email protected]) | ||
anno = COCO(anno_json) # init annotations api | ||
pred = anno.loadRes(pred_json) # init predictions api | ||
eval = COCOeval(anno, pred, 'bbox') | ||
if is_coco: | ||
eval.params.imgIds = [int(Path(x).stem) for x in dataloader.dataset.img_files] # image IDs to evaluate | ||
eval.evaluate() | ||
eval.accumulate() | ||
eval.summarize() | ||
map, map50 = eval.stats[:2] # update results ([email protected]:0.95, [email protected]) | ||
except Exception as e: | ||
print('ERROR: pycocotools unable to run: %s' % e) | ||
|
||
|