qqwweee / keras-yolo3

A Keras implementation of YOLOv3 (Tensorflow backend)
MIT License
7.14k stars 3.44k forks source link

"image_shape:01 is disconnected" error on model conversion to ONNX #724

Open semihcanturk opened 4 years ago

semihcanturk commented 4 years ago

I am trying to convert the YOLOv3 model here to ONNX. This is also crossposted to the keras-onnx repository (https://github.com/onnx/keras-onnx/issues/606), but I wanted to post it here for outreach as well given that it may be of interest to others.

The README on https://github.com/onnx/keras-onnx/tree/master/applications/yolov3 states the following:

The original keras model was coming from: https://github.com/qqwweee/keras-yolo3, clone the project and follow the 'Quick Start' to get the pre-trained model. We have converted yolov3 model successfully and uploaded to the model zoo https://github.com/onnx/models/tree/master/yolov3

When I try to run https://github.com/onnx/keras-onnx/blob/master/applications/yolov3/yolov3.py after following the Quick Start section, I get the following error:

Traceback (most recent call last):
  File "/Users/semo/keras-yolo3/yolov3_test.py", line 473, in <module>
    onnxmodel = convert_model(yolo, target_opset)
  File "/Users/semo/keras-yolo3/yolov3_test.py", line 418, in convert_model
    onnxmodel_2 = convert_keras(yolo.evaluation_model, target_opset=target_opset)
  File "/Users/semo/keras-yolo3/venv/lib/python3.7/site-packages/keras2onnx/main.py", line 105, in convert_keras
    return convert_topology(topology, name, doc_string, target_opset, channel_first_inputs)
  File "/Users/semo/keras-yolo3/venv/lib/python3.7/site-packages/keras2onnx/topology.py", line 317, in convert_topology
    nodes = _remove_unused_nodes(container.nodes, container.inputs + extra_inputs, container.outputs)
  File "/Users/semo/keras-yolo3/venv/lib/python3.7/site-packages/keras2onnx/topology.py", line 222, in _remove_unused_nodes
    "{} is disconnected, check the parsing log for more details.".format(in_)
AssertionError: image_shape:01 is disconnected, check the parsing log for more details.

I'm using the following relevant package versions: keras2onnx 1.7.1 (latest master) onnx 1.7.0 onnxconverter-common 1.7.0 (latest master) onnxruntime 1.4.0 tensorflow 2.2.0 (I've also tried with 2.0.2, and am using tensorflow.keras with TF_KERAS=1)

I've tried with opsets 10 to 12 along with the respective ONNX versions, but haven't been able to resolve the issue. Any help is much appreciated

tfukumori commented 4 years ago

I use an automated translation site, so please forgive us if it is difficult to read.

"keras2onnx", "onnx", "onnxconverter-common" versions, etc. could be the problem.

I'm using a different repository, with a different version of "keras2onnx", but I hope this will be of some help.

Repositories used

https://github.com/axinc-ai/yolov3-face https://github.com/qqwweee/keras-yolo3

Clone the source

  1. Clone from axinc-ai/yolov3-face with git

    git clone https://github.com/axinc-ai/yolov3-face.git
  2. Clone keras-yolo3 linked in yolov3-face

    cd .\yolov3-face
    git clone https://github.com/qqwweee/keras-yolo3.git

Conversion

  1. Creating the conversion environment

    Create the virtual environment "yolov3-face" with conda

    conda create -n yolov3-face python=3.6 -y

    Install the necessary modules. As the version is specified in "axinc-ai/yolov3-face" and is not available in the conda repository, install it by pip.

    conda activate yolov3-face
    pip install tensorflow==1.13.2
    pip install keras==2.2.4
    pip install keras2onnx==1.5.1
    pip install opencv-python
    pip install pillow
    pip install matplotlib
  2. Perform version down of "onnxconverter-common" and "onnx".

    "axinc-ai/yolov3-face" does not work with ONNX 1.7.0 (ONNX v7), so I had to use ONNX v6.

    Reduce to onnx==1.6.0 and onnxconverter-common==1.6.0.

    pip install onnxconverter-common==1.6.0
    pip install onnx==1.6.0

    conda list(in Japanese)

    To support TensorRT 5.x, do the following

    pip install onnxconverter-common==1.6.0
    pip install onnx==1.4.1
  3. Perform the conversion

    cd .\keras-onnx
    conda activate yolov3-face
    python keras-yolo3-to-onnx.py ../model_data/logs/trained_weights_final.h5 ../model_data/face_classes.txt ../model_data/tiny_yolo_anchors.txt ../model_data/ax_face.onnx

Inference

  1. Create an environment for inference

    Create the virtual environment "yolov3-face-inference" with conda

    conda create -n yolov3-face-inference python=3.6 -y

    Install the necessary modules. As the version is specified in "axinc-ai/yolov3-face" and is not available in the conda repository, install it by pip.

    conda activate yolov3-face-inference
    pip install tensorflow==1.13.2
    pip install onnxruntime
    pip install keras==2.2.4
    pip install pillow

    conda list(in japanese)

  2. Perform the inference

    cd .\keras-onnx
    conda activate yolov3-face-inference
    python inference.py ../model_data/ax_face.onnx ../model_data/face_classes.txt ../images/couple.jpg output.jpg

reference

https://qiita.com/tfukumori/items/66966074b746bc815f18 The results of "conda list" are described. This is a web page in Japanese.