naman
naman

Reputation: 490

Run multiple Keras models in sequence

I am running 3 keras cnn models in sequence on all images in a folder. After completing prediction of all three models on 1 image, I am getting below error in next iteration of the loop.

  File "/home/ubuntu/.local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1078, in _run
    'Cannot interpret feed_dict key as Tensor: ' + e.args[0])
TypeError: Cannot interpret feed_dict key as Tensor: Tensor Tensor("input_1:0", shape=(?, ?, ?, 3), dtype=float32) is not an element of this graph.

My code structure for one model:

def model_1():

    K.clear_session()

    cnn_model = load_model(model_path, compile=False)


    with K.get_session().as_default() as sess:
        ...Do inference....

I have tried solutions mentioned here and here but none of them worked for me.

Upvotes: 0

Views: 821

Answers (1)

naman
naman

Reputation: 490

Adding tf.Session().as_default() resolved the issue:

def model_1():

    K.clear_session()

    cnn_model = load_model(model_path, compile=False)

    tf.Session().as_default()

    with K.get_session().as_default() as sess:
        ...Do inference....

Adding answer that worked for me in case someone wants to resolve similar issue.

Upvotes: 1

Related Questions