英文:
Output of Conv op in onnx is of Shape None
问题
我训练了一个PyTorch模型,并尝试将其导出为ONNX格式。我使用的脚本如下:
```python
!git clone https://github.com/WongKinYiu/yolov7
!pip install -r yolov7/requirements.txt
...
!python3 yolov7/export.py --weights {my_checkpoint_path} --grid --simplify
然后,当我尝试使用onnx2tf
导出到TensorFlow时,我遇到了以下问题:
模型转换开始 ============================================================
INFO: input_op_name: images shape: [1, 3, 640, 640] dtype: float32
INFO: onnx_op_type: Conv onnx_op_name: /model.0/conv/Conv
INFO: input_name.1: images shape: [1, 3, 640, 640] dtype: float32
INFO: input_name.2: model.0.conv.weight shape: [32, 3, 3, 3] dtype: <class 'numpy.float32'>
INFO: input_name.3: model.0.conv.bias shape: [32] dtype: <class 'numpy.float32'>
INFO: output_name.1: /model.0/conv/Conv_output_0 shape: None dtype: None
ERROR: 下面是跟踪日志。
Traceback (most recent call last):
File "/usr/local/google/home/huiliukeith/pytorchenv/lib/python3.10/site-packages/onnx2tf/utils/common_functions.py", line 267, in print_wrapper_func
result = func(*args, **kwargs)
File "/usr/local/google/home/huiliukeith/pytorchenv/lib/python3.10/site-packages/onnx2tf/utils/common_functions.py", line 329, in inverted_operation_enable_disable_wrapper_func
result = func(*args, **kwargs)
File "/usr/local/google/home/huiliukeith/pytorchenv/lib/python3.10/site-packages/onnx2tf/ops/Conv.py", line 111, in make_node
and graph_node.inputs[0].shape[2:] == output_tensor_shape[2:]:
TypeError: 'NoneType' object is not subscriptable
ERROR: 阅读此内容并处理。 https://github.com/PINTO0309/onnx2tf#parameter-replacement
ERROR: 或者,如果输入操作具有动态维度,请使用-b或-ois选项将其重写为静态形状,然后重试。
ERROR: 如果ONNX转换之前的输入操作是NHWC,请使用-kt选项。
我觉得这可能不是onnx2tf
的问题,因为我确实看到一些示例中Conv的输出形状不为None(https://github.com/PINTO0309/onnx2tf/issues/15)?
如果是这样的话,使用torch.onnx.export
时是否有一些选项可能会导致输出形状为None?
谢谢!
<details>
<summary>英文:</summary>
I trained a pyTorch model and tried to export as onnx format. Script I used
!git clone https://github.com/WongKinYiu/yolov7
!pip install -r yolov7/requirements.txt
...
!python3 yolov7/export.py --weights {my_checkpoint_path} --grid --simplify
Then, when I tried to export to tf using `onnx2tf`, I got
Model convertion started ============================================================
INFO: input_op_name: images shape: [1, 3, 640, 640] dtype: float32
INFO: onnx_op_type: Conv onnx_op_name: /model.0/conv/Conv
INFO: input_name.1: images shape: [1, 3, 640, 640] dtype: float32
INFO: input_name.2: model.0.conv.weight shape: [32, 3, 3, 3] dtype: <class 'numpy.float32'>
INFO: input_name.3: model.0.conv.bias shape: [32] dtype: <class 'numpy.float32'>
INFO: output_name.1: /model.0/conv/Conv_output_0 shape: None dtype: None
ERROR: The trace log is below.
Traceback (most recent call last):
File "/usr/local/google/home/huiliukeith/pytorchenv/lib/python3.10/site-packages/onnx2tf/utils/common_functions.py", line 267, in print_wrapper_func
result = func(*args, **kwargs)
File "/usr/local/google/home/huiliukeith/pytorchenv/lib/python3.10/site-packages/onnx2tf/utils/common_functions.py", line 329, in inverted_operation_enable_disable_wrapper_func
result = func(*args, **kwargs)
File "/usr/local/google/home/huiliukeith/pytorchenv/lib/python3.10/site-packages/onnx2tf/ops/Conv.py", line 111, in make_node
and graph_node.inputs[0].shape[2:] == output_tensor_shape[2:]:
TypeError: 'NoneType' object is not subscriptable
ERROR: Read this and deal with it. https://github.com/PINTO0309/onnx2tf#parameter-replacement
ERROR: Alternatively, if the input OP has a dynamic dimension, use the -b or -ois option to rewrite it to a static shape and try again.
ERROR: If the input OP of ONNX before conversion is NHWC, use the -kt option.
I feel like it is not of `onnx2tf` issue because I do see examples where output shape of Conv is not None (https://github.com/PINTO0309/onnx2tf/issues/15)?
If so, is there some options when using `torch.onnx.export` that might cause the output shape to be None?
Thanks!
</details>
# 答案1
**得分**: 0
以下是翻译好的部分:
假设您正在使用Google Colaboratory,以下是您成功转换的命令。您可能需要指定python3.9并运行以下命令。
此外,如果已正确安装onnxsim,则不会出现您遇到的错误。因此,我敢于不使用yolov7的requirements.txt文件,只安装了最低要求的软件包。
!sudo add-apt-repository -y ppa:deadsnakes/ppa
!sudo apt-get -y update
!sudo apt-get -y install python3.9
!sudo apt-get -y install python3.9-dev
!sudo apt-get -y install python3-pip
!sudo apt-get -y install python3.9-distutils
!python3.9 -m pip install -U setuptools
&& python3.9 -m pip install -U pip
&& python3.9 -m pip install -U distlib
!sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 1
!sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 2
!python3.9 -m pip install tensorflow==2.10.0
&& python3.9 -m pip install -U onnx
&& python3.9 -m pip install -U nvidia-pyindex
&& python3.9 -m pip install -U onnx-graphsurgeon
&& python3.9 -m pip install -U onnxsim
&& python3.9 -m pip install -U simple_onnx_processing_tools
&& python3.9 -m pip install -U onnx2tf
&& python3.9 -m pip install -U protobuf==3.20.3
&& python3.9 -m pip install -U h5py==3.7.0
!python3.9 -m pip install -U
torch
torchvision
pandas
opencv-python
tqdm
pyyaml
matplotlib
seaborn
scipy
coremltools
onnxruntime
!git clone https://github.com/WongKinYiu/yolov7
%cd yolov7
!wget https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7.pt
!python3.9 export.py --weights yolov7.pt --grid --simplify
!onnx2tf -i yolov7.onnx -cotof -cotoa 1e-1
请注意,ONNX和TensorFlow的输出完全匹配。
INFO: onnx_output_name: /model.105/Mul_5_output_0 shape: (1, 3, 20, 20, 2) dtype: float32 validate_result: Matches
INFO: onnx_output_name: /model.105/Concat_2_output_0 shape: (1, 3, 20, 20, 85) dtype: float32 validate_result: Matches
INFO: onnx_output_name: /model.105/Reshape_5_output_0 shape: (1, 1200, 85) dtype: float32 validate_result: Matches
INFO: onnx_output_name: output shape: (1, 25200, 85) dtype: float32 validate_result: Matches
<details>
<summary>英文:</summary>
Assuming you are using Google Colaboratory, here are the commands you were able to convert successfully.You will probably need to specify python3.9 and run the command.
Also, if onnxsim is installed correctly, the error you have encountered will not occur. So I dared not use yolov7's requirements.txt and installed only the minimum required packages.
!sudo add-apt-repository -y ppa:deadsnakes/ppa
!sudo apt-get -y update
!sudo apt-get -y install python3.9
!sudo apt-get -y install python3.9-dev
!sudo apt-get -y install python3-pip
!sudo apt-get -y install python3.9-distutils
!python3.9 -m pip install -U setuptools
&& python3.9 -m pip install -U pip
&& python3.9 -m pip install -U distlib
!sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 1
!sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 2
!python3.9 -m pip install tensorflow==2.10.0
&& python3.9 -m pip install -U onnx
&& python3.9 -m pip install -U nvidia-pyindex
&& python3.9 -m pip install -U onnx-graphsurgeon
&& python3.9 -m pip install -U onnxsim
&& python3.9 -m pip install -U simple_onnx_processing_tools
&& python3.9 -m pip install -U onnx2tf
&& python3.9 -m pip install -U protobuf==3.20.3
&& python3.9 -m pip install -U h5py==3.7.0
!python3.9 -m pip install -U
torch
torchvision
pandas
opencv-python
tqdm
pyyaml
matplotlib
seaborn
scipy
coremltools
onnxruntime
!git clone https://github.com/WongKinYiu/yolov7
%cd yolov7
!wget https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7.pt
!python3.9 export.py --weights yolov7.pt --grid --simplify
!onnx2tf -i yolov7.onnx -cotof -cotoa 1e-1
Note that the output from ONNX and TensorFlow match perfectly.
INFO: onnx_output_name: /model.105/Mul_5_output_0 shape: (1, 3, 20, 20, 2) dtype: float32 validate_result: Matches
INFO: onnx_output_name: /model.105/Concat_2_output_0 shape: (1, 3, 20, 20, 85) dtype: float32 validate_result: Matches
INFO: onnx_output_name: /model.105/Reshape_5_output_0 shape: (1, 1200, 85) dtype: float32 validate_result: Matches
INFO: onnx_output_name: output shape: (1, 25200, 85) dtype: float32 validate_result: Matches
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论