Had some issues on this when developing image recognition website
[2021-07-19 02:27:21,880] ERROR in app: Exception on /uploads/comedy3.jpg [GET]
Traceback (most recent call last):
File "C:\Users\Ram Raj\anaconda3\envs\Tutorial\lib\site-packages\flask\app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\Ram Raj\anaconda3\envs\Tutorial\lib\site-packages\flask\app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\Ram Raj\anaconda3\envs\Tutorial\lib\site-packages\flask\app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\Ram Raj\anaconda3\envs\Tutorial\lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\Ram Raj\anaconda3\envs\Tutorial\lib\site-packages\flask\app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\Ram Raj\anaconda3\envs\Tutorial\lib\site-packages\flask\app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\Ram Raj\Desktop\Ai project\website.py", line 83, in uploaded_file
result = myModel.predict(test_image)
File "C:\Users\Ram Raj\anaconda3\envs\Tutorial\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1671, in predict
version_utils.disallow_legacy_graph('Model', 'predict')
File "C:\Users\Ram Raj\anaconda3\envs\Tutorial\lib\site-packages\tensorflow\python\keras\utils\version_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.
Here is the code
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)
I have just showed part where it shows wrong only. Hope able to understand it
@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()
Any idea on where is the fault that i did ? As i couldn't understand much when i searched it at tensorflow website as well. The version that i'm using was 2.5.0 thank you in advance for the help
正如error所说,加载模型图模式并调用model.predict函数
将模型加载到图表中,如下所示:
相关问题 更多 >
编程相关推荐