Torchscript FasterRCNN model is not saving using _save_for_lite_interpreter

username: umaidzubair
cooked: <p>System and Packages<br>
CUDA version: 11.8<br>
PyTorch version: 2.0.1+cu118<br>
cuDNN version: 8700<br>
Python 3.10.11<br>
OS: Windows</p>
<p>I am trying to use FasterRCNN model on android. To do that I am trying to convert my pytorch model into torchscript using  _save_for_lite_interpreter. I get this error with all object detection models. Here is my code:</p>
<pre><code class="lang-auto">import torch
import torchvision
from torch.utils.mobile_optimizer import optimize_for_mobile

model = torchvision.models.detection.fasterrcnn_resnet50_fpn_v2()
model.eval()
script_model = torch.jit.script(model)
script_model.eval()
optimized = optimize_for_mobile(script_model)
optimized._save_for_lite_interpreter('test_model.ptl')
</code></pre>
<p>I get error on the last line.The error is as followed</p>
<pre><code class="lang-auto">Exception has occurred: RuntimeError
__torch__ types other than custom c++ classes (__torch__.torch.classes)are not supported in lite interpreter. Workaround: instead of using arbitrary class type (class Foo()), define a pytorch class (class Foo(torch.nn.Module)). The problematic type is: __torch__.torchvision.models.detection.image_list.ImageList
  File "U:\workspace\ai_pipline\FasterRCNN\test.py", line 11, in &lt;module&gt;
    optimized._save_for_lite_interpreter('test_model.ptl')
RuntimeError: __torch__ types other than custom c++ classes (__torch__.torch.classes)are not supported in lite interpreter. Workaround: instead of using arbitrary class type (class Foo()), define a pytorch class (class Foo(torch.nn.Module)). The problematic type is: __torch__.torchvision.models.detection.image_list.ImageList
</code></pre>
<p>I am adding a picture as well to make it clear<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/3/6/36adb401a6a9e7771e4ad36192e7ec8eb5402084.png" data-download-href="https://discuss.pytorch.org/uploads/default/36adb401a6a9e7771e4ad36192e7ec8eb5402084" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/3/6/36adb401a6a9e7771e4ad36192e7ec8eb5402084_2_689x143.png" alt="image" data-base62-sha1="7NHWk05DdtACyrPMuPztGUEb7Gk" width="689" height="143" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/3/6/36adb401a6a9e7771e4ad36192e7ec8eb5402084_2_689x143.png, https://discuss.pytorch.org/uploads/default/optimized/3X/3/6/36adb401a6a9e7771e4ad36192e7ec8eb5402084_2_1033x214.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/3/6/36adb401a6a9e7771e4ad36192e7ec8eb5402084_2_1378x286.png 2x" data-dominant-color="392927"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">2748×572 77.1 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>I would really appreciate if someone can help me out. Thanks</p>

username: majikesdocII
cooked: <p>This works for me when I use a Net() object instead of a model.  I trained the net similar to this: <a href="https://pytorch.org/tutorials/beginner/blitz/cifar10_tutorial.html" class="inline-onebox" rel="noopener nofollow ugc">Training a Classifier — PyTorch Tutorials 2.4.0+cu121 documentation</a></p>
<p>traced_script_module = torch.jit.script(net)<br>
traced_script_module.eval()<br>
optimized = optimize_for_mobile(traced_script_module)<br>
optimized._save_for_lite_interpreter(("test_model.ptl’)</p>
<p>print(f"Saved PTL file in {john_ptl}")</p>