{"record":{"id":"c76377b83fddc13b","repo":"open-mmlab/mmdetection","slug":"boxes-should-be-tensor-ndarray-or-sequence-but","errorCode":null,"errorMessage":"boxes should be Tensor, ndarray, or Sequence, but got {type(data)}","messagePattern":"boxes should be Tensor, ndarray, or Sequence, but got (.+?)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"mmdet/structures/bbox/base_boxes.py","lineNumber":64,"sourceCode":"        dtype (torch.dtype, Optional): data type of boxes. Defaults to None.\n        device (str or torch.device, Optional): device of boxes.\n            Default to None.\n        clone (bool): Whether clone ``boxes`` or not. Defaults to True.\n    \"\"\"\n\n    # Used to verify the last dimension length\n    # Should override it in subclass.\n    box_dim: int = 0\n\n    def __init__(self,\n                 data: Union[Tensor, np.ndarray, Sequence],\n                 dtype: Optional[torch.dtype] = None,\n                 device: Optional[DeviceType] = None,\n                 clone: bool = True) -> None:\n        if isinstance(data, (np.ndarray, Tensor, Sequence)):\n            data = torch.as_tensor(data)\n        else:\n            raise TypeError('boxes should be Tensor, ndarray, or Sequence, ',\n                            f'but got {type(data)}')\n\n        if device is not None or dtype is not None:\n            data = data.to(dtype=dtype, device=device)\n        # Clone the data to avoid potential bugs\n        if clone:\n            data = data.clone()\n        # handle the empty input like []\n        if data.numel() == 0:\n            data = data.reshape((-1, self.box_dim))\n\n        assert data.dim() >= 2 and data.size(-1) == self.box_dim, \\\n            ('The boxes dimension must >= 2 and the length of the last '\n             f'dimension must be {self.box_dim}, but got boxes with '\n             f'shape {data.shape}.')\n        self.tensor = data\n\n    def convert_to(self, dst_type: Union[str, type]) -> 'BaseBoxes':","sourceCodeStart":46,"sourceCodeEnd":82,"githubUrl":"https://github.com/open-mmlab/mmdetection/blob/cfd5d3a985b0249de009b67d04f37263e11cdf3d/mmdet/structures/bbox/base_boxes.py#L46-L82","documentation":"BaseBoxes.__init__ (HorizontalBoxes, etc.) only accepts np.ndarray, torch.Tensor, or Python Sequence data. Any other type (int, dict, None, PIL object) raises TypeError before tensor conversion.","triggerScenarios":"Constructing HorizontalBoxes with a scalar, None, a generator, or an uninitialized data path result; also passing data that has already been wrapped (e.g. a BaseBoxes instance is not a Sequence).","commonSituations":"Empty/missing annotations from a dataset sample passed into loss computation, or glue code converting raw dataset dicts to HorizontalBoxes without extracting the array first.","solutions":["Ensure data is a Tensor, ndarray, or list/tuple of numbers before wrapping","Guard empty annotations: use HorizontalBoxes(torch.zeros(0,4), ...) instead of None","Extract the right field from data samples (e.g. results.gt_bboxes.tensor) before re-wrapping"],"exampleFix":"# before\nboxes = HorizontalBoxes(None) if len(anns) == 0 else ...\n# after\nimport torch\nboxes = HorizontalBoxes(torch.zeros(0, 4)) if len(anns) == 0 else ...","handlingStrategy":"type-guard","validationCode":"import numpy as np, torch\nassert isinstance(data, (np.ndarray, torch.Tensor, list, tuple)), type(data)","typeGuard":"def is_valid_boxes_data(d) -> bool:\n    import numpy as np, torch\n    return isinstance(d, (np.ndarray, torch.Tensor, list, tuple))","tryCatchPattern":"try:\n    boxes = HorizontalBoxes(data)\nexcept TypeError:\n    boxes = HorizontalBoxes(torch.zeros(0, 4))","preventionTips":["Convert None/empty annotations to empty (0,4) tensors","Always extract .tensor or arrays before re-wrapping boxes"],"tags":["mmdetection","bbox","type-validation"],"backgroundTag":"unsupported-type","analyzedSha":"cfd5d3a985b0249de009b67d04f37263e11cdf3d","analyzedAt":"2026-08-27T20:54:20.183Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}