-
Notifications
You must be signed in to change notification settings - Fork 73
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
Question about channel_transpose in common_functions.py #18
Comments
Thanks. Please engage in a little discussion with me. I am aware of that problem. However, I am struggling to implement a realistic measure. This problem is especially common with All of the following Y expression patterns on ONNX need to be converted to NHWC for processing. Also, the example below is a very simple pattern that only needs to be broadcast with all 1's except for all but one dimension. e.g.
I have no idea how to successfully implement constant broadcasts in all dimensions up to xD, not just 2D to 5D. Right now I am forced to deal with only a limited pattern. Realistically, I believe we need to not only implement Numpy-style broadcasts, but also an implementation that mechanically reads the constants that assume NCHW format into NHWC format and then broadcasts them. It is very easy to implement a simple broadcast. |
Now I understood how difficult to solve this problem since there is no information to guess the order of tensor during conversion. However, Is comparing input and output shape between onnx and tensorflow not enough? Anyway the onnx model follows numpy broadcasting rule. In that case, the tensor shapes are compared from backward. It looks patterns like 4, 5, 12 cannot exist in onnx. Considering that only the channel dimension should changed in onnx to tensorflow, I think procedure stated below can work for arbitrary dimension broadcasting.
As a result, one reshape layer one transpose layer will be added. |
Unfortunately, there is a problem in shufflenet-based models that prevents locating the channel dimension when all dimensions except batch size are the same. There are quite a few models where the very operation of comparing ONNX shapes to TensorFlow shapes breaks down. e.g. onnx x: It might be possible to respond in a very limited way... 🤔 |
What about comparing intermediate output using dummy input? For the cases you mentioned, brute-force looks like the only solution. |
Thanks. |
Notes on implementation ideas.
Need to separate the logic for ambiguous match comparison for each pattern of integers and decimals. https://numpy.org/doc/stable/reference/generated/numpy.isclose.html |
|
Current onnx2tf/onnx2tf/utils/common_functions.py Lines 641 to 651 in 86cc1a0
For now, I almost fixed these bugs. Do you mind if I open PR after checking some patterns to make sure bug is fixed? |
I see. Thanks.
Consider switching the order of decisions as follows. # If const_or_var_2.shape is all 1's, do not broadcast and return as is
shape_for_judging_skip_processing = [
i if i is not None else INF_INDEX_VALUE for i in const_or_var_2.shape
]
if np.prod(shape_for_judging_skip_processing) == 1:
return const_or_var_1, const_or_var_2
# Swap: len(const_or_var_1.shape) > len(const_or_var_2.shape)
if len(const_or_var_1.shape) < len(const_or_var_2.shape):
const_or_var_1, const_or_var_2 = const_or_var_2, const_or_var_1
graph_node_input_name1, graph_node_input_name2 = graph_node_input_name2, graph_node_input_name1
Consider adding logic to determine if all dimensions match before calculating |
Sorry. I was so focused on the text pointing out the bug that I missed this last sentence. Of course. You are welcome. :) |
Issue Type
Others
onnx2tf version number
1.1.25
Download URL for ONNX
gist for reproduction : https://colab.research.google.com/gist/Hyunseok-Kim0/d0aaf6e9ac6fbe461c5f2364db4bc0b2/onnx2tf_20221117.ipynb
Parameter Replacement JSON
N/A
Description
channel_transpose
in common_functions.py is used in arithmetic operations like Add, Sub, Mul, etc. What is the main purpose of this function? When second input has more dimension,channel_transpose
adds additional squeeze layer and changes the output shape, not vice versa.Please see the gist (https://colab.research.google.com/gist/Hyunseok-Kim0/d0aaf6e9ac6fbe461c5f2364db4bc0b2/onnx2tf_20221117.ipynb). When the output of network is
x = x + y
, onnx and tflite has same output shape. Converted tflite has wrong shape forx = y + x
.The text was updated successfully, but these errors were encountered: