TypeError: conv2d(): argument 'input' (position 1) must be Tensor, not int

username: Hacking_Pirate
cooked: <p>I am getting this error when I am running my training loop<br>
<code> TypeError: conv2d(): argument 'input' (position 1) must be Tensor, not int</code><br>
This is a simple CNN model, here is the model arch:</p>
<pre><code class="lang-auto">class ResNet18(nn.Module):

    def __init__(self):

        super(ResNet18, self).__init__()

        self.network = nn.Sequential(

            nn.Conv2d(3, 16, kernel_size=(3,3), stride=1),

            nn.ReLU(),

            nn.MaxPool2d(kernel_size=(2,2)),

            

            nn.Conv2d(16, 32, kernel_size=(3,3), stride=1),

            nn.ReLU(),

            nn.MaxPool2d(kernel_size=(2,2)),

                        

            nn.Flatten(),

            nn.Linear(16*16*32, 64),

            nn.ReLU(),

            nn.Linear(64, 32),

            nn.ReLU(),

            nn.Linear(32, 7),

            )

        

    def forward(self, x):

        return self.network(x)
</code></pre>
<p>Can someone help?<br>
P.S: I know I called this model ResNet18 even though it’s not lol</p>

username: Hacking_Pirate
cooked: <p>Here is the complete error traceback:</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "c:/Users/LENOVO/Desktop/Dev/Currency-Classification/Using Pytorch/engine.py", line 54, in &lt;module&gt;
    train(model, optimizer, train_dl, 20)
  File "c:/Users/LENOVO/Desktop/Dev/Currency-Classification/Using Pytorch/engine.py", line 46, in train
    out = model(img)
  File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python36\lib\site-packages\torch\nn\modules\module.py", line 550, in __call__   
    result = self.forward(*input, **kwargs)
  File "c:\Users\LENOVO\Desktop\Dev\Currency-Classification\Using Pytorch\model.py", line 28, in forward
    return self.network(x)
  File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python36\lib\site-packages\torch\nn\modules\module.py", line 550, in __call__   
    result = self.forward(*input, **kwargs)
  File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python36\lib\site-packages\torch\nn\modules\container.py", line 100, in forward 
    input = module(input)
  File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python36\lib\site-packages\torch\nn\modules\module.py", line 550, in __call__   
    result = self.forward(*input, **kwargs)
  File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python36\lib\site-packages\torch\nn\modules\conv.py", line 349, in forward      
    return self._conv_forward(input, self.weight)
  File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python36\lib\site-packages\torch\nn\modules\conv.py", line 346, in _conv_forward
    self.padding, self.dilation, self.groups)
TypeError: conv2d(): argument 'input' (position 1) must be Tensor, not int
</code></pre>
<p>And my dataset class is returning image and label:</p>
<pre><code class="lang-auto">class CurrencyDataset(Dataset):
    def __init__(self, DATA_DIR, transforms=None):
        self.DATA_DIR = DATA_DIR
        self.transforms = transforms
        
    def __len__(self):
        return len(self.DATA_DIR)
        
    def __getitem__(self, idx):
        img, label = ImageFolder(self.DATA_DIR, transform=self.transforms)[idx]
        return img, label
</code></pre>

username: ptrblck
cooked: <p>Could you check the type of the input to the model?<br>
Your model works fine if I pass a random input tensor in the shape <code>[batch_size, 3, 70, 70]</code> to it so I guess your custom <code>Dataset</code> is returning ints where tensors are expected.</p>
<p>Also note that your current implementation of your custom <code>Dataset</code> is recreating the <code>ImageFolder</code> in the <code>__getitem__</code> method, which is not necessary and will slow down the code.<br>
Use <code>ImageFolder</code> directly or initialize it in the <code>__init__</code> method.</p>

username: Hacking_Pirate
cooked: <p>Yes I know that my Dataset class is just using the imagefolder. I did that just to get some practice with the custom dataset usage.</p>

username: Hacking_Pirate
cooked: <p>I have resized the image and converted to tensor it’s still not working</p>
<pre><code class="lang-auto">train_transforms = T.Compose([T.Resize((64, 64)),
                              T.ToTensor()])
train_ds = CurrencyDataset(TRAIN_DIR, transforms=train_transforms)
train_dl = DataLoader(train_ds,
                      batch_size=8, 
                      shuffle=True, 
                      num_workers=0,
                      pin_memory=True)
</code></pre>

username: ptrblck
cooked: <p>Could you post the type and shape of the input before passing it to the model?</p>

username: Hacking_Pirate
cooked: <p>All images are of different shape.</p>

username: Hacking_Pirate
cooked: <p>Anything you can do to help sir?</p>

username: ptrblck
cooked: <p>Unfortunately, I cannot do more without an executable code snippet, as your model works fine using the random input as mentioned previously.</p>