Tensorflow gives different prediction than Keras

Viewed 184

I have a model trained in Keras with 1.10 Tensorflow backend and I want to make inferences using Tensorflow 2.4.

I converted the .h5 model to SavedModel format:

import tensorflow as tf
from tensorflow.keras.models import load_model
from tensorflow.python.saved_model import builder
from tensorflow.python.saved_model.signature_def_utils import predict_signature_def
from tensorflow.python.saved_model import tag_constants

def export_h5_to_pb(path_to_h5, export_path):
    if tf.executing_eagerly():
        tf.compat.v1.disable_eager_execution()
    loaded_model = load_model(path_to_h5)
    b = builder.SavedModelBuilder(export_path)
    signature = predict_signature_def(inputs={"inputs": loaded_model.input},
                                      outputs={"score": loaded_model.output})
    session = tf.compat.v1.Session()
    init_op = tf.compat.v1.global_variables_initializer()
    session.run(init_op)
    b.add_meta_graph_and_variables(
        sess=session, tags=[tag_constants.SERVING], signature_def_map={"serving_default": signature})
    b.save()
    
export_h5_to_pb('./trained_nework_VGG3_5comp.h5', './export/Servo/1')

Tensorflow prediction gives me:

import tensorflow as tf
imported = tf.saved_model.load('./export/Servo/1', tags='serve')

f = imported.signatures["serving_default"]
f(inputs=tf.constant(test_payload))

> {'score': <tf.Tensor: shape=(1, 6), dtype=float32, numpy=array([[0.16693498, 0.16678475, 0.16666655, 0.16653116, 0.16678214,
     0.16630043]], dtype=float32)>}

While the original (correct) Keras prediction gives:

from keras.models import load_model

model = load_model('./trained_nework_VGG3_5comp.h5')
model.predict(test_payload)

> array([[1.0000000e+00, 3.0078113e-09, 2.0143587e-10, 5.7580127e-09, 1.9100479e-09, 4.1776910e-10]], dtype=float32)

What am I doing wrong?

1 Answers

I had a very similar problem, which you replied to here, and I'll share what worked for me. If you are doing this for Sagemaker/AWS (which by the file directory path and the use of the word "payload" I assume you are?), then the problem is caused by a discrepancy in TensorFlow versions.

In all of the blogs I found (e.g. this one), they used framework_version 1.12 when loading their model using TensorflowModel. Because of this, I reinstalled TensorFlow in the Sagemaker Jupyter instance to version 1.12, retrained my model using 1.12, and changed the framework_version to 1.12, and this worked for me. If you aren't using the model for AWS this very well may not apply, but if you are then this is a potential solution. Good luck!

Related