Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inference result is different between Tensorflow and ONNX model #2386

Open
MartinCartajena opened this issue Feb 4, 2025 · 0 comments
Open
Labels
bug An unexpected problem or unintended behavior

Comments

@MartinCartajena
Copy link

MartinCartajena commented Feb 4, 2025

Hello,

I have a binary classifier in TensorFlow and I converted it to ONNX using tf2onnx with the following command:

python -m tf2onnx.convert --saved-model C:\example_path\pb_format --output C:\example_path\model.onnx --opset 17

When running inference on the model.onnx with the proper format, the result (with the same input) is different from the TensorFlow model. This is the system information:

TensorFlow Version: 2.9.0
Keras: 2.9.0
Python version: 3.9.0
tf2onnx: '1.16.1'

I have also tried converting the model to ONNX using this other method. The inference results are different from the other two models...

import tensorflow as tf
from keras.layers import Input, Dense, Flatten, Dropout, BatchNormalization
from keras.layers import Conv2D, SeparableConv2D, MaxPool2D
from keras.models import Model
import os
import tf2onnx

def main():    
    checkpoint_path = r"C:\example_path\best_loss_model"
    
    model = binary_classifier()
    
    model.load_weights(checkpoint_path)

    onnx_model, _ = tf2onnx.convert.from_keras(model, opset=17)

    onnx_model_path = "model.onnx"
    with open(onnx_model_path, "wb") as f:
        f.write(onnx_model.SerializeToString())


def binary_classifier():
    model = tf.keras.models.Sequential([
        tf.keras.layers.Conv2D(16, (3, 3), activation='relu', input_shape=(512, 512, 3)),
        tf.keras.layers.MaxPooling2D(2, 2),
        tf.keras.layers.Conv2D(32, (3, 3), activation='relu'),
        tf.keras.layers.MaxPooling2D(2, 2),
        tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),
        tf.keras.layers.MaxPooling2D(2, 2),
        tf.keras.layers.Flatten(),
        tf.keras.layers.Dense(512, activation='relu'),
        tf.keras.layers.Dense(1, activation='sigmoid')
    ])

    return model


if __name__ == "__main__":
    main()

Have you found the reason or any solution?

Thanks,

Martin

@MartinCartajena MartinCartajena added the bug An unexpected problem or unintended behavior label Feb 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug An unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

1 participant