我在android模型上运行PyTorch模型时收到以下错误?
Lite Interpreter verson number does not match. The model version must be between 3 and 5But the model version is 7 ()
有什么建议吗?
以下代码将版本7的模型转换为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))
可能导出模型时使用的PyTorch版本与您要在Android应用程序中使用的Android PyTorch API不匹配。似乎PyTorch版本发布较晚,因此旧版本的Android PyTorch API不支持较新的PyTorch模型版本。
你应该更新你的Android项目的构建依赖关系。使用最新版本的pytorch_android_lite和pytorch_android_tarchvision_lite,应该可以消除您报告的问题。
如果您使用Gradle,您应该将两个implementation
配置添加到项目的build.gradle
文件中(假设这两个依赖项的最新版本是1.13.1(:
dependencies {
...
implementation 'org.pytorch:pytorch_android_lite:1.13.1'
implementation 'org.pytorch:pytorch_android_torchvision_lite:1.13.1'
}