open-mmlab/mmdetection · error · TypeError

neck inputs should be tuple or torch.tensor

Error message

neck inputs should be tuple or torch.tensor

What it means

GlobalAvgPooling (Gap neck used in ReID models) only accepts a tuple/list of tensors or a single torch.Tensor as input. Any other type raises this TypeError in forward.

Source

Thrown at mmdet/models/reid/gap.py:39

            self.gap = nn.AdaptiveAvgPool2d((1, 1))
        else:
            self.gap = nn.AvgPool2d(kernel_size, stride)

    def forward(self, inputs):
        if isinstance(inputs, tuple):
            outs = tuple([self.gap(x) for x in inputs])
            outs = tuple([
                out.view(x.size(0),
                         torch.tensor(out.size()[1:]).prod())
                for out, x in zip(outs, inputs)
            ])
        elif isinstance(inputs, torch.Tensor):
            outs = self.gap(inputs)
            outs = outs.view(
                inputs.size(0),
                torch.tensor(outs.size()[1:]).prod())
        else:
            raise TypeError('neck inputs should be tuple or torch.tensor')
        return outs

View on GitHub (pinned to cfd5d3a985)

Solutions

  1. Ensure the input is a stacked torch.Tensor of shape (N, C, H, W) or a tuple of tensors
  2. Add mmcv.transforms ToTensor / use the configured DataPreprocessor so images become tensors
  3. If inputs come from a loader, check that batch['inputs'] is tensorized before forward

Example fix

# before
neck_out = gap_neck(batch['inputs'])  # numpy arrays
# after
import torch
neck_out = gap_neck(torch.stack([torch.as_tensor(i) for i in batch['inputs']]))
Defensive patterns

Strategy: type-guard

Validate before calling

import torch
assert isinstance(inputs, torch.Tensor) or (isinstance(inputs, (tuple, list)) and all(isinstance(t, torch.Tensor) for t in inputs))

Type guard

def is_tensorlike(x): import torch; return isinstance(x, torch.Tensor) or (isinstance(x,(tuple,list)) and all(isinstance(t, torch.Tensor) for t in x))

Prevention

When it happens

Trigger: Passing a numpy array, a dict, or None to the Gap neck; a neck whose forward receives a non-tensor (e.g. when data samples or list-wrapped inputs are fed directly instead of the batched image tensor).

Common situations: Custom pipelines feeding numpy images without ToTensor; chaining necks incorrectly so a non-tensor flows into the reid neck.

Related errors


AI-assisted analysis of open-mmlab/mmdetection@cfd5d3a985 (2026-08-27). Data as JSON: /api/errors/b60eab7f53c7b955. Report an issue: GitHub.