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

Creating my first pytorch image classification project, stuck at this error, not really sure what I am doing wrong. Please help!!!..

class GarbageNet(nn.Module):
def init(self):
super(GarbageNet,self).init()
self.conv1 = nn.Conv2d(in_channels=3, out_channels=12, kernel_size=3,stride=1, padding=1)
self.relu1 = nn.ReLU()
self.maxpool1 = nn.MaxPool2d(kernel_size=2)
self.conv2 = nn.Conv2d(in_channels=12, out_channels=24, kernel_size=3, stride=1, padding=1)
self.relu2 = nn.ReLU()
self.lf = nn.Linear(in_features=32 * 32 * 24, out_features=6)

    #Defining a forward pass
    
def forward(self, x):
    output = self.conv1(input)
    output = self.relu1(output)
    output = self.maxpool1(output)
    output = self.conv2(output)
    output = self.relu2(output)
    return x

def trainNet(net, n_epochs, learning_rate):
#Backpropagation - Using SGD
optimizer = optim.SGD(net.parameters(), lr = 0.001, momentum = 0.9)
#Defining an optimizer - SGD
loss = nn.CrossEntropyLoss()
print(net)
for epoch in range(n_epochs):
running_loss = 0.0
start_time = time.time()
total_train_loss = 0

    for i,data in enumerate(train_loader):
        images, labels = data
        print((images.shape))
        print((labels.shape))
        optimizer.zero_grad()

        #Forward pass, backward pass
        outputs = net(images)
        loss_size = loss(outputs, labels)
        loss_size.backward()
        optimizer.step()

=======Hyperparameters=========
epochs= 5
learning_rate= 0.001

GarbageNet(
(conv1): Conv2d(3, 12, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(relu1): ReLU()
(maxpool1): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(conv2): Conv2d(12, 24, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(relu2): ReLU()
(lf): Linear(in_features=24576, out_features=6, bias=True)
)
torch.Size([4, 3, 224, 224])
torch.Size([4])


TypeError Traceback (most recent call last)
in
----> 1 trainNet(cnn_model, n_epochs=5, learning_rate=0.001)

in trainNet(net, n_epochs, learning_rate)
43
44 #Forward pass, backward pass
β€”> 45 outputs = net(images)
46 loss_size = loss(outputs, labels)
47 loss_size.backward()

~\AppData\Local\conda\conda\envs\asdf\lib\site-packages\torch\nn\modules\module.py in call(self, *input, **kwargs)
530 result = self._slow_forward(*input, **kwargs)
531 else:
–> 532 result = self.forward(*input, **kwargs)
533 for hook in self._forward_hooks.values():
534 hook_result = hook(self, input, result)

in forward(self, x)
14
15 def forward(self, x):
β€”> 16 output = self.conv1(input)
17 output = self.relu1(output)
18 output = self.maxpool1(output)

~\AppData\Local\conda\conda\envs\asdf\lib\site-packages\torch\nn\modules\module.py in call(self, *input, **kwargs)
530 result = self._slow_forward(*input, **kwargs)
531 else:
–> 532 result = self.forward(*input, **kwargs)
533 for hook in self._forward_hooks.values():
534 hook_result = hook(self, input, result)

~\AppData\Local\conda\conda\envs\asdf\lib\site-packages\torch\nn\modules\conv.py in forward(self, input)
343
344 def forward(self, input):
–> 345 return self.conv2d_forward(input, self.weight)
346
347 class Conv3d(_ConvNd):

~\AppData\Local\conda\conda\envs\asdf\lib\site-packages\torch\nn\modules\conv.py in conv2d_forward(self, input, weight)
339 weight, self.bias, self.stride,
340 _pair(0), self.dilation, self.groups)
–> 341 return F.conv2d(input, weight, self.bias, self.stride,
342 self.padding, self.dilation, self.groups)
343

TypeError: conv2d(): argument β€˜input’ (position 1) must be Tensor, not method

Your forward method accepts an x input, while you are using input as the first argument to your conv layer.
Change it to self.conv1(x) and it should work.

2 Likes

Careless mistake, Thanks @ptrblck for the solution. It works!!!