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

Hello

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.

import torch
import torchvision

model = torchvision.models.resnet18(pretrained=True)
model.eval()
example = torch.rand(1, 3, 224, 224)
traced_script_module = torch.jit.trace(model, example)
traced_script_module.save(“app/src/main/assets/model.pt”)

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.

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 -

Thanks for your reply
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?

so can you help me how can i convert my .pth pytorch model into torchscipt model for load on andorid.

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

Hi @David_Reiss

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

import torch
import torchvision

model = “/home/kamleshpanchal/Downloads/PytorchMobile-master/agestage250.pth”
input_tensor = torch.rand(1,3,224,224)

script_model = torch.jit.trace(model,input_tensor)
script_model.save(“converted.pt”)

after run this code i m getting following error

Traceback (most recent call last):
File “convert.py”, line 10, in
script_model = torch.jit.trace(model,input_tensor)
File “/usr/local/lib/python3.6/dist-packages/torch/jit/init.py”, line 880, in trace
name = _qualified_name(func)
File “/usr/local/lib/python3.6/dist-packages/torch/_jit_internal.py”, line 636, in _qualified_name
name = obj.name
AttributeError: ‘str’ object has no attribute ‘name

It looks like you’re just passing the model file name to torch.jit.trace. You need to load the model first and then pass the model object instead.

@David_Reiss I m using following code for load model and passing model object Please look into it. is it correct way?

import torch
import torchvision
model = torch.load(“agestage250.pth”)
model.eval()
input_tensor = torch.rand(1,3,224,224)

script_model = torch.jit.trace(model,input_tensor)
script_model.save(“converted.pt”)

after run this code i m getting following error

Traceback (most recent call last):
File “convert.py”, line 6, in
model.eval()
AttributeError: ‘collections.OrderedDict’ object has no attribute ‘eval’

It looks like you’re not loading the model properly. See https://pytorch.org/tutorials/beginner/saving_loading_models.html#save-load-state-dict-recommended

Use this script :
requirements : pytorch 1.5 and torchvision 0.6.0 use this command conda install pytorch torchvision cudatoolkit=10.1 -c pytorch-nightly

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")