How to load model weights that are stored as an ordereddict?

seankala: <p>Hello. I’m not sure if I’m just unfamiliar with saving and loading Torch models, but I’m facing this predicament and am not sure how to proceed about it.</p>
<p>I’m currently wanting to load someone else’s model to try and run it. I downloaded their <code>pt</code> file that contains the model, and upon performing <code>model = torch.load(PATH)</code> I noticed that <code>model</code> is a dictionary with the keys <code>model</code>, <code>opt</code>, and <code>optims</code>.</p>
<p>I’ve never really seen that before, but I figured that the person saved the optimizer as well as the model’s weights. I assigned <code>model['model']</code> to another variable, but when I tried to run the model via a <code>output = model2(input)</code> call I got a <code>TypeError: 'collections.OrderedDict' object is not callable</code> and realized that the weights were stored as an <code>ordereddict</code>.</p>
<p>How might I proceed to achieve what I want? I don’t have much experience with loading and using pre-trained models, but something tells me it should be relatively straightforward.</p>

ptrblck: <p>The stored checkpoints contains most likely the <code>state_dict</code>s of the model and optimizer.<br>
You would have to create a model instance first and load the <code>state_dict</code> afterwards as explained in the <a href="https://pytorch.org/docs/stable/notes/serialization.html">serialization docs</a>:</p>
<pre><code class="lang-python">model = MyModel()
optimizer = torch.optim.Adam(model.parameters(), lr=1e-3) # change to whatever optimizer was used

checkpoint = torch.load(path_to_checkpoint)
model.load_state_dict(checkpoint['model'])
optimizer.load_state_dict(checkpoint['opt'])
</code></pre>
<p>Note that you need the model definition to create the model instance.</p>

Vishesh_Kaushik: <p>Thanks <a class="mention" href="/u/ptrblck">@ptrblck</a> . How to get the model definition? I have OrderedDict for pre-trained LayouLM model. I dont know the model definition. I have one config.json file and one args.bin file with me. I have just started learning pytorch so I dont have much idea about it.</p>

ptrblck: <p>I’m not sure how the <code>config.json</code> and <code>args.bin</code> files are used, but you would need to use the model source code in order to create an instance of it before loading the <code>state_dict</code>.<br>
How are you creating an object of the model at the moment?</p>

Vishesh_Kaushik: <p>I am trying to load pre-trained LayoutLM model. I have downloaded the pre-trained model from <a href="https://drive.google.com/open?id=1Htp3vq8y2VRoTAwpHbwKM0lzZ2ByB8xM" rel="noopener nofollow ugc">https://drive.google.com/open?id=1Htp3vq8y2VRoTAwpHbwKM0lzZ2ByB8xM</a>. It has only the state_dict and not the model itself.<br>
I am using the classes given on the following link for model definition <a href="https://huggingface.co/transformers/_modules/transformers/models/layoutlm/modeling_layoutlm.html#LayoutLMModel" class="inline-onebox" rel="noopener nofollow ugc">transformers.models.layoutlm.modeling_layoutlm — transformers 4.5.0.dev0 documentation</a><br>
Am I on the right path?</p>

ptrblck: <p>The latter link seems to point to the model definition, so you might create an object using:</p>
<pre><code class="lang-python">model = LayoutLMModel(...)
</code></pre>
<p>and load the <code>state_dict</code> via:</p>
<pre><code class="lang-python">model.load_state_dict(torch.load(PATH))
</code></pre>

Vishesh_Kaushik: <p><a class="mention" href="/u/ptrblck">@ptrblck</a>. Following your suggestions,<br>
I have tried doing<br>
model = LayoutlmModel.from_pretrained(“microsoft/layoutlm-base-uncased”)<br>
then<br>
model.load_state_dict(torch.load(‘pytorch_model.bin’, map_location=‘cpu’))</p>
<p>Following error comes<br>
RuntimeError: Error(s) in loading state_dict for LayoutlmModel:<br>
Missing key(s) in state_dict: “embeddings.word_embeddings.weight”, “embeddings.position_embeddings.weight”, “embeddings.x_position_embeddings.weight”, “embeddings.y_position_embeddings.weight”, …</p>
<p>but both the keys in state_dict(pytorch_model.bin)  and the keys in the model are same then why this error is shown?</p>
<p>PS : model = LayoutlmModel.from_pretrained(“microsoft/layoutlm-base-uncased”,state_dict=torch.load(‘pytorch_model.bin’, map_location=‘cpu’)). This has worked for me.<br>
Thank you <a class="mention" href="/u/ptrblck">@ptrblck</a> . Your suggestions were valuable.</p>