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:
RuntimeError: Error(s) in loading state_dict for NeuralNet:
Unexpected key(s) in state_dict: "conv1.weight", "conv1.bias", "conv2.weight", "conv2.bias".
This is how I load my model:
model = NeuralNet()
model.load_state_dict(torch.load('./results/model_mar30_batch10.pth'))
model.eval()
This is my neural network structure:
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
Usually I do not have problems, but recently I added this SqueezeAndExcitation block, and the error has occurred.