roboflow/supervision · error · ValueError

Dense mask shape {dense.shape[1:]} does not match CompactMas

Error message

Dense mask shape {dense.shape[1:]} does not match CompactMask image_shape {image_shape}.

What it means

Error "Dense mask shape {dense.shape[1:]} does not match CompactMask image_shape {image_shape}." thrown in roboflow/supervision.

Source

Thrown at src/supervision/detection/core.py:2470

            # Mixed dense and CompactMask: convert dense masks to CompactMask to
            # avoid materialising a full (N, H, W) stack.
            compact_image_shapes = {
                m.image_shape for m in masks if isinstance(m, CompactMask)
            }
            if len(compact_image_shapes) != 1:
                raise ValueError(
                    "Cannot merge CompactMask objects with different image shapes: "
                    f"{sorted(compact_image_shapes)}"
                )
            image_shape: tuple[int, int] = next(iter(compact_image_shapes))
            compact_list: list[CompactMask] = []
            for d, m in zip(detections_list, masks):
                if isinstance(m, CompactMask):
                    compact_list.append(m)
                else:
                    dense = np.asarray(m, dtype=bool)
                    if dense.shape[1:] != image_shape:
                        raise ValueError(
                            f"Dense mask shape {dense.shape[1:]} does not match "
                            f"CompactMask image_shape {image_shape}."
                        )
                    compact_list.append(
                        CompactMask.from_dense(dense, d.xyxy, image_shape)
                    )
            return CompactMask.merge(compact_list)

        def stack_or_none(name: str) -> npt.NDArray[np.generic] | None:
            values = [getattr(d, name) for d in detections_list]
            if all(v is None for v in values):
                return None
            if any(v is None for v in values):
                raise ValueError(f"All or none of the '{name}' fields must be None")
            return cast(npt.NDArray[np.generic], np.hstack(values))

        mask = cast(npt.NDArray[np.bool_] | CompactMask | None, stack_mask_or_none())
        confidence = cast(npt.NDArray[np.floating] | None, stack_or_none("confidence"))

View on GitHub (pinned to 7f254d9784)

Solutions

  1. Ensure each dense mask's (H, W) matches the CompactMask image_shape before conversion.
  2. Resize or crop the dense masks to the expected image_shape, or create the CompactMask with the matching shape.

When it happens

Trigger: Thrown at src/supervision/detection/core.py:2470 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of roboflow/supervision@7f254d9784 (2026-08-15). Data as JSON: /api/errors/dce1fed5e5ef381f. Report an issue: GitHub.