创建一个容器化的机器学习模型
机器学习 容器
![](/data/attachment/album/201812/15/091829w45rgg1peoawzce7.jpg)


数据科学家在创建机器学习模型后，必须将其部署到生产中。要在不同的基础架构上运行它，使用容器并通过 REST API 公开模型是部署机器学习模型的常用方法。本文演示了如何在 [Podman](https://fedoramagazine.org/running-containers-with-podman/) 容器中使用 [Connexion](https://connexion.readthedocs.io/en/latest/) 推出使用 REST API 的 [TensorFlow](https://www.tensorflow.org) 机器学习模型。


### 准备


首先，使用以下命令安装 Podman：



```
sudo dnf -y install podman
```

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



```
mkdir deployment_container && cd deployment_container
```

### TensorFlow 模型的 REST API


下一步是为机器学习模型创建 REST API。这个 [github 仓库](https://github.com/svenboesiger/titanic_tf_ml_model)包含一个预训练模型，以及能让 REST API 工作的设置。


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



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

#### prediction.py 和 ml\_model/


[prediction.py](https://github.com/svenboesiger/titanic_tf_ml_model/blob/master/prediction.py) 能进行 Tensorflow 预测，而 20x20x20 神经网络的权重位于文件夹 [ml\_model/](https://github.com/svenboesiger/titanic_tf_ml_model/tree/master/ml_model/titanic) 中。


#### swagger.yaml


[swagger.yaml](https://github.com/svenboesiger/titanic_tf_ml_model/blob/master/swagger.yaml) 使用 [Swagger规范](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md) 定义 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](https://github.com/svenboesiger/titanic_tf_ml_model/blob/master/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](https://github.com/svenboesiger/titanic_tf_ml_model/blob/master/requirements.txt) 定义了运行程序所需的 python 包。



```
connexion
tensorflow
pandas
```

### 容器化！


为了让 Podman 构建映像，请在上面的准备步骤中创建的 `deployment_container` 目录中创建一个名为 `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](http://0.0.0.0:5000/) 访问 Swagger/Connexion UI 并测试模型：


![](/data/attachment/album/201812/15/091830ggg293pc3q19z377.png)


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




---


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


作者：[Sven Bösiger](https://fedoramagazine.org/author/r00nz/) 选题：[lujun9972](https://github.com/lujun9972) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
After data scientists have created a machine learning model, it has to be deployed into production. To run it on different infrastructures, using containers and exposing the model via a REST API is a common way to deploy a machine learning model. This article demonstrates how to roll out a [TensorFlow](https://www.tensorflow.org) machine learning model, with a REST API delivered by [Connexion](https://connexion.readthedocs.io/en/latest/) in a container with [Podman](https://fedoramagazine.org/running-containers-with-podman/).


## Preparation

First, install Podman with the following command:

sudo dnf -y install podman

Next, create a new folder for the container and switch to that directory.

mkdir deployment_container && cd deployment_container

## REST API for the TensorFlow model

The next step is to create the REST-API for the machine learning model. This [github repository](https://github.com/svenboesiger/titanic_tf_ml_model) contains a pretrained model, and well as the setup already configured for getting the REST API working.

Clone this in the deployment_container directory with the command:

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

#### prediction.py & ml_model/

The [prediction.py](https://github.com/svenboesiger/titanic_tf_ml_model/blob/master/prediction.py) file allows for a Tensorflow prediction, while the weights for the 20x20x20 neural network are located in folder [ ml_model/](https://github.com/svenboesiger/titanic_tf_ml_model/tree/master/ml_model/titanic).

#### swagger.yaml

The file swagger.yaml defines the API for the Connexion library using the [Swagger specification](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md). This file contains all of the information necessary to configure your server to provide input parameter validation, output response data validation, URL endpoint definition.

As a bonus Connexion will provide you also with a simple but useful single page web application that demonstrates using the API with JavaScript and updating the DOM with it.

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](https://github.com/svenboesiger/titanic_tf_ml_model/blob/master/server.py) defines an entry point to start the Connexion server.

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

[ requirements.txt](https://github.com/svenboesiger/titanic_tf_ml_model/blob/master/requirements.txt) defines the python requirements we need to run the program.

connexion tensorflow pandas

## Containerize!

For Podman to be able to build an image, create a new file called “Dockerfile” in the **deployment_container** directory created in the preparation step above:

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

Next, build the container image with the command:

podman build -t ml_deployment .

## Run the container

With the Container image built and ready to go, you can run it locally with the command:

podman run -p 5000:5000 ml_deployment |


Navigate to [http://0.0.0.0:5000/ui](http://0.0.0.0:5000/) in your web browser to access the Swagger/Connexion UI and to test-drive the model:

Of course you can now also access the model with your application via the REST-API.

## Robert

Hi Sven, really enjoyed about your article. Things like ml, neural, ai, … are very exciting !

If you build “podman build -t ml_deployment . ” , I get this message , “port bindings are not yet supported by rootless containers”

How it works (Fedora 29) for me is build as sudo:

sudo podman build -t ml_deployment .

and run as sudo:

sudo podman run -p 5000:5000 ml_deployment

Thanks to write this awesome article, looking forward to seeing more of these stuff in the future.

## Costas A.

I guess this is not something for the average Joe…

Except if it helps predict lottery winning numbers 😛

## Yazan Al Monshed

Nice Blog , but I need More learn About Podman in the future

## Ejner

Thanks for the article, it is a great demo! – even if I can’t make heads or tails of the results 🙂

To get the page to load correctly, I struggled a bit with the json input for ‘person’, trying to read from the low-res image. So I’ll paste it here:

{

“attributes”: {

“pclass”: 1,

“age”: 66,

“sibsp”: 1,

“parch”: 4,

“sex_male”:1,

“sex_female”:0,

“embarked_C”:1,

“embarked_Q”:0,

“embarked_S”: 0

}

}

Podman is a very nice project, as well as Buildah. Thanks to everybody involved.

## Roberto F.

Hi Sven, nice article!

If you want to get it working in a rooless environment you have to run with the switch –net=host, like below

podman run -p 5000:5000 –net=host ml_deployment

Cheers!

Roberto

## Marcos Insfran

I don’t have enough space in my /home, when run ‘podman build -t ml_deployment .’ show error of space. Is it possible to download podman images in another directory/path (like another disk)?

Tks.