Tensor is not an element of this graph; deploying Keras model Tensor is not an element of this graph; deploying Keras model flask flask

Tensor is not an element of this graph; deploying Keras model


Flask uses multiple threads. The problem you are running into is because the tensorflow model is not loaded and used in the same thread. One workaround is to force tensorflow to use the gloabl default graph .

Add this after you load your model

global graphgraph = tf.get_default_graph() 

And inside your predict

with graph.as_default():    y_hat = keras_model_loaded.predict(predict_request, batch_size=1, verbose=1)


It's so much simpler to wrap your keras model in a class and that class can keep track of it's own graph and session. This prevents the problems that having multiple threads/processes/models can cause which is almost certainly the cause of your issue. While other solutions will work this is by far the most general, scalable and catch all. Use this one:

import osfrom keras.models import model_from_jsonfrom keras import backend as Kimport tensorflow as tfimport logginglogger = logging.getLogger('root')class NeuralNetwork:    def __init__(self):        self.session = tf.Session()        self.graph = tf.get_default_graph()        # the folder in which the model and weights are stored        self.model_folder = os.path.join(os.path.abspath("src"), "static")        self.model = None        # for some reason in a flask app the graph/session needs to be used in the init else it hangs on other threads        with self.graph.as_default():            with self.session.as_default():                logging.info("neural network initialised")    def load(self, file_name=None):        """        :param file_name: [model_file_name, weights_file_name]        :return:        """        with self.graph.as_default():            with self.session.as_default():                try:                    model_name = file_name[0]                    weights_name = file_name[1]                    if model_name is not None:                        # load the model                        json_file_path = os.path.join(self.model_folder, model_name)                        json_file = open(json_file_path, 'r')                        loaded_model_json = json_file.read()                        json_file.close()                        self.model = model_from_json(loaded_model_json)                    if weights_name is not None:                        # load the weights                        weights_path = os.path.join(self.model_folder, weights_name)                        self.model.load_weights(weights_path)                    logging.info("Neural Network loaded: ")                    logging.info('\t' + "Neural Network model: " + model_name)                    logging.info('\t' + "Neural Network weights: " + weights_name)                    return True                except Exception as e:                    logging.exception(e)                    return False    def predict(self, x):        with self.graph.as_default():            with self.session.as_default():                y = self.model.predict(x)        return y


Just after loading the model add model._make_predict_function()`

# Model reload from jSON:print('Load model...')json_file = open('models/model_temp.json', 'r')loaded_model_json = json_file.read()json_file.close()keras_model_loaded = model_from_json(loaded_model_json)print('Model loaded...')# Weights reloaded from .h5 inside the modelprint('Load weights...')keras_model_loaded.load_weights("models/Model_temp.h5")print('Weights loaded...')keras_model_loaded._make_predict_function()