【问题标题】:Running out of memory when deploying an extremely simple Flask app in Heroku在 Heroku 中部署一个极其简单的 Flask 应用程序时内存不足
【发布时间】:2021-07-20 17:37:04
【问题描述】:

我想将使用 Fast AI 制作的简单机器学习模型 (resnet34) 部署到 Heroku。

我的整个烧瓶应用程序是一个文件:

from flask import Flask
from fastai.vision.all import *

app = Flask(__name__)

learn = load_learner("./export.pkl")

@app.route("/<path:image_url>")
def hello_world(image_url):
    print(image_url)
    response = requests.get(image_url)
    img = PILImage.create(response.content)
    predictions = learn.predict(img)
    print(predictions)
    return predictions[0]

它运行良好几次,但heroku然后开始记录以下内容:

我不明白为什么会这样……我的直觉告诉我垃圾收集器在这里应该没问题。

这是我的要求.txt

-f https://download.pytorch.org/whl/torch_stable.html

torch==1.8.1+cpu
torchvision==0.9.1+cpu
fastai>=2.3.1
Flask==2.0.1
gunicorn==20.1.0

Pillow

requests==2.26.0

编辑:我自己发布的答案并不完全正确。根本原因是我没有关闭图像: 正确的代码:

@app.route("/<path:image_url>")
def hello_world(image_url):
    print(image_url)
    response = requests.get(image_url)
    img = PILImage.create(response.content)
    predictions = learn.predict(img)
    img.close()
    return predictions[0]

【问题讨论】:

  • 你能编辑你的问题并添加 requirements.txt
  • 嗨@charchit 我已经添加了它们:)
  • 抱歉,我不明白为什么您的 slug 尺寸太大。即使在使用 cpu 版本之后。也许是 fastai 使尺寸变大了。也许这对course.fast.ai/deployment_heroku
  • 嗨@charchit,我的蛞蝓尺寸不是太大。我能够部署该应用程序,并且它适用于几个预测。发生的情况是,似乎在进行 X 次预测后内存没有被收集,所以 heroku 开始记录我上面显示的日志
  • 我不懂机器学习。但我认为您将数据存储在 pkl 文件中。在 X 次预测后哪个变得太大?我说的对吗?

标签: python machine-learning flask heroku fast-ai


【解决方案1】:

我认为问题在于 pycache 越来越大。

确保使用以下环境变量集运行您的应用:

PYTHONDONTWRITEBYTECODE=1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-22
    • 2014-09-02
    相关资源
    最近更新 更多