Error loading saved Pytorch neural network

username: s_mkb
cooked: <p>Hello. I am having trouble loading a saved Pytorch network and I have no idea why. I have done this several times before, and I have never had this error.  This is the error I get:</p>
<pre><code class="lang-auto">RuntimeError: Error(s) in loading state_dict for NeuralNet:
	Unexpected key(s) in state_dict: "conv1.weight", "conv1.bias", "conv2.weight", "conv2.bias".
</code></pre>
<p>This is how I load my model:</p>
<pre><code class="lang-auto">model = NeuralNet()
model.load_state_dict(torch.load('./results/model_mar30_batch10.pth'))
model.eval()
</code></pre>
<p>This is my neural network structure:</p>
<pre><code class="lang-auto">    def __init__(self, **kwargs):
        super(NeuralNet, self).__init__(**kwargs)
        
        self.conv3 = nn.Conv2d(in_channels=24, out_channels=256, kernel_size=(2,2), stride=(2,2)) 
        self.conv4 = nn.Conv2d(in_channels=256, out_channels=512, kernel_size=(10,6), stride=(1,1))
    
        self.batch3 = nn.BatchNorm2d(512)
        self.batch4 = nn.BatchNorm2d(256)
        self.batch = nn.BatchNorm2d(24)

        #DECONVOLUTION

        self.deconv1 = nn.ConvTranspose2d(in_channels=512, out_channels=256, kernel_size=(10,6), stride=(1,1))
        self.deconv2 = nn.ConvTranspose2d(in_channels=256, out_channels=24, kernel_size=(2,2), stride=(2,2), padding=0)
        self.deconv3 = nn.ConvTranspose2d(in_channels=24, out_channels=3, kernel_size=(5,5), stride=(5,5), padding=0)

    def SqueezeExcite(self,channels,reduction,x):
        
        mid_cannels = channels // reduction

        self.pool = nn.AdaptiveAvgPool2d(output_size=1)
        self.conv1 = nn.Conv2d(in_channels=channels, out_channels=mid_cannels, kernel_size=(1,1), stride=(1,1), groups=1, bias=True)
        self.activ = nn.ReLU()
        self.conv2 = nn.Conv2d(in_channels=mid_cannels, out_channels=channels, kernel_size=(1,1), stride=(1,1), groups=1, bias=True)
        self.sigmoid = nn.Sigmoid()
        w = self.pool(x)
        w = self.conv1(w.float())
        w = self.activ(w.float())
        w = self.conv2(w.float())
        w = self.sigmoid(w.float())
        x = x * w.float()
        return x.float()
        
        encode1 = self.batch(torch.tanh(func.conv2d(input=x_in, weight=weights, stride=(5,5), padding=(0,0), groups=3)))

        encode2 = self.batch4(torch.tanh(self.conv3(encode1)))
        encode2 = self.SqueezeExcite(channels=256, reduction=16, x=encode2.float())
        encode3 = self.batch3(torch.tanh(self.conv4(encode2)))
  
        decode1 = torch.tanh(self.deconv1(encode3))
        decode5 = torch.tanh(self.deconv2(decode1))
        decode6 = func.relu(self.deconv3(decode5))
  
        decode6[:,0,:,:] = decode6[:,0,:,:] * x[:,0,:,:]
        decode6[:,1,:,:] = decode6[:,1,:,:] * x[:,0,:,:]
        decode6[:,2,:,:] = decode6[:,2,:,:] * x[:,0,:,:]
        
        return decode6

</code></pre>
<p>Usually I do not have problems, but recently I added this SqueezeAndExcitation block, and the error has occurred.</p>

username: Monne
cooked: <p>if im not mistaken, you try to load conv1 weight but in <code>__init__</code> it got commented. also im not seeing conv2 either in <code>__init__</code></p>

username: s_mkb
cooked: <p>Sorry, I don’t use that conv layer anymore. Just removed the comment to avoid confusion.</p>
<p>If that was the issue my training wouldn’t have run. It ran and I saved the trained model, I just cannot load it for some reason.</p>

username: Monne
cooked: <p>but the saved model you load had conv1 and conv2 weight, maybe you load weight before edit the model?</p>

username: s_mkb
cooked: <p>Yes, those are coming from the SqueezeExcite function.</p>

username: Monne
cooked: <p>maybe it cannot detect latter parameter assignment, since there is no conv1 and conv2 in the first place. can you check what model.parameters() or model.state_dict().keys() before load model?</p>

username: s_mkb
cooked: <p>That is true. It worked once I removed the function, and just assigned conv1 and conv2 without them being inside a function. Thanks!</p>