I've converted a model from Keras to Onnx with the following code:
import tensorflow as tf
import onnx
import tf2onnx.convert
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.preprocessing import image
model = keras.models.load_model('model_13.h5')
model.load_weights('model_13.h5')
onnx_model, _ = tf2onnx.convert.from_keras(model)
onnx.save(onnx_model, 'model.onnx')
Given the same input ("test1.jpg), the Keras model returns a score of 9.104029e-08 while the Onnx model returns an object that appears to include a totally different score:
[array([[0.72882545]], dtype=float32)]
Am I missing something or not unpacking the output properly? The code I'm using for inference with Onnx is:
import onnxruntime
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.imagenet_utils import decode_predictions, preprocess_input
import PIL
import sys
import numpy as np
sess_options = onnxruntime.SessionOptions()
sess = onnxruntime.InferenceSession('model.onnx', sess_options)
image_size = (180, 180)
batch_size = 32
img = keras.preprocessing.image.load_img(
"test1.jpg", target_size=image_size
)
x = image.img_to_array(img)
x = preprocess_input(x, mode='torch')
inputs = np.expand_dims(x, 0)
sess_options = onnxruntime.SessionOptions()
sess = onnxruntime.InferenceSession('model.onnx', sess_options)
data = [inputs]
input_names = sess.get_inputs()
feed = zip(sorted(i_.name for i_ in input_names), data)
actual = sess.run(None, dict(feed))
print(actual)