RuntimeError: Only tuples, lists and Variables supported as JIT inputs/outputs. Dictionaries and strings are also accepted but their usage is not recommended.

See original GitHub issue

Hi, I have issue with exporting pytorch models to onnx with output of List[Dict[str, tensor]], just like some detection models in torchvision.

### test return with dict
import torch
import onnx
import io
import onnx.helper as helper
from torch.jit.annotations import Tuple, List, Dict, Optional
from torch import Tensor

class DummyModel(torch.nn.Module):
    def __init__(self):
        super(DummyModel, self).__init__()

    def forward(self, x: Tensor, y: Tensor, z: Tensor)->List[Dict[str, Tensor]]:
        res = torch.jit.annotate(List[Dict[str, torch.Tensor]], [])
        xy = x + y
        xz = x + z
        yz = y + z
        res.append({'xy' : xy, 'xz' : xz, 'yz' : yz})
        return res

model = DummyModel()
x = torch.arange(4).reshape(2,2)
y = torch.arange(4).reshape(2,2)
z = torch.arange(4).reshape(2,2)
input_data = (x, y, z)

desired = model(*input_data)
print(f'Expect:\n{desired}')

# onnx_io = "test_dict.onnx"
torch.onnx.export(
    model,
    input_data,
    onnx_io,
    verbose=False,
    input_names=None,
    output_names=None,
)

model_onnx = onnx.load(onnx_io)

sess = rt.InferenceSession(onnx_io)
input_names = [_.name for _ in sess.get_inputs()]
print(f'input_names={input_names}')
#forward model
input_data_npy = [_.detach().cpu().numpy() for _ in input_data]
actual = sess.run(None, {name : data for name, data in zip(input_names, input_data_npy)})
desired = desired[0]
print(f'Actual:\n{actual}')

Any ideas? Thanks in advance.

cc @neginraoof

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
fmassacommented, Nov 16, 2020

Thanks a lot for the help @zhiqwang !

0reactions
RunningLeoncommented, Nov 12, 2020

@zhiqwang Thanks again.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Only tuples, lists and Variables supported as JIT inputs ...
RuntimeError : Only tuples, lists and Variables supported as JIT inputs/outputs. Dictionaries and strings are also accepted but their usage is not ......
Read more >
RuntimeError: Only tuples, lists and Variables are supported ...
RuntimeError: Only tuples, lists and Variables are supported as JIT inputs/outputs. Dictionaries and strings are also accepted · Ask Question.
Read more >
Error while tracing for onnx export. "Only tuples, lists and ...
RuntimeError : Only tuples, lists and Variables supported as JIT inputs/outputs. Dictionaries and strings are also accepted but their usage is ...
Read more >
torch.onnx — PyTorch master documentation
Only tuples, lists and Variables are supported as JIT inputs/outputs. Dictionaries and strings are also accepted but their usage is not recommended.
Read more >
Common Data Structures: Lists, Tuples, and Dictionaries
Below we cover a few data structures in the Python language. There are a few more not covered here, but these are by...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found