Search code examples
onnx

Output of Conv op in onnx is of Shape None


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!


Solution

  • 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