Calling `Model.predict` in graph mode is not supported when the `Model` instance was constructed with eager mode enabled
The answer is simple, just load your model inside the graph just like this:
with graph.as_default(): json_file = open('models/model.json','r') loaded_model_json = json_file.read() json_file.close() loaded_model = model_from_json(loaded_model_json) #load weights into new model loaded_model.load_weights("models/model.h5") print("Loaded Model from disk") #compile and evaluate loaded model loaded_model.compile(loss='sparse_categorical_crossentropy',optimizer='adam',metrics=['accuracy']) # perform the prediction out = loaded_model.predict(img) print(out) print(class_names[np.argmax(out)]) # convert the response to a string response = class_names[np.argmax(out)] return str(response)
@Ilham: Try to wrap the call method in a tf.function
, right after defining your network. Something like this:
model = Sequential()model.call = tf.function(model.call)
I had an issue similar to yours. I solved it just by adding that second line of code.See the following link for more details: https://www.tensorflow.org/guide/intro_to_graphs