Resnet50 - conv2d() received an invalid combination of arguments

dev1: <p>Hi everyone. I’m trying to implement the code for <em>DETR</em> from the paper <em>End-to-End Object Detection with Transformers</em>. I need to use <em>resnet50</em>.</p>
<p>The first part of the code looks like this:</p>
<pre><code class="lang-auto">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
</code></pre>
<p>I try to test the model with</p>
<pre><code class="lang-auto">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))
</code></pre>
<p>I simply fed an input <code>torch.randn(2, 3, 256, 256)</code> to <code>resnet50</code> just like the code in the paper. However I encounter with the following error:</p>
<pre><code class="lang-auto">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)
</code></pre>
<p>I use</p>
<pre><code class="lang-auto">torch==2.1.0+cu118
torchvision==0.16.0+cu118
</code></pre>
<p>I just couldn’t solve it.</p>

KFrank: <p>Hi Uygar!</p>
<aside class="quote no-group" data-username="dev1" data-post="1" data-topic="191181" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/d/cdc98d/48.png" class="avatar"> dev1:</div>
<blockquote>
<pre><code class="lang-auto">  def forward(self, inputs):
    x = self.backbone([input])
    return x
</code></pre>
</blockquote>
</aside>
<p>First, a minor issue:</p>
<p><code>x = self.backbone([input])</code> is almost certainly a typo.  <code>input</code> is a built-in<br>
python function.  You want <code>inputs</code> instead of <code>input</code>.</p>
<p>The bigger point is that <code>inputs</code> is a tensor – appropriate to pass into the<br>
<code>Conv2d</code> that is the first layer of <code>resnet50</code>.  But you are wrapping <code>inputs</code><br>
in a list (that contains <code>inputs</code> as its single element).  So <code>Conv2d</code> issues the<br>
error message that complains about being passed a <code>list</code> instead of a <code>Tensor</code>.</p>
<p>Change that line of code to <code>x = self.backbone (input)</code> – no <code>[]</code>.</p>
<p>Best.</p>
<p>K. Frank</p>

dev1: <p>Hi Frank!</p>
<p>It works now yes! thank you <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>
<p>I spent so much time on it. I can’t believe I missed this.</p>
<p>Thank you.</p>