Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Tensorflow 当“Model”实例是在启用了渴望模式的情况下构造的时,不支持在图形模式下调用“Model.predict”_Tensorflow_Flask_Web_Keras - Fatal编程技术网

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

Tensorflow 当“Model”实例是在启用了渴望模式的情况下构造的时,不支持在图形模式下调用“Model.predict”,tensorflow,flask,web,keras,Tensorflow,Flask,Web,Keras,所以我只是跟随某人的项目,当我遇到这个错误时,我就来到了这里: [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 r

所以我只是跟随某人的项目,当我遇到这个错误时,我就来到了这里:

[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.
以下是我编写的代码:

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)

你知道这个吗?因为我发现同样的问题

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

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)
model = Sequential()
model.call = tf.function(model.call)

@Ilham:在定义网络之后,尝试将调用方法包装在
tf.function
中。大概是这样的:

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)
model = Sequential()
model.call = tf.function(model.call)
我有一个和你类似的问题。我通过添加第二行代码就解决了这个问题。
有关详细信息,请参阅以下链接:

您是否解决了此问题?