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

前端 未结 6 881
佛祖请我去吃肉
佛祖请我去吃肉 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:35

    Ya their is a bug when you predict from model with keras. Keras will not be able to build graph due to some error. Try to predict images from model with the help of tensor flow. Just replace this line of code

    Keras code:

    features = model_places.predict( img )
    

    tensorflow code:

    import tensorflow as tf
    
    graph = tf.get_default_graph()
    

    import this library in your code and replace.

     with graph.as_default():
        features = model_places.predict( img ).tolist()
    

    If Problem still not solved :

    if still problem not solved than try to refresh the graph.

    As your code is fine, running with a clean environment should solve it.

    Clear keras cache at ~/.keras/

    Run on a new environment, with the right packages (can be done easily with anaconda)

    Make sure you are on a fresh session, keras.backend.clear_session() should remove all existing tf graphs.

    Keras Code:

    keras.backend.clear_session()
    features = model_places.predict( img )
    

    TensorFlow Code:

    import tensorflow as tf
    with tf.Session() as sess:
        tf.reset_default_graph()
    

提交回复
热议问题