如何使用 dockerfile 在 aws sagemaker 中运行 python 文件 [英] How to run a python file inside a aws sagemaker using dockerfile

查看:37
本文介绍了如何使用 dockerfile 在 aws sagemaker 中运行 python 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Python 代码和一个经过预训练的模型,并且在我的代码所在的同一目录中有一个 model.pkl 文件,现在我必须运行或部署它到 aws sagemaker 但没有得到任何对此的解决方案,因为 aws sagemaker 仅支持两个命令 train 或 serve 分别用于训练和部署.

I have a python code and a model that is pre-trained and has a model.pkl file with me in the same directory where the code i, now i have to run or deploy this to the aws sagemaker but not getting any solution for this as aws sagemaker supports only two commands train or serve for training and deploying respectively.

目前,我正在使用命令python filename.py"运行该程序,并且它运行成功,我希望它也能在 aws sagemaker 上运行.

currently, I am running the program using the command "python filename.py" and it is running successfully I want the same to run on the aws sagemaker.

有什么解决办法吗??

我试过和把模型部署到s3一样,在部署的时候调用不知道是对还是错.

I tried the same as deploying the model to the s3 and call at the time of deploy I don't know is it correct or wrong.

推荐答案

如果您有预训练模型和要在 SageMaker Endpoints 上运行的文件 filename.py,您只需要打包将其作为 Docker 映像创建一个模型,然后您可以将其部署到端点并进行调用.

If you have a pretrained model and a file filename.py that you want to run on SageMaker Endpoints, you just need to package this up as a Docker image to create a model which you can then deploy to an Endpoint and make invocations.

为此,我只是在 使用您自己的推理代码.

步骤如下:

  1. 创建模型代码
  2. 从代码中创建一个 Docker 镜像
  3. 用这张图片创建我们的端点

第一步:创建模型代码

让我们以 Python 中的这个简单模型为例:

Step 1: Create the model code

Let's take this simple model as an example in Python:

from flask import Flask, request
app = Flask(__name__)

@app.route('/ping')
def ping():
    return ''

@app.route('/invocations')
def invoke():
    return 'should do inference with your model here'


if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=8080)

这是requirements.txt:

Here's are requirements.txt:

Flask==0.10.1

第 2 步:创建 Docker 镜像

我们需要一个 Dockerfile 来构建我们的镜像.这是我用过的:

Step 2: Create the Docker image

We need a Dockerfile to build our image. Here's the one I used:

Dockerfile:

Dockerfile:

FROM ubuntu:16.04

RUN apt-get update -y && apt-get install -y python-pip python-dev

COPY ./requirements.txt /app/requirements.txt

WORKDIR /app

RUN pip install -r requirements.txt

COPY . /app

EXPOSE 8080

ENTRYPOINT ["python"]
CMD ["model.py"]

我们可以通过运行来构建镜像:docker build -t simple-model:latest .

We can build the image by running: docker build -t simple-model:latest .

这将创建图像,现在我们可以通过运行它来测试它:

This will create the image and now we can test it by running it:

docker run -d -p 8080:8080 simple-model

如果它正在运行,您应该能够curl任何端点:

If it is running, you should be able to curl any of the endpoints:

curl localhost:8080/ping
> ok

现在我们需要将其发布到 ECR,因为 SageMaker 从 ECR 读取模型.我正在关注 AWS 的本指南

Now we need to publish it to ECR as SageMaker reads the model from ECR. I'm following this guide from AWS

通过运行docker images

在这里使用它.为方便起见,我使用 us-west-2.将其替换为您选择的区域:

Use that here. For convenience, I'm using us-west-2. Replace that with your chosen region:

docker tag <image id> <aws accound id>.dkr.ecr.us-west-2.amazonaws.com/simple-model

现在我们应该将其推送到 ECR:

Now we should push it to ECR:

docker push <aws accound id>.dkr.ecr.us-west-2.amazonaws.com/simple-model

第 3 步:创建端点

现在我们可以用这张图片创建一个模型.首先,您需要一个 SageMaker 执行角色.这将用于访问您的图像和其他资源.您可以在此 AWS 文档页面上进行设置.

其次,您需要拥有 AWS CLI 设置.

Secondly, you need to have the AWS CLI setup.

让我们开始吧.

让我们先创建模型.这将指向您在上一步中创建的 ECR 映像.在此命令中替换您在上面创建的角色名称:

Let's create the model first. This will point to your ECR image you created in the last step. Substitute the role name you created above in this command:

aws sagemaker create-model --model-name "SimpleModel" --execution-role-arn "arn:aws:iam::<aws account id>:role/<role name>" --primary-container "{
    "ContainerHostname": "ModelHostname",
    "Image": "<aws account id>.dkr.ecr.us-west-2.amazonaws.com/simple-model:latest"
}"

这将创建您的模型.现在我们需要创建一个 EndpointConfig,它会告诉您的 SageMaker Endpoint 它需要如何配置:

That'll create your model. Now we need to create an EndpointConfig which will tell your SageMaker Endpoint how it needs to be configured:

aws sagemaker create-endpoint-config --endpoint-config-name "SimpleConfig" --production-variants "[
    {
        "VariantName" : "SimpleVariant",
        "ModelName" : "SimpleModel",
        "InitialInstanceCount" : 1,
        "InstanceType" : "ml.t2.medium"
    }
]"

现在终于,我们可以使用该配置创建我们的端点:

And now finally, we can create our Endpoint using that config:

aws sagemaker create-endpoint --endpoint-name "SimpleEndpoint" --endpoint-config-name "SimpleConfig"

如果一切正常,请等到 aws sagemaker describe-endpoint --endpoint-name SimpleEndpoint 说它是 InService.

If all that works, wait until aws sagemaker describe-endpoint --endpoint-name SimpleEndpoint says it is InService.

一旦它是,我们现在可以调用对它的调用:

Once it is, we can now call invocations against it:

aws sagemaker-runtime invoke-endpoint --endpoint-name SimpleEndpoint --body "empty"

结论

如果一切顺利,您将拥有自己的端点.接下来的步骤是自定义该 Python 脚本以使用您自己的模型进行您自己的推理.SageMaker 还能够自动抓取您的模型工件,您不必将它们包含在您的模型容器中.请参阅 此处的文档.

希望有帮助!

这篇关于如何使用 dockerfile 在 aws sagemaker 中运行 python 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