当前位置: 首页 > 面试题库 >

将巨大的Keras模型加载到Flask应用中

谯乐池
2023-03-14
问题内容

我正在构建一个小的Flask应用程序,该应用程序在后台使用卷积神经网络对用户上传的图像进行预测。如果我这样加载它,它将起作用:

@app.route("/uploader", methods=["GET","POST"])
def get_image():
    if request.method == 'POST':
        f = request.files['file']
        sfname = 'static/'+str(secure_filename(f.filename))
        f.save(sfname)
        clf = catdog.classifier()
        return render_template('result.html', pred = clf.predict(sfname), imgpath = sfname)

但是,这要求在用户添加图像之后加载分类器(clf)。这需要一段时间,因为它需要根据一个pickle文件为200层以上的神经网络设置所有权重。

我想要做的是在生成应用程序时加载所有权重。为此,我尝试了此操作(为HTML模板/导入/应用启动切出了不相关的代码):

# put model into memory on spawn
clf = catdog.classifier()
# Initialize the app
app = flask.Flask(__name__)

@app.route("/uploader", methods=["GET","POST"])
def get_image():
    if request.method == 'POST':
        f = request.files['file']
        sfname = 'static/'+str(secure_filename(f.filename))
        f.save(sfname)
        return render_template('result.html', pred = clf.predict(sfname), imgpath = sfname)

当我这样做时,我得到了这个回溯(跳过所有烧瓶特定的跟踪在顶部):

 File "/Users/zachariahmiller/Documents/Metis/test_area/flask_catdog/flask_backend.py", line 26, in get_image
    return render_template('result.html', pred = clf.predict(sfname), imgpath = sfname)
  File "/Users/zachariahmiller/Documents/Metis/test_area/flask_catdog/catdog.py", line 56, in predict
    prediction = self.model.predict(img_to_predict, batch_size=1, verbose=1)
  File "/Users/zachariahmiller/anaconda/lib/python2.7/site-packages/keras/engine/training.py", line 1569, in predict
    self._make_predict_function()
  File "/Users/zachariahmiller/anaconda/lib/python2.7/site-packages/keras/engine/training.py", line 1037, in _make_predict_function
    **kwargs)
  File "/Users/zachariahmiller/anaconda/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 2095, in function
    return Function(inputs, outputs, updates=updates)
  File "/Users/zachariahmiller/anaconda/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 2049, in __init__
    with tf.control_dependencies(self.outputs):
  File "/Users/zachariahmiller/anaconda/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 3583, in control_dependencies
    return get_default_graph().control_dependencies(control_inputs)
  File "/Users/zachariahmiller/anaconda/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 3314, in control_dependencies
    c = self.as_graph_element(c)
  File "/Users/zachariahmiller/anaconda/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2405, in as_graph_element
    return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
  File "/Users/zachariahmiller/anaconda/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2484, in _as_graph_element_locked
    raise ValueError("Tensor %s is not an element of this graph." % obj)
ValueError: Tensor Tensor("dense_2/Softmax:0", shape=(?, 2), dtype=float32) is not an element of this graph.

我不确定为什么将特定调用之外的分类器作为全局对象加载到应用程序会导致失败。它应该在内存中,并且我已经看到其他人使用SKLearn分类器执行此操作的示例。关于为什么会导致此错误的任何想法?


问题答案:

我正在将我的python服务器作为threaded = True运行。删除它,让我的工作

app.run(host='0.0.0.0', port=5000, threaded=True)

---->

app.run(host='0.0.0.0', port=5000)

调试似乎对我没有任何影响



 类似资料:
  • 用例:我试图在Google App Engine中加载一个预先训练好的Keras模型为.h5文件。我正在Python Runtime3.7和标准环境中运行App Engine。 (2)也许有一种方法可以将model.h5从Google Storage加载到Google App Engine中,这是我没有想到的,例如使用其他函数()或其他格式? 我只是想读一下模型,以便进行预测。不需要编写或训练模型

  • 问题内容: 在Keras中,如果您需要自定义损失以及其他参数,我们可以像https://datascience.stackexchange.com/questions/25029/custom- loss-function-with-additional-parameter-in- 凯拉斯 当我训练模型时,上述方法有效。但是,一旦训练了模型,我将很难加载模型。当我尝试在load_model中使用c

  • 我是一个使用java的Android应用程序开发人员,我开始了解Flutter使用谷歌创建的飞镖语言。我发现它非常有趣,因为它为Android和iOS构建了应用程序。我已经为Android创建了一个简单的TabLayout应用程序,应用程序的大小为27MB,请看看简单的TabLayout Flutter App的屏幕截图。 > 我已经看到Flitter应用程序的大小超过了25MB,那么为什么Fli

  • 问题内容: 我正在用Django开发应用程序。 我想将数据加载到模型中,即,但数据存储在xlsx文件中,即。 为了实现这一目标,我开发了以下脚本: 但是当我从Anaconda提示符下运行它时,我得到了 文件“ load_glossary.py”,模块7中的第7行, 引发AppRegistryNotReady(“应用尚未加载。”)django.core.exceptions.AppRegistryN

  • 我想从InstaCart https://www.InstaCart.com/datasets/grocery-shopping-2017加载大型.csv(3.4百万行,20.6万用户)开源数据集 基本上,我在将orders.csv加载到Pandas数据帧中时遇到了麻烦。我想学习将大文件加载到Pandas/Python中的最佳实践。