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

Why does onnx-tensorflow add Transpose layers for each Conv2D layer? #782

Open
AlexeyAB opened this issue Oct 11, 2020 · 7 comments
Open

Comments

@AlexeyAB
Copy link

AlexeyAB commented Oct 11, 2020

Describe the bug

  1. Why does onnx-tensorflow add Transpose layers for each Conv2D layer?

  2. Why does onnx-tensorflow use multiple Conv2D layers instead of one GroupedConv2D layer?

It increases inference time, so PB-model that is converted by using onnx-tensorflow is slower than the same native TF-model.

If I want to convert NCHW-ONNX to NCHW-PB to run on GPU, and then convert NCHW-PB to NHWC-TFLITE, then how should I do this? (Currently tf.lite.TFLiteConverter.from_saved_model(saved_model_export_dir) allows to convert NCHW-PB to NHWC-TFLITE without adding extra-transpose layers.

To Reproduce

https://colab.research.google.com/gist/AlexeyAB/10b1bf880152b1ad7ca116b428863068/pytorch-onnx-tf-extra-transpose.ipynb

pytorch_onnx_tf_extra_transpose_ipynp.zip

Python, ONNX, ONNX-TF, Tensorflow version

This section can be obtained by running get_version.py from util folder.

Python version:
3.6.9 (default, Jul 17 2020, 12:50:27) 
[GCC 8.4.0]
ONNX version:
1.7.0
ONNX-TF version:
1.6.0
Tensorflow version:
2.4.0-dev20201010

Additional context

PB-model with extra Transpose layers:
transpose


Result TFLITE-model with extra Transpose layers:
image

@li2508264
Copy link

@AlexeyAB I have the same problem,transpose op increases inference time。Do you have any solutions?

@ZhangJunsong-NR
Copy link

Same with you. Any method to deal with these transpose ops? it cost too much time.

@Mkarami3
Copy link

Any update on this issue?

@shashwat14
Copy link

I have the same issue

@TalhaUsuf
Copy link

I have same issue, pytorch(NCHW) --> onnx(NCHW) --> tf-saved-model(NHWC) [transpose layers introduced here].

@AlexeyAB Please suggest if you found solution?

@TalhaUsuf
Copy link

TalhaUsuf commented Feb 8, 2022

I have same issue, pytorch(NCHW) --> onnx(NCHW) --> tf-saved-model(NHWC) [transpose layers introduced here].

@AlexeyAB Please suggest if you found solution?

I found the solution, perform following to convert form pytorch ---> tflite (NCHW ---> NHWC):

  • pytorch official export to onnx [N,C,H,W] ---> [N,C,H,W]
  • onnx conversion to open-vino IR format [N,C,H,W] ---> [N,C,H,W] (using openvino docker image)
  • open-vino-IR format to tensorflow format [N,C,H,W] ---> [N,H,W,C] (using openvino2tensorflow ⚡ 💡 docker image, see https://github.com/PINTO0309/openvino2tensorflow.git)
  • tensorflow format to tflite [N,H,W,C] ---> [N,H,W,C] 😄

@PINTO0309
Copy link

PINTO0309 commented Nov 16, 2022

At this time, this tool, which is a greatly improved version of openvino2tensorflow, is much easier and more accurate for conversion. Incidentally, GroupedConvolution is also supported.

"Self-Created Tools to convert ONNX files (NCHW) to TensorFlow/TFLite/Keras format (NHWC). The purpose of this tool is to solve the massive Transpose extrapolation problem in onnx-tensorflow (onnx-tf)."
https://github.com/PINTO0309/onnx2tf

image

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

7 participants