WZMIAOMIAO/deep-learning-for-image-processing · error · ValueError

Expected target boxes to be of type Tensor, got {:}.

Error message

Expected target boxes to be of type Tensor, got {:}.

What it means

forward checks that each target['boxes'] is a torch.Tensor during training. If boxes is any other type (list, numpy array, tuple), it raises this ValueError including the actual Python type. The model needs tensor ops on boxes, so non-tensor inputs are rejected.

Source

Thrown at pytorch_object_detection/mask_rcnn/network_files/faster_rcnn_framework.py:72

                During training, it returns a dict[Tensor] which contains the losses.
                During testing, it returns list[BoxList] contains additional fields
                like `scores`, `labels` and `mask` (for Mask R-CNN models).

        """
        if self.training and targets is None:
            raise ValueError("In training mode, targets should be passed")

        if self.training:
            assert targets is not None
            for target in targets:         # 进一步判断传入的target的boxes参数是否符合规定
                boxes = target["boxes"]
                if isinstance(boxes, torch.Tensor):
                    if len(boxes.shape) != 2 or boxes.shape[-1] != 4:
                        raise ValueError("Expected target boxes to be a tensor"
                                         "of shape [N, 4], got {:}.".format(
                                          boxes.shape))
                else:
                    raise ValueError("Expected target boxes to be of type "
                                     "Tensor, got {:}.".format(type(boxes)))

        original_image_sizes = torch.jit.annotate(List[Tuple[int, int]], [])
        for img in images:
            val = img.shape[-2:]
            assert len(val) == 2  # 防止输入的是个一维向量
            original_image_sizes.append((val[0], val[1]))
        # original_image_sizes = [img.shape[-2:] for img in images]

        images, targets = self.transform(images, targets)  # 对图像进行预处理
        # print(images.tensors.shape)
        features = self.backbone(images.tensors)  # 将图像输入backbone得到特征图
        if isinstance(features, torch.Tensor):  # 若只在一层特征层上预测,将feature放入有序字典中,并编号为‘0’
            features = OrderedDict([('0', features)])  # 若在多层特征层上预测,传入的就是一个有序字典

        # 将特征层以及标注target信息传入rpn中
        # proposals: List[Tensor], Tensor_shape: [num_proposals, 4],
        # 每个proposals是绝对坐标,且为(x1, y1, x2, y2)格式

View on GitHub (pinned to 1ec3fe6f37)

Solutions

  1. Convert boxes with torch.as_tensor(boxes, dtype=torch.float32) when building each target dict
  2. Fix the dataset/collate_fn to always emit tensors for 'boxes' and 'labels'
  3. Add a per-batch assert isinstance(t['boxes'], torch.Tensor) to catch bad samples early

Example fix

// before
target = {'boxes': [[10, 20, 110, 120]], 'labels': [1]}
// after
target = {'boxes': torch.as_tensor([[10, 20, 110, 120]], dtype=torch.float32), 'labels': torch.as_tensor([1], dtype=torch.int64)}
Defensive patterns

Strategy: type-guard

Validate before calling

for t in targets:
    if not isinstance(t['boxes'], torch.Tensor):
        t['boxes'] = torch.as_tensor(t['boxes'], dtype=torch.float32)

Type guard

def is_tensor_boxes(t):
    return isinstance(t.get('boxes'), torch.Tensor)

Try / catch

try:
    losses = model(images, targets)
except ValueError as e:
    if 'of type Tensor' in str(e):
        targets = [{**t, 'boxes': torch.as_tensor(t['boxes'], dtype=torch.float32)} for t in targets]
        losses = model(images, targets)
    else:
        raise

Prevention

When it happens

Trigger: Building targets from a dataset that returns boxes as lists of lists or numpy arrays and never converting to torch.Tensor before model(images, targets).

Common situations: Custom datasets/collate functions omitting torch.as_tensor conversion; JSON-loaded annotations passed directly; mixing torchvision versions where some converters are no longer applied.

Understand the failure class

Background: "Wrong argument type", "must be a string", "expected Array or Prism::Scope": TypeError and ArgumentError when a library receives a value of the wrong type — this error's family across 28 libraries.

Related errors


AI-assisted analysis of WZMIAOMIAO/deep-learning-for-image-processing@1ec3fe6f37 (2026-08-30). Data as JSON: /api/errors/42ea9c8057eaba24. Report an issue: GitHub.