創(chuàng)建一個容器化的機器學習模型

發(fā)布時間:2024-02-28
數(shù)據(jù)科學家在創(chuàng)建機器學習模型后,必須將其部署到生產(chǎn)中。要在不同的基礎架構上運行它,使用容器并通過 rest api 公開模型是部署機器學習模型的常用方法。本文演示了如何在 podman 容器中使用 connexion 推出使用 rest api 的 tensorflow 機器學習模型。
準備
首先,使用以下命令安裝 podman:
sudo dnf -y install podman
接下來,為容器創(chuàng)建一個新文件夾并切換到該目錄。
mkdir deployment_container && cd deployment_container
tensorflow 模型的 rest api
下一步是為機器學習模型創(chuàng)建 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 神經(jīng)網(wǎng)絡的權重位于文件夾 ml_model/ 中。
swagger.yaml
swagger.yaml 使用 swagger規(guī)范 定義 connexion 庫的 api。此文件包含讓你的服務器提供輸入?yún)?shù)驗證、輸出響應數(shù)據(jù)驗證、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: objectserver.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 構建映像,請在上面的準備步驟中創(chuàng)建的 deployment_container 目錄中創(chuàng)建一個名為 dockerfile 的新文件:
from fedora:28 # file author / maintainer maintainer sven boesiger <donotspam@ujelang.com> # 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 并測試模型:
當然,你現(xiàn)在也可以在應用中通過 rest api 訪問模型。
via: https://fedoramagazine.org/create-containerized-machine-learning-model/
作者:sven b?siger 選題:lujun9972 譯者:geekpi 校對:wxy
上一個:文件類型都有哪些,文件的常見類型
下一個:HTML5開發(fā)實戰(zhàn)之網(wǎng)易微博

托利多ind331
什么是emmc接口?
筆記本電腦win10藍牙怎么連接(win10藍牙怎么連接音響)
選擇tygon流量計有哪些好處
在脅迫的情況下簽訂的合同生效嗎
鍵盤不能打字怎么回事筆記本(筆記本 電腦鍵盤 無法輸入)
深入理解函數(shù)式編程(函數(shù)如何理解)
香樟夏秋季病蟲害防治
香樟大樹移栽的關鍵技術
料位計工作時的注意
十八禁 网站在线观看免费视频_2020av天堂网_一 级 黄 色 片免费网站_绝顶高潮合集Videos