Skip to content

Commit

Permalink
fix annotation filename to id
Browse files Browse the repository at this point in the history
  • Loading branch information
Mehrdad committed Nov 13, 2023
1 parent 2ab0f04 commit d9a877f
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion ultralytics/models/yolo/detect/val.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def save_one_txt(self, predn, save_conf, shape, file):
def pred_to_json(self, predn, filename):
"""Serialize YOLO predictions to COCO json format."""
stem = Path(filename).stem
image_id = int(stem) if stem.isnumeric() else stem
image_id = (int(stem) if stem.isnumeric() else stem) if self.is_coco else os.path.basename(filename)
box = ops.xyxy2xywh(predn[:, :4]) # xywh
box[:, :2] -= box[:, 2:] / 2 # xy center to top-left corner
for p, b in zip(predn.tolist(), box.tolist()):
Expand Down
5 changes: 3 additions & 2 deletions ultralytics/models/yolo/pose/val.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Ultralytics YOLO 🚀, AGPL-3.0 license

import os
from pathlib import Path

import numpy as np
Expand Down Expand Up @@ -202,7 +203,7 @@ def plot_predictions(self, batch, preds, ni):
def pred_to_json(self, predn, filename):
"""Converts YOLO predictions to COCO JSON format."""
stem = Path(filename).stem
image_id = int(stem) if stem.isnumeric() else stem
image_id = (int(stem) if stem.isnumeric() else stem) if self.is_coco else os.path.basename(filename)
box = ops.xyxy2xywh(predn[:, :4]) # xywh
box[:, :2] -= box[:, 2:] / 2 # xy center to top-left corner
for p, b in zip(predn.tolist(), box.tolist()):
Expand All @@ -217,7 +218,7 @@ def eval_json(self, stats):
"""Evaluates object detection model using COCO JSON format."""
if self.args.save_json and (self.is_coco or self.args.anno_json) and len(self.jdict):
anno_json = Path(self.args.anno_json) if self.args.anno_json else self.data['path'] / 'annotations/person_keypoints_val2017.json' # annotations
pred_json = self.save_dir / 'predictions.json' # predictions
pred_json = self.save_dir / 'predictions-pose.json' # predictions
LOGGER.info(f'\nEvaluating pycocotools mAP using {pred_json} and {anno_json}...')
try: # https://github.com/cocodataset/cocoapi/blob/master/PythonAPI/pycocoEvalDemo.ipynb
check_requirements('pycocotools>=2.0.6')
Expand Down
5 changes: 3 additions & 2 deletions ultralytics/models/yolo/segment/val.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Ultralytics YOLO 🚀, AGPL-3.0 license

import os
from multiprocessing.pool import ThreadPool
from pathlib import Path

Expand Down Expand Up @@ -231,7 +232,7 @@ def single_encode(x):
return rle

stem = Path(filename).stem
image_id = int(stem) if stem.isnumeric() else stem
image_id = (int(stem) if stem.isnumeric() else stem) if self.is_coco else os.path.basename(filename)
box = ops.xyxy2xywh(predn[:, :4]) # xywh
box[:, :2] -= box[:, 2:] / 2 # xy center to top-left corner
pred_masks = np.transpose(pred_masks, (2, 0, 1))
Expand All @@ -249,7 +250,7 @@ def eval_json(self, stats):
"""Return COCO-style object detection evaluation metrics."""
if self.args.save_json and (self.is_coco or self.args.anno_json) and len(self.jdict):
anno_json = Path(self.args.anno_json) if self.args.anno_json else self.data['path'] / 'annotations/instances_val2017.json' # annotations
pred_json = self.save_dir / 'predictions.json' # predictions
pred_json = self.save_dir / 'predictions-seg.json' # predictions
LOGGER.info(f'\nEvaluating pycocotools mAP using {pred_json} and {anno_json}...')
try: # https://github.com/cocodataset/cocoapi/blob/master/PythonAPI/pycocoEvalDemo.ipynb
check_requirements('pycocotools>=2.0.6')
Expand Down

0 comments on commit d9a877f

Please sign in to comment.