onnx / onnx-tensorflow

Tensorflow Backend for ONNX
Other
1.27k stars 296 forks source link

Can't convert onnx to tf format on Google Colab #1026

Open wiky231 opened 2 years ago

wiky231 commented 2 years ago

I trained a custom YOLOv5s model on Google Colab and I'm trying to convert the onnx file to a tf file so that I can subsequently convert it to a tflite file for an android app. However, I'm getting an error 'BackendIsNotSupposedToImplementIt: Unsqueeze version 13 is not implemented.' I'm completely new to this and have been referring to tutorials so far. The code is attached here.

`import onnx import tensorflow as tf import numpy as np !pip install onnx_tf from onnx_tf.backend import prepare onnx_model = onnx.load('/content/yolov5/runs/train/exp/weights/best.onnx') tf_rep = prepare(onnx_model) pb_path = "/content/yolov5/runs/train/exp/weights.pb" tf_rep.export_graph(pb_path)

assert os.path.exists(pb_path) print(".pb model converted successfully.")`

chinhuang007 commented 2 years ago

Unsqueeze for opset 13 is now supported. You need to do a build from source. Do a git clone and run pip install -e .

ildar-ceo commented 2 years ago

@chinhuang007, when will it be released on pypi.org with this support?

ildar-ceo commented 2 years ago

@axlecky I found the solution. If you use PyTorch, should build onnx model with opset_version = 12 like this:

data_input = torch.zeros(net.input_shape)
data_input = data_input[None,:]
torch.onnx.export(
  net.model,
  data_input,
  onnx_model_path,
  opset_version = 12,
  input_names = ['input'],
  output_names = ['output'],
  verbose=False
)

And it works on onnx-tf 1.10.0 version from pypi.org

xkasberg commented 1 year ago

@ildar-ceo I am converting a simple text classification model from PyTorch to onnx to TensorFlow. PyTorch to Onyx is working fine, but I cannot convert from onyx to TF on google collar. I had specified opset_version = 12, and onnx to TF still does not work. This my traceback:

TypeError: No common supertype of TensorArraySpec(TensorShape([64]), tf.float32, True, True) and TensorArraySpec(TensorShape([64]), tf.float32, None, True).

MarcoEsposito890 commented 1 year ago

Hi, I am facing the same issue, but I can't fix it with your method. I saved the pytorch model with this code, specifying the opset version

torch.onnx.export( model, # PyTorch Model sample_input, # Input tensor 'EQT.onnx', # Output file name input_names=['input'], # Input tensor name (arbitary) output_names=['output'], # Output tensor name (arbitary) opset_version=12 )

and then convert to Tensorflow

onnx_model = onnx.load("EQT.onnx") # load onnx model tf_rep = prepare(converted_model) # prepare tf representation tf_rep.export_graph("./EQT") # export the model

but I still get error BackendIsNotSupposedToImplementIt: Unsqueeze version 13 is not implemented.

I am using onnx-tf 1.10.0 from pypi