创建一个容器化的机器学习模型

人工智能 机器学习 架构
数据科学家在创建机器学习模型后,必须将其部署到生产中。要在不同的基础架构上运行它,使用容器并通过 REST API 公开模型是部署机器学习模型的常用方法。本文演示了如何在 Podman 容器中使用 Connexion 推出使用 REST API 的 TensorFlow 机器学习模型。

[[252634]]

数据科学家在创建机器学习模型后,必须将其部署到生产中。要在不同的基础架构上运行它,使用容器并通过 REST API 公开模型是部署机器学习模型的常用方法。本文演示了如何在 Podman 容器中使用 Connexion 推出使用 REST API 的 TensorFlow 机器学习模型。

准备

首先,使用以下命令安装 Podman:

  1. sudo dnf -y install podman

接下来,为容器创建一个新文件夹并切换到该目录。

  1. mkdir deployment_container && cd deployment_container

TensorFlow 模型的 REST API

下一步是为机器学习模型创建 REST API。这个 github 仓库包含一个预训练模型,以及能让 REST API 工作的设置。

使用以下命令在 deployment_container 目录中克隆它:

  1. git clone https://github.com/svenboesiger/titanic_tf_ml_model.git

prediction.py 和 ml_model/

prediction.py 能进行 Tensorflow 预测,而 20x20x20 神经网络的权重位于文件夹 ml_model/ 中。

swagger.yaml

swagger.yaml 使用 Swagger规范 定义 Connexion 库的 API。此文件包含让你的服务器提供输入参数验证、输出响应数据验证、URL 端点定义所需的所有信息。

额外地,Connexion 还将给你提供一个简单但有用的单页 Web 应用,它演示了如何使用 Javascript 调用 API 和更新 DOM。

  1. swagger: "2.0"
  2. info:
  3. description: This is the swagger file that goes with our server code
  4. version: "1.0.0"
  5. title: Tensorflow Podman Article
  6. consumes:
  7. - "application/json"
  8. produces:
  9. - "application/json"
  10.  
  11.  
  12. basePath: "/"
  13.  
  14. paths:
  15. /survival_probability:
  16. post:
  17. operationId: "prediction.post"
  18. tags:
  19. - "Prediction"
  20. summary: "The prediction data structure provided by the server application"
  21. description: "Retrieve the chance of surviving the titanic disaster"
  22. parameters:
  23. - in: body
  24. name: passenger
  25. required: true
  26. schema:
  27. $ref: '#/definitions/PredictionPost'
  28. responses:
  29. '201':
  30. description: 'Survival probability of an individual Titanic passenger'
  31.  
  32. definitions:
  33. PredictionPost:
  34. type: object

server.py 和 requirements.txt

server.py 定义了启动 Connexion 服务器的入口点。

  1. import connexion
  2.  
  3. app = connexion.App(__name__, specification_dir='./')
  4.  
  5. app.add_api('swagger.yaml')
  6.  
  7. if __name__ == '__main__':
  8. app.run(debug=True)

requirements.txt 定义了运行程序所需的 python 包。

  1. connexion
  2. tensorflow
  3. pandas

容器化!

为了让 Podman 构建映像,请在上面的准备步骤中创建的 deployment_container 目录中创建一个名为 Dockerfile 的新文件:

  1. FROM fedora:28
  2.  
  3. # File Author / Maintainer
  4. MAINTAINER Sven Boesiger <donotspam@ujelang.com>
  5.  
  6. # Update the sources
  7. RUN dnf -y update --refresh
  8.  
  9. # Install additional dependencies
  10. RUN dnf -y install libstdc++
  11.  
  12. RUN dnf -y autoremove
  13.  
  14. # Copy the application folder inside the container
  15. ADD /titanic_tf_ml_model /titanic_tf_ml_model
  16.  
  17. # Get pip to download and install requirements:
  18. RUN pip3 install -r /titanic_tf_ml_model/requirements.txt
  19.  
  20. # Expose ports
  21. EXPOSE 5000
  22.  
  23. # Set the default directory where CMD will execute
  24. WORKDIR /titanic_tf_ml_model
  25.  
  26. # Set the default command to execute
  27. # when creating a new container
  28. CMD python3 server.py

接下来,使用以下命令构建容器镜像:

  1. podman build -t ml_deployment .

运行容器

随着容器镜像的构建和准备就绪,你可以使用以下命令在本地运行它:

  1. podman run -p 5000:5000 ml_deployment

在 Web 浏览器中输入 http://0.0.0.0:5000/ui 访问 Swagger/Connexion UI 并测试模型:

当然,你现在也可以在应用中通过 REST API 访问模型。 

责任编辑:庞桂玉 来源: Linux中国
相关推荐

2020-10-14 14:18:33

机器学习机器学习架构人工智能

2017-10-13 15:59:24

iPhone机器学习iOS

2022-06-02 15:42:05

Python机器学习

2020-11-20 10:50:01

Docker容器

2024-04-10 12:39:08

机器学习python

2018-12-29 08:00:00

机器学习TensorFlowKubeflow

2021-11-02 08:00:00

机器学习API技术

2023-12-25 10:53:54

机器学习模型性能

2010-08-05 15:46:13

Flex行为Flex效果

2021-06-23 16:40:58

JavaTomcatWeb

2020-09-28 12:42:17

机器学习语言GitHub

2017-07-25 09:19:02

2021-04-09 10:02:29

机器学习人工智能计算机

2021-08-30 09:25:25

Bert模型PyTorch语言

2022-03-25 11:11:50

机器学习TiDBSQL

2020-02-21 11:23:11

机器学习技术人生第一份工作

2017-09-25 15:30:21

机器学习入门

2021-09-02 08:02:50

深度学习Kubernetes集群管理

2022-06-07 10:25:45

机器学习Shapash

2021-07-07 11:08:21

机器学习数据集PHP
点赞
收藏

51CTO技术栈公众号