From 672a833fd1378eba2a35cde4e95e7be10909a893 Mon Sep 17 00:00:00 2001 From: Murat Raimbekov Date: Fri, 2 Jan 2026 07:58:54 +0600 Subject: [PATCH] Fix incorrect shape comments in mask processing (#23100) Co-authored-by: Glenn Jocher --- ultralytics/models/yolo/segment/predict.py | 4 ++-- ultralytics/utils/ops.py | 12 ++++++------ ultralytics/utils/plotting.py | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/ultralytics/models/yolo/segment/predict.py b/ultralytics/models/yolo/segment/predict.py index c94de68b..9246603a 100644 --- a/ultralytics/models/yolo/segment/predict.py +++ b/ultralytics/models/yolo/segment/predict.py @@ -98,9 +98,9 @@ class SegmentationPredictor(DetectionPredictor): masks = None elif self.args.retina_masks: pred[:, :4] = ops.scale_boxes(img.shape[2:], pred[:, :4], orig_img.shape) - masks = ops.process_mask_native(proto, pred[:, 6:], pred[:, :4], orig_img.shape[:2]) # HWC + masks = ops.process_mask_native(proto, pred[:, 6:], pred[:, :4], orig_img.shape[:2]) # NHW else: - masks = ops.process_mask(proto, pred[:, 6:], pred[:, :4], img.shape[2:], upsample=True) # HWC + masks = ops.process_mask(proto, pred[:, 6:], pred[:, :4], img.shape[2:], upsample=True) # NHW pred[:, :4] = ops.scale_boxes(img.shape[2:], pred[:, :4], orig_img.shape) if masks is not None: keep = masks.amax((-2, -1)) > 0 # only keep predictions with masks diff --git a/ultralytics/utils/ops.py b/ultralytics/utils/ops.py index 1da0b418..8a0658b4 100644 --- a/ultralytics/utils/ops.py +++ b/ultralytics/utils/ops.py @@ -484,15 +484,15 @@ def process_mask(protos, masks_in, bboxes, shape, upsample: bool = False): are the height and width of the input image. The mask is applied to the bounding boxes. """ c, mh, mw = protos.shape # CHW - masks = (masks_in @ protos.float().view(c, -1)).view(-1, mh, mw) # CHW + masks = (masks_in @ protos.float().view(c, -1)).view(-1, mh, mw) # NHW width_ratio = mw / shape[1] height_ratio = mh / shape[0] ratios = torch.tensor([[width_ratio, height_ratio, width_ratio, height_ratio]], device=bboxes.device) - masks = crop_mask(masks, boxes=bboxes * ratios) # CHW + masks = crop_mask(masks, boxes=bboxes * ratios) # NHW if upsample: - masks = F.interpolate(masks[None], shape, mode="bilinear")[0] # CHW + masks = F.interpolate(masks[None], shape, mode="bilinear")[0] # NHW return masks.gt_(0.0).byte() @@ -506,12 +506,12 @@ def process_mask_native(protos, masks_in, bboxes, shape): shape (tuple): Input image size as (height, width). Returns: - (torch.Tensor): Binary mask tensor with shape (H, W, N). + (torch.Tensor): Binary mask tensor with shape (N, H, W). """ c, mh, mw = protos.shape # CHW masks = (masks_in @ protos.float().view(c, -1)).view(-1, mh, mw) - masks = scale_masks(masks[None], shape)[0] # CHW - masks = crop_mask(masks, bboxes) # CHW + masks = scale_masks(masks[None], shape)[0] # NHW + masks = crop_mask(masks, bboxes) # NHW return masks.gt_(0.0).byte() diff --git a/ultralytics/utils/plotting.py b/ultralytics/utils/plotting.py index 734bde26..ba6f9b39 100644 --- a/ultralytics/utils/plotting.py +++ b/ultralytics/utils/plotting.py @@ -401,7 +401,7 @@ class Annotator: masks = masks.unsqueeze(3) # shape(n,h,w,1) masks_color = masks * (colors * alpha) # shape(n,h,w,3) inv_alpha_masks = (1 - masks * alpha).cumprod(0) # shape(n,h,w,1) - mcs = masks_color.max(dim=0).values # shape(n,h,w,3) + mcs = masks_color.max(dim=0).values # shape(h,w,3) im_gpu = im_gpu.flip(dims=[0]).permute(1, 2, 0).contiguous() # shape(h,w,3) im_gpu = im_gpu * inv_alpha_masks[-1] + mcs