Skip to content

Commit

Permalink
Display Val images per class (#12645)
Browse files Browse the repository at this point in the history
Co-authored-by: UltralyticsAssistant <web@ultralytics.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
  • Loading branch information
3 people committed May 29, 2024
1 parent b95b583 commit 7cd871d
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
1 change: 0 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ jobs:
if [[ "${{ github.event_name }}" =~ ^(schedule|workflow_dispatch)$ ]]; then
slow="pycocotools mlflow ray[tune]"
fi
slow="pycocotools mlflow ray[tune]"
pip install -e ".[export]" $torch $slow pytest-cov --extra-index-url https://download.pytorch.org/whl/cpu
- name: Check environment
run: |
Expand Down
14 changes: 9 additions & 5 deletions ultralytics/models/yolo/detect/val.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def __init__(self, dataloader=None, save_dir=None, pbar=None, args=None, _callba
"""Initialize detection model with necessary variables and settings."""
super().__init__(dataloader, save_dir, pbar, args, _callbacks)
self.nt_per_class = None
self.nt_per_image = None
self.is_coco = False
self.is_lvis = False
self.class_map = None
Expand Down Expand Up @@ -77,7 +78,7 @@ def init_metrics(self, model):
self.confusion_matrix = ConfusionMatrix(nc=self.nc, conf=self.args.conf)
self.seen = 0
self.jdict = []
self.stats = dict(tp=[], conf=[], pred_cls=[], target_cls=[])
self.stats = dict(tp=[], conf=[], pred_cls=[], target_cls=[], target_img=[])

def get_desc(self):
"""Return a formatted string summarizing class metrics of YOLO model."""
Expand Down Expand Up @@ -130,6 +131,7 @@ def update_metrics(self, preds, batch):
cls, bbox = pbatch.pop("cls"), pbatch.pop("bbox")
nl = len(cls)
stat["target_cls"] = cls
stat["target_img"] = cls.unique()
if npr == 0:
if nl:
for k in self.stats.keys():
Expand Down Expand Up @@ -168,11 +170,11 @@ def finalize_metrics(self, *args, **kwargs):
def get_stats(self):
"""Returns metrics statistics and results dictionary."""
stats = {k: torch.cat(v, 0).cpu().numpy() for k, v in self.stats.items()} # to numpy
self.nt_per_class = np.bincount(stats["target_cls"].astype(int), minlength=self.nc)
self.nt_per_image = np.bincount(stats["target_img"].astype(int), minlength=self.nc)
stats.pop("target_img", None)
if len(stats) and stats["tp"].any():
self.metrics.process(**stats)
self.nt_per_class = np.bincount(
stats["target_cls"].astype(int), minlength=self.nc
) # number of targets per class
return self.metrics.results_dict

def print_results(self):
Expand All @@ -185,7 +187,9 @@ def print_results(self):
# Print results per class
if self.args.verbose and not self.training and self.nc > 1 and len(self.stats):
for i, c in enumerate(self.metrics.ap_class_index):
LOGGER.info(pf % (self.names[c], self.seen, self.nt_per_class[c], *self.metrics.class_result(i)))
LOGGER.info(
pf % (self.names[c], self.nt_per_image[c], self.nt_per_class[c], *self.metrics.class_result(i))
)

if self.args.plots:
for normalize in True, False:
Expand Down
3 changes: 2 additions & 1 deletion ultralytics/models/yolo/pose/val.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def init_metrics(self, model):
is_pose = self.kpt_shape == [17, 3]
nkpt = self.kpt_shape[0]
self.sigma = OKS_SIGMA if is_pose else np.ones(nkpt) / nkpt
self.stats = dict(tp_p=[], tp=[], conf=[], pred_cls=[], target_cls=[])
self.stats = dict(tp_p=[], tp=[], conf=[], pred_cls=[], target_cls=[], target_img=[])

def _prepare_batch(self, si, batch):
"""Prepares a batch for processing by converting keypoints to float and moving to device."""
Expand Down Expand Up @@ -118,6 +118,7 @@ def update_metrics(self, preds, batch):
cls, bbox = pbatch.pop("cls"), pbatch.pop("bbox")
nl = len(cls)
stat["target_cls"] = cls
stat["target_img"] = cls.unique()
if npr == 0:
if nl:
for k in self.stats.keys():
Expand Down
3 changes: 2 additions & 1 deletion ultralytics/models/yolo/segment/val.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def init_metrics(self, model):
self.process = ops.process_mask_upsample # more accurate
else:
self.process = ops.process_mask # faster
self.stats = dict(tp_m=[], tp=[], conf=[], pred_cls=[], target_cls=[])
self.stats = dict(tp_m=[], tp=[], conf=[], pred_cls=[], target_cls=[], target_img=[])

def get_desc(self):
"""Return a formatted description of evaluation metrics."""
Expand Down Expand Up @@ -112,6 +112,7 @@ def update_metrics(self, preds, batch):
cls, bbox = pbatch.pop("cls"), pbatch.pop("bbox")
nl = len(cls)
stat["target_cls"] = cls
stat["target_img"] = cls.unique()
if npr == 0:
if nl:
for k in self.stats.keys():
Expand Down

0 comments on commit 7cd871d

Please sign in to comment.