How to convert pretrained .pth extension pytorch model into torchvision model to load on android

kamlesh.panchal: <p>Hello</p>
<p>I have one .pth extension pytorch model this model works and predict correctly on web app but now i want to use and load this model on android app i know i have to convert this .pth extension model into torchvision for load model on android i have found code snippet and guide on pytorch guide to convert pretrained model into torchvision this is following code and its working correctly and convert pretrained mobile net model into torchvision but how to convert my .pth extension pytorch model into torchvision what changes i have to make in code i have tried to put my model path with model variable in following code but its not working.</p>
<p>import torch<br>
import torchvision</p>
<p>model = torchvision.models.resnet18(pretrained=True)<br>
model.eval()<br>
example = torch.rand(1, 3, 224, 224)<br>
traced_script_module = torch.jit.trace(model, example)<br>
traced_script_module.save(“app/src/main/assets/model.pt”)</p>

supriyar: <p>If you already have a pre-trained torchscript model (.pth file), you don’t need to use torchvision to load it. In the tutorial, we use torchvision to get the model so you can skip this step as you already have the model.</p>
<p>You can follow the rest of the tutorial steps to load the model and pass in the inputs based on what it expects. See link below for more information -<br>
<aside class="onebox whitelistedgeneric">
  <header class="source">
      <img src="https://pytorch.org/favicon.ico?" class="site-icon" width="32" height="32">
      <a href="https://pytorch.org/mobile/android/#5-loading-torchscript-module" target="_blank" rel="nofollow noopener">pytorch.org</a>
  </header>
  <article class="onebox-body">
    <img src="https://pytorch.org/assets/images/pytorch-logo.png" class="thumbnail onebox-avatar" width="500" height="500">

<h3><a href="https://pytorch.org/mobile/android/#5-loading-torchscript-module" target="_blank" rel="nofollow noopener">PyTorch</a></h3>

<p>An open source deep learning platform that provides a seamless path from research prototyping to production deployment.</p>


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

kamlesh.panchal: <p>Thanks for your reply<br>
yes i have pre-trained pytorch model with extension .pth but i want to load this on android so this pytorch model cant load directly on android for loading pytorch model on adnroid i need to convert into torchscript model right?</p>
<p>so can you help me how can i convert my .pth pytorch model into torchscipt model for load on andorid.</p>

David_Reiss: <p>The code snippet in your original post should be sufficient.  <code>torch.jit.trace</code> will produce TorchScript, and the file you get from calling <code>save</code> will load on Android.</p>

kamlesh.panchal: <p>Hi <a class="mention" href="/u/david_reiss">@David_Reiss</a></p>
<p>Thanks for reply i m using this following code for produce torchscript is it all correct? i m using torch version 1.3.0 and torchvision 0.4.1</p>
<p>import torch<br>
import torchvision</p>
<p>model = “/home/kamleshpanchal/Downloads/PytorchMobile-master/agestage250.pth”<br>
input_tensor = torch.rand(1,3,224,224)</p>
<p>script_model = torch.jit.trace(model,input_tensor)<br>
script_model.save(“converted.pt”)</p>
<p>after run this code i m getting following error</p>
<p>Traceback (most recent call last):<br>
File “convert.py”, line 10, in <br>
script_model = torch.jit.trace(model,input_tensor)<br>
File “/usr/local/lib/python3.6/dist-packages/torch/jit/<strong>init</strong>.py”, line 880, in trace<br>
name = _qualified_name(func)<br>
File “/usr/local/lib/python3.6/dist-packages/torch/_jit_internal.py”, line 636, in _qualified_name<br>
name = obj.<strong>name</strong><br>
AttributeError: ‘str’ object has no attribute ‘<strong>name</strong>’</p>

David_Reiss: <p>It looks like you’re just passing the model file name to <code>torch.jit.trace</code>.  You need to load the model first and then pass the model object instead.</p>

kamlesh.panchal: <p><a class="mention" href="/u/david_reiss">@David_Reiss</a> I m using following code for load model and passing model object Please look into it. is it correct way?</p>
<p>import torch<br>
import torchvision<br>
model = torch.load(“agestage250.pth”)<br>
model.eval()<br>
input_tensor = torch.rand(1,3,224,224)</p>
<p>script_model = torch.jit.trace(model,input_tensor)<br>
script_model.save(“converted.pt”)</p>
<p>after run this code i m getting following error</p>
<p>Traceback (most recent call last):<br>
File “convert.py”, line 6, in <br>
model.eval()<br>
AttributeError: ‘collections.OrderedDict’ object has no attribute ‘eval’</p>

David_Reiss: <p>It looks like you’re not loading the model properly.  See <a href="https://pytorch.org/tutorials/beginner/saving_loading_models.html#save-load-state-dict-recommended">https://pytorch.org/tutorials/beginner/saving_loading_models.html#save-load-state-dict-recommended</a></p>

hurricane013: <p>Use this script :<br>
requirements : pytorch 1.5 and torchvision 0.6.0 use this command <code>conda install pytorch torchvision cudatoolkit=10.1 -c pytorch-nightly</code></p>
<pre><code class="lang-auto">import torch
import torchvision

model = torchvision.models.detection.keypointrcnn_resnet50_fpn(pretrained=True)
model.eval()
example = torch.rand(1, 3, 800, 800)
traced_script_module = torch.jit.script(model, example)
traced_script_module.save("/home/parth/Desktop/model4.pt")
</code></pre>

Ajay-Reva: <p>I have the same error. How do I solve this? I have tried the proper load and save as suggested by <a class="mention" href="/u/david_reiss">@David_Reiss</a>, but still I see the following errors<br>
AttributeError: ‘collections.OrderedDict’ object has no attribute ‘save’<br>
AttributeError: ‘collections.OrderedDict’ object has no attribute ‘eval’</p>