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

Converting a Yolov5 model to tensorflow #135

Closed notAstereo closed 1 year ago

notAstereo commented 1 year ago

Issue Type

Others

OS

Ubuntu

OS architecture

x86_64

Programming Language

Python

Framework

PyTorch

Download URL for ONNX / OpenVINO IR

model.zip

Convert Script

The model model.onnx in model.zip was created using Yolov5

I'm running all the following scripts in this docker container

sudo docker run -it --rm -v `pwd`:/home/user/workdir ghcr.io/pinto0309/
openvino2tensorflow:latest

First, I simplify the model (from model.zip)

  1. onnxsim model.onnx model_sim.onnx
  2. mo --input_model model_sim.onnx
  3. openvino2tensorflow --model_path model_sim.xml --model_output_path model -- output_saved_model --output_pb --non_verbose

At this point I got errors ERROR: axes don't match array but I could solve this using your instructions in https://github.com/PINTO0309/openvino2tensorflow/issues/125 using

{
  "format_version": 2,
  "layers": [
    {
      "layer_id": "229",
      "type": "Const",
      "replace_mode": "direct",
      "values": [
        0,
        3,
        1,
        2,
        4
      ]
    }
  ]
}

Description

Now I'm facing a different error that does not seem to be related from the transpose error?

ERROR: int() argument must be a string, a bytes-like object or a number, not 'KerasTensor'
ERROR: model_path  : model.xml
ERROR: weights_path: model.bin
ERROR: layer_id    : 290
ERROR: The trace log is below.
Traceback (most recent call last):
  File "/usr/local/bin/openvino2tensorflow", line 5397, in convert
    axis = int(tf_layers_dict[get_tf_edges_from(tf_edges, layer_id, 1)])
TypeError: int() argument must be a string, a bytes-like object or a number, not 'KerasTensor'
ERROR: Please refer to 6-7 in the README first. https://github.com/PINTO0309/openvino2tensorflow

Is there something wrong with my model?

Relevant Log Output

