Calling `Model.predict` in graph mode is not supported when the `Model` instance was constructed with eager mode enabled

Viewed 10758

So I just followed someone project and make it to here when I got this error:

[2020-10-12 15:33:21,128] ERROR in app: Exception on /predict/ [POST]
Traceback (most recent call last):
  File "c:\users\mr777\anaconda3\envs\gpu\lib\site-packages\flask\app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "c:\users\mr777\anaconda3\envs\gpu\lib\site-packages\flask\app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "c:\users\mr777\anaconda3\envs\gpu\lib\site-packages\flask\app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "c:\users\mr777\anaconda3\envs\gpu\lib\site-packages\flask\_compat.py", line 39, in reraise
    raise value
  File "c:\users\mr777\anaconda3\envs\gpu\lib\site-packages\flask\app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "c:\users\mr777\anaconda3\envs\gpu\lib\site-packages\flask\app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "D:\Ngoding Python\Skripsi\deploy\app.py", line 70, in predict
    out = model.predict(img)
  File "c:\users\mr777\anaconda3\envs\gpu\lib\site-packages\tensorflow\python\keras\engine\training.py", line 130, in _method_wrapper
    return method(self, *args, **kwargs)
  File "c:\users\mr777\anaconda3\envs\gpu\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1562, in predict
    version_utils.disallow_legacy_graph('Model', 'predict')
  File "c:\users\mr777\anaconda3\envs\gpu\lib\site-packages\tensorflow\python\keras\utils\version_utils.py", line 122, in disallow_legacy_graph
    raise ValueError(error_msg)
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.

Here's the code I wrote:

with graph.as_default():
        # perform the prediction
        out = model.predict(img)
        print(out)
        print(class_names[np.argmax(out)])
        # convert the response to a string
        response = class_names[np.argmax(out)]
        return str(response)

any idea with this? because I found the same question here

2 Answers

The answer is simple, just load your model inside the graph just like this:

with graph.as_default():
        json_file = open('models/model.json','r')
        loaded_model_json = json_file.read()
        json_file.close()
        loaded_model = model_from_json(loaded_model_json)
        #load weights into new model
        loaded_model.load_weights("models/model.h5")
        print("Loaded Model from disk")
        #compile and evaluate loaded model
        loaded_model.compile(loss='sparse_categorical_crossentropy',optimizer='adam',metrics=['accuracy'])
        # perform the prediction
        out = loaded_model.predict(img)
        print(out)
        print(class_names[np.argmax(out)])
        # convert the response to a string
        response = class_names[np.argmax(out)]
        return str(response)

@Ilham: Try to wrap the call method in a tf.function, right after defining your network. Something like this:

model = Sequential()
model.call = tf.function(model.call)

I had an issue similar to yours. I solved it just by adding that second line of code. See the following link for more details: https://www.tensorflow.org/guide/intro_to_graphs

Related