Ssdlite320_mobilenet_v3_large failed to run with torchscript

username: Lyken17
cooked: <p>Hi there,</p>
<p>I am trying to use <code>torchscript</code> to trace the <code>ssdlite320_mobilenet_v3_large</code> provided in torchvision. However, there was unexpected errors when I follow <a href="https://pytorch.org/vision/stable/models.html#torchvision.models.detection.ssdlite320_mobilenet_v3_large" rel="noopener nofollow ugc">the docs</a>.</p>
<pre><code class="lang-python">import torch
import torch as th
import torch.nn as nn
from torchvision import models
import torch.onnx 

model = models.detection.ssdlite320_mobilenet_v3_large(pretrained=True)
model.eval()

x = [torch.rand(3, 320, 320), torch.rand(3, 500, 400)]

predictions = model(x) # successfully run without error
model_trace = torch.jit.trace(model, x) # fail to run
</code></pre>
<p>The error log</p>
<pre><code class="lang-auto">/home/ligeng/.local/lib/python3.8/site-packages/torch/functional.py:445: UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the indexing argument. (Triggered internally at  ../aten/src/ATen/native/TensorShape.cpp:2157.)
  return _VF.meshgrid(tensors, **kwargs)  # type: ignore[attr-defined]
Traceback (most recent call last):
  File "ssd_torchscript.py", line 15, in &lt;module&gt;
    model_trace = torch.jit.trace(model, x)
  File "/home/ligeng/.local/lib/python3.8/site-packages/torch/jit/_trace.py", line 741, in trace
    return trace_module(
  File "/home/ligeng/.local/lib/python3.8/site-packages/torch/jit/_trace.py", line 958, in trace_module
    module._c._create_method_from_trace(
  File "/home/ligeng/.local/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1102, in _call_impl
    return forward_call(*input, **kwargs)
  File "/home/ligeng/.local/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1090, in _slow_forward
    result = self.forward(*input, **kwargs)
  File "/home/ligeng/.local/lib/python3.8/site-packages/torchvision/models/detection/ssd.py", line 310, in forward
    images, targets = self.transform(images, targets)
  File "/home/ligeng/.local/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1102, in _call_impl
    return forward_call(*input, **kwargs)
  File "/home/ligeng/.local/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1090, in _slow_forward
    result = self.forward(*input, **kwargs)
  File "/home/ligeng/.local/lib/python3.8/site-packages/torchvision/models/detection/transform.py", line 100, in forward
    for k, v in t.items():
AttributeError: 'Tensor' object has no attribute 'items'
</code></pre>

username: Lyken17
cooked: <p>torch:1.10.0+cu102<br>
torchvision:0.11.1+cu102</p>

username: ptrblck
cooked: <p>Could you try to <code>torch.jit.script</code> the model instead of tracing it? I haven’t checked the model implementation, but note that tracing a model would not capture any data-dependent control flow etc. so unsure if it’s a good fit for a detection model.</p>

username: Collier
cooked: <p>I had executed object detection using Deep Neural Network which is SSD with backbone of MobilenetV3 on raspberry pi 4B, but it turns out this error “Attributeerror module ‘torchvision.models.detection’ has no attribute ‘ssdlite320_mobilenet_v3_large’”<a href="https://www.myloweslife.top/" rel="noopener nofollow ugc">.</a></p>

username: ptrblck
cooked: <p>I think the model was added ~2 months ago in <a href="https://github.com/pytorch/vision/pull/4875">this PR</a> so you might need to update your <code>torchvision</code> version e.g. by installing the nightly binary.</p>