Convert Pytorch checkpoint to state_dict only

Peter_Featherstone: <p>I have a model checkpoint saved as <code>torch.save(model, 'model.pt')</code>, which includes the model definition. How can I load the state dict only without requiring any of the source code for the model definition?</p>
<p>I want something like <code>torch.load('model.pt', state_dict_only=True)</code> or something like that. I know this doesn’t exist but is there a similar hack?</p>
<p>Basically I have a checkpoint but none of the original code used to save the model. So I want to work with the state dict only.</p>

SchulzKilian: <p>Does loading the checkpoint and then saying .state_dict work for you?<br>
Like so:</p>
<aside class="quote no-group">
<blockquote>
<p>model_checkpoint = torch.load(‘model.pt’)<br>
state_dict = model_checkpoint.state_dict()</p>
</blockquote>
</aside>

Peter_Featherstone: <p>No because I don’t have the original code. So the line</p>
<pre><code class="lang-auto">model_checkpoint = torch.load(‘model.pt’)
</code></pre>
<p>breaks</p>

SchulzKilian: <p>so in what shape do you have the checkpoint saved? if its a dictionary you can access by checkpoint[“state_dict”]</p>

toufiq: <p>From my experience, the state_dict alone is not usable without the model implementation class.<br>
Can you either implement the model class or find a repository with the implementation and try the ff:</p>
<pre><code class="lang-auto">loaded_state_dict = torch.load('model.pt')['state_dict']
model.load_state_dict(loaded_state_dict)
</code></pre>
<p>Reply in case you run into state_dict key errors, etc…</p>

AlphaBetaGamma96: <p>Hi <a class="mention" href="/u/peter_featherstone">@Peter_Featherstone</a>,</p>
<p><a class="mention" href="/u/toufiq">@toufiq</a> is correct that you can only load a model if you have access to its original source code. All the <code>state_dict</code> object does is save the weights and buffers of an <code>nn.Module</code> to a dict, and when you load the <code>state_dict</code> object, it just matches the <code>keys</code> of the <code>dict</code> to your existing <code>nn.Module</code>.</p>

Peter_Featherstone: <p>Thank you all. Yes I understand the subtleties. Basically I have a pytorch model file I got from somewhere but i don’t have the code. I can reverse engineer the model but it won’t have exactly the same code. If i just had the state dict, i could figure out how to load the weights. But I just need pytorch to load the state dict without complaining it can’t find the the code. I’m tempted to hack the .pt file by unzipping it and manually extracting the state dict.</p>

AlphaBetaGamma96: <aside class="quote no-group" data-username="Peter_Featherstone" data-post="7" data-topic="204307">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/peter_featherstone/48/12813_2.png" class="avatar"> Peter_Featherstone:</div>
<blockquote>
<p>But I just need pytorch to load the state dict without complaining it can’t find the the code.</p>
</blockquote>
</aside>
<p>Can you explain what you mean by this? If you have an <code>nn.Module</code> with the same <code>key</code> structure, PyTorch shouldn’t complain, I think.</p>