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

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

Hello,

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.

https://github.com/sacmehta/ESPNet/tree/master/pretrained/encoder

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

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

self.encoder.load_state_dict(torch.load(encoderFile)

With

self.encoder.load_state_dict(torch.load(encoderFile,map_location='cpu'))

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)

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

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