Problems deploying a resnet_6blocks net with weights

andrew_germ: <p>Hello,</p>
<p>I was doing some research with this pix2pix implmenetation <a href="https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix" class="inline-onebox" rel="noopener nofollow ugc">GitHub - junyanz/pytorch-CycleGAN-and-pix2pix: Image-to-Image Translation in PyTorch</a> and was hoping to be able to deploy the trained models for further research and speed tests. To be honest this is my first approach in saving and loading a model. I found two topics on google about the issue below but nothing worked for me. I fear I might do something fundamentally wrong.</p>
<p>I inserted this code (from the pytorch tutorial site):</p>
<blockquote>
<p>checkpoint = {‘model’: net.module.cpu().model,<br>
‘state_dict’: net.module.cpu().state_dict()}<br>
torch.save(checkpoint, save_path)</p>
</blockquote>
<p>I’m using this code to load it:</p>
<blockquote>
<p>def load_checkpoint(filepath):<br>
checkpoint = torch.load(filepath)<br>
print(checkpoint[‘model’])<br>
print(checkpoint[‘state_dict’])<br>
model = checkpoint[‘model’]<br>
model.load_state_dict(checkpoint[‘state_dict’])<br>
for parameter in model.parameters():<br>
parameter.requires_grad = False</p>
<pre><code>model.eval()
return model
</code></pre>
<p>model = load_checkpoint(‘5_net_G.pth’)</p>
</blockquote>
<p>For debugging I printed out the model and dictionary. This is the model:</p>
<blockquote>
<p>Sequential(<br>
(0): ReflectionPad2d((3, 3, 3, 3))<br>
(1): Conv2d(3, 64, kernel_size=(7, 7), stride=(1, 1), bias=False)<br>
(2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(3): ReLU(inplace)<br>
(4): Conv2d(64, 128, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)<br>
(5): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(6): ReLU(inplace)<br>
(7): Conv2d(128, 256, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)<br>
(8): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(9): ReLU(inplace)<br>
(10): ResnetBlock(<br>
(conv_block): Sequential(<br>
(0): ReflectionPad2d((1, 1, 1, 1))<br>
(1): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), bias=False)<br>
(2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(3): ReLU(inplace)<br>
(4): Dropout(p=0.5)<br>
(5): ReflectionPad2d((1, 1, 1, 1))<br>
(6): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), bias=False)<br>
(7): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
)<br>
)<br>
(11): ResnetBlock(<br>
(conv_block): Sequential(<br>
(0): ReflectionPad2d((1, 1, 1, 1))<br>
(1): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), bias=False)<br>
(2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(3): ReLU(inplace)<br>
(4): Dropout(p=0.5)<br>
(5): ReflectionPad2d((1, 1, 1, 1))<br>
(6): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), bias=False)<br>
(7): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
)<br>
)<br>
(12): ResnetBlock(<br>
(conv_block): Sequential(<br>
(0): ReflectionPad2d((1, 1, 1, 1))<br>
(1): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), bias=False)<br>
(2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(3): ReLU(inplace)<br>
(4): Dropout(p=0.5)<br>
(5): ReflectionPad2d((1, 1, 1, 1))<br>
(6): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), bias=False)<br>
(7): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
)<br>
)<br>
(13): ResnetBlock(<br>
(conv_block): Sequential(<br>
(0): ReflectionPad2d((1, 1, 1, 1))<br>
(1): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), bias=False)<br>
(2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(3): ReLU(inplace)<br>
(4): Dropout(p=0.5)<br>
(5): ReflectionPad2d((1, 1, 1, 1))<br>
(6): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), bias=False)<br>
(7): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
)<br>
)<br>
(14): ResnetBlock(<br>
(conv_block): Sequential(<br>
(0): ReflectionPad2d((1, 1, 1, 1))<br>
(1): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), bias=False)<br>
(2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(3): ReLU(inplace)<br>
(4): Dropout(p=0.5)<br>
(5): ReflectionPad2d((1, 1, 1, 1))<br>
(6): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), bias=False)<br>
(7): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
)<br>
)<br>
(15): ResnetBlock(<br>
(conv_block): Sequential(<br>
(0): ReflectionPad2d((1, 1, 1, 1))<br>
(1): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), bias=False)<br>
(2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(3): ReLU(inplace)<br>
(4): Dropout(p=0.5)<br>
(5): ReflectionPad2d((1, 1, 1, 1))<br>
(6): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), bias=False)<br>
(7): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
)<br>
)<br>
(16): ConvTranspose2d(256, 128, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), output_padding=(1, 1), bias=False)<br>
(17): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(18): ReLU(inplace)<br>
(19): ConvTranspose2d(128, 64, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), output_padding=(1, 1), bias=False)<br>
(20): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(21): ReLU(inplace)<br>
(22): ReflectionPad2d((3, 3, 3, 3))<br>
(23): Conv2d(64, 3, kernel_size=(7, 7), stride=(1, 1))<br>
(24): Tanh()<br>
)</p>
</blockquote>
<p>But in the code line model = checkpoint[‘model’] I get the following error:</p>
<blockquote>
<p>Error(s) in loading state_dict for Sequential:<br>
Missing key(s) in state_dict: “1.weight”, “2.weight”, “2.bias”, “2.running_mean”,…</p>
</blockquote>
<p>I would be glad for any suggestions. Thanks in advance!<br>
Cheers!</p>