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.
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â
Traceback (most recent call last):
File âconvert.pyâ, line 6, in
model.eval()
AttributeError: âcollections.OrderedDictâ object has no attribute âevalâ
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")
I have the same error. How do I solve this? I have tried the proper load and save as suggested by @David_Reiss, but still I see the following errors
AttributeError: âcollections.OrderedDictâ object has no attribute âsaveâ
AttributeError: âcollections.OrderedDictâ object has no attribute âevalâ