Skip to content

Inference result is different between Tensorflow and ONNX model #2386

Open
@MartinCartajena

Description

@MartinCartajena

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugAn unexpected problem or unintended behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions