TypeError: conv2d() received an invalid combination of arguments -

Hello all,
I have been dealing with an error.
At first, I saved the weights of a trained model as follows:

torch.save(model.state_dict(), 'final_model.pth')

Then try to load the ‘pth’ file for evaluation by the following code:

device = "cuda" if torch.cuda.is_available() else "cpu"
print(f"Using {device} device")
model = ConvNeuralNet(num_classes=4)
model.load_state_dict(torch.load('final_model.pth', map_location=device))

The definition of the model is as follows:

class ConvNeuralNet(nn.Module):
# Determine what layers and their order in CNN object
    def __init__(self, num_classes):
        super(ConvNeuralNet, self).__init__()

        self.Conv1 = torch.nn.Conv2d(in_channels=3, out_channels=64, kernel_size=(3, 3), padding=1, padding_mode='zeros')
        self.Conv2 = torch.nn.Conv2d(in_channels=64, out_channels=128, kernel_size=(3, 3), padding=1, padding_mode='zeros')
        self.Conv3 = torch.nn.Conv2d(in_channels=128, out_channels=128, kernel_size=(3, 3), padding=1, padding_mode='zeros')
        self.Conv4 = torch.nn.Conv2d(in_channels=128, out_channels=256, kernel_size=(3, 3), padding=1, padding_mode='zeros')
        self.Conv5 = torch.nn.Conv2d(in_channels=256, out_channels=256, kernel_size=(3, 3), padding=1, padding_mode='zeros')
        self.Conv6 = torch.nn.Conv2d(in_channels=256, out_channels=512, kernel_size=(3, 3), padding=1, padding_mode='zeros')

        self.BatchNorm2d_1 = torch.nn.BatchNorm2d(64)
        self.BatchNorm2d_2 = torch.nn.BatchNorm2d(128)
        self.BatchNorm2d_3 = torch.nn.BatchNorm2d(128)
        self.BatchNorm2d_4 = torch.nn.BatchNorm2d(256)
        self.BatchNorm2d_5 = ​​torch.nn.BatchNorm2d(256)
        self.BatchNorm2d_6 = torch.nn.BatchNorm2d(512)

        self.MaxPool2d = torch.nn.MaxPool2d(kernel_size=(2, 2), padding=0)
        self.Dropout_25 = torch.nn.Dropout(p=0.25)
        self.Relu = torch.nn.ReLU()

        self.FC1 = nn.Linear(32768, 1024)
        self.FC2 = nn.Linear(1024, 512)
        self.FC3 = nn.Linear(512, num_classes)

    # Progresses data across layers
    def forward(self, x):
        out = self.Conv1(x)
        out = self.Relu(out)
        out = self.BatchNorm2d_1(out)
        out = self.MaxPool2d(out)
        
        out = self.Conv2(out)
        out = self.Relu(out)
        out = self.BatchNorm2d_2(out)
        out = self.MaxPool2d(out)
                
        out = self.Conv3(out)
        out = self.Relu(out)
        out = self.BatchNorm2d_3(out)
        out = self.MaxPool2d(out)
                
        out = self.Conv4(out)
        out = self.Relu(out)
        out = self.BatchNorm2d_4(out)
        out = self.MaxPool2d(out)
                
        out = self.Conv5(out)
        out = self.Relu(out)
        out = self.BatchNorm2d_5(out)
        out = self.MaxPool2d(out)
                
        out = self.Conv6(out)
        out = self.Relu(out)
        out = self.BatchNorm2d_6(out)
        out = self.MaxPool2d(out)
                
        out = torch.flatten(out, 1)
        
        out = self.FC1(out)
        out = self.Relu(out)
        out = self.Dropout_25(out)
        out = self.FC2(out)
        out = self.Relu(out)
        out = self.Dropout_25(out)
        out = self.FC3(out)
        return out

The error message is as follows:

TypeError: conv2d() received an invalid combination of arguments - got (str, Parameter, Parameter, 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: (str, Parameter, Parameter, tuple, tuple, tuple, 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: (str, Parameter, Parameter, tuple, tuple, tuple, int)

would you please help me to fix it?

Your model works fine in my environment and based on the error message it seems you are trying to pass a str to the first conv layer, while a tensor is expected.
Could you check the type of x inside the forward method and make sure it’s a valid tensor?

1 Like

Thank you so much. I didn’t convert the Input to tensor.
By converting, the problem solved.