当“Model”实例是在启用急切模式的情况下构造的时,不支持在图形模式下调用“Model.predict”

Ilh*_*ham 5 flask web keras tensorflow

所以我只是跟随某人的项目并在收到此错误时到达这里:

[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.
Run Code Online (Sandbox Code Playgroud)

这是我写的代码:

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)
Run Code Online (Sandbox Code Playgroud)

有什么想法吗?因为我在这里发现了同样的问题

Ilh*_*ham 5

答案很简单,只需将模型加载到图表中,如下所示:

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)
Run Code Online (Sandbox Code Playgroud)