Pytorch for Android - LiteModuleLoader

I am getting an error as

java.lang.RuntimeException: Unable to start activity ComponentInfo{org.pytorch.helloworld/org.pytorch.helloworld.MainActivity}: com.facebook.jni.CppException: Lite Interpreter verson number does not match. The model version must be between 3 and 5But the model version is 8 ()
Exception raised from parseMethods at …/torch/csrc/jit/mobile/import.cpp:320 (most recent call first):
(no backtrace available)

My gradle settings
dependencies {
implementation ‘androidx.appcompat:appcompat:1.1.0’
implementation ‘org.pytorch:pytorch_android_lite:1.9.0’
implementation ‘org.pytorch:pytorch_android_torchvision:1.9.0’
}

GitHub - pytorch/android-demo-app: PyTorch android examples of usage in applications HelloWorld from this app.

I converted my GAN model to .ptl and then I tried to load the .ptl file in the same way as mentioned in this sample. I am consistently getting the above error

1 Like

Looks like the model is generated from a newer runtime, while 1.9 is more than one year old. Possibly try

implementation ‘org.pytorch:pytorch_android_lite:1.12.0’
implementation ‘org.pytorch:pytorch_android_torchvision:1.12.0’

and see if it works?

1 Like

@cccclai No it’s not working

import org.pytorch.IValue;
import org.pytorch.LiteModuleLoader;
import org.pytorch.Module;
import org.pytorch.Tensor;
import org.pytorch.torchvision.TensorImageUtils;
import org.pytorch.MemoryFormat;

If I use 1.12 then I cannot resolve any of the pytorch symbols

I was able to fix it by changing to

implementation ‘org.pytorch:pytorch_android_lite:1.12.2’
implementation ‘org.pytorch:pytorch_android_torchvision:1.12.2’

Seems like 1.12.0 is not stable. By the way do we have any website where we can see the stable android builds. It’s hard to trial and error

Hi Vijay,

I got the same error when I tried to work on HelloWorldApp (MobilenetV3 based object classification on static image) from https://github.com/pytorch/android-demo-app/tree/master/HelloWorldApp

I was able to fix my issue with your solution, but now, though the app works, the classification result changes and is wrong now.

Any idea how to fix this?

Also one more observation.

The repository provides a script named trace_model.py to create a PyTorch Mobile model file.
I used the same script to generate the model.
If the extension is .pt, I don’t even face the above problem and everything works perfectly fine.
If i rename the extension to .ptl, I get the problem, and after fixing it using your solution, the problem goes away, but classification results go for a toss.

Overall file is same, just changing extension causes problems.

@kartikpodugu , I think you should export the model to .ptl file through mobile lite interpreter, just renaming it form .pt to .ptl will not work accurately