![](/img/trans.png)
[英]How can i solve Application Error caused after deploying my model on Heroku using Flask?
[英]How can I deploy my Tensorflow Deep Learning model as a web application using Flask, GitHub and Heroku
我已经训练了一个卷积神经网络,将模型保存到一个文件 (model.h5) 中,现在我想将它部署到一个网站上。 应该可以将文件上传到网站以供模型分析。 部署我的模型的最简单方法是什么?
我发现部署深度学习模型的最简单方法是制作一个简单的 Flask 应用程序并使用 Heroku 将其部署到网站。
首先,让我们看看您需要哪些文件以及它们应该如何组织:
project
│ runtime.txt
│ requirements.txt
│ Procfile
│ main.py
│
└───assets
│ model.h5
│
└───templates
│ base.html
│ index.html
│
└───uploaded_files
from flask import Flask, url_for, request, render_template, jsonify, flash
import tensorflow as tf
from tensorflow import keras
from werkzeug.utils import secure_filename
import os
import numpy as np
app = Flask(__name__)
# Load your model here
@app.route("/")
def index():
return render_template("index.html")
@app.route('/', methods=['POST'])
def submit_file():
if request.method == 'POST':
f = request.files['file']
f.save('uploaded_files/'+ secure_filename(f.filename))
# do prediction here using the uploaded file at:
# 'uploaded_files/'+ secure_filename(f.filename)
return render_template('index.html', prediction_text='The predictions are: {}'.format(result_string))
if __name__ == '__main__':
app.run()
我们将使用 base.html 作为 HTML 页面的模板。 如果我们以后想制作更多页面,这会更容易。
基本文件<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> <title>{% block title %}{% endblock %}</title> </head> <body> <div class="container-fluid"> {% block content %} {% endblock %} </div> <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script> </body> </html>
索引.html
{% extends "base.html" %} {% block title %}ECG app{% endblock %} {% block content %} <div class="jumbotron" lang="en"> <div class="container"> <div class="text-center"> <h1 class="display-4">Your header</h1> <p class="lead">something more</p> </div> </div> </div> <form method="post" action="/" enctype="multipart/form-data" lang="en"> <div class="container"> <div class="text-center"> <div class="file-field"> <div class="btn btn-pink btn-rounded btn-sm float-left"> <input type="file" name="file" autocomplete="off" required placeholder="Upload your file"> <input type="submit" class="btn btn-primary" value="Submit"> </div> </div> </div> </div> </form> <br> <br> <br> <div class="container"> <div class="p-3 mb-2 bg-dark text-white">{{ prediction_text }}</div> </div> {% endblock %}
此时,您应该具有以下文件和文件夹结构
pip freeze > requirements. txt
这应该足以在您的计算机上本地运行您的 Flask 应用程序。 就我个人而言,我使用 Anaconda 来运行 Python 脚本并控制我的环境,我建议您也这样做。 此时,您应该创建一个新的 python 环境并安装必要的包以在此环境中运行 Flask 应用程序。
现在,在本地运行的 Flask 应用程序和新的 Python 环境中,您需要创建一个需求文件。 这可以从运行的命令行轻松完成
pip freeze > requirements. txt
在命令行中(确保您位于正确的文件夹中并激活了正确的环境)。
确保在运行 pip freeze 之前安装最新版本的 gunicorn将应用程序部署到 Heroku 时需要使用 gunicorn。
为了能够在 Heroku 中运行 Flask 应用程序,您需要一个 Procfile。 在我们的例子中,这是一个非常简单的文件,包含这行简单的代码
web: gunicorn main:app
记住:
main.py
。 以及 Flask 应用程序的名称,即app = Flask(__name__)
如果您的 Flask 应用程序需要特定的 Python 版本,您可以在 runtime.txt 文件中指定它。 在我们的例子中,我们在 runtime.txt 中编写了这行代码:
python-3.7.2
此时,您应该拥有将 Flask 应用程序部署到 Heroku 所需的所有文件。
现在,如果您还没有这样做,请为您的应用程序创建一个 Github 存储库并将您的代码提交给它。
转到 heroku.com 并注册一个帐户。 注册后转到仪表板并单击“新建”以创建新应用程序。
选择应用程序的名称后,您将有机会将 GitHub 存储库链接到部署管道。 如果您选择连接到 GitHub 存储库,Heroku 将自动构建到所选存储库的 master 分支的最新提交。
现在继续将 Heroku 与我们新的 Github 存储库连接起来。
对你的 master 分支进行一次新的提交,Heroku 应该构建你的 Flask 应用程序并部署它!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.