From 083f4fa8635775a12a09710134531bcff6a5c4b4 Mon Sep 17 00:00:00 2001 From: Richardson Souza Date: Sun, 23 Sep 2018 21:57:59 -0400 Subject: [PATCH] Update run_keras_server.py when the flask server tries to process the prediction the errors happen: raise ValueError("Tensor %s is not an element of this graph." % obj) ValueError: Tensor Tensor("activation 5/Sigmoid:0", shape=(?, 1), dtype=float32) is not an element of this graph. to get around the bug a global variable graph was created. global model, graph model = ResNet50(weights="imagenet") graph = tf.get_default_graph() And with default graph predictions were made: with graph.as_default(): preds = model.predict(image) --- run_keras_server.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/run_keras_server.py b/run_keras_server.py index d20d79c..53b56ce 100644 --- a/run_keras_server.py +++ b/run_keras_server.py @@ -11,6 +11,7 @@ from keras.preprocessing.image import img_to_array from keras.applications import imagenet_utils from PIL import Image +import tensorflow as tf import numpy as np import flask import io @@ -23,8 +24,9 @@ def load_model(): # load the pre-trained Keras model (here we are using a model # pre-trained on ImageNet and provided by Keras, but you can # substitute in your own networks just as easily) - global model + global model, graph model = ResNet50(weights="imagenet") + graph = tf.get_default_graph() def prepare_image(image, target): # if the image mode is not RGB, convert it @@ -58,7 +60,8 @@ def predict(): # classify the input image and then initialize the list # of predictions to return to the client - preds = model.predict(image) + with graph.as_default(): + preds = model.predict(image) results = imagenet_utils.decode_predictions(preds) data["predictions"] = [] @@ -80,4 +83,4 @@ def predict(): print(("* Loading Keras model and Flask starting server..." "please wait until server has fully started")) load_model() - app.run() \ No newline at end of file + app.run()