创建一个容器化的机器学习模型
创始人
2024-03-02 01:24:36
0

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

准备

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

sudo dnf -y install podman

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

mkdir deployment_container && cd deployment_container

TensorFlow 模型的 REST API

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

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

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。

swagger: "2.0"
info:
  description: This is the swagger file that goes with our server code
  version: "1.0.0"
  title: Tensorflow Podman Article
consumes:
  - "application/json"
produces:
  - "application/json"


basePath: "/"

paths:
  /survival_probability:
    post:
      operationId: "prediction.post"
      tags:
        - "Prediction"
      summary: "The prediction data structure provided by the server application"
      description: "Retrieve the chance of surviving the titanic disaster"
      parameters:
        - in: body
          name: passenger
          required: true
          schema:
            $ref: '#/definitions/PredictionPost'
      responses:
        '201':
          description: 'Survival probability of an individual Titanic passenger'

definitions:
  PredictionPost:
    type: object

server.py 和 requirements.txt

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

import connexion

app = connexion.App(__name__, specification_dir='./')

app.add_api('swagger.yaml')

if __name__ == '__main__':
 app.run(debug=True)

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

connexion
tensorflow
pandas

容器化!

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

FROM fedora:28

# File Author / Maintainer
MAINTAINER Sven Boesiger 

# Update the sources
RUN dnf -y update --refresh

# Install additional dependencies
RUN dnf -y install libstdc++

RUN dnf -y autoremove

# Copy the application folder inside the container
ADD /titanic_tf_ml_model /titanic_tf_ml_model

# Get pip to download and install requirements:
RUN pip3 install -r /titanic_tf_ml_model/requirements.txt

# Expose ports
EXPOSE 5000

# Set the default directory where CMD will execute
WORKDIR /titanic_tf_ml_model

# Set the default command to execute
# when creating a new container
CMD python3 server.py

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

podman build -t ml_deployment .

运行容器

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

podman run -p 5000:5000 ml_deployment

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

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


via: https://fedoramagazine.org/create-containerized-machine-learning-model/

作者:Sven Bösiger 选题:lujun9972 译者:geekpi 校对:wxy

本文由 LCTT 原创编译,Linux中国 荣誉推出

相关内容

想成为AI人才,报考什么专...
人工智能专业 涵盖机器学习、深度学习、计算机视觉、自然语言处理、强...
2025-05-02 11:18:06
智能问答的AI知识库与大模...
内容概要 在当今信息爆炸的时代,智能问答技术的迅速发展为各行业的信...
2025-04-27 19:17:38
(图表·漫画)新华视点丨市...
新华社图表,北京,2025年3月26日 2025年春招中,人工智能...
2025-03-26 19:49:48
Linux运维相关认证有哪...
在 Linux 运维领域,拥有相关认证不仅能证明个人的专业能力,还...
2025-03-01 12:23:35
【深度】探索AGI的“小团...
2月21日,DeepSeek在社交媒体宣布,接下来一周,将开源5个...
2025-02-21 19:46:20
不重复使用flexbox容...
下面是一个示例,展示了如何使用flexbox来创建一个复杂的布局。...
2025-01-12 13:00:37

热门资讯

使用 KRAWL 扫描 Kub... 用 KRAWL 脚本来识别 Kubernetes Pod 和容器中的错误。当你使用 Kubernet...
Helix:高级 Linux ... 说到 基于终端的文本编辑器,通常 Vim、Emacs 和 Nano 受到了关注。这并不意味着没有其他...
通过 SaltStack 管理... 我在搜索Puppet的替代品时,偶然间碰到了Salt。我喜欢puppet,但是我又爱上Salt了:)...
Epic 游戏商店现在可在 S... 现在可以在 Steam Deck 上运行 Epic 游戏商店了,几乎无懈可击! 但是,它是非官方的。...
如何在 Github 上创建一... 学习如何复刻一个仓库,进行更改,并要求维护人员审查并合并它。你知道如何使用 git 了,你有一个 G...
2024 开年,LLUG 和你... Hi,Linuxer,2024 新年伊始,不知道你是否已经准备好迎接新的一年~ 2024 年,Lin...
什么是 KDE Connect... 什么是 KDE Connect?它的主要特性是什么?它应该如何安装?本文提供了基本的使用指南。科技日...
JStock:Linux 上不... 如果你在股票市场做投资,那么你可能非常清楚投资组合管理计划有多重要。管理投资组合的目标是依据你能承受...
Opera 浏览器内置的 VP... 昨天我们报道过 Opera 浏览器内置了 VPN 服务,用户打开它可以防止他们的在线活动被窥视。不过...
Bazzite:专为 Stea... 为 Linux 桌面或者 Steam Deck 做好游戏准备,听起来都很刺激!对于一个专为 Linu...