Closed Aspirinkb closed 2 years ago
torch: 1.12.0+cu116
torch: 1.12.0+cu116
hello, have you solved the problem ?i meet the same problem when i run pytorch2onnx.py
torch: 1.12.0+cu116
hello, have you solved the problem ?i meet the same problem when i run pytorch2onnx.py
No. I guess this is a problem of MMSeg to support onnx export.
Hi @Aspirinkb, I can run the following command successfully with torch1.9 on the CPU.
python tools/pytorch2onnx.py configs/convnext/upernet_convnext_base_fp16_512x512_160k_ade20k.py --checkpoint checkpoints/upernet_convnext_base_fp16_512x512_160k_ade20k_20220227_181227-02a24fc6.pth --input-img demo/demo.png --show --verify --cfg-options model.test_cfg.mode='whole'
Could you try removing --dynamic-export
in your command? And I'll test it with torch 1.12.
Hi @Aspirinkb, I can run the following command successfully with torch1.9 on the CPU.
python tools/pytorch2onnx.py configs/convnext/upernet_convnext_base_fp16_512x512_160k_ade20k.py --checkpoint checkpoints/upernet_convnext_base_fp16_512x512_160k_ade20k_20220227_181227-02a24fc6.pth --input-img demo/demo.png --show --verify --cfg-options model.test_cfg.mode='whole'
Could you try removing
--dynamic-export
in your command? And I'll test it with torch 1.12.
Same error with torch1.12. Hi @RunningLeon, and @zhouzaida, Could you take a look at this issue if you're available?
I change the signature and body of the forward function as following:
@auto_fp16(apply_to=('img', ))
def forward(self, img, **kwargs): # img_metas, return_loss=True
"""Calls either :func:`forward_train` or :func:`forward_test` depending
on whether ``return_loss`` is ``True``.
Note this setting will change the expected inputs. When
``return_loss=True``, img and img_meta are single-nested (i.e. Tensor
and List[dict]), and when ``resturn_loss=False``, img and img_meta
should be double nested (i.e. List[Tensor], List[List[dict]]), with
the outer list indicating test time augmentations.
"""
try:
return_loss = kwargs.pop("return_loss")
img_metas = kwargs.pop("img_metas")
except KeyError as e:
raise Exception(f"Miss params return_loss or img_meats: {e}")
if return_loss:
return self.forward_train(img, img_metas, **kwargs)
else:
return self.forward_test(img, img_metas, **kwargs)
and export the onnx model. Running by onnxruntime is ok.
But please note that, one should change it back for training models!!!
It is not the correct way to fix the onnx export problem, but I can not do nothing...
Waiting for official bug fix...
Is there any progress on this issue? I have the same problem.
something wrong with auto_fp16
and partial
the model forward. cc @zhouzaida Could comment out the @auto_fp16
decorator while using pytorch2onnx.py
for now.
https://github.com/open-mmlab/mmsegmentation/blob/dd42fa8d0125632371a41a87c20485494c973535/mmseg/models/segmentors/base.py#L96
@Aspirinkb @AndPuQing Hi, could you guys try with mmdeploy? The deployment feature in mmseg would be removed in the future.
I tried mmdeploy and it worked for me.
Thanks for your error report and we appreciate it a lot.
Checklist
Describe the bug Can not convert trained model into onnx format when using
tools/pytorch2onnx.py
. When runtorch.onnx.export
, there is a error:TypeError: forward() got multiple values for argument 'img_metas'
. I read the code, the model's forward function is wrapped and img_metas is set. I can not figure out why arising this error while just set img_metas once.Reproduction
Did you make any modifications on the code or config? Did you understand what you have modified? No
What dataset did you use? My own
Environment
python mmseg/utils/collect_env.py
to collect necessary environment information and paste it here.$PATH
,$LD_LIBRARY_PATH
,$PYTHONPATH
, etc.)Error traceback
If applicable, paste the error trackback here.
Bug fix
If you have already identified the reason, you can provide the information here. If you are willing to create a PR to fix it, please also leave a comment here and that would be much appreciated!