====================================================================================
layer_type: Convolution
layer_id: 270
input_layer0: layer_id=253: KerasTensor(type_spec=TensorSpec(shape=(1, 6, 6, 32), dtype=tf.float32, name=None), name='tf.concat_10/concat:0', description="created by layer 'tf.concat_10'")
input_layer1_shape: layer_id=269: (16, 32, 1, 1)
tf_layers_dict: KerasTensor(type_spec=TensorSpec(shape=(1, 6, 6, 16), dtype=tf.float32, name=None), name='conv2d_44/Conv2D:0', description="created by layer 'conv2d_44'")
====================================================================================
layer_type: Const
layer_id: 271
tf_layers_dict_shape: (1, 16, 1, 1)
====================================================================================
layer_type: Add
layer_id: 272
input_layer0: layer_id=270: KerasTensor(type_spec=TensorSpec(shape=(1, 6, 6, 16), dtype=tf.float32, name=None), name='conv2d_44/Conv2D:0', description="created by layer 'conv2d_44'")
input_layer1_shape: layer_id=271: (1, 16, 1, 1)
tf_layers_dict: KerasTensor(type_spec=TensorSpec(shape=(1, 6, 6, 16), dtype=tf.float32, name=None), name='tf.math.add_49/Add:0', description="created by layer 'tf.math.add_49'")
====================================================================================
layer_type: Clamp
layer_id: 273
input_layer0: layer_id=272: KerasTensor(type_spec=TensorSpec(shape=(1, 6, 6, 16), dtype=tf.float32, name=None), name='tf.math.add_49/Add:0', description="created by layer 'tf.math.add_49'")
tf_layers_dict: KerasTensor(type_spec=TensorSpec(shape=(1, 6, 6, 16), dtype=tf.float32, name=None), name='tf.nn.relu6_43/Relu6:0', description="created by layer 'tf.nn.relu6_43'")
====================================================================================
layer_type: Concat
layer_id: 274
input_layer0: layer_id=268: KerasTensor(type_spec=TensorSpec(shape=(1, 6, 6, 16), dtype=tf.float32, name=None), name='tf.nn.relu6_42/Relu6:0', description="created by layer 'tf.nn.relu6_42'")
input_layer1: layer_id=273: KerasTensor(type_spec=TensorSpec(shape=(1, 6, 6, 16), dtype=tf.float32, name=None), name='tf.nn.relu6_43/Relu6:0', description="created by layer 'tf.nn.relu6_43'")
tf_layers_dict: KerasTensor(type_spec=TensorSpec(shape=(1, 6, 6, 32), dtype=tf.float32, name=None), name='tf.concat_11/concat:0', description="created by layer 'tf.concat_11'")
axis: -1
====================================================================================
layer_type: Const
layer_id: 275
tf_layers_dict_shape: (32, 32, 1, 1)
====================================================================================
layer_type: Convolution
layer_id: 276
input_layer0: layer_id=274: KerasTensor(type_spec=TensorSpec(shape=(1, 6, 6, 32), dtype=tf.float32, name=None), name='tf.concat_11/concat:0', description="created by layer 'tf.concat_11'")
input_layer1_shape: layer_id=275: (32, 32, 1, 1)
tf_layers_dict: KerasTensor(type_spec=TensorSpec(shape=(1, 6, 6, 32), dtype=tf.float32, name=None), name='conv2d_45/Conv2D:0', description="created by layer 'conv2d_45'")
====================================================================================
layer_type: Const
layer_id: 277
tf_layers_dict_shape: (1, 32, 1, 1)
====================================================================================
layer_type: Add
layer_id: 278
input_layer0: layer_id=276: KerasTensor(type_spec=TensorSpec(shape=(1, 6, 6, 32), dtype=tf.float32, name=None), name='conv2d_45/Conv2D:0', description="created by layer 'conv2d_45'")
input_layer1_shape: layer_id=277: (1, 32, 1, 1)
tf_layers_dict: KerasTensor(type_spec=TensorSpec(shape=(1, 6, 6, 32), dtype=tf.float32, name=None), name='tf.math.add_50/Add:0', description="created by layer 'tf.math.add_50'")
====================================================================================
layer_type: Clamp
layer_id: 279
input_layer0: layer_id=278: KerasTensor(type_spec=TensorSpec(shape=(1, 6, 6, 32), dtype=tf.float32, name=None), name='tf.math.add_50/Add:0', description="created by layer 'tf.math.add_50'")
tf_layers_dict: KerasTensor(type_spec=TensorSpec(shape=(1, 6, 6, 32), dtype=tf.float32, name=None), name='tf.nn.relu6_44/Relu6:0', description="created by layer 'tf.nn.relu6_44'")
====================================================================================
layer_type: Const
layer_id: 280
tf_layers_dict_shape: (6, 32, 1, 1)
====================================================================================
layer_type: Convolution
layer_id: 281
input_layer0: layer_id=279: KerasTensor(type_spec=TensorSpec(shape=(1, 6, 6, 32), dtype=tf.float32, name=None), name='tf.nn.relu6_44/Relu6:0', description="created by layer 'tf.nn.relu6_44'")
input_layer1_shape: layer_id=280: (6, 32, 1, 1)
tf_layers_dict: KerasTensor(type_spec=TensorSpec(shape=(1, 6, 6, 6), dtype=tf.float32, name=None), name='conv2d_46/Conv2D:0', description="created by layer 'conv2d_46'")
====================================================================================
layer_type: Const
layer_id: 282
tf_layers_dict_shape: (1, 6, 1, 1)
====================================================================================
layer_type: Add
layer_id: 283
input_layer0: layer_id=281: KerasTensor(type_spec=TensorSpec(shape=(1, 6, 6, 6), dtype=tf.float32, name=None), name='conv2d_46/Conv2D:0', description="created by layer 'conv2d_46'")
input_layer1_shape: layer_id=282: (1, 6, 1, 1)
tf_layers_dict: KerasTensor(type_spec=TensorSpec(shape=(1, 6, 6, 6), dtype=tf.float32, name=None), name='tf.math.add_51/Add:0', description="created by layer 'tf.math.add_51'")
====================================================================================
layer_type: Const
layer_id: 284
tf_layers_dict_shape: (5,)
====================================================================================
layer_type: Reshape
layer_id: 285
input_layer0: layer_id=283: KerasTensor(type_spec=TensorSpec(shape=(1, 6, 6, 6), dtype=tf.float32, name=None), name='tf.math.add_51/Add:0', description="created by layer 'tf.math.add_51'")
input_layer1_shape: layer_id=284: (5,)
tf_layers_dict: KerasTensor(type_spec=TensorSpec(shape=(1, 6, 6, 1, 6), dtype=tf.float32, name=None), name='tf.reshape_2/Reshape:0', description="created by layer 'tf.reshape_2'")
====================================================================================
layer_type: Const
layer_id: 286
tf_layers_dict_shape: (5,)
====================================================================================
layer_type: Transpose
layer_id: 287
input_layer0: layer_id=285: KerasTensor(type_spec=TensorSpec(shape=(1, 6, 6, 1, 6), dtype=tf.float32, name=None), name='tf.reshape_2/Reshape:0', description="created by layer 'tf.reshape_2'")
input_layer1_shape: layer_id=286: (5,)
tf_layers_dict: KerasTensor(type_spec=TensorSpec(shape=(1, 1, 6, 6, 6), dtype=tf.float32, name=None), name='tf.compat.v1.transpose_1/transpose:0', description="created by layer 'tf.compat.v1.transpose_1'")
====================================================================================
layer_type: Sigmoid
layer_id: 288
input_layer0: layer_id=287: KerasTensor(type_spec=TensorSpec(shape=(1, 1, 6, 6, 6), dtype=tf.float32, name=None), name='tf.compat.v1.transpose_1/transpose:0', description="created by layer 'tf.compat.v1.transpose_1'")
tf_layers_dict: KerasTensor(type_spec=TensorSpec(shape=(1, 1, 6, 6, 6), dtype=tf.float32, name=None), name='tf.math.sigmoid_1/Sigmoid:0', description="created by layer 'tf.math.sigmoid_1'")
====================================================================================
layer_type: Const
layer_id: 289
tf_layers_dict_shape: (1,)
====================================================================================
ERROR: int() argument must be a string, a bytes-like object or a number, not 'KerasTensor'
ERROR: model_path  : model.xml
ERROR: weights_path: model.bin
ERROR: layer_id    : 290
ERROR: The trace log is below.
ERROR: Please refer to 6-7 in the README first. https://github.com/PINTO0309/openvino2tensorflow

Source code for simple inference testing code

-

PINTO0309 commented 1 year ago

I recommend the use of the latest tool here. No conversion errors occur and no accuracy errors are introduced. It also supports complex models such as Transformer. openvino2tensorflow is not actively maintained at this time. https://github.com/PINTO0309/onnx2tf

onnx2tf -i best.onnx -osd -oh5 -cotof

image