简体   繁体   中英

Keras using tensorflow backend with Flask graph error

I am deploying a Keras InceptionV3 model using Tensorflow back-end using Flask-Python . It is trained on Places2-Challange dataset by MIT. Code works fine on Google Colab but now I am getting this error:

on line: features = model_places.predict( img )

ValueError: Tensor Tensor("dense_2/Softmax:0", shape=(?, 162), dtype=float32) is not an element of this graph
def load_model_places():
    global base_model
    base_model = InceptionV3( weights=None , include_top=False )
    x = base_model.output
    x = GlobalAveragePooling2D()( x )
    x = Dense( 1024 , activation='relu' )( x )
    predictions = Dense( 162 , activation='softmax' )( x )
    global model_places
    model_places = Model( inputs=base_model.input , outputs=predictions )
    model_places.load_weights('metadata/places_weights.hdf5')

def prepare_image(image, target_dim):
    if image.mode != "RGB":
        image = image.convert( "RGB" )
    image = image.resize(target_dim)
    image = img_to_array( image )
    image = np.expand_dims( image , axis=0 )
    image = preprocess_input( image )
    return image

def image_pass_places(input_img):
    img_target_size = (224,224)
    img = prepare_image(input_img, img_target_size)
    features = model_places.predict( img )

Complete Code: https://github.com/nottahagilani/tagpakistan-deploy-ml/blob/master/app.py

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM