使用Flask进行机器学习模型部署

In this article, we are going to discuss majorly machine learning model deployment using flask API but the code for model building and deployment using flask API will be available in my GitHub repository so that you can also try this in your own project.

在本文中,我们将主要讨论使用flask API进行机器学习模型的部署,但是使用flask API进行模型构建和部署的代码将在我的GitHub中提供。 存储库,以便您也可以在自己的项目中尝试此操作。

I assume that you have some experience in machine learning or deep learning model building and wish to productionize model with flask API.

我假设您在机器学习或深度学习模型构建方面有一定经验,并希望使用Flask API 生产模型

Flask API简介 (Introduction to Flask API)

Flask is a lightweight Web Server Gateway Interface(WSGI) a micro-framework written in python. This means flask provides us with tools, libraries and technologies that allow us to build a web application. This web application can be some web pages, a blog, or our machine learning model prediction web application. Flask is an intermediate medium to connect our model with front end web page for prediction as shown in below image.

Flask是一个轻量级的Web服务器网关接口(WSGI),它是用python编写的微框架。 这意味着flask为我们提供了工具,库和技术,使我们能够构建Web应用程序。 该Web应用程序可以是某些网页,博客或我们的机器学习模型预测Web应用程序。 Flask是将我们的模型与前端网页连接以进行预测的中间媒介, 如下图所示。

使用Flask进行机器学习模型部署_第1张图片
API Data Flow API数据流

先决条件 (Prerequisites)

We assume that all of us have knowledge about model training in jupyter notebook. This post is aimed to only provide insights on deploying a machine learning model into production using Flask API.

我们假设我们所有人都有关于Jupyter Notebook模型训练的知识。 这篇文章旨在仅提供有关使用Flask API将机器学习模型部署到生产中的见解。

Libraries that require in model Deployment:

模型部署中需要的库:

pip install pickle-mixin
pip install Flask
  1. pickle: A native python library to save (serialize) and load (de-serialize) python objects as files on the disk.

    pickle :一个本地python库,用于将python对象保存(序列化)和加载(反序列化)为文件在磁盘上。

  2. Flask: A python-based easy to use micro web framework.

    Flask :一个基于python的易于使用的微型Web框架。

项目结构 (Project Structure)

This project has four major parts :

该项目包括四个主要部分:

  1. iris.py — This contains code for our Machine Learning model to predict Iris Plant(classification) on training data in ‘iris.data’ file.

    iris.py —包含我们的机器学习模型的代码,以预测'iris.data'文件中的训练数据上的虹膜植物(分类)。
  2. app.py — This contains Flask APIs that receives Iris data input through GUI or API calls, computes the precited value based on our model and returns it.

    app.py-包含Flask API,它们通过GUI或API调用接收Iris数据输入,并根据我们的模型计算推定值并返回。
  3. iris.pkl — This contains a pre-train model which is obtain after training and will use in app.py for prediction.

    iris.pkl —包含训练前获得的训练前模型,该模型将在app.py中用于预测。
  4. templates — This folder contains the HTML template to allow the user to enter input features and displays the predicted output in a webpage.

    模板-此文件夹包含HTML模板,以允许用户输入输入功能并在网页中显示预测的输出。

数据集 (Dataset)

使用Flask进行机器学习模型部署_第2张图片
Pandas DataFrame 熊猫数据框

模型训练 (Model Training)

First, we read data through CSV file and we have four input features and three possible output variable so we labelEncode them for training. After that, we use SVC(support vector classification) for model training and save the model(iri.pkl) using pickle. It may be different for your problem we took simple example for understanding.

首先,我们通过CSV文件读取数据,我们有四个输入功能和三个可能的输出变量,因此我们对它们进行编码以进行训练。 之后,我们使用SVC(支持向量分类)进行模型训练,并使用pickle保存模型(iri.pkl)。 对于您的问题,可能会有所不同,我们以简单的示例进行了理解。

