question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is it possible to convert this model to ONNX format?

See original GitHub issue

Hello, I used your model to train it on custom dataset, everything works fine, but I want to convert it to ONNX format, so it could be used with ML.NET. To convert I used the following code:

from ssd.config import cfg from ssd.modeling.detector import build_detection_model import torch cfg.merge_from_file(‘configs/vgg_custom_ssd512.yaml’) model = build_detection_model(cfg) state_dict = torch.load(‘model_final.pth’, map_location=lambda storage, loc: storage)[‘model’] model.load_state_dict(state_dict) dummy_input = torch.randn(1, 3, 512, 512, device=‘cpu’) torch.onnx.export(model, dummy_input, ‘ssdpytorch.onnx’, verbose = True)

I got the following error: RuntimeError: Only tuples, lists and Variables supported as JIT inputs, but got Container

Though Container isn’t used either in model or in state_dict, this data type is used while converting model to ONNX. From your point of view, is it solvable? Exactly the same error with vgg_ssd512_voc0712, so this error is easily reproducible.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
lufficccommented, Jul 10, 2019

Since the return values are not tuples or lists, it cannot be directly exported. But a model wrapper will solve this(You can ignore some warnings).

from ssd.config import cfg
from ssd.modeling.detector import build_detection_model
import torch
from torch import nn


class ONNXExportableModel(torch.nn.Module):
    def __init__(self, model):
        super().__init__()
        self.model = model

    def forward(self, *args, **kwargs):
        detections = self.model(*args, **kwargs)
        detections = [(det['boxes'], det['labels'], det['scores']) for det in detections]
        return detections


if __name__ == '__main__':
    cfg.merge_from_file('configs/vgg_ssd300_voc0712.yaml')
    model = build_detection_model(cfg)
    state_dict = torch.load('weights/vgg_ssd300_voc0712.pth', map_location=lambda storage, loc: storage)['model']
    model.load_state_dict(state_dict)
    dummy_input = torch.randn(1, 3, 300, 300, device='cpu')
    torch.onnx.export(ONNXExportableModel(model), dummy_input, 'ssdpytorch.onnx', verbose=True)

0reactions
pawopawocommented, Dec 27, 2019

image I converted the onnx file as shown in the picture. Does anyone know what is wrong? Hope for help

Read more comments on GitHub >

github_iconTop Results From Across the Web

Convert your PyTorch training model to ONNX - Microsoft Learn
Export the model · Copy the following code into the PyTorchTraining.py file in Visual Studio, above your main function. · To run the...
Read more >
How to Convert a PyTorch Model to ONNX in 5 Minutes - Deci AI
Converting deep learning models from PyTorch to ONNX is quite straightforward. Start by loading a pre-trained ResNet-50 model from PyTorch's ...
Read more >
(optional) Exporting a Model from PyTorch to ONNX and ...
To export a model, we call the torch.onnx.export() function. This will execute the model, recording a trace of what operators are used to...
Read more >
Convert Transformers to ONNX with Hugging Face Optimum
To convert your Transformers model to ONNX you simply have to pass from_transformers=True to the from_pretrained() method and your model will be ...
Read more >
Boost any Machine Learning model with ONNX conversion
If you only save the model weights, you will not be able to convert it to ONNX, because the model architecture is required...
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 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