onnx-tensorflow icon indicating copy to clipboard operation
onnx-tensorflow copied to clipboard

Unable to convert onnx to tensorflow - help

Open ushasai opened this issue 1 year ago • 1 comments

Hi,

Kindly help me with the following issue:

Code used to convert to onnx from .pt:

def convert_to_onnx(model, input_size=(1, 3, 128, 128), onnx_model_path="model.onnx", device='cuda'): dummy_input = torch.randn(*input_size).to(device) # Move the dummy input to the specified device

# Check if the model is wrapped in DataParallel and unwrap it
if isinstance(model, torch.nn.DataParallel):
    model = model.module

model = model.to(device)  # Ensure the model is on the correct device

# Export the model to ONNX format
torch.onnx.export(model, dummy_input, onnx_model_path, export_params=True,
                  opset_version=12, input_names=['input'], output_names=['output'])
print(f"ONNX model export")

onnx_model_path = 'model.onnx' tf_model_path = 'model_tf' tflite_model_path = 'model.tflite'

onnx_model = onnx.load(onnx_model_path) tf_rep = prepare(onnx_model) tf_rep.export_graph(tf_model_path) converter = tf.lite.TFLiteConverter.from_saved_model(tf_model_path) converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS] tflite_model = converter.convert() with open(tflite_model_path, 'wb') as f: f.write(tflite_model)

print("TFLite model saved at:", tflite_model_path)

But I am getting this issue: ValueError: Dimensions must be equal, but are 8 and 7 for '{{node onnx_tf_prefix_/level5_0/eesp/Add}} = AddV2[T=DT_FLOAT](transpose_139, transpose_136)' with input shapes: [1,64,8,8], [1,64,7,7].

To Reproduce

I am using the latest onnx from pip I also tried install from the current repo

Please help me fix this issue. I have been struggling with this for a while. Atleast please let me know if its due to my architecture to the compatibility.

ONNX file : https://duceretechsp-my.sharepoint.com/:u:/g/personal/usha_goparaju_duceretech_com/ES4smGJkZ9VHlAGWqwVWGKgBSg-ej_oWdaGfcFPcY0YVzA?e=rJq9O8

Python, ONNX, ONNX-TF, Tensorflow version

This section can be obtained by running get_version.py from util folder.

  • Python version: 3.10
  • ONNX version: Latest from pip
  • ONNX-TF version:Latest from pip
  • Tensorflow version: 2.10.0

ushasai avatar Sep 24 '24 10:09 ushasai

i also encountered a problem, but mine is related to a compatibility issue. Tensorflow is not compatible with tensorflow-probability, i used Chargpt to help me. it suggested me to install version 2.14.0 for tensorflow and 0.21.0 for tensorflow-probability. `import onnx from onnx_tf.backend import prepare

onnx_model = onnx.load("temp_humidity_model.onnx") tf_rep = prepare(onnx_model) tf_rep.export_graph("temp_humidity_model_tf")

print("ONNX model converted to TensorFlow and saved at: temp_humidity_model_tf")`

KittyGill avatar May 06 '25 07:05 KittyGill