在开发图像识别网站时遇到了一些问题
[2021-07-19 02:27:21,880] ERROR in app: Exception on /uploads/comedy3.jpg [GET]
Traceback (most recent call last):
File "C:UsersRam Rajanaconda3envsTutoriallibsite-packagesflaskapp.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "C:UsersRam Rajanaconda3envsTutoriallibsite-packagesflaskapp.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:UsersRam Rajanaconda3envsTutoriallibsite-packagesflaskapp.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:UsersRam Rajanaconda3envsTutoriallibsite-packagesflask_compat.py", line 39, in reraise
raise value
File "C:UsersRam Rajanaconda3envsTutoriallibsite-packagesflaskapp.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "C:UsersRam Rajanaconda3envsTutoriallibsite-packagesflaskapp.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:UsersRam RajDesktopAi projectwebsite.py", line 83, in uploaded_file
result = myModel.predict(test_image)
File "C:UsersRam Rajanaconda3envsTutoriallibsite-packagestensorflowpythonkerasenginetraining.py", line 1671, in predict
version_utils.disallow_legacy_graph('Model', 'predict')
File "C:UsersRam Rajanaconda3envsTutoriallibsite-packagestensorflowpythonkerasutilsversion_utils.py", line 130, 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.
这是代码
from flask import render_template
from flask import Flask, flash, request, redirect, url_for
from werkzeug.utils import secure_filename
import numpy as np
from tensorflow.keras.preprocessing import image
from tensorflow.keras.models import load_model
from keras.backend import set_session
import tensorflow as tf
app = Flask(__name__)
def load_model_from_file():
mySession = tf.compat.v1.Session()
set_session(mySession)
myModel = load_model('saved_model.h5')
myGraph = tf.compat.v1.get_default_graph()
return (mySession, myModel, myGraph)
我刚刚展示了它只显示错误的部分。希望能够理解
@app.route('/uploads/<filename>')
def uploaded_file(filename):
test_image = image.load_img(UPLOAD_FOLDER+"/"+filename, target_size =(150,150))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis=0)
mySession = app.config['SESSION']
myModel = app.config['MODEL']
myGraph = app.config['GRAPH']
with myGraph.as_default():
set_session(mySession)
result = myModel.predict(test_image)
image_src = "/"+UPLOAD_FOLDER +"/"+filename
if result[0] < 0.5 :
answer = "<div class='col text-center'><img width='150' height='150' src='"+image_src+"' class='img-thumbnail' /><h4>guess:"+x+" "+str(result[0])+"</h4></div><div class='col'></div><div class='w-100'></div>"
else:
answer = "<div class='col'></div><div class='col text-center'><img width='150' height='150' src='"+image_src+"' class='img-thumbnail' /><h4>guess:"+y+" "+str(result[0])+"</h4></div><div class='w-100'></div>"
results.append(answer)
return render_template('index.html',myX=x,myY=y,mySampleX=samplex,mySampleY=sampley,len=len(results),results=results)
def main():
(mySession, myModel, myGraph) = load_model_from_file()
app.config['SECRET_KEY'] = 'super secret key'
app.config['SESSION'] = mySession
app.config['MODEL'] = myModel
app.config['GRAPH'] = myGraph
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
app.run()
results = []
main()
知道我的错在哪里吗?当我在tensorflow网站上搜索时,我也不太明白。我使用的版本是2.5.0提前感谢的帮助
ValueError:不支持在图形模式下调用
Model.predict
当CCD_ 2实例是在启用渴望模式的情况下构造的。启用渴望模式的Please construct your Model instance in graph mode or call Model.predict
。
错误显示加载模型图模式并调用model.prpredict函数。
在图中加载您的模型,如下所示:
with graph.as_default():
#Load your model
#call model.predict
Model.predict()
return