mmsegmentation icon indicating copy to clipboard operation
mmsegmentation copied to clipboard

Support for img norm in graph during onnx export

Open nick-clarifai opened this issue 4 years ago • 16 comments
trafficstars

Motivation

The purpose of this PR is to provide an option that does the image norm step of inference within the graph itself during the export to an ONNX model.

Modification

mmseg/datasets/pipelines/transforms.py:

  • an init flag is added which causes the image norm to be skipped during __call__ and causes the flag to be passed along in the img_norm_cfg

mmseg/models/segmentors/base.py

  • forward modified to do image normalization to the input if img_norm_cfg['normalize_in_graph'] is True

tools/pytorch2onnx.py:

  • command line arg added
  • normalize step in test pipeline is modified to skip normalization but still add img_norm_cfg
  • input is cloned in verification step so the pre-image norm input can be compared

Use cases

This would allow one to have a totally contained model that doesn't require extra python code for inference so that the ONNX model could easily be used in a setting such as NVIDIA Triton.

nick-clarifai avatar Nov 09 '21 21:11 nick-clarifai

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Nov 09 '21 21:11 CLAassistant

Codecov Report

Attention: Patch coverage is 27.27273% with 8 lines in your changes missing coverage. Please review.

Project coverage is 89.98%. Comparing base (9975c67) to head (f62cf69). Report is 257 commits behind head on master.

Files with missing lines Patch % Lines
mmseg/models/segmentors/base.py 0.00% 6 Missing and 1 partial :warning:
mmseg/datasets/pipelines/transforms.py 75.00% 0 Missing and 1 partial :warning:
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1027      +/-   ##
==========================================
- Coverage   90.11%   89.98%   -0.14%     
==========================================
  Files         125      125              
  Lines        7262     7277      +15     
  Branches     1206     1210       +4     
==========================================
+ Hits         6544     6548       +4     
- Misses        515      524       +9     
- Partials      203      205       +2     
Flag Coverage Δ
unittests 89.98% <27.27%> (-0.14%) :arrow_down:

Flags with carried forward coverage won't be shown. Click here to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov[bot] avatar Nov 10 '21 05:11 codecov[bot]

Hi @nick-clarifai Thanks for your contribution. We will review it in the near future.

Junjun2016 avatar Nov 11 '21 01:11 Junjun2016

Please merge the master branch into your branch, thank you.

RockeyCoss avatar Nov 15 '21 08:11 RockeyCoss

Hi @RunningLeon Please review it.

Junjun2016 avatar Nov 16 '21 12:11 Junjun2016

@nick-clarifai Hi, thanks for your PR. If you add --normalize-in-graph in pytorch2onnx.py, then you might need to support it in downstream tool onnx2tensorrt.py as well.

RunningLeon avatar Nov 17 '21 03:11 RunningLeon

@nick-clarifai Hi, thanks for your PR. If you add --normalize-in-graph in pytorch2onnx.py, then you might need to support it in downstream tool onnx2tensorrt.py as well.

Yeah this makes sense. The changes I can see being necessary would be allowing image normalization in the preprocessing step to be skipped and adding a command line arg to skip normalization? I'm happy to make these changes if we're on the same page. Let me know if there's something else I'm forgetting there.

nick-clarifai avatar Nov 18 '21 19:11 nick-clarifai

@nick-clarifai Hi, thanks for your PR. If you add --normalize-in-graph in pytorch2onnx.py, then you might need to support it in downstream tool onnx2tensorrt.py as well.

Yeah this makes sense. The changes I can see being necessary would be allowing image normalization in the preprocessing step to be skipped and adding a command line arg to skip normalization? I'm happy to make these changes if we're on the same page. Let me know if there's something else I'm forgetting there.

Yes. --normalize-in-graph should be added to onnx2tensorrt.py as well.

RunningLeon avatar Nov 19 '21 06:11 RunningLeon

Thanks for the feedback. onnx2tensorrt.py now has an option to skip image normalization in the preprocessing step. I didn't think it made sense to name the arg --normalize-in-graph because the tensorrt model will only do so if the input onnx model does normalize in graph, so I named the arg --skip-normalize instead. Let me know if you suggest any more changes.

nick-clarifai avatar Nov 19 '21 20:11 nick-clarifai

Thanks for the feedback. onnx2tensorrt.py now has an option to skip image normalization in the preprocessing step. I didn't think it made sense to name the arg --normalize-in-graph because the tensorrt model will only do so if the input onnx model does normalize in graph, so I named the arg --skip-normalize instead. Let me know if you suggest any more changes.

Actually, skip-normalize works for me.

RunningLeon avatar Nov 22 '21 02:11 RunningLeon

Is there something else that should be done before this could be merged? Let me know if there's anything I can do.

nick-clarifai avatar Dec 20 '21 20:12 nick-clarifai

Is there something else that should be done before this could be merged? Let me know if there's anything I can do.

@nick-clarifai Hi, sorry for the late reply. After resolve the rest comments, this PR is ready to go merge for me.

RunningLeon avatar Dec 23 '21 11:12 RunningLeon

Is there something else that should be done before this could be merged? Let me know if there's anything I can do.

@nick-clarifai Hi, sorry for the late reply. After resolve the rest comments, this PR is ready to go merge for me.

Thanks for the feedback @RunningLeon. I've made the requested changes.

nick-clarifai avatar Dec 30 '21 16:12 nick-clarifai

Hi, @nick-clarifai nick, sorry for late reply.

This pr is closely to merge, could you please fix unit test problem? Your modification is not coveraged by unit test.

image

Best,

MengzhangLI avatar Jan 16 '22 06:01 MengzhangLI

Hi, @nick-clarifai nick, sorry for late reply.

This pr is closely to merge, could you please fix unit test problem? Your modification is not coveraged by unit test.

image

Best,

Sure, can you help me decide which is the best test to write? Should I write a test that runs the code from pytorch2onnx.py with the verification enabled? Or should I do something like add a test to tests/test_models/test_segmentors/test_encoder_decoder.py that makes sure the added code in mmseg/models/segmentors/base.py is run?

nick-clarifai avatar Feb 01 '22 20:02 nick-clarifai

Hi @nick-clarifai !We are grateful for your efforts in helping improve this open-source project during your personal time. Welcome to join OpenMMLab Special Interest Group (SIG) private channel on Discord, where you can share your experiences, ideas, and build connections with like-minded peers. To join the SIG channel, simply message moderator— OpenMMLab on Discord or briefly share your open-source contributions in the #introductions channel and we will assist you. Look forward to seeing you there! Join us :https://discord.gg/UjgXkPWNqA If you have a WeChat account,welcome to join our community on WeChat. You can add our assistant :openmmlabwx. Please add "mmsig + Github ID" as a remark when adding friends:) Thank you again for your contribution❤

OpenMMLab-Assistant005 avatar Apr 14 '23 08:04 OpenMMLab-Assistant005