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

前端 未结 6 869
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-28 15:30

Im deploying a keras model and sending the test data to the model via a flask api. I have two files:

First: My Flask App:

# Let\'s startup the Flask          


        
6条回答
  •  执笔经年
    2020-12-28 16:31

    It turns out this way does not need a clear_session call and is at the same time configuration friendly, using the graph object from configured session session = tf.Session(config=_config); self.graph = session.graph and the prediction by the created graph as default with self.graph.as_default(): offers a clean approach

    from keras.backend.tensorflow_backend import set_session
    ...
    def __init__(self):
        config = self.keras_resource()
        self.init_model(config)
    
    def init_model(self, _config, *args):
        session = tf.Session(config=_config)
        self.graph = session.graph
        #set configured session 
        set_session(session)
        self.model = load_model(file_path)
    
    def keras_resource(self):
        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True
        return config
    
    def predict_target(self, to_predict):
        with self.graph.as_default():
            predict = self.model.predict(to_predict)
        return predict
    

提交回复
热议问题