PINTO0309 / openvino2tensorflow

This script converts the ONNX/OpenVINO IR model to Tensorflow's saved_model, tflite, h5, tfjs, tftrt(TensorRT), CoreML, EdgeTPU, ONNX and pb. PyTorch (NCHW) -> ONNX (NCHW) -> OpenVINO (NCHW) -> openvino2tensorflow -> Tensorflow/Keras (NHWC/NCHW) -> TFLite (NHWC/NCHW). And the conversion from .pb to saved_model and from saved_model to .pb and from .pb to .tflite and saved_model to .tflite and saved_model to onnx. Support for building environments with Docker. It is possible to directly access the host PC GUI and the camera to verify the operation. NVIDIA GPU (dGPU) support. Intel iHD GPU (iGPU) support.
MIT License
334 stars 40 forks source link

`ValueError: axes don't match array` when converting Yolov5s model #115

Closed mikel-brostrom closed 2 years ago

mikel-brostrom commented 2 years ago

Issue Type

Bug

OS

Ubuntu

OS architecture

x86_64

Programming Language

Python

Framework

OpenVINO

Download URL for ONNX / OpenVINO IR

ONNX generated from Yolov5s with the export tool provided here: https://github.com/ultralytics/yolov5/blob/master/export.py

Convert Script

H=640
W=640

openvino2tensorflow \
    --model_path /datadrive/mikel/yolov5/openvino/best.xml \
    --output_saved_model \
    --output_pb \
    --output_no_quant_float32_tflite \
    --output_dynamic_range_quant_tflite \
    --output_weight_quant_tflite \
    --output_float16_quant_tflite \
    --output_integer_quant_tflite \
    --output_integer_quant_type 'uint8' \
    --string_formulas_for_normalization 'data / 255.0' \
    --output_tfjs \
    --output_coreml \
    --non_verbose \
    --weight_replacement_config replace.json

Description

ONNX is exported to OpenVINO by:

mo --input_model /path/to/my/yolov5s_best.onnx --output_dir ./openvino --data_type FP32

Then I use:

H=640
W=640
MODEL=v5lite_s
openvino2tensorflow \
--model_path /datadrive/mikel/yolov5/openvino/best.xml \
--output_saved_model \
--output_pb \
--output_no_quant_float32_tflite \
--output_dynamic_range_quant_tflite \
--output_weight_quant_tflite \
--output_float16_quant_tflite \
--output_integer_quant_tflite \
--output_integer_quant_type 'uint8' \
--string_formulas_for_normalization 'data / 255.0' \
--output_tfjs \
--output_coreml \
--non_verbose \
--weight_replacement_config replace.json

for TFLite conversion, where replace.json is:

{
    "format_version": 2,
    "layers": [
        {
            "layer_id": "509",
            "type": "Const",
            "replace_mode": "direct",
            "values": [
                0,
                3,
                1,
                2,
                4
            ]
        },
        {
            "layer_id": "512",
            "type": "Const",
            "replace_mode": "direct",
            "values": [
                4
            ]
        },
        {
            "layer_id": "513",
            "type": "Const",
            "replace_mode": "direct",
            "values": [
                2,
                2,
                81
            ]
        },
        {
            "layer_id": "570",
            "type": "Const",
            "replace_mode": "direct",
            "values": [
                0,
                3,
                1,
                2,
                4
            ]
        },
        {
            "layer_id": "573",
            "type": "Const",
            "replace_mode": "direct",
            "values": [
                4
            ]
        },
        {
            "layer_id": "574",
            "type": "Const",
            "replace_mode": "direct",
            "values": [
                2,
                2,
                81
            ]
        },
        {
            "layer_id": "631",
            "type": "Const",
            "replace_mode": "direct",
            "values": [
                0,
                3,
                1,
                2,
                4
            ]
        },
        {
            "layer_id": "634",
            "type": "Const",
            "replace_mode": "direct",
            "values": [
                4
            ]
        },
        {
            "layer_id": "635",
            "type": "Const",
            "replace_mode": "direct",
            "values": [
                2,
                2,
                81
            ]
        }
    ]
}

Relevant Log Output

ValueError: Dimensions must be equal, but are 80 and 3 for '{{node tf.math.add_54/Add}} = AddV2[T=DT_FLOAT](Placeholder, tf.math.add_54/Add/y)' with input shapes: [1,80,3,17,2], [1,3,80,80,2].

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/datadrive/mikel/venvs/yolov5/bin/openvino2tensorflow", line 1056, in convert
    tf_layers_dict[layer_id] = tf.math.add(tf_layers_dict[edge_id0], tf_layers_dict[edge_id1].transpose(0,2,3,1))
ValueError: axes don't match array
ERROR: Please refer to 6-7 in the README first. https://github.com/PINTO0309/openvino2tensorflow

Source code for simple inference testing code

No response

PINTO0309 commented 2 years ago

https://github.com/PINTO0309/openvino2tensorflow/issues/102