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

I am getting this error when I am running my training loop
TypeError: conv2d(): argument 'input' (position 1) must be Tensor, not int
This is a simple CNN model, here is the model arch:

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)

Can someone help?
P.S: I know I called this model ResNet18 even though it’s not lol

Here is the complete error traceback:

Traceback (most recent call last):
  File "c:/Users/LENOVO/Desktop/Dev/Currency-Classification/Using Pytorch/engine.py", line 54, in <module>
    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

And my dataset class is returning image and label:

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

Could you check the type of the input to the model?
Your model works fine if I pass a random input tensor in the shape [batch_size, 3, 70, 70] to it so I guess your custom Dataset is returning ints where tensors are expected.

Also note that your current implementation of your custom Dataset is recreating the ImageFolder in the __getitem__ method, which is not necessary and will slow down the code.
Use ImageFolder directly or initialize it in the __init__ method.

1 Like

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.

I have resized the image and converted to tensor it’s still not working

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)

Could you post the type and shape of the input before passing it to the model?

1 Like

All images are of different shape.

Anything you can do to help sir?

Unfortunately, I cannot do more without an executable code snippet, as your model works fine using the random input as mentioned previously.