AttributeError: 'collections.OrderedDict' object has no attribute 'named_children'

username: Lu_Zou
cooked: <p>import numpy as np</p>
<p>import torch</p>
<p>from data_load import NpyDataset</p>
<p>from main import Generator</p>
<p>generator = Generator(channels=15)</p>
<p>checkpoint = torch.load(‘netd_0.pth’)</p>
<p>generator.load_state_dict(checkpoint, strict=False)</p>
<p>generator.eval()</p>
<p>device = torch.device(‘cpu’)</p>
<p>fix_noises = torch.randn(64, 100, 4, 4).to(device)</p>
<p>dataset = NpyDataset(‘10000_npy/’)</p>
<p>fix_fake_image = generator(fix_noises)</p>
<p>fix_fake_image = (fix_fake_image * (dataset.max - dataset.min)) + dataset.min</p>
<p>fix_fake_image = fix_fake_image[:64].detach().cpu().numpy()</p>
<p>np.save(‘PERM/perm_1’, fix_fake_image)</p>
<p>RuntimeError: Error(s) in loading state_dict for Generator:<br>
size mismatch for main_module.0.weight: copying a param with shape torch.Size([256, 15, 4, 5]) from checkpoint, the shape in current model is torch.Size([100, 1024, 4, 3]).<br>
size mismatch for main_module.0.bias: copying a param with shape torch.Size([256]) from checkpoint, the shape in current model is torch.Size([1024]).<br>
size mismatch for main_module.1.weight: copying a param with shape torch.Size([256]) from checkpoint, the shape in current model is torch.Size([1024]).<br>
size mismatch for main_module.1.bias: copying a param with shape torch.Size([256]) from checkpoint, the shape in current model is torch.Size([1024]).<br>
size mismatch for main_module.1.running_mean: copying a param with shape torch.Size([256]) from checkpoint, the shape in current model is torch.Size([1024]).<br>
size mismatch for main_module.1.running_var: copying a param with shape torch.Size([256]) from checkpoint, the shape in current model is torch.Size([1024]).<br>
size mismatch for main_module.3.weight: copying a param with shape torch.Size([512, 256, 4, 5]) from checkpoint, the shape in current model is torch.Size([1024, 512, 4, 3]).<br>
size mismatch for main_module.6.weight: copying a param with shape torch.Size([1024, 512, 4, 5]) from checkpoint, the shape in current model is torch.Size([512, 256, 4, 2]).<br>
size mismatch for main_module.6.bias: copying a param with shape torch.Size([1024]) from checkpoint, the shape in current model is torch.Size([256]).<br>
size mismatch for main_module.7.weight: copying a param with shape torch.Size([1024]) from checkpoint, the shape in current model is torch.Size([256]).<br>
size mismatch for main_module.7.bias: copying a param with shape torch.Size([1024]) from checkpoint, the shape in current model is torch.Size([256]).<br>
size mismatch for main_module.7.running_mean: copying a param with shape torch.Size([1024]) from checkpoint, the shape in current model is torch.Size([256]).<br>
size mismatch for main_module.7.running_var: copying a param with shape torch.Size([1024]) from checkpoint, the shape in current model is torch.Size([256]).</p>

username: ptrblck
cooked: <p>The error message from the title is raised if you try to call <code>.named_children()</code> on a <code>state_dict</code> instead of the model object. The errors mentioned in the post are raised since the model architecture and <code>state_dict</code> parameters do not match in shape.</p>