[Missing Keys error] Pretrained resnet18 model works but when I load the saved weights file from training this model It doesn't work

Afaq_Saeed: <pre><code class="lang-auto">
                                           batch_size=batch_size, shuffle=True,

                                           num_workers=2, drop_last=True)

val_loader = torch.utils.data.DataLoader(dataset=val_dataset,batch_size=batch_size, shuffle=False,num_workers=2, drop_last=True)
params = add_weight_decay(network, l2_value=0.0001)
optimizer = torch.optim.Adam(params, lr=learning_rate)
# loss function
loss_fn = nn.CrossEntropyLoss()#weight=class_weights)


# pre-trained weights

device = torch.device("cuda")```
# This line works
weights_path = proj_dir+"pretrained_models/model_13_2_2_2_epoch_580.pth" # start training from orignal pretrained model
# But if I run this it doesnt although this is just saved file of training this data 
weights_path = proj_dir+"training_logs/model_1/checkpoints/"+os.listdir(proj_dir+"training_logs/model_1/checkpoints")[-1] # start training from last saved weights
network.load_state_dict(torch.load(weights_path,map_location="cuda:0"))

print(len(torch.load(weights_path)))

network.to(device)
# Error Message that comes out if I run the other weights path in the second line
 Error(s) in loading state_dict for DeepLabV3:
	Missing key(s) in state_dict: "resnet.resnet.0.weight", "resnet.resnet.1.weight", "resnet.resnet.1.bias", "resnet.resnet.1.running_mean", "resnet.resnet.1.running_var", "resnet.resnet.4.0.conv1.weight", "resnet.resnet.4.0.bn1.weight", "resnet.resnet.4.0.bn1.bias", "resnet.resnet.4.0.bn1.running_mean", "resnet.resnet.4.0.bn1.running_var", "resnet.resnet.4.0.conv2.weight", "resnet.resnet.4.0.bn2.weight", "resnet.resnet.4.0.bn2.bias", "resnet.resnet.4.0.bn2.running_mean", "resnet.resnet.4.0.bn2.running_var", "resnet.resnet.4.1.conv1.weight", "resnet.resnet.4.1.bn1.weight", "resnet.resnet.4.1.bn1.bias", "resnet.resnet.4.1.bn1.running_mean", "resnet.resnet.4.1.bn1.running_var", "resnet.resnet.4.1.conv2.weight", "resnet.resnet.4.1.bn2.weight", "resnet.resnet.4.1.bn2.bias", "resnet.resnet.4.1.bn2.running_mean", "resnet.resnet.4.1.bn2.running_var", "resnet.resnet.5.0.conv1.weight", "resnet.resnet.5.0.bn1.weight", "resnet.resnet.5.0.bn1.bias", "resnet.resnet.5.0.bn1.running_mean", "resnet.resnet.5.0.bn1.running_var", "resnet.resnet.5.0.conv2.weight", "resnet.resnet.5.0.bn2.weight", "resnet.resnet.5.0.bn2.bias", "resnet.resnet.5.0.bn2.running_mean", "resnet.resnet.5.0.bn2.running_var", "resnet.resnet.5.0.downsample.0.weight", "resnet.resnet.5.0.downsample.1.weight", "resnet.resnet.5.0.downsample.1.bias", "resnet.resnet.5.0.downsample.1.running_mean", "resnet.resnet.5.0.downsample.1.running_var", "resnet.resnet.5.1.conv1.weight", "resnet.resnet.5.1.bn1.weight", "resnet.resnet.5.1.bn...
	Unexpected key(s) in state_dict: "pretrained_model.resnet.resnet.0.weight", "pretrained_model.resnet.resnet.1.weight", "pretrained_model.resnet.resnet.1.bias", "pretrained_model.resnet.resnet.1.running_mean", "pretrained_model.resnet.resnet.1.running_var", "pretrained_model.resnet.resnet.1.num_batches_tracked", "pretrained_model.resnet.resnet.4.0.conv1.weight", "pretrained_model.resnet.resnet.4.0.bn1.weight", "pretrained_model.resnet.resnet.4.0.bn1.bias", "pretrained_model.resnet.resnet.4.0.bn1.running_mean", "pretrained_model.resnet.resnet.4.0.bn1.running_var", "pretrained_model.resnet.resnet.4.0.bn1.num_batches_tracked", "pretrained_model.resnet.resnet.4.0.conv2.weight", "pretrained_model.resnet.resnet.4.0.bn2.weight", "pretrained_model.resnet.resnet.4.0.bn2.bias", "pretrained_model.resnet.resnet.4.0.bn2.running_mean", "pretrained_model.resnet.resnet.4.0.bn2.running_var", "pretrained_model.resnet.resnet.4.0.bn2.num_batches_tracked", "pretrained_model.resnet.resnet.4.1.conv1.weight", "pretrained_model.resnet.resnet.4.1.bn1.weight", "pretrained_model.resnet.resnet.4.1.bn1.bias", "pretrained_model.resnet.resnet.4.1.bn1.running_mean", "pretrained_model.resnet.resnet.4.1.bn1.running_var", "pretrained_model.resnet.resnet.4.1.bn1.num_batches_tracked", "pretrained_model.resnet.resnet.4.1.conv2.weight", "pretrained_model.resnet.resnet.4.1.bn2.weight", "pretrained_model.resnet.resnet.4.1.bn2.bias", "pretrained_model.resnet.resnet.4.1.bn2.running_mean", "pretrained_model.resnet.re...</code></pre>

ptrblck: <p>I don’t know how you’ve created the model, but based on the error message I assume you are using a custom <code>nn.Module</code> with an internal <code>resnet</code>.<br>
The raised error while loading the <code>state_dict</code> points towards an attribute name mismatch, i.e. while storing the <code>state_dict</code> it seems you’ve used the internal <code>self.resnet</code> attribute while the current model uses <code>self.pretrained_model.resnet</code>. You could either make sure to use the same model definition or change the keys in the <code>state_dict</code> manually.</p>
<p>PS: you can post code snippets by wrapping them into three backticks ```, which makes debugging easier. <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>