PINTO0309 / onnx2tf

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). I don't need a Star, but give me a pull request.
MIT License
662 stars 65 forks source link

[MMVC] "IndexError: list index out of range" occurs after flattening #265

Closed stealthinu closed 1 year ago

stealthinu commented 1 year ago

Issue Type

Others

onnx2tf version number

1.7.33

onnx version number

1.13.1

tensorflow version number

2.12.0rc1

Download URL for ONNX

https://github.com/stealthinu/MMVC_Trainer/releases/download/v1.5onnx-tf/G_200000.onnx

Parameter Replacement JSON

{}

Description

  1. Purpose Product development

  2. What I was trying to convert the currently-in-development alpha version of the MMVC-1.5 ONNX model, which has a fixed-length input. When attempting the conversion as shown below, I encountered an error.

$ onnx2tf -i G_200000.onnx -cotof -cotoa 1e-1
INFO: onnx_op_type: Flatten onnx_op_name: /dec/blocks.0/Flatten
INFO:  input_name.1: /dec/blocks.0/nonlinears.0/LeakyRelu_output_0 shape: [1, 256, 584] dtype: float32
INFO:  output_name.1: /dec/blocks.0/Flatten_output_0 shape: [149504, 1] dtype: float32
ERROR: The trace log is below.
Traceback (most recent call last):
  File "/home/stealth/anaconda3/envs/mmvc15/lib/python3.9/site-packages/onnx2tf/utils/common_functions.py", line 280, in print_wrapper_func
    result = func(*args, **kwargs)
  File "/home/stealth/anaconda3/envs/mmvc15/lib/python3.9/site-packages/onnx2tf/utils/common_functions.py", line 358, in inverted_operation_enable_disable_wrapper_func
    result = func(*args, **kwargs)
  File "/home/stealth/anaconda3/envs/mmvc15/lib/python3.9/site-packages/onnx2tf/utils/common_functions.py", line 49, in get_replacement_parameter_wrapper_func
    func(*args, **kwargs)
  File "/home/stealth/anaconda3/envs/mmvc15/lib/python3.9/site-packages/onnx2tf/ops/Flatten.py", line 61, in make_node
    axis = convert_axis(
  File "/home/stealth/anaconda3/envs/mmvc15/lib/python3.9/site-packages/onnx2tf/utils/common_functions.py", line 571, in convert_axis
    converted_axis = convertion_table[converted_axis]
IndexError: list index out of range
ERROR: input_onnx_file_path: G_200000.onnx
ERROR: onnx_op_name: /dec/blocks.0/Flatten
  1. How In PyTorch, the network structure is believed to correspond to the following source code section based on the flow of processing. https://github.com/isletennos/MMVC_Trainer/blob/v1.5.0.0_SiFiGAN/sifigan/generator.py#L248-L251 https://github.com/isletennos/MMVC_Trainer/blob/v1.5.0.0_SiFiGAN/sifigan/generator.py#L99-L112

It seems that "Flatten" here might not be necessary, but I couldn't figure out how to remove it.

I thought this error might be caused by the large size of the Tensor after flattening, so I reduced the size in the time dimension and changed the shape after flattening to [6144, 1]. However, the same error occurred.

  1. Why I would like to make these modifications because I want to run the model in TFLite.

  2. Resources None

PINTO0309 commented 1 year ago

This ONNX refers to an axis that I am not assuming. image

The error is correct. Because I did not implement it assuming axis=3. https://github.com/onnx/onnx/blob/main/docs/Changelog.md#Flatten-13 image

PINTO0309 commented 1 year ago

The fix is already done and undergoing regression testing by CI, but I do not recommend using RundomNormalLike. If you want to generate random numbers by normal fraction, it is better to pass the random number part that is assumed to be generated by RundomNormalLike from the Android or Java logic as a variable in the input OP.

stealthinu commented 1 year ago

Thank you for your great advice! As you suggested, I will try giving randomly generated Tensors as initial values.

PINTO0309 commented 1 year ago

Fixes: https://github.com/PINTO0309/onnx2tf/releases/tag/1.8.1

stealthinu commented 1 year ago

I tried it with version 1.8.1 and confirmed that it can be converted to TF completely. Thank you very much!

PINTO0309 commented 1 year ago

I'm glad it worked out. I will close this issue once.

Incidentally, my suggestion that it might be better to replace RandomNormalLike with an external input variable was because I thought it might be possible to keep the length of the model input OP dynamic.

stealthinu commented 1 year ago

Incidentally, my suggestion that it might be better to replace RandomNormalLike with an external input variable was because I thought it might be possible to keep the length of the model input OP dynamic.

Thank you for your response. In fact, I changed the model to have a fixed length in version 1.5 because it wouldn't work in ONNX without a fixed length.