自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

創(chuàng)建一個(gè)容器化的機(jī)器學(xué)習(xí)模型

人工智能 機(jī)器學(xué)習(xí) 架構(gòu)
數(shù)據(jù)科學(xué)家在創(chuàng)建機(jī)器學(xué)習(xí)模型后,必須將其部署到生產(chǎn)中。要在不同的基礎(chǔ)架構(gòu)上運(yùn)行它,使用容器并通過 REST API 公開模型是部署機(jī)器學(xué)習(xí)模型的常用方法。本文演示了如何在 Podman 容器中使用 Connexion 推出使用 REST API 的 TensorFlow 機(jī)器學(xué)習(xí)模型。

[[252634]]

數(shù)據(jù)科學(xué)家在創(chuàng)建機(jī)器學(xué)習(xí)模型后,必須將其部署到生產(chǎn)中。要在不同的基礎(chǔ)架構(gòu)上運(yùn)行它,使用容器并通過 REST API 公開模型是部署機(jī)器學(xué)習(xí)模型的常用方法。本文演示了如何在 Podman 容器中使用 Connexion 推出使用 REST API 的 TensorFlow 機(jī)器學(xué)習(xí)模型。

準(zhǔn)備

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

  1. sudo dnf -y install podman

接下來,為容器創(chuàng)建一個(gè)新文件夾并切換到該目錄。

  1. mkdir deployment_container && cd deployment_container

TensorFlow 模型的 REST API

下一步是為機(jī)器學(xué)習(xí)模型創(chuàng)建 REST API。這個(gè) github 倉庫包含一個(gè)預(yù)訓(xùn)練模型,以及能讓 REST API 工作的設(shè)置。

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

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

prediction.py 和 ml_model/

prediction.py 能進(jìn)行 Tensorflow 預(yù)測,而 20x20x20 神經(jīng)網(wǎng)絡(luò)的權(quán)重位于文件夾 ml_model/ 中。

swagger.yaml

swagger.yaml 使用 Swagger規(guī)范 定義 Connexion 庫的 API。此文件包含讓你的服務(wù)器提供輸入?yún)?shù)驗(yàn)證、輸出響應(yīng)數(shù)據(jù)驗(yàn)證、URL 端點(diǎn)定義所需的所有信息。

額外地,Connexion 還將給你提供一個(gè)簡單但有用的單頁 Web 應(yīng)用,它演示了如何使用 Javascript 調(diào)用 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 定義了啟動(dòng) Connexion 服務(wù)器的入口點(diǎn)。

  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 定義了運(yùn)行程序所需的 python 包。

  1. connexion
  2. tensorflow
  3. pandas

容器化!

為了讓 Podman 構(gòu)建映像,請(qǐng)?jiān)谏厦娴臏?zhǔn)備步驟中創(chuàng)建的 deployment_container 目錄中創(chuàng)建一個(gè)名為 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

接下來,使用以下命令構(gòu)建容器鏡像:

  1. podman build -t ml_deployment .

運(yùn)行容器

隨著容器鏡像的構(gòu)建和準(zhǔn)備就緒,你可以使用以下命令在本地運(yùn)行它:

  1. podman run -p 5000:5000 ml_deployment

在 Web 瀏覽器中輸入 http://0.0.0.0:5000/ui 訪問 Swagger/Connexion UI 并測試模型:

當(dāng)然,你現(xiàn)在也可以在應(yīng)用中通過 REST API 訪問模型。 

責(zé)任編輯:龐桂玉 來源: Linux中國
相關(guān)推薦

2024-06-13 08:36:11

2024-08-15 14:48:57

2020-10-14 14:18:33

機(jī)器學(xué)習(xí)機(jī)器學(xué)習(xí)架構(gòu)人工智能

2017-10-13 15:59:24

iPhone機(jī)器學(xué)習(xí)iOS

2022-06-02 15:42:05

Python機(jī)器學(xué)習(xí)

2024-04-10 12:39:08

機(jī)器學(xué)習(xí)python

2020-11-20 10:50:01

Docker容器

2021-11-02 08:00:00

機(jī)器學(xué)習(xí)API技術(shù)

2025-01-21 08:11:24

2018-12-29 08:00:00

機(jī)器學(xué)習(xí)TensorFlowKubeflow

2010-08-05 15:46:13

Flex行為Flex效果

2023-12-25 10:53:54

機(jī)器學(xué)習(xí)模型性能

2020-09-28 12:42:17

機(jī)器學(xué)習(xí)語言GitHub

2017-07-25 09:19:02

2021-04-09 10:02:29

機(jī)器學(xué)習(xí)人工智能計(jì)算機(jī)

2022-03-25 11:11:50

機(jī)器學(xué)習(xí)TiDBSQL

2021-06-23 16:40:58

JavaTomcatWeb

2021-08-30 09:25:25

Bert模型PyTorch語言

2024-09-30 05:43:44

2020-02-21 11:23:11

機(jī)器學(xué)習(xí)技術(shù)人生第一份工作
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)