Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run Keras.model() for prediction inside a tensorflow session?

I am currently having an issue, while executing my model predict of keras inside a tensorflow session.

with tf.Session(graph=graph) as sess:
    sess.run(tf.global_variables_initializer())
    ## want to know how to add model.predict() inside this condition
    predictions = model.predict(#my_model)
    #predictions output is same not appending

or any alternative method will be helpful.

Any help would be appreciated.

like image 954
Balaji Avatar asked May 10 '18 09:05

Balaji


2 Answers

from keras import backend as K

with tf.Graph().as_default():

    with tf.Session() as sess:

        K.set_session(sess)
        model = load_model(model_path)
        preds = model.predict(in_data)
like image 163
TaeWoo Avatar answered Sep 28 '22 02:09

TaeWoo


from keras.models import load_model
with tf.Session(graph=K.get_session().graph) as session:
    session.run(tf.global_variables_initializer())
    model = load_model('model.h5')
    predictions = model.predict(input)

Above code works for me. I am using keras mobilenet inside tensorflow.

like image 30
Isam Abdullah Avatar answered Sep 28 '22 03:09

Isam Abdullah