Resnet50 - conv2d() received an invalid combination of arguments

Hi everyone. I’m trying to implement the code for DETR from the paper End-to-End Object Detection with Transformers. I need to use resnet50.

The first part of the code looks like this:

class DETR(nn.Module):
  def __init__(self, num_classes, hidden_dim, nheads, num_encoder_layers, num_decoder_layers):
    super().__init__()
    self.backbone = resnet50(weights=ResNet50_Weights.DEFAULT)
    del self.backbone.avgpool
    del self.backbone.fc

  def forward(self, inputs):
    x = self.backbone([input])
    return x

I try to test the model with

detr = DETR(num_classes=91, hidden_dim=256, nheads=8, num_encoder_layers=6, num_decoder_layers=6)
inputs = torch.randn(2, 3, 256, 256)
print(detr(inputs))

I simply fed an input torch.randn(2, 3, 256, 256) to resnet50 just like the code in the paper. However I encounter with the following error:

TypeError: conv2d() received an invalid combination of arguments - got (list, Parameter, NoneType, tuple, tuple, tuple, int), but expected one of:
 * (Tensor input, Tensor weight, Tensor bias, tuple of ints stride, tuple of ints padding, tuple of ints dilation, int groups)
      didn't match because some of the arguments have invalid types: (!list of [method]!, !Parameter!, !NoneType!, !tuple of (int, int)!, !tuple of (int, int)!, !tuple of (int, int)!, int)
 * (Tensor input, Tensor weight, Tensor bias, tuple of ints stride, str padding, tuple of ints dilation, int groups)
      didn't match because some of the arguments have invalid types: (!list of [method]!, !Parameter!, !NoneType!, !tuple of (int, int)!, !tuple of (int, int)!, !tuple of (int, int)!, int)

I use

torch==2.1.0+cu118
torchvision==0.16.0+cu118

I just couldn’t solve it.

Hi Uygar!

First, a minor issue:

x = self.backbone([input]) is almost certainly a typo. input is a built-in
python function. You want inputs instead of input.

The bigger point is that inputs is a tensor – appropriate to pass into the
Conv2d that is the first layer of resnet50. But you are wrapping inputs
in a list (that contains inputs as its single element). So Conv2d issues the
error message that complains about being passed a list instead of a Tensor.

Change that line of code to x = self.backbone (input) – no [].

Best.

K. Frank

Hi Frank!

It works now yes! thank you :slight_smile:

I spent so much time on it. I can’t believe I missed this.

Thank you.