I am getting the following error while running a PyTorch model on android model?

Lite Interpreter verson number does not match. The model version must be between 3 and 5But the model version is 7 ()

Any suggestion?

2

There are 2 answers

2
Celik On

following code converts the version 7 model to 5:

convert2version5 = True
if convert2version5:
    from torch.jit.mobile import (
        _backport_for_mobile,
        _get_model_bytecode_version,
    )

    MODEL_INPUT_FILE = "model_v7.ptl"
    MODEL_OUTPUT_FILE = "model_v5.ptl"

    print("model version", _get_model_bytecode_version(f_input=MODEL_INPUT_FILE))

    _backport_for_mobile(f_input=MODEL_INPUT_FILE, f_output=MODEL_OUTPUT_FILE, to_version=5)

    print("new model version", _get_model_bytecode_version(MODEL_OUTPUT_FILE))
0
Adam Fónagy On

Probably the version of the PyTorch the model was exported with and the Android PyTorch API that you want to use in your Android app don't match. It seems that the PyTorch version was released later, hence the old version of the Android PyTorch API does not support the newer PyTorch model version.

You should update the build dependencies of your Android project. With the latest version of the pytorch_android_lite and the pytorch_android_torchvision_lite the issue you've reported should be eliminated.

If you use Gradle, you should add the two implementation configuration to your project's build.gradle file (supposing the latest version of the two dependencies is 1.13.1):

dependencies {
    ...

    implementation 'org.pytorch:pytorch_android_lite:1.13.1'
    implementation 'org.pytorch:pytorch_android_torchvision_lite:1.13.1'
}