首页 > 解决方案 > 为什么我的带有 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 会话的帖子。这是问题吗?

感谢您的任何帮助。

标签: python-3.xtensorflowflaskkeras

解决方案


根据我的评论,我已经稍微更改了您的代码,它现在应该可以工作了。

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)```

推荐阅读