FLask API (FLask API)

  • Here, we initialize our flask app by calling Flask(__name__) and load the model with pickle.

    在这里,我们通过调用Flask(__ name__)来初始化flask应用,并用pickle加载模型。
  • “@app.route(‘/’)” here we initialize the root directory of our flask app and in it we definite function that will be call itself at this root directory.

    “ @ app.route('/')”在这里初始化烧瓶应用程序的根目录,并在其中定义函数,该函数将在此根目录中自行调用。
  • “@app.route(‘/predict’, methods=[‘POST’])” here we define home function where we get data from a frontend HTML page using request.form[‘ ’] and predict output, then send it back to an HTML page for prediction visualization.

    “ @ app.route('/ predict',Methods = ['POST']))”在这里我们定义了home函数,我们使用request.form ['']从前端HTML页面获取数据并预测输出,然后将其发送回去到HTML页面进行预测可视化。

By default, the Flask route responds to the GET requests. However, this preference can be altered by providing methods argument to route() decorator.

默认情况下,Flask路由会响应GET请求。 但是,可以通过为route()装饰器提供方法参数来更改此首选项。

In order to demonstrate the use of POST method in URL routing, first let us create an HTML form and use the POST method to send form data to a URL.

为了演示POST方法在URL路由中的使用,首先让我们创建一个HTML表单,然后使用POST方法将表单数据发送到URL。

The following script starts the flask server on localhost and default port (5000) making the URL: http://127.0.0.1:5000/

以下脚本在本地主机和默认端口(5000)上启动Flask服务器,并创建URL:http : //127.0.0.1 :5000 /

Just paste http://127.0.0.1:5000/ on browser and press enter to see the server working.

只是粘贴 http://127.0.0.1:5000/ 在浏览器上,然后按Enter键以查看服务器的运行情况。

HTML模板 (Html Template)

templates folder
|
+-- home.html
|
+-- after.html
  1. home.html is the default root page that opens when we open a link in a browser. It contains form through which we provide input feature(total four) in text format and on clicking on submit button it calls home function for prediction in app.py.

    home.html是我们在浏览器中打开链接时打开的默认根目录。 它包含表单,通过该表单我们以文本格式提供输入功能(总共四个),然后单击“提交”按钮,它将调用home函数以在app.py中进行预测。

2. after.html is the page calls by home function in ‘/predict’ directory and it displays the prediction of the model.

2. after.html是“ / predict”目录中home函数的页面调用,它显示模型的预测。

在本地系统上托管 (Hosting on a local system)

使用Flask进行机器学习模型部署_第3张图片
Flask API 烧瓶API

Model building and Deployment Flow Chart

模型构建和部署流程图

使用Flask进行机器学习模型部署_第4张图片

结论 (Conclusion)

Model Deployment is a critical part of any machine learning pipeline.

模型部署是任何机器学习管道的关键部分。

Flask is a simple web application framework that can be easily built. Hosting and sharing machine learning models can be really easy with Flask API.

Flask是一个易于构建的简单Web应用程序框架。 使用Flask API,托管和共享机器学习模型非常容易。

For Complete Code:- Github

对于完整的代码: -Github

If you want to learn more about Model Deployment, I would like to call out this excellent playlist on model deployment using flask and docker on youtube by Krish Naik. This was the one that helped me a lot. Do check it out.

如果您想了解有关模型部署的更多信息,我想在Krish Naik的 YouTube上使用flask和docker进行模型部署方面的出色播放列表 。 这对我很有帮助。 请检查一下。

Thanks for the read. I wish to write more beginner-friendly posts in the future too, by the way, this is my first post on Medium.

感谢您的阅读。 我希望将来也能写更多对初学者友好的文章,顺便说一句,这是我关于Medium的第一篇文章。

Follow me up at Medium. As always, I welcome feedback and constructive criticism and can be reached on Linkedin.

Medium跟我来。 与往常一样,我欢迎您提供反馈和建设性的批评,可以通过Linkedin与我们联系

Gain Access to Expert View — Subscribe to DDI Intel

获得访问专家视图的权限- 订阅DDI Intel

翻译自: https://medium.com/datadriveninvestor/deployment-of-machine-learning-project-using-flask-bf6e5a750319

你可能感兴趣的:(机器学习,python,人工智能,大数据,java)