Accessing weights in a pretrained model fro handwritten text recognition

BouzidiImen: <p>I am trying to access weights in a model that when loaded I find these keys:</p>
<p>dict_keys([‘optimizer_state_dict’, ‘epoch’, ‘scaler_state_dict’, ‘best’, ‘curriculum_config’, ‘encoder_state_dict’, ‘decoder_state_dict’])</p>
<p>This is a screenshot of he output I have:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/1/4/14d199b09bf218f0fe39164f39abf1d82a9a5698.png" data-download-href="https://discuss.pytorch.org/uploads/default/14d199b09bf218f0fe39164f39abf1d82a9a5698" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/1/4/14d199b09bf218f0fe39164f39abf1d82a9a5698.png" alt="image" data-base62-sha1="2YaCcfwEutOv5Erm8cvAqo5w0s0" width="690" height="59" data-dominant-color="191B1D"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1376×118 5.18 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>I would really appreciate your help.<br>
Thannk you in advance! <img src="https://discuss.pytorch.org/images/emoji/apple/blush.png?v=12" title=":blush:" class="emoji" alt=":blush:" loading="lazy" width="20" height="20"></p>

ptrblck: <p>It seems you are loading a custom <code>dict</code>, which includes a few <code>state_dict</code>s. To check the weights (or any other parameter/buffer) of the models(s), you could use:</p>
<pre><code class="lang-python">checkpoint = torch.load(...)
encoder_state_dict = checkpoint['encoder_state_dict']
print(encoder_state_dict[parameter_name])
# same for decoder_state_dict
</code></pre>

BouzidiImen: <p>Thank you so much for your reply <img src="https://discuss.pytorch.org/images/emoji/apple/pray.png?v=12" title=":pray:" class="emoji" alt=":pray:" loading="lazy" width="20" height="20">, I am now able to load the weights thanks to you.<br>
One extra question, I did exactly what you told me and this is what I got:</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/c/d/cd29458375bb046b370264c4a6484618201344f5.png" data-download-href="https://discuss.pytorch.org/uploads/default/cd29458375bb046b370264c4a6484618201344f5" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/c/d/cd29458375bb046b370264c4a6484618201344f5.png" alt="image" data-base62-sha1="tgWiVAahYGroGY8govOF5n3gwbX" width="690" height="302" data-dominant-color="282828"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1379×605 36.8 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>I want to use this model to make a prediction for one image.<br>
Could you please help me figure out how to do so?</p>

ptrblck: <p>You would need to create a model object first and load the <code>state_dict</code> afterwards:</p>
<pre><code class="lang-python">model = MyModel()
model.load_state_dict(encoder_state_dict)
</code></pre>
<p>where <code>MyModel</code> is defined in your code somewhere as a custom <code>nn.Module</code> class.</p>

BouzidiImen: <p>Thank you so much for your help ^^<br>
What I did is the following:</p>
<pre><code class="lang-auto">features = encoder(image)
output = decoder(features)
output

</code></pre>
<p>and I got the following output:</p>
<pre><code class="lang-auto">tensor([[[[-1.0373e+01, -9.2096e+00, -5.9885e+00,  ..., -5.2428e+02,
           -3.8752e+02, -4.4228e+02],
          [-2.8680e+00, -3.1839e+00, -2.2322e+00,  ..., -5.3969e+02,
           -5.8710e+02, -6.5721e+02],
          [-5.8041e+00, -6.1079e+00, -5.0006e+00,  ..., -5.9030e+02,
           -6.4768e+02, -7.5395e+02],
          ...
</code></pre>
<p>Do you have an idea about getting the prediction that is a paragraph from this output?<br>
PS: The CTCloss is then computed to make the paragraph transcription and I can’t seem to figure out how it must be loaded.</p>

ptrblck: <p>I’m not familiar with your use case, but based on the output it seems you might be working on a sequence prediction task? If so, is each sample supposed to predict a word/token etc.?<br>
In case you are using another repository as your code base, I would probably check if this repo might already implement a <code>predict</code> method.</p>