Error(s) in loading state_dict missing keys in inference model

Navdeep: <p>Hi,<br>
I have my model like this<br>
from hrnet import HRNet</p>
<p>Model = HRNet(in_c, out_c )<br>
class modyfied_model():</p>
<pre><code>        def __init__(self, backbone):
        super(new_model, self).__init__()
        self.backbone = backbone
       self.conv1 = nn.Conv2d(128,64,3,2, padding=1)
       self.conv2 = nn.Conv2d(64,50,3,2, padding=1)
       self.conv3 = nn.Conv2d(50,50,3,2, padding=1)
                                      
    
    
def forward(self, x):
    x = self.backbone(x)
    x = self.conv1(x)
    #print(x.shape)
    x = self.conv2(x)
    #print(x.shape)
    x = self.conv3(x)
    #print(x.shape)
    #x = self.max_pool(x)
    #print(x.shape)
    x = x.view(2,25,1024)
    #print(x.shape)
    #x = self.flatten(x)
    #coord_x = self.pred_x(x)
    coord_x = x[0,:,:].reshape(1,25,1024)
    #print(coord_x.shape)
    #coord_y = self.pred_y(x)
    coord_y = x[1,:,:].reshape(1,25,1024)
    #print(coord_y.shape)
    return coord_x, coord_y
</code></pre>
<p>new_model = modified_model(Model)<br>
… training and saving the state dictionary<br>
for inference I use the same model<br>
Model  = HRNet(in, out)<br>
modified_model()…<br>
test_model = modified_model(Model)</p>
<p>PATH = ‘path/to/checkpoint/’<br>
checkpoint = torch.load(PATH,map_location=torch.device(‘cpu’))<br>
model.load_state_dict(checkpoint[‘state_dict’])<br>
now it is giving error missing keys…</p>

Michal_Bogacz: <p>Hi,<br>
Can you show the part where you save the model.</p>
<p>As per documentation, there are two ways of saving the model.<br>
First - save state dict, where only the weights are saved <code>torch.save(model.state_dict(), PATH)</code> and than loading model requires creating the model class separately from loading weights <code>model = TheModelClass(*args, **kwargs) model.load_state_dict(torch.load(PATH))</code></p>
<p>Second - saving and loading whole model <code>torch.save(model, PATH)</code> and <code>model = torch.load(PATH)</code></p>
<p>I presume you have mixed the methods, but to be sure it would be great to see the saving code.</p>
<p><a href="https://pytorch.org/tutorials/beginner/saving_loading_models.html#:~:text=%2C%204675714720%5D%7D%5D-,Saving%20%26%20Loading%20Model%20for%20Inference,(torch.load(PATH)" class="inline-onebox" rel="noopener nofollow ugc">Saving and Loading Models — PyTorch Tutorials 1.13.1+cu117 documentation</a>)%0Amodel.eval(),-NOTE</p>

Navdeep: <p>Hi,<br>
Thanks for the reply,<br>
I saved the model as follows:</p>
<p>checkpoint = {<br>
‘epoch’: epoch + 1,<br>
‘valid_loss_min’: valid_loss,<br>
‘state_dict’: model.state_dict(),<br>
‘optimizer’: optimizer.state_dict(),<br>
}<br>
def save_ckp(state, is_best, checkpoint_path, best_model_path):<br>
“”"<br>
state: checkpoint we want to save<br>
is_best: is this the best checkpoint; min validation loss<br>
checkpoint_path: path to save checkpoint<br>
best_model_path: path to save best model<br>
“”"<br>
f_path = checkpoint_path<br>
# save checkpoint data to the path given, checkpoint_path<br>
torch.save(state, f_path)<br>
# if it is a best model, min validation loss<br>
if is_best:<br>
best_fpath = best_model_path<br>
# copy that checkpoint file to best path given, best_model_path<br>
shutil.copyfile(f_path, best_fpath)</p>