简体   繁体   English

如何使用 dockerfile 在 aws sagemaker 中运行 python 文件

[英]How to run a python file inside a aws sagemaker using dockerfile

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 代码和一个 model 是预先训练的,并且有一个 model。因为 aws sagemaker 仅支持两个命令 train 或 serve 分别用于训练和部署。

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.目前,我正在使用命令“python filename.py”运行程序并且它运行成功我希望在 aws sagemaker 上运行同样的程序。

Any Solution??有什么解决办法??

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.我尝试与将 model 部署到 s3 并在部署时调用相同,我不知道它是正确还是错误。

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. 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.

To do this, I'm just following this guide on the AWS documentation on using your own inference code .为此,我只是按照 AWS 文档中关于使用您自己的推理代码的指南进行操作。

The steps will be:步骤将是:

  1. Create the model code创建 model 代码
  2. Create a Docker image out of the code从代码中创建一个 Docker 镜像
  3. Create our Endpoint with this image使用此图像创建我们的端点

Step 1: Create the model code第 1 步:创建 model 代码

Let's take this simple model as an example in Python:我们以Python中的这个简单的model为例:

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)

Here's are requirements.txt:这是requirements.txt:

Flask==0.10.1

Step 2: Create the Docker image第 2 步:创建 Docker 映像

We need a Dockerfile to build our image.我们需要一个 Dockerfile 来构建我们的镜像。 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"]

We can build the image by running: docker build -t simple-model:latest.我们可以通过运行: 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

If it is running, you should be able to curl any of the endpoints:如果它正在运行,您应该能够curl任何端点:

curl localhost:8080/ping
> ok

Now we need to publish it to ECR as SageMaker reads the model from ECR.现在我们需要将它发布到 ECR,因为 SageMaker 从 ECR 读取 model。 I'm following this guide from AWS我正在遵循AWS 的本指南

Grab the image id by running docker images通过运行docker images获取图像 ID

Use that here.在这里使用它。 For convenience, I'm using us-west-2.为方便起见,我使用的是 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

Now we should push it to ECR:现在我们应该将其推送到 ECR:

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

Step 3: Create Endpoint第 3 步:创建端点

Now we can create a model with this image.现在我们可以用这个图像创建一个 model。 First, you need a SageMaker Execution role.首先,您需要一个 SageMaker 执行角色。 This will be used to access your image and other resources.这将用于访问您的图像和其他资源。 You can set that up here on this AWS doc page .您可以在此AWS 文档页面上进行设置。

Secondly, you need to have the AWS CLI setup.其次,您需要设置AWS CLI

Let's get started.让我们开始吧。

Let's create the model first.让我们首先创建 model。 This will point to your ECR image you created in the last step.这将指向您在上一步中创建的 ECR 映像。 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\"
}"

That'll create your model.这将创建您的 model。 Now we need to create an EndpointConfig which will tell your SageMaker Endpoint how it needs to be configured:现在我们需要创建一个EndpointConfig ,它将告诉您的 SageMaker Endpoint 需要如何配置:

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"

If all that works, wait until aws sagemaker describe-endpoint --endpoint-name SimpleEndpoint says it is InService .如果一切正常,请等到aws sagemaker describe-endpoint --endpoint-name SimpleEndpoint说它是InService

Once it is, we can now call invocations against it:一旦它是,我们现在可以调用它:

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

Conclusion结论

If that all worked, you'll have your own endpoint.如果一切正常,您将拥有自己的端点。 The next steps would be to customize that Python script to do your own inference with your own model.接下来的步骤是自定义 Python 脚本,以使用您自己的 model 进行自己的推理。 SageMaker also has the ability to grab your model artifacts automatically and you don't have to include them in your model container. SageMaker 还能够自动获取 model 工件,您不必将它们包含在 model 容器中。 See the documentation here .请参阅此处的文档

Hopefully that helps!希望这会有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在AWS上使用包含Dockerfile和python脚本的git存储库运行批处理作业? - How to run a batch job using a git repository containing a Dockerfile and a python script on AWS? 如何使用 CDK、sagemaker 部署 AWS? - How to deploy AWS using CDK, sagemaker? 如何使用 wget 作为 Dockerfile 运行命令将文件下载到特定路径? - How to download a file to a specific path using wget as a Dockerfile run command? 如何在使用 aws Sagemaker python SDK 时保存训练作业的未压缩输出? - how to save uncompressed outputs from a training job in using aws Sagemaker python SDK? 如何在 SageMaker 上运行和部署 AWS 的 XGBoost MNIST 示例笔记本? - How to run and deploy AWS's XGBoost MNIST sample notebook on SageMaker? 如何在 AWS Sagemaker 笔记本实例中有条件地运行不同的 Jupyter 笔记本 - How to run different Jupyter notebooks conditionally in a AWS Sagemaker notebook instance Dockerfile在项目目录中运行python脚本文件 - Dockerfile run a python script file in a project directory AWS SageMaker,使用 python SDK 描述特定的训练作业 - AWS SageMaker, describe a specific training job using python SDK 如何在 python 中运行循环来运行 AWS CLI? - How to run loop inside python to run AWS CLI? 如何使用 python 在 gnuplot 的所有子目录中运行 a.plt 文件? - How to run a .plt file inside all subdirectories with gnuplot using python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM