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

billysutomo: <p>Following guide at <a href="https://pytorch.org/mobile/android/" rel="nofollow noopener">https://pytorch.org/mobile/android/</a>.<br>
Try to generate model with script provided</p>
<pre><code class="lang-auto">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")
</code></pre>
<p>after that<br>
build android app from <a href="https://github.com/pytorch/android-demo-app/tree/master/HelloWorldApp" rel="nofollow noopener">https://github.com/pytorch/android-demo-app/tree/master/HelloWorldApp</a><br>
and got error <strong>"Attempted to read a PyTorch file with version 3, but the maximum supported version for reading is 2"</strong></p>
<p>my python package version</p>
<pre><code class="lang-auto">&gt;&gt;&gt; import torch
&gt;&gt;&gt; import torchvision
&gt;&gt;&gt; print(torch.__version__)
1.6.0
&gt;&gt;&gt; print(torchvision.__version__)
0.7.0
&gt;&gt;&gt;
</code></pre>

KevinZ1992: <p>Use torch.save( …, _use_new_zipfile_serialization=False) for 1.6.0 when load pt file lower than 1.6.0</p>

billysutomo: <p><a class="mention" href="/u/kevinz1992">@KevinZ1992</a>  do you have the example for my code?</p>
<p>I try with</p>
<pre><code class="lang-auto">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)
</code></pre>
<p>But still got the same error when try to load the model into android torch</p>

KevinZ1992: <aside class="quote no-group" data-username="billysutomo" data-post="1" data-topic="93193">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/billysutomo/48/27909_2.png" class="avatar"> billysutomo:</div>
<blockquote>
<p><code>traced_script_module.save</code></p>
</blockquote>
</aside>
<p>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.</p>

sachinsharma9780: <p>Hi,</p>
<p>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:<br>
conda install pytorch==1.5.1 torchvision==0.6.1 cudatoolkit=10.2 -c pytorch</p>

suyash: <p>I’ve fixed this issue and submitted a Pull Request see : <a href="https://github.com/pytorch/android-demo-app/issues/79" rel="noopener nofollow ugc">https://github.com/pytorch/android-demo-app/issues/79</a></p>

Russell_SB: <aside class="quote no-group" data-username="billysutomo" data-post="1" data-topic="93193">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/billysutomo/48/27909_2.png" class="avatar"> billysutomo:</div>
<blockquote>
<pre><code class="lang-auto">model = torchvision.models.resnet18(pretrained=True)
</code></pre>
</blockquote>
</aside>
<p>Solved the issue for me! Thank you</p>