python-3.x - 为什么我的带有 keras 和 tensorflow 后端的小 Flask 应用程序在后续图片上传时崩溃?
问题描述
对于一项任务,我们使用 Keras 和 TF 进行图像分类,图像对狗和猫进行分类。我编写了一个简单的 Flask 应用程序,它允许演示上传图片并显示上传的图片得分是狗还是猫。
我可以运行一次上传,并且显示效果很好,但是如果我上传另一张图片,我会得到:
TypeError: Cannot interpret feed_dict key as Tensor: Tensor Tensor("Placeholder:0", shape=(3, 3, 3, 32), dtype=float32) is not an element of this graph.
与行:
model = load_model('/Volumes/T5_500G/Capstone/v2/flask/models/model_weights.h5')
在烧瓶调试页面中突出显示
我是新手,所以我对如何解决这个问题感到困惑。这是我的代码:
@app.route('/upload_image', methods=['GET', 'POST'])
def upload_image():
prediction_results = {}
animals = ['% Cat Score', '% Dog Score']
animals_scores = []
imageUrl = ""
if request.method == 'POST':
if request.files:
import keras
from keras.models import load_model
from keras import backend as K
import numpy as np
model = load_model('/Volumes/T5_500G/Capstone/v2/flask/models/model_weights.h5')
print(request.files)
print(request.files['image'].filename)
imageUrl = "/static/uploads/"+request.files['image'].filename
image2 = app.config['IMAGE_UPLOADS']+"/"+request.files['image'].filename
request.files['image'].save(image2)
img_path = image2
img = keras.preprocessing.image.load_img(img_path, target_size=(224,224))
img_array = keras.preprocessing.image.img_to_array(img)
expanded_img_array = np.expand_dims(img_array, axis=0)
preprocessed_img = expanded_img_array / 255. # Preprocess the image
prediction = model.predict(preprocessed_img)
pred_list = prediction.tolist()
animals_scores.append(pred_list[0][0])
animals_scores.append(pred_list[0][1])
print(prediction_results)
imageUrl = "/static/uploads/"+request.files['image'].filename
return render_template('/upload_image.html',imageUrl=imageUrl, animals=animals, animals_scores=animals_scores)
上传路径是我笔记本电脑上的路径。我希望解决这个问题,推动heroku或相关。
我读过一些关于清除 keras 会话的帖子。这是问题吗?
感谢您的任何帮助。
解决方案
根据我的评论,我已经稍微更改了您的代码,它现在应该可以工作了。
import keras
from keras.models import load_model
from keras import backend as K
import numpy as np
model = load_model('/Volumes/T5_500G/Capstone/v2/flask/models/model_weights.h5')
@app.route('/upload_image', methods=['GET', 'POST'])
def upload_image():
prediction_results = {}
animals = ['% Cat Score', '% Dog Score']
animals_scores = []
imageUrl = ""
if request.method == 'POST':
if request.files:
print(request.files)
print(request.files['image'].filename)
imageUrl = "/static/uploads/"+request.files['image'].filename
image2 = app.config['IMAGE_UPLOADS']+"/"+request.files['image'].filename
request.files['image'].save(image2)
img_path = image2
img = keras.preprocessing.image.load_img(img_path, target_size=(224,224))
img_array = keras.preprocessing.image.img_to_array(img)
expanded_img_array = np.expand_dims(img_array, axis=0)
preprocessed_img = expanded_img_array / 255. # Preprocess the image
prediction = model.predict(preprocessed_img)
pred_list = prediction.tolist()
animals_scores.append(pred_list[0][0])
animals_scores.append(pred_list[0][1])
print(prediction_results)
imageUrl = "/static/uploads/"+request.files['image'].filename
return render_template('/upload_image.html',imageUrl=imageUrl, animals=animals, animals_scores=animals_scores)```
推荐阅读
- excel - 我的 VBA 代码中是否存在导致溢出错误的内容?
- netlogo - 如何在模拟过程中将代理从一个品种切换到另一个品种?
- python - 不和谐的作品问题
- python-2.7 - pyqtgraph中类似于matplotlib的`Gridspec`和`GridSpecFromSubplotSpec`的模块
- winapi - 对于 RICHEDIT 控件,什么是字符类,什么是有效值?
- excel - 如果条件为真,则过滤该值并在其旁边放置“是”,否则为“否”
- jquery - 触发从 url 获取值的更改事件
- c++ - C ++:按位移位为零值
- r - ggplot() 手动为每个点着色
- java - 有没有办法可以在执行时覆盖 JRE 中的文件?也就是说,无需编辑 JRE?