Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Anyone succeed in converting pth model to onnx format? #84

Open
liamsun2019 opened this issue Feb 24, 2021 · 2 comments
Open

Anyone succeed in converting pth model to onnx format? #84

liamsun2019 opened this issue Feb 24, 2021 · 2 comments

Comments

@liamsun2019
Copy link

Hi Pals,

I tried to convert the trained pth model to onnx format, but encounter several issues. The code segment are as follows:

model = Trainer.build_model(cfg)
state = torch.load(cfg.MODEL.WEIGHTS, map_location=lambda storage, loc: storage)
model.load_state_dict(state['model'])
model.eval()
model.cuda()

dummy_input = torch.randn(1, 3, 448, 448).to("cuda")
torch.onnx.export(model, dummy_input, "model.onnx", verbose=True, input_names=['image'], output_names=['pred'])

The used config file is centermask_lite_V_19_slim_dw_eSE_FPN_ms_4x.yaml.

a. The input to the network whose backbone is a rcnn is a list which contains dictionaries composed of input image data and some other attirbutes. Looks like there's no way to pass similar input for onnx exporting. I added a member function in rcnn.py (detectron2/modeling/meta_arch/rcnn.py) which accepts such input to solve this issue.

b. Based on above, following error rises up during conversion:
RuntimeError: Failed to export an ONNX attribute 'onnx::Sub', since it's not constant, please try to make things (e.g., kernel size) static if possible

I cannot google any clues to the error. Anyone can kindly help? Thanks.

@xiehousen
Copy link

xiehousen commented Sep 29, 2021

Hello, @liamsun2019!
I've been troubled by this problem recently. I use the detectron2 tool to convert centermask to onnx, but I need to rewrite the underlying code of detectron2 to onnx (the existing code is written according to mask RCNN), which is difficult to implement.
Can you use this method to convert onnx?

@abhigoku10
Copy link

@xiehousen can you please share some reference to convert the tool , @liamsun2019 wer eyou able to convert the model to onnx module

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants