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

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

问题描述

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

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.

为此,我只是遵循AWS文档上

To do this, I'm just following this guide on the AWS documentation on using your own inference code.

步骤将是:

  1. 创建模型代码
  2. 使用代码创建Docker映像
  3. 使用此图像创建端点

第1步:创建模型代码

让我们以这个简单的模型作为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

现在,当SageMaker从ECR读取模型时,我们需要将其发布到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天全站免登陆