Given groups=1, weight of size [6, 1, 3, 3], expected input[13, 3, 100, 100] to have 1 channels, but got 3 channels instead

I have edited the previous reply and posted the error and class

Could you run all cells again? It looks like the model definition cell might have changed or wasn’t successfully run.

PS: you can add code using three backticks ` :wink:
This will make searching for certain code snippets easier and also (as in my case now) looking at your code on a mobile device.

I am still having the same error…

import torch
net=CNN()
checkpoint = torch.load('C:/Users/Khilan Pandya/project/model/model.pt')
net.load_state_dict(checkpoint['state_dict'])
optimizer.load_state_dict(checkpoint['optimizer'])

code For my class

class CNN(nn.Module):
    
    def __init__(self, out_1=13, out_2=32):
        super(CNN, self).__init__()
        self.cnn1 = nn.Conv2d(in_channels=3, out_channels=out_1, kernel_size=3, padding=1)
        self.relu1 = nn.ReLU()
        self.maxpool1 = nn.MaxPool2d(kernel_size=2)
        self.cnn2 = nn.Conv2d(in_channels=out_1, out_channels=out_2, kernel_size=5, stride=1, padding=0)
        self.relu2 = nn.ReLU()
        self.maxpool2 = nn.MaxPool2d(kernel_size=2)
        self.fc1 = nn.Linear(out_2 * 23 * 23, 2)
    
    def forward(self, x):
        out = self.cnn1(x)
        out = self.relu1(out)
        out = self.maxpool1(out)
        out = self.cnn2(out)
        out = self.relu2(out)
        out = self.maxpool2(out)
        out = out.view(out.size(0), -1)
        out = self.fc1(out)
        return out
    
    def activations(self, x):
        z1 = self.cnn1(x)
        a1 = self.relu1(z1)
        out = self.maxpool1(a1)
        
        z2 = self.cnn2(out)
        a2 = self.relu2(z2)
        out = self.maxpool2(a2)
        out = out.view(out.size(0),-1)
        return z1, a1, z2, a2, out

this is how I saved my model

c="C:/Users/Khilan Pandya/project/model/model.pt"
torch.save(net.state_dict(),c)
torch.save(optimizer.state_dict(),c)

You are overwriting your model’s state_dict with the optimizer’s one.
If you would like to save both state_dicts, you could provide a dict:

net=CNN()
optimizer = torch.optim.Adam(net.parameters(), lr=1e-3)

c = './tmp.pt'
torch.save({'state_dict': net.state_dict(),
            'optimizer': optimizer.state_dict()
            },
            c)

checkpoint = torch.load(c)
net.load_state_dict(checkpoint['state_dict'])
optimizer.load_state_dict(checkpoint['optimizer'])

I am still getting the same error.

name 'CNN' is not defined

Run the cell with the model definition again and check for errors.
I’ve used your posted code and it works on my machine.

What happens if you run the cell containing the CNN definition?

It runs perfectly… Without any error

That’s good to hear!
Is the initialization working now?

This code runs perfectly on my machine:

import torch
import torch.nn as nn

class CNN(nn.Module):
    
    def __init__(self, out_1=13, out_2=32):
        super(CNN, self).__init__()
        self.cnn1 = nn.Conv2d(in_channels=3, out_channels=out_1, kernel_size=3, padding=1)
        self.relu1 = nn.ReLU()
        self.maxpool1 = nn.MaxPool2d(kernel_size=2)
        self.cnn2 = nn.Conv2d(in_channels=out_1, out_channels=out_2, kernel_size=5, stride=1, padding=0)
        self.relu2 = nn.ReLU()
        self.maxpool2 = nn.MaxPool2d(kernel_size=2)
        self.fc1 = nn.Linear(out_2 * 23 * 23, 2)
    
    def forward(self, x):
        out = self.cnn1(x)
        out = self.relu1(out)
        out = self.maxpool1(out)
        out = self.cnn2(out)
        out = self.relu2(out)
        out = self.maxpool2(out)
        out = out.view(out.size(0), -1)
        out = self.fc1(out)
        return out
    
    def activations(self, x):
        z1 = self.cnn1(x)
        a1 = self.relu1(z1)
        out = self.maxpool1(a1)
        
        z2 = self.cnn2(out)
        a2 = self.relu2(z2)
        out = self.maxpool2(a2)
        out = out.view(out.size(0),-1)
        return z1, a1, z2, a2, out


net=CNN()
optimizer = torch.optim.Adam(net.parameters(), lr=1e-3)

c = './tmp.pt'
torch.save({'state_dict': net.state_dict(),
            'optimizer': optimizer.state_dict()
            },
            c)

checkpoint = torch.load(c)
net.load_state_dict(checkpoint['state_dict'])
optimizer.load_state_dict(checkpoint['optimizer'])

Could you compare yours to mine and check for any differences?

1 Like

I do have a very silly doubt, should I run all these command in a single script??

Yeah, do it for now please.
If that’s working, you could try to create the notebook again.

That is working…

net=CNN()
checkpoint = torch.load('C:/Users/Khilan Pandya/project/model/model.pt')
net.load_state_dict(checkpoint['state_dict'])
optimizer.load_state_dict(checkpoint['optimizer'])
net.eval()

then I get

CNN(
  (cnn1): Conv2d(3, 13, 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)
  (cnn2): Conv2d(13, 32, kernel_size=(5, 5), stride=(1, 1))
  (relu2): ReLU()
  (maxpool2): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
  (fc1): Linear(in_features=16928, out_features=2, bias=True)
)

Now how I should I load in new notebook.

OK, perfect!
Just copy and past our code in the same order as in your script into a new notebook.
Alternatively, just stick to the script if that’s working for you.

So I will have to define the model every time I want to load the model parameters…

Yes, since PyTorch uses a dynamic computation graph, so that each iteration recreates the computation graph.
That’s why you should save the model code with its state_dict.

1 Like

Can you give me reference to a post or an article on how to perform segmentation and recognition using pytorch…

Have a look at this thread and also this blog post might be helpful.

1 Like