[英]Running out of memory when deploying an extremely simple Flask app in Heroku
我想將使用 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]
我不明白為什么會發生這種情況……我的直覺告訴我垃圾收集器在這里應該沒問題。
這是我的要求.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]
我相信問題是pycache越來越大。
請務必使用以下環境變量集運行您的應用程序:
PYTHONDONTWRITEBYTECODE=1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.