Error 'load_state_dict'

maryam_hayat: <p>I am a beginner in deep learning. I am trying to run a GAN (UNet based) model on the data - implemented in pytorch. but when I try to run the code. I get the following error :<br>
**KeyError: ‘generatorX’   **</p>
<p>in the line:</p>
<p><strong>generatorX.load_state_dict( checkpoint[‘generatorX’])</strong></p>
<p>I am using the pretrained model / checkpoint (a file already provided by authors with .pth extension.<br>
I cannot figure out what is ‘generatorX’. When I print the model I can see weights.<br>
Any help will be highly appreciated.</p>

Diego: <p>Looks like the checkpoint file does not have the key “generatorX”. Could you run<br>
<code>print checkpoint.keys()</code> and see what you get?</p>

maryam_hayat: <p>Thanks for the reply. When I print, I get this:</p>
<p>odict_keys([‘conv1.0.weight’, ‘conv1.0.bias’, ‘conv2.0.weight’, ‘conv2.0.bias’, ‘conv3.0.weight’, ‘conv3.0.bias’, ‘conv4.0.weight’, ‘conv4.0.bias’, ‘conv5.0.weight’, ‘conv5.0.bias’, ‘conv51.weight’, ‘conv51.bias’, ‘conv52.weight’, ‘conv52.bias’, ‘conv53.weight’, ‘conv53.bias’, ‘fc.0.weight’, ‘fc.0.bias’, ‘fc.2.weight’, ‘fc.2.bias’, ‘conv6.weight’, ‘conv6.bias’, ‘conv7.weight’, ‘conv7.bias’, ‘dconv1.2.weight’, ‘dconv1.2.bias’, ‘dconv2.2.weight’, ‘dconv2.2.bias’, ‘dconv3.2.weight’, ‘dconv3.2.bias’, ‘dconv4.2.weight’, ‘dconv4.2.bias’, ‘conv8.2.weight’, ‘conv8.2.bias’, ‘conv9.2.weight’, ‘conv9.2.bias’])</p>

Diego: <p>Then <code>checkpoint</code> is already the <code>state_dict</code> so you can just load it directly, like so:</p>
<pre><code class="lang-auto">generatorX.load_state_dict(checkpoint)
</code></pre>

maryam_hayat: <p><a class="mention" href="/u/diego">@Diego</a> Thank u very much for the timely response.<br>
When I make the change as u suggested,<br>
generatorX.load_state_dict( checkpoint,strict=False)</p>
<p>I get this error . Although I used the images with same dimensions as used by authors.</p>
<p>RuntimeError: Error(s) in loading state_dict for Generator:<br>
<strong>size mismatch for conv8.2.weight:</strong> copying a param with shape torch.Size([16, 48, 5, 5]) from checkpoint, the shape in current model is torch.Size([16, 48, 1, 1]).<br>
<strong>size mismatch for conv9.2.weight:</strong> copying a param with shape torch.Size([3, 16, 5, 5]) from checkpoint, the shape in current model is torch.Size([3, 16, 1, 1]).</p>
<p>So I need to train from scratch . can i use this pretrained model ?</p>

Diego: <p>You are probably defining the wrong model for generatorX since the weights you are trying to load don’t match the model. Is there a different model to try? Or a different set of weights?</p>

maryam_hayat: <p>Thank you very much for reply. I run another file where model is trained from scratch.<br>
I am getting the following errors: '<strong>Caught RuntimeError in replica 0 on device 0’</strong><br>
in the line</p>
<p>fakeEnhanced = generatorX(realInput)</p>
<p>and the following error:<br>
<strong>’Sizes of tensors must match except in dimension 2. Got 64 and 33 (The offending index is 0)'</strong><br>
where generator method is defined inside the model.py file in the line:<br>
x6 = self.conv7(torch.cat([x5, x53_temp], dim=1))</p>
<p>I searched to correct the errors. Some people say that maybe there is some issue of DataParallel module and the original code was written to run on multiple GPUs.<br>
Do  u have idea if it could be the case</p>

user_123454321: <p>It seems x5 and x53_temp are not having same dimensions. Oddly, it says the batch dimension is not matching. Can you print the model definition please?</p>

maryam_hayat: <p><a class="mention" href="/u/user_123454321">@user_123454321</a><br>
Thanks for reply. I get the following when I print model definition:</p>
<h1>==========================================================================================<br>
Layer (type:depth-idx)                   Output Shape              Param #</h1>
<h1>├─Sequential: 1-1                        [-1, 16, 512, 512]        –<br>
|    └─ReflectionPad2d: 2-1              [-1, 3, 516, 516]         –<br>
|    └─Conv2d: 2-2                       [-1, 16, 512, 512]        1,216<br>
|    └─SELU: 2-3                         [-1, 16, 512, 512]        –<br>
|    └─BatchNorm2d: 2-4                  [-1, 16, 512, 512]        32<br>
├─Sequential: 1-2                        [-1, 32, 256, 256]        –<br>
|    └─ReflectionPad2d: 2-5              [-1, 16, 516, 516]        –<br>
|    └─Conv2d: 2-6                       [-1, 32, 256, 256]        12,832<br>
|    └─SELU: 2-7                         [-1, 32, 256, 256]        –<br>
|    └─BatchNorm2d: 2-8                  [-1, 32, 256, 256]        64<br>
├─Sequential: 1-3                        [-1, 64, 128, 128]        –<br>
|    └─ReflectionPad2d: 2-9              [-1, 32, 260, 260]        –<br>
|    └─Conv2d: 2-10                      [-1, 64, 128, 128]        51,264<br>
|    └─SELU: 2-11                        [-1, 64, 128, 128]        –<br>
|    └─BatchNorm2d: 2-12                 [-1, 64, 128, 128]        128<br>
├─Sequential: 1-4                        [-1, 128, 64, 64]         –<br>
|    └─ReflectionPad2d: 2-13             [-1, 64, 132, 132]        –<br>
|    └─Conv2d: 2-14                      [-1, 128, 64, 64]         204,928<br>
|    └─SELU: 2-15                        [-1, 128, 64, 64]         –<br>
|    └─BatchNorm2d: 2-16                 [-1, 128, 64, 64]         256<br>
├─Sequential: 1-5                        [-1, 128, 32, 32]         –<br>
|    └─ReflectionPad2d: 2-17             [-1, 128, 68, 68]         –<br>
|    └─Conv2d: 2-18                      [-1, 128, 32, 32]         409,728<br>
|    └─SELU: 2-19                        [-1, 128, 32, 32]         –<br>
|    └─BatchNorm2d: 2-20                 [-1, 128, 32, 32]         256<br>
├─Sequential: 1-6                        [-1, 128, 16, 16]         –<br>
|    └─ReflectionPad2d: 2-21             [-1, 128, 36, 36]         –<br>
|    └─Conv2d: 2-22                      [-1, 128, 16, 16]         409,728<br>
├─Sequential: 1-7                        [-1, 128, 8, 8]           –<br>
|    └─ReflectionPad2d: 2-23             [-1, 128, 20, 20]         –<br>
|    └─Conv2d: 2-24                      [-1, 128, 8, 8]           409,728<br>
├─Conv2d: 1-8                            [-1, 128, 1, 1]           1,048,704<br>
├─Sequential: 1-9                        [-1, 128, 1, 1]           –<br>
|    └─SELU: 2-25                        [-1, 128, 1, 1]           –<br>
|    └─Conv2d: 2-26                      [-1, 128, 1, 1]           16,512<br>
├─Sequential: 1-10                       [-1, 128, 32, 32]         –<br>
|    └─Conv2d: 2-27                      [-1, 128, 32, 32]         16,512</h1>
<h1>Total params: 2,581,888<br>
Trainable params: 2,581,888<br>
Non-trainable params: 0<br>
Total mult-adds (G): 3.40</h1>

user_123454321: <p>Thanks for this, although this is informative, I cannot discern the shapes of x5 and x53_temp from this, sorry. I need the class definition (mainly the forward and init functions) of generator maybe…</p>

maryam_hayat: <p><a class="mention" href="/u/user_123454321">@user_123454321</a>   thank you very much for the fast response. The error occurs in the forward function of Generator actually.<br>
One thing I noticed is that the size of images forward takes is 516x516 (height and width) when I print. Although I explicitly resized the images to 512 x512 and this is the size network accepts . This is the apparent reason of error. I am trying to check in the DataLoader where this dimension mismatch is occurring.<br>
I will post here if there is still tensor size mismatch in the later layers of network.<br>
thanks a lot for your suggestions.</p>