Begginer: Loading bin model and predicting image

username: tenet2001tenet
cooked: <p>I’m a begginer using Pytorch, and i’m trying new things. I found a pre-trained model in PyTorch and i’d like to use it to extract the last layer’s output of the network (not the labels, but the last matrix used to extract the layers).</p>
<p>This is the network im using: <a href="https://onedrive.live.com/?authkey=%21AMkPexmU%2DlBVnZs&amp;id=C826C2D6F4C7D993%213757&amp;cid=C826C2D6F4C7D993" rel="nofollow noopener">https://onedrive.live.com/?authkey=!AMkPexmU-lBVnZs&amp;id=C826C2D6F4C7D993!3757&amp;cid=C826C2D6F4C7D993</a></p>
<p>what i’m trying is:</p>
<pre><code class="lang-auto">def image_loader(image_name):

    """load image, returns cuda tensor"""

    image = Image.open(image_name)

    image = loader(image).float()

    image = Variable(image, requires_grad=True)

    image = image.unsqueeze(0)  #this is for VGG, may not be needed for ResNet

    return image.cuda()  #assumes that you're using GPU
</code></pre>
<pre><code class="lang-auto">model = torch.load('pytorch_model.bin', map_location='cpu')
image = image_loader(imagepath)
predictions = model(image)
</code></pre>
<p>but i get the error:<br>
<code>TypeError: 'collections.OrderedDict' object is not callable</code></p>
<p>And i don’t know why…<br>
Any suggestions?</p>

username: Usama_Hasan
cooked: <p>Hy there</p>
<aside class="quote no-group quote-modified" data-username="tenet2001tenet" data-post="1" data-topic="90006">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/t/838e76/48.png" class="avatar"> tenet2001tenet:</div>
<blockquote>
<p>model = torch.load(‘pytorch_model.bin’, map_location=‘cpu’)</p>
</blockquote>
</aside>
<p>This will load the pretrained parameters of the model not the model defination itself, which are stored in the form of Ordered Dict hence the error.<br>
You should create your model class first.</p>
<pre><code class="lang-auto">class Net(nn.Module):
        // Your Model for which you want to load parameters 

model = Net()
torch.optim.SGD(lr=0.001) #According to your own Configuration.
checkpoint = torch.load(pytorch_model)
model.load_state_dict(checkpoint['model'])
optimizer.load_state_dict(checkpoint['opt']) 

</code></pre>
<p>Also if you want to use pretrained model in pytorch, the best way is to get them through<br>
torchvision package. Check this tutorial.<br>
<a href="https://pytorch.org/tutorials/beginner/transfer_learning_tutorial.html" class="onebox" target="_blank" rel="noopener nofollow ugc">https://pytorch.org/tutorials/beginner/transfer_learning_tutorial.html</a></p>

username: tenet2001tenet
cooked: <p>Hey! Considering this:<br>
</p><aside class="onebox githubblob">
  <header class="source">
      <a href="https://github.com/microsoft/unilm/blob/master/layoutlm/layoutlm/modeling/layoutlm.py" target="_blank" rel="nofollow noopener">github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://github.com/microsoft/unilm/blob/master/layoutlm/layoutlm/modeling/layoutlm.py" target="_blank" rel="nofollow noopener">microsoft/unilm/blob/master/layoutlm/layoutlm/modeling/layoutlm.py</a></h4>
<pre><code class="lang-py">import logging

import torch
from torch import nn
from torch.nn import CrossEntropyLoss, MSELoss
from transformers import BertConfig, BertModel, BertPreTrainedModel
from transformers.modeling_bert import BertLayerNorm

logger = logging.getLogger(__name__)

LAYOUTLM_PRETRAINED_MODEL_ARCHIVE_MAP = {}

LAYOUTLM_PRETRAINED_CONFIG_ARCHIVE_MAP = {}


class LayoutlmConfig(BertConfig):
    pretrained_config_archive_map = LAYOUTLM_PRETRAINED_CONFIG_ARCHIVE_MAP
    model_type = "bert"

    def __init__(self, max_2d_position_embeddings=1024, **kwargs):
</code></pre>

  This file has been truncated. <a href="https://github.com/microsoft/unilm/blob/master/layoutlm/layoutlm/modeling/layoutlm.py" target="_blank" rel="nofollow noopener">show original</a>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>

<p>Should I use on of these?</p>
<p>src <a href="https://github.com/microsoft/unilm/tree/master/layoutlm/layoutlm" rel="nofollow noopener">https://github.com/microsoft/unilm/tree/master/layoutlm/layoutlm</a></p>

username: Usama_Hasan
cooked: <p>It depends on what your goal is, if you want to learn I highly recommend you to check the tutorial at pytorch website. else if you want to test a model on some dataset you can try this one too, but I guess it would be hard considering the complexity of the model.<br>
<a href="https://pytorch.org/tutorials/" class="onebox" target="_blank" rel="nofollow noopener">https://pytorch.org/tutorials/</a></p>

username: tenet2001tenet
cooked: <p>Firstly i’d like to run it and see the result. And yes, this is the model i’d like to use. Can you give me some help? Thanks!</p>

username: Usama_Hasan
cooked: <p>Sure, the community can help you alot if you ran into an error, or find it difficult to understand something. Plus I’ll still suggest you to get some basic understanding of pytorch first.</p>

username: Vishesh_Kaushik
cooked: <p>Hello. <a class="mention" href="/u/tenet2001tenet">@tenet2001tenet</a> . Have you run the pre-trained model of LyoutLM? Which model definition did you use in loading the model using state_dict ?</p>