溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何在Podman容器中創(chuàng)建一個容器化的機(jī)器學(xué)習(xí)模型

發(fā)布時間:2022-03-31 10:19:49 來源:億速云 閱讀:215 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要為大家展示了“如何在Podman容器中創(chuàng)建一個容器化的機(jī)器學(xué)習(xí)模型”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“如何在Podman容器中創(chuàng)建一個容器化的機(jī)器學(xué)習(xí)模型”這篇文章吧。

準(zhǔn)備

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

sudo dnf -y install podman

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

mkdir deployment_container && cd deployment_container

TensorFlow 模型的 REST API

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

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

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 還將給你提供一個簡單但有用的單頁 Web 應(yīng)用,它演示了如何使用 Javascript 調(diào)用 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 Articleconsumes:  - "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 服務(wù)器的入口點(diǎn)。

import connexion app = connexion.App(__name__, specification_dir='./') app.add_api('swagger.yaml') if __name__ == '__main__': app.run(debug=True)

requirements.txt 定義了運(yùn)行程序所需的 python 包。

connexiontensorflowpandas

容器化!

為了讓 Podman 構(gòu)建映像,請在上面的準(zhǔn)備步驟中創(chuàng)建的 deployment_container 目錄中創(chuàng)建一個名為 Dockerfile 的新文件:

FROM fedora:28 # File Author / MaintainerMAINTAINER Sven Boesiger <donotspam@ujelang.com> # Update the sourcesRUN dnf -y update --refresh # Install additional dependenciesRUN dnf -y install libstdc++ RUN dnf -y autoremove # Copy the application folder inside the containerADD /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 portsEXPOSE 5000 # Set the default directory where CMD will executeWORKDIR /titanic_tf_ml_model # Set the default command to execute# when creating a new containerCMD python3 server.py

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

podman build -t ml_deployment .

運(yùn)行容器

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

podman run -p 5000:5000 ml_deployment

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

如何在Podman容器中創(chuàng)建一個容器化的機(jī)器學(xué)習(xí)模型

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

以上是“如何在Podman容器中創(chuàng)建一個容器化的機(jī)器學(xué)習(xí)模型”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI