I want to do machine learning with android

Question about “torch.jit.xxxx”.

I would like to know how to serialize the models in “pytorch mobie”.

import torch
import torchvision

model = torchvision.models.resnet18(pretrained=True)
example = torch.rand(1, 3, 224, 224)
traced_script_module = torch.jit.trace(model, example)

But,the network I want to use is another network, such as ESPNet.
Does anyone know how to make a “.pt” file?


ESPNet is not saved by PyTorch script (i mean by torch.jit.save) it has been saved with torch.save already so you can directly use it i think.


Anyway, I managed to load the encoder this way by previously downloading the pre-trained model from Github.

import torch
model = ESPNet(20,encoderFile="espnet_p_2_q_8.pth", p=2, q=8)
example = torch.rand(1, 3, 224, 224)
traced_script_module = torch.jit.trace(model, example)
traced_script_module.save("model.pt") #Will do the same save a load_dict
#Exemple de save du modele ESPNet avec TorchScript
torch.jit.save(traced_script_module, "scriptmodel.pth")
#Load example with ToarchScript
modelLoaded = torch.jit.load("scriptmodel.pth")

The output is

Encoder loaded!

For ESPNet to be noted that it uses the GPU so tu use CPU i copy Model.py from https://github.com/sacmehta/ESPNet/tree/master/train in my Notebook then i change this instruction




Thank you pytorchtester0!!!

I’m Japanese and I’m sorry for my poor English.
I’m going to try it out as soon as possible.

I just have two more question,

example = torch.rand(1, 3, 224, 224)

I believe the ESPNet input image is resized to 1024x512.
I don’t have to change?

I would like to test it with ESPNet_Encoder.
So could you tell me how to use “ESPNet_Encoder” as well?