Attempted to read a PyTorch file with version 3, but the maximum supported version for reading is 2

Following guide at https://pytorch.org/mobile/android/.
Try to generate model with script provided

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

after that
build android app from https://github.com/pytorch/android-demo-app/tree/master/HelloWorldApp
and got error "Attempted to read a PyTorch file with version 3, but the maximum supported version for reading is 2"

my python package version

>>> import torch
>>> import torchvision
>>> print(torch.__version__)
1.6.0
>>> print(torchvision.__version__)
0.7.0
>>>
2 Likes

Use torch.save( …, _use_new_zipfile_serialization=False) for 1.6.0 when load pt file lower than 1.6.0

1 Like

@KevinZ1992 do you have the example for my code?

I try with

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

torch.save(model, "app/src/main/assets/model.pt", _use_new_zipfile_serialization=False)

But still got the same error when try to load the model into android torch

Maybe you can change the version of org.pytorch:pytorch_android from 1.4.0 to 1.6.0 in HelloWorldApp/app/build.gradle file.

Hi,

I have encountered the same problem and the solution is to downgrade your torch version to 1.5.1 and torchvision to 0.6.0 using below command:
conda install pytorch==1.5.1 torchvision==0.6.1 cudatoolkit=10.2 -c pytorch

I’ve fixed this issue and submitted a Pull Request see : https://github.com/pytorch/android-demo-app/issues/79