Eager vs graph mode with Keras and TF2.0

1.9k views Asked by At

I'm compiling a previously trained model in the graph mode:

inference_model = model_from_json(json.dumps(inference_model_dict))
inference_model.set_weights(original_model.get_weights())
inference_model.compile('adam', 'binary_crossentropy', run_eagerly=False)

When using it for prediction:

predictions = inference_model.predict(gen)

getting the following error:

ValueError: Calling Model.predict in graph mode is not supported when the Model instance was constructed with eager mode enabled. Please construct your Model instance in graph mode or call Model.predict with eager mode enabled.

I was trying to disable the TF2.0 default by

tf.compat.v1.disable_eager_execution()
  • this didn't help neither.

Reading thru the Keras documentation, don't find how to follow this recommendation: "call Model.predict with eager mode enabled"

Originally, this model was trained in the eager mode. Does this matter? Can't this be overwritten somehow?

3

There are 3 answers

1
Yuri Miroshnik On

Solved the problem by re-arranging the session related code.

0
Zijian Chen On

I have emploied the tf.compat.v1.disable_eager_execution() which solved my problem

0
Yuri Miroshnik On

I believe I had some issue with the way the sessions were defined (inherited from the TF1.15 code). Once simplified this, the problem disappeared.