Skip to content

Unable to convert onnx to tensorflow - help #1079

@ushasai

Description

@ushasai

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions