How to use predefined models in a closed system

username: bing
cooked: <p>Hi Guys,<br>
I want to use predefined models in a system with no internet connectivity.<br>
I know how to download it but I don’t know how to use them in a model-<br>
I downloaded it using the below method -<br>
<aside class="onebox stackexchange">
  <header class="source">
      <a href="https://stackoverflow.com/questions/52628270/is-there-any-way-i-can-download-the-pre-trained-models-available-in-pytorch-to-a" target="_blank" rel="nofollow noopener">stackoverflow.com</a>
  </header>
  <article class="onebox-body">
      <a href="https://stackoverflow.com/users/8085890/gopalkrizna" target="_blank" rel="nofollow noopener">
    <img alt="gopalkrizna" src="https://lh5.googleusercontent.com/-B4HnFBD_cVM/AAAAAAAAAAI/AAAAAAAAAC8/6z-DsM2EJ4o/photo.jpg?sz=128" class="thumbnail onebox-avatar" width="60" height="60">
  </a>
<h4>
  <a href="https://stackoverflow.com/questions/52628270/is-there-any-way-i-can-download-the-pre-trained-models-available-in-pytorch-to-a" target="_blank" rel="nofollow noopener">Is there any way I can download the pre-trained models available in PyTorch to a specific path?</a>
</h4>

<div class="tags">
  <strong>python, deep-learning, pytorch, pre-trained-model</strong>
</div>

<div class="date">
  asked by
  
  <a href="https://stackoverflow.com/users/8085890/gopalkrizna" target="_blank" rel="nofollow noopener">
    gopalkrizna
  </a>
  on <a href="https://stackoverflow.com/questions/52628270/is-there-any-way-i-can-download-the-pre-trained-models-available-in-pytorch-to-a" target="_blank" rel="nofollow noopener">01:35PM - 03 Oct 18 UTC</a>
</div>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>
</p>
<p>but now I don’t know how to use them in a network-</p>

username: babababadukeduke
cooked: <p>Given that you have the pre-trained model, you can always use load() to load the state dictionary from pre-trained model file into your model. The link below is very detailed and answers what you need. <a href="https://pytorch.org/tutorials/beginner/saving_loading_models.html" rel="nofollow noopener">https://pytorch.org/tutorials/beginner/saving_loading_models.html</a></p>

username: bing
cooked: <p>I mean I want to use, let’s say - resnet50 as a transfer learning but using the above method, they are asking me to define the model.<br>
I used the below statement -</p>
<pre><code class="lang-auto">model_transfer = model.load_state_dict(torch.load('model.path'))

</code></pre>

username: babababadukeduke
cooked: <p>You need to define the model before you load weight in the model. This definition has to match the values in the state dictionary. If you are using standard ResNet50 for ImageNet then you can do</p>
<p>import torchvision.models as models<br>
model = models.resnet50()</p>
<p>and then load your state dictionary into the model</p>

username: kshitij
cooked: <p>It is like <a class="mention" href="/u/babababadukeduke">@babababadukeduke</a> has mentioned, you need to first define the model.</p>
<p>Below is the example with your own defined model.</p>
<pre><code class="lang-python"># Custom Model
class TwoLayerNet(torch.nn.Module):
   def __init__(self, D_in, H, D_out):
       """
       In the constructor we instantiate two nn.Linear modules and assign them as
       member variables.
       """
       super(TwoLayerNet, self).__init__()
       self.linear1 = torch.nn.Linear(D_in, H)
       self.linear2 = torch.nn.Linear(H, D_out)

   def forward(self, x):
       """
       In the forward function we accept a Tensor of input data and we must return
       a Tensor of output data. We can use Modules defined in the constructor as
       well as arbitrary operators on Tensors.
       """
       h_relu = self.linear1(x).clamp(min=0)
       y_pred = self.linear2(h_relu)
       return y_pred

# Model Object / Blueprint
model = TwoLayerNet(D_in, H, D_out)


# Load the weights into the model.
model_transfer = model.load_state_dict(torch.load('model.path')) # Weight from same blue print.
</code></pre>

username: bing
cooked: <p>I just did that but I don’t think it is behaving exactly I intend it to behave-</p>
<pre><code class="lang-auto">model_transfer = models.resnet50()
model_transfer = model_transfer.load_state_dict(torch.load(model_path))
</code></pre>
<p>Now, if I print  <code>model_transfer</code>  then isn’t the complete architecture should come up but I am getting -<br>
<code>&lt;All keys matched successfully&gt;</code>  and no architecture.<br>
Also, if then just want to modify last layers using  <code>model_transfer.fc.in_features</code>  then it should work but instead I am getting<br>
<code>AttributeError: '_IncompatibleKeys' object has no attribute 'fc'</code></p>

username: babababadukeduke
cooked: <p><code> &lt;All keys matched successfully&gt;</code> means that you were able to successfully load the model parameters in the file. You can do <code>print(model_transfer)</code> and see the architecture. If you can post the code of how you are trying to modify the model then maybe I will be able to point out the exact problem.</p>

username: bing
cooked: <p>I went  <a href="https://github.com/pytorch/vision/blob/master/torchvision/models/resnet.py" rel="nofollow noopener">here</a> , then downloaded the required model file and saved in my computer.<br>
Then, I did -</p>
<pre><code class="lang-auto">import torchvision.models as models
import torch.nn as nn
model_path = 'Path_to_model/resnet_50.pth'
model_transfer = models.resnet50()
model_transfer = model_transfer.load_state_dict(torch.load(model_path))
print(model_transfer)
</code></pre>
<p>and I am getting <code>&lt;All keys matched successfully&gt;</code><br>
on doing this.</p>

username: kshitij
cooked: <pre><code class="lang-python">import torchvision.models as models
import torch.nn as nn
model_path = 'Path_to_model/resnet_50.pth'
model_transfer = models.resnet50()
missing_keys, unexpected_keys = model_transfer.load_state_dict(torch.load(model_path))
print(model_transfer)
</code></pre>
<p><a href="https://pytorch.org/docs/stable/nn.html?highlight=load_state_dict#torch.nn.Module.load_state_dict" class="onebox" target="_blank" rel="nofollow noopener">https://pytorch.org/docs/stable/nn.html?highlight=load_state_dict#torch.nn.Module.load_state_dict</a></p>
<p>Looking at the documentation, <code>load_state_dict</code> returns <code>missing_keys,unexpected_keys</code> and loads the model in the <code>model_transfer</code> object itself.</p>

username: bing
cooked: <p>Thank You, it worked.</p>