ultralytics 8.3.128 Fix ReID feature shape check and add tests (#20499)

Signed-off-by: Mohammed Yasin <32206511+Y-T-G@users.noreply.github.com>
Signed-off-by: Glenn Jocher <glenn.jocher@ultralytics.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
Co-authored-by: UltralyticsAssistant <web@ultralytics.com>
This commit is contained in:
Mohammed Yasin 2025-05-07 03:25:03 +06:00 committed by GitHub
parent c102b66a7a
commit ca27ffcf53
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 8 additions and 9 deletions

View File

@ -188,11 +188,11 @@ def test_track_stream():
model.track(video_url, imgsz=160, tracker="bytetrack.yaml")
model.track(video_url, imgsz=160, tracker="botsort.yaml", save_frames=True) # test frame saving also
# Test Global Motion Compensation (GMC) methods
for gmc in "orb", "sift", "ecc":
# Test Global Motion Compensation (GMC) methods and ReID
for gmc, reidm in zip(["orb", "sift", "ecc"], ["auto", "auto", "yolo11n-cls.pt"]):
default_args = YAML.load(ROOT / "cfg/trackers/botsort.yaml")
custom_yaml = TMP / f"botsort-{gmc}.yaml"
YAML.save(custom_yaml, {**default_args, "gmc_method": gmc})
YAML.save(custom_yaml, {**default_args, "gmc_method": gmc, "with_reid": True, "model": reidm})
model.track(video_url, imgsz=160, tracker=custom_yaml)

View File

@ -1,6 +1,6 @@
# Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
__version__ = "8.3.127"
__version__ = "8.3.128"
import os

View File

@ -82,11 +82,9 @@ class DetectionPredictor(BasePredictor):
def get_obj_feats(self, feat_maps, idxs):
"""Extract object features from the feature maps."""
from math import gcd
import torch
s = gcd(*[x.shape[1] for x in feat_maps]) # find smallest vector length
s = min([x.shape[1] for x in feat_maps]) # find smallest vector length
obj_feats = torch.cat(
[x.permute(0, 2, 3, 1).reshape(x.shape[0], -1, s, x.shape[1] // s).mean(dim=-1) for x in feat_maps], dim=1
) # mean reduce all vectors to same length

View File

@ -255,6 +255,6 @@ class ReID:
def __call__(self, img, dets):
"""Extract embeddings for detected objects."""
feats = self.model([save_one_box(det, img, save=False) for det in xywh2xyxy(torch.from_numpy(dets[:, :4]))])
if feats.shape[0] != dets.shape[0] and feats[0].shape[0] == dets.shape[0]:
if len(feats) != dets.shape[0] and feats[0].shape[0] == dets.shape[0]:
feats = feats[0] # batched prediction with non-PyTorch backend
return [f.cpu().numpy() for f in feats]

View File

@ -44,6 +44,8 @@ def on_predict_start(predictor: object, persist: bool = False) -> None:
if cfg.tracker_type not in {"bytetrack", "botsort"}:
raise AssertionError(f"Only 'bytetrack' and 'botsort' are supported for now, but got '{cfg.tracker_type}'")
predictor._feats = None # reset in case used earlier
predictor.save_feats = False
if cfg.tracker_type == "botsort" and cfg.with_reid and cfg.model == "auto":
from ultralytics.nn.modules.head import Detect
@ -55,7 +57,6 @@ def on_predict_start(predictor: object, persist: bool = False) -> None:
cfg.model = "yolo11n-cls.pt"
else:
predictor.save_feats = True
predictor._feats = None
# Register hook to extract input of Detect layer
def pre_hook(module, input):