Ssdlite320_mobilenet_v3_large failed to run with torchscript

Hi there,

I am trying to use torchscript to trace the ssdlite320_mobilenet_v3_large provided in torchvision. However, there was unexpected errors when I follow the docs.

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

The error log

/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 <module>
    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'

torch:1.10.0+cu102
torchvision:0.11.1+cu102

Could you try to torch.jit.script 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.

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’”.

I think the model was added ~2 months ago in this PR so you might need to update your torchvision version e.g. by installing the nightly binary.