前端代码部署到docker_使用s3将具有sagemaker的自定义docker模型部署到无服务器前端...

前端代码部署到docker

介绍 (Introduction)

Deploying a model with AWS SageMaker is a great way to allow users or customers to interact with it. While you can use the many algorithms and models that come with SageMaker’s Python SDK, there are also many cases where you might want to create your own algorithm. This requires the use of Docker.

使用AWS SageMaker部署模型是允许用户或客户与之交互的好方法。 尽管您可以使用SageMaker的Python SDK随附的许多算法和模型,但在很多情况下,您可能想创建自己的算法。 这需要使用Docker。

The easiest way to let people interact with your model is by employing AWS Lambda and API Gateway to setup an API POST request to the model.

使人们与您的模型进行交互的最简单方法是使用AWS Lambda和API Gateway设置对模型的API POST请求。

Finally, having an API for sending requests and receiving inference responses from the model is great, but a simple and nice looking web app for users to interact with the data is even better. This is where hosting a static website with S3 and accessing the endpoint with AJAX comes into play.

最后,拥有一个用于发送请求和从模型接收推理响应的API很棒,但是一个简单美观的Web应用程序供用户与数据进行交互就更好了。 这是使用S3托管静态网站并使用AJAX访问终结点的地方。

All of these components of AWS working in unison allows for a sleek development and inference serving framework. This tutorial also covers using AWS CloudWatch to understand ambiguous errors such as 500 Internal Server Errors from the custom model.

AWS的所有这些组件都可以协同工作,从而实现了时尚的开发和推理服务框架。 本教程还介绍了如何使用AWS CloudWatch来了解模棱两可的错误,例如来自自定义模型的500个内部服务器错误。

Check out the repo for the some of the code mentioned below, here.

在此处 ,查看仓库中以下提到的一些代码。

Docker模型 (Docker Model)

To deploy a custom model with SageMaker it must be wrapped by SageMaker’s Estimator class. This can be done by creating a Docker image that interfaces with this class.

要使用SageMaker部署自定义模型,它必须由SageMaker的Estimator类包装。 这可以通过创建与此类连接的Docker映像来完成。

Also, check out this post, if you don’t know what Docker is and why it's so important nowadays.

另外,如果您不知道Docker是什么以及为什么它在当今如此重要,请查看这篇文章

安装Docker (Install Docker)

Install Docker for your respective OS with these links: Mac, Ubuntu and Windows. Windows Home edition is a bit more difficult so I will cover some of the steps here, since Windows is the OS I use. Follow these steps first and see how far you get. Refer to the steps below if you get stuck.

通过以下链接为您各自的OS安装Docker: Mac, UbuntuWindows 。 Windows Home Edition有点困难,因此在这里我将介绍一些步骤,因为Windows是我使用的操作系统。 请先执行以下步骤,然后看您能走多远。 如果卡住,请参考以下步骤。

WSL 2 is required in Windows Home edition to run Docker Desktop.

Windows Home Edition需要WSL 2才能运行Docker Desktop。

Image for post
Click “Check for updates” and install until there are no more, or find the 2004 update itself
单击“检查更新”并安装,直到没有更多,或查找2004年更新本身

First, you must update to Windows 2004, do this by manually checking for updates and installing any that appear (warning: this will take a lot of memory and time). Then you can install WSL 2 using these directions.

首先,您必须更新到Windows 2004,方法是手动检查更新并安装所有出现的更新(警告:这会占用大量内存和时间)。 然后,你可以使用安装WSL 2 这些方向

Docker镜像 (Docker Image)

This tutorial does a great job of explaining each step to setting up a Docker image for our custom model. So follow that tutorial, but first keep these issues in mind:

本教程在解释为我们的自定义模型设置Docker映像的每个步骤方面做得非常出色。 因此,请遵循该教程,但请首先牢记以下问题:

A problem I ran into with the template provided is with installing gevent. As you can see in my Dockerfile, I use easy_install instead of pip.

我使用提供的模板遇到的一个问题是安装gevent。 如您在Dockerfile中看到的我使用easy_install而不是pip。

RUN easy_install gevent

Important: If you create the image code on Windows then you have to make the files Unix compatible, as the endline symbols are different. This can be done with the command dos2unix:

重要提示:如果您在Windows上创建图像代码,则必须使文件与Unix兼容,因为结尾符号是不同的。 这可以通过命令dos2unix完成:

find some/directory -type f -exec dos2unix {} \;

Where /some/directory is the directory of the image code.

/ some / directory是映像代码的目录。

Also, make sure to allow files such as model/serve and model/train to be executed with:

另外 ,请确保允许使用以下命令执行模型/服务模型/训练等文件:

chmod +x file/path

Now follow the steps of the other tutorial until you get to the AWS ECR section. Make sure to create the image and locally test your model as described.

现在,按照其他教程的步骤进行操作,直到进入“ AWS ECR”部分。 确保创建映像并按照说明在本地测试模型。

仅推论模型 (Inference-only Model)

Sometimes you may not need to actually train your model before inference. This was my case with my application I will show at the end of this article. Unfortunately, SageMaker still requires that the model must be fit before deploying. However, you can setup a dummy training script in the Docker image very easily. For example, you can just open the model path and write a dummy string as training.

有时您可能不需要在推理之前实际训练模型。 这是我的应用程序案例,我将在本文结尾处展示。 不幸的是,SageMaker仍然要求模型在部署之前必须适合。 但是,您可以非常轻松地在Docker映像中设置虚拟训练脚本。 例如,您可以打开模型路径并编写一个虚拟字符串作为训练。

Here is (some of) my training script: docker-image-code/anomaly-model/train

这是我的一些训练脚本: docker-image-code / anomaly-model / train

prefix = '/opt/ml/'


input_path = prefix + 'input/data'
output_path = os.path.join(prefix, 'output')
model_path = os.path.join(prefix, 'model')


# The function to execute the training.
def train():
    print('\nStarting the training.')
    with open(os.path.join(model_path, 'model'), 'w') as f:
        f.write('dummy model params');
    print('Training complete.')


if __name__ == '__main__':
    train()
    sys.exit(0)

注册Docker映像 (Register Docker Image)

Once your model image has been created and it works locally, for SageMaker to be able to access it, it must be stored in AWS Elastic Container Registry (ECR).

创建模型图像并在本地运行后,SageMaker才能访问它,必须将其存储在AWS Elastic Container Registry(ECR)中。

Go to AWS ECR and click “Get Started”. Click “Create repository” in orange.

转到AWS ECR ,然后单击“入门”。 单击橙色的“创建存储库”。

Image for post

Enter a name that represents your model under “Repository name”. And then “Create repository” again. You don’t need to change any of the other options.

在“存储库名称”下输入代表您的模型的名称。 然后再次“创建存储库”。 您无需更改任何其他选项。

Then select the new repo and click “View push commands”

然后选择新的仓库,然后单击“查看推送命令”

Image for post

Follow the “macOS / Linux” commands that are shown on your page. If this is your first time doing something like this you will need AWS Command Line Interface (CLI). Follow these instructions. And authenticate Docker with this. Once CLI and authentication is setup you won’t have to do it again.

遵循页面上显示的“ macOS / Linux”命令。 如果您是第一次这样做,则需要AWS Command Line Interface(CLI)。 请按照以下说明进行操作 。 并以此验证Docker。 一旦设置了CLI和身份验证,您将无需再次进行。

Whenever you want to update your model: update the image code, re-create the image, test it locally, and follow the push commands again.

每当您要更新模型时:更新图像代码,重新创建图像,在本地对其进行测试,然后再次执行push命令。

Phew, that probably took a while and some googling, especially if you are on Windows but hopefully I got you through it successfully. If not, let me know in the comments where the problem resides. At this point, you should have a working Docker image for your model and have stored it in the registry.

Phew ,这可能要花一些时间和一些搜索时间,特别是如果您使用的是Windows,但希望我能成功完成它。 如果不是,请在评论中让我知道问题所在。 此时,您应该为模型拥有一个正常工作的Docker映像,并将其存储在注册表中。

使用SageMaker进行部署 (Deploying with SageMaker)

Now we get into using other parts of AWS. First, we have to deploy the model with SageMaker, then use AWS Lambda and API Gateway to set up an API for posting data to your model and receiving an inference response.

现在,我们开始使用AWS的其他部分。 首先,我们必须使用SageMaker部署模型,然后使用AWS Lambda和API Gateway设置API,以将数据发布到模型并接收推理响应。

In a little more detail, the client calls the API created with API Gateway and passes in data for inference. API Gateway then passes this data to the Lambda function. Here it is parsed and sent to the SageMaker model endpoint (known as “invoking”). The model performs prediction with this data and the output is sent back through lambda and API Gateway which responds to the client with the predicted value.

更详细一点,客户端调用使用API​​ Gateway创建的API,并传递数据以进行推断。 然后,API网关将此数据传递给Lambda函数。 在这里,它被解析并发送到SageMaker模型端点(称为“调用”)。 该模型使用此数据执行预测,然后通过lambda和API网关将输出发送回去,API网关以预测值响应客户端。

Image for post
this blog post 这篇博客文章

SageMaker笔记本 (SageMaker Notebook)

First, we are going to deploy our model with SageMaker.

首先,我们将使用SageMaker部署模型。

Create a new notebook instance (or use an existing one). You can use the default settings, and set a name. Upload this notebook from my repo. The only thing that needs to be set is the docker_image_name variable, to the name of your registered docker image. It can be found in ECR under “repositories” as the URI:

创建一个新的笔记本实例(或使用现有的笔记本实例)。 您可以使用默认设置并设置名称。 从我的仓库上传这个笔记本 。 唯一需要设置的是docker_image_name变量, 即已注册的Docker映像的名称。 可以在ECR的“资源库”下找到它作为URI:

Image for post

Let me explain the code for the notebook below:

让我在下面解释笔记本的代码:

# Install sagemaker (version 1.72)
import sys
!{sys.executable} -m pip install --quiet sagemaker==1.72 -U


# Imports
import io
import os
import sys
import time
import json
from IPython.display import display
from time import strftime, gmtime
import boto3
import re
import sagemaker
from sagemaker import get_execution_role


# Get the boto3 session and sagemaker client, as well as the current execution role
sess = boto3.Session()
sm = sess.client('sagemaker')
role = sagemaker.get_execution_role()


# Name of the docker image containing the model code
docker_image_name = '<Name of docker image in registry on AWS>'


# Name and prefix for the S3 bucket storing the model output
account_id = sess.client('sts', region_name=sess.region_name).get_caller_identity()["Account"]
bucket = 'sagemaker-studio-{}-{}'.format(sess.region_name, account_id)
prefix = 'anomaly-detection'

First install SageMaker version 1.72 (I could not get this to work with the most recent version but it might work now). The session, client and execution role are grabbed to create a bucket for the model output. The docker image name is the repo URI for your uploaded image.

首先安装SageMaker 1.72版(我无法将其与最新版本一起使用,但现在可能可以使用)。 会话,客户端和执行角色将被抓取以创建模型输出的存储桶。 泊坞窗图片名称是您上传的图片的仓库URI。

sess = sagemaker.session.Session()
anomaly_detection = sagemaker.estimator.Estimator(image_name=docker_image_name,
                                    role=role,
                                    train_instance_count=1, 
                                    train_instance_type='ml.m4.xlarge',
                                    output_path='s3://{}/{}/output'.format(bucket, prefix),
                                    base_job_name="anomaly-detection",
                                    sagemaker_session=sess)

Create the estimator which takes many arguments. The one that can be changed based on your needs is train_instance_type. This is the AWS cloud compute instance that will be used to train the model when fit is called. Note: m1.m4.xlarge seems to be the least expensive one allowed for training. More instance types that may or may not work for training are listed here. If you need more memory for training or just want it to be faster try more expensive instances.

创建带有许多参数的估计器。 可以根据您的需求进行更改的是train_instance_type 。 这是AWS云计算实例,将在调用fit时用于训练模型。 注意: m1.m4.xlarge似乎是允许进行培训的最便宜的一种。 此处列出更多可能无法进行训练的实例类型。 如果您需要更多的内存来进行训练,或者只是想要更快的速度,请尝试使用更昂贵的实例。

anomaly_detection.fit()

Next, we call fit to train the model. This runs the train script in the docker image model directory. You will need to call fit even if the model’s only purpose is inference. This was the case for my project.

接下来,我们称之为拟合训练模型。 这将在docker映像模型目录中运行训练脚本。 即使模型的唯一目的是推理,也需要调用fit 。 我的项目就是这种情况。

endpoint_name = 'anomaly-detection-endpoint'
anomaly_detection.deploy(initial_instance_count=1, 
                                             instance_type='ml.t2.medium', 
                                             endpoint_name=endpoint_name)
# Delete endpoint when you don't need your model deployed
sm.delete_endpoint(EndpointName=endpoint_name)

Finally, we make the endpoint by deploying the model. endpoint_name should be something recognizable. The instance types for inference can be even less intensive than for training such as m1.t2.medium which is the cheapest.

最后,我们通过部署模型制作端点。 endpoint_name应该可以识别。 推理的实例类型可能比训练最简单的实例类型(例如最便宜的m1.t2.medium)密集。

Here is more about the price of SageMaker. Use AWS Cost Management to track training and deployment cost of your model (costs can quickly get out of hand if you are not careful about this).

是有关SageMaker价格的更多信息。 使用 AWS Cost Management 跟踪模型的培训和部署成本(如果您对此不小心,则成本很快就会失控)。

Now we have the model deployed! Lets figure out how to actually allow people to interact with it.

现在我们已经部署了模型! 让我们弄清楚如何真正允许人们与之交互。

使用Lambda和API网关创建API (Creating an API with Lambda & API Gateway)

We have to create a lambda function to invoke the endpoint. Go to AWS Lambda and create a new function. Name it something helpful, change the runtime to Python 3.6 and then select an existing execution role that has permission to invoke a model endpoint or create a new execution role.

我们必须创建一个lambda函数来调用端点。 转到AWS Lambda并创建一个新功能。 将其命名为有用的东西,将运行时更改为Python 3.6,然后选择一个具有执行权限的现有执行角色,以调用模型端点或创建新的执行角色。

Image for post

IAM角色权限 (IAM Role Permissions)

To give permission to invoke model endpoints to your role go to AWS IAM and then “Roles” on the sidebar:

要授予对角色调用模型端点的权限,请转到AWS IAM ,然后在侧边栏上单击 “ Roles”:

Image for post

Click on the role which for the above example would be “myLambdaFunction-role-….”. In the open “Permissions” tab, click the only policy.

单击上面示例中的角色为“ myLambdaFunction-role-…。”。 在打开的“权限”选项卡中,单击唯一策略。

Image for post

A JSON description of the policy should come up. Click “Edit policy” and then the “JSON” tabg and add the line “sagemaker:InvokeEndpoint” like so:

应该显示该策略的JSON描述。 单击“编辑策略”,然后单击“ JSON”选项卡,并添加“ sagemaker:InvokeEndpoint”行,如下所示:

Image for post

Which allows the role to interact with SageMaker endpoints. Click “Review policy” and “Save changes” in blue at the bottom.

这允许角色与SageMaker端点进行交互。 点击底部的蓝色“审核政策”和“保存更改”。

Back to lambda, in the “lambda_function” code panel replace existing code with this code.

返回lambda,在“ lambda_function”代码面板中,用此代码替换现有代码

import os
import io
import boto3
import json
import csv




# grab environment variables
ENDPOINT_NAME = os.environ['ENDPOINT_NAME']
# grab runtime client
runtime = boto3.client('runtime.sagemaker')


def lambda_handler(event, context):
    # Load data from POST request
    data = json.loads(json.dumps(event))
    
    # Grab the payload
    payload = data['body']
    
    # Invoke the model. In this case the data type is a JSON but can be other things such as a CSV
    response = runtime.invoke_endpoint(EndpointName=ENDPOINT_NAME,
                                   ContentType='application/json',
                                   Body=payload)
    
    # Get the body of the response from the model
    result = response['Body'].read().decode()


    # Return it along with the status code of 200 meaning this was succesful 
    return {
        'statusCode': 200,
        'body': result
    }

First you will notice the ENDPOINT_NAME variable. This links our model’s endpoint to this function. Which in my case is anomaly-detection-endpoint:

首先,您会注意到ENDPOINT_NAME变量。 这会将模型的端点链接到此功能。 在我的情况下,这是异常检测端点

endpoint_name = 'anomaly-detection-endpoint'
anomaly_detection.deploy(initial_instance_count=1, 
                                             instance_type='ml.t2.medium', 
                                             endpoint_name=endpoint_name)
# Delete endpoint when you don't need your model deployed
sm.delete_endpoint(EndpointName=endpoint_name)

Edit the “Environment variables” below the function code:

编辑功能代码下方的“环境变量”:

Image for post

Add an environment variable with the key as ENDPOINT_NAME and the value as the endpoint name from SageMaker. In my case it would be anomaly-detection-endpoint. And save it.

从SageMaker添加一个环境变量,其键为ENDPOINT_NAME ,值为端点名称。 就我而言,这将是异常检测端点。 并保存。

Looking back at the lambda function code the next important thing is ContentType=’application/json’ in invoking the endpoint. In my case the input data is a JSON because I included some hyperparameters for inference, not just the input data for the model. However, if you don’t need inference hyperparameters then you can make the type ‘text/csv’ and the steps are all pretty much the same.

回顾lambda函数代码,下一个重要的事情是调用端点的ContentType ='application / json' 。 在我的情况下,输入数据是JSON,因为我包括了一些用于推理的超参数,而不仅仅是模型的输入数据。 但是,如果不需要推理超参数,则可以将类型设为“ text / csv” ,并且步骤几乎相同。

创建一个API网关 (Creating an API Gateway)

The last step of accessing the deployed model with a POST request is setting up an API. Go to AWS API Gatway and click “Create API” in the top right after logging in. Then choose “HTTP API” as the type and click “Build”. Add a Lambda Integration and choose the correct AWS Region and the name of the lambda function you just made. Then name the gateway. Something like this:

使用POST请求访问已部署模型的最后一步是设置API。 登录后 ,转到AWS API Gatway ,然后单击右上角的“创建API”。然后选择“ HTTP API”作为类型,然后单击“构建”。 添加一个Lambda集成,然后选择正确的AWS区域以及您刚创建的lambda函数的名称。 然后命名网关。 像这样:

Image for post

Click “Next” and add a route. Select “POST” for the method. In “Resource path” input the lambda name again like /myLambdaFunction. And under “Integration target” put the lambda function name yet again.

点击“下一步”并添加一条路线。 选择“ POST”作为方法。 在“资源路径”中,再次输入lambda名称,如/ myLambdaFunction 。 然后在“集成目标”下再次输入lambda函数名称。

Image for post

For stages, add a stage called something like test and click the switch so that this stage is auto-deployed. This will be a part of the POST URI.

对于阶段,添加一个称为test之类的阶段,然后单击开关,以便自动部署该阶段。 这将是POST URI的一部分。

Image for post

Finally, hit “Create” at the bottom.

最后,点击底部的“创建”。

If you go back to your lambda function you will see API Gateway in the diagram at the top. Click it and then show the details. “API endpoint” is the URL for the POST request that people can use to get predictions from your model. It follows the format:

如果返回lambda函数,您将在顶部的图中看到API Gateway。 单击它,然后显示详细信息。 “ API端点”是POST请求的URL,人们可以用来从模型中获取预测。 它遵循以下格式:

https://{restapi_id}.execute-api.{region}.amazonaws.com/{stage_name}/{resource_name}
Image for post

Finally, you will probably want to allow all origins and headers in the CORS configuration for this API Gateway, otherwise you are going to run into CORS issues while trying to make a POST request in your static S3 website with AJAX.

最后,您可能希望允许此API网关的CORS配置中包含所有起源和标头,否则您将在尝试使用AJAX在静态S3网站中发出POST请求时遇到CORS问题。

Go back to API Gateway and click on your API. Then go to “CORS” under “Develop” in the sidebar.

返回API网关,然后单击您的API。 然后转到侧边栏中“开发”下的“ CORS”。

Image for post

Click “Configure” and add * for “Access-Control-Allow-Origins”, * for “Access-Control-Allow-Headers” and POST for “Access-Control-Allow-Methods”. Then save it.

单击“配置”,然后为“ Access-Control-Allow-Origins”添加* 为“ Access-Control-Allow-Header”添加* ,为“ Access-Control-Allow-Methods”添加POST 。 然后保存。

Image for post

You may wish to be more specific with these options as the wildcard * allows all possible options such as all possible origins accessing this API.

您可能希望对这些选项更加具体,因为通配符*允许所有可能的选项,例如访问此API的所有可能的来源。

Well that was another big section completed! Now the API is setup to receive predictions from your model. In the next section I will describe how to test the calls with Postman and use AWS CloudWatch to fix any ambiguous errors.

好吧,那是另一个很大的部分! 现在,已设置API以接收来自模型的预测。 在下一部分中,我将描述如何使用Postman测试呼叫并使用AWS CloudWatch修复任何模棱两可的错误。

使用Postman和CloudWatch进行测试 (Testing with Postman & CloudWatch)

Lets test the inference code of the model with Postman which is an API development platform and helpful for easily checking our new API. Install Postman for your OS if you haven’t already. Then create a new request and select POST as the type. In the URL tab set the URL I mentioned above.

让我们使用Postman测试模型的推理代码,Postman是API开发平台,有助于轻松检查我们的新API。 如果尚未安装Postman ,请为您的操作系统安装。 然后创建一个新请求并选择POST作为类型。 在“ URL”选项卡中,设置我上面提到的URL。

Image for post

You can change certain things about the request such as authorization or headers, although I did not change any of that from the default.

您可以更改有关请求的某些内容,例如授权或标头,尽管我没有更改默认值。

Next, we are have to actually input the body of the request. This will contain the data we want the model to use to make a prediction. I’ve found that the easiest way to do this is use the “raw” tab under “Body” for both JSON and CSV data. This will avoid any encoding of the data (unless you need that).

接下来,我们必须实际输入请求的主体。 这将包含我们希望模型用来进行预测的数据。 我发现最简单的方法是对JSON和CSV数据使用“正文”下的“原始”标签。 这将避免对数据进行任何编码(除非您需要)。

For a CSV body, paste the CSV data itself as a raw string. In the format of:

对于CSV正文,请将CSV数据本身粘贴为原始字符串。 格式为:

"First,Line\r\nSecond,Line\n\r......"

For a JSON body, write the JSON string. If the actual model input itself is still in CSV format then you can add a key such as “data” with the same value as above. Something like:

对于JSON正文,请编写JSON字符串。 如果实际模型输入本身仍为CSV格式,则可以添加具有与上述相同值的键,例如“ data” 。 就像是:

{"hyperparameter_1": #, "hyperparameter_2": #, "data": "First,Line\r\nSecond,Line\n\r......"}

Then click “Send”! Chances are something probably didn’t work. Make sure your endpoint is still up from SageMaker (the notebook itself does NOT need to be running). Lambda is connected to the proper endpoint (from the env variables). And API Gateway is connected to Lambda, with the proper URL for the request. If you get a “500 internal server error” or “ModelError” then something is amiss in your Docker image code OR lambda function code. This is where AWS CloudWatch comes in handy.

然后点击“发送”! 机会可能是行不通的。 确保您的端点仍从SageMaker启动(笔记本计算机本身不需要运行)。 Lambda连接到适当的端点(通过env变量)。 并且API网关已连接到Lambda,具有用于请求的正确URL。 如果收到“ 500内部服务器错误”或“ ModelError”,则说明您的Docker映像代码或lambda函数代码中有问题。 这是AWS CloudWatch派上用场的地方。

AWS CloudWatch (AWS CloudWatch)

The best way to figure out exactly what line of code is triggering an ambiguous error is to use AWS CloudWatch. I’m not gonna go into a lot of detail on CloudWatch but I will mention some helpful things. AWS CloudWatch allows you to monitor running AWS processes that are recorded in logs. For example SageMaker training jobs, notebook instances or endpoints can be tracked.

弄清楚究竟哪一行代码触发了模棱两可的错误,最好的方法是使用AWS CloudWatch。 我不会在CloudWatch上详细介绍,但我会提到一些有用的东西。 AWS CloudWatch允许您监视记录在日志中的正在运行的AWS流程。 例如,可以跟踪SageMaker培训作业,笔记本实例或端点。

To figure out what is triggering an error go to AWS CloudWatch and on the left sidebar go to “Log groups” under “Logs”. You should have some log groups created. First check your lambda function logs. Click on it and you will see a bunch of log streams.

要确定引发错误的原因,请转到AWS CloudWatch,然后在左侧栏中转到“日志”下的“日志组”。 您应该创建了一些日志组。 首先检查您的lambda函数日志。 单击它,您将看到一堆日志流。

Image for post

The top most one is the most recent so click that and check out the specific messages. If there are issues from this log group then your lambda function is to blame.

最热门的邮件是最新的邮件,因此请单击该邮件并查看特定的邮件。 如果此日志组中存在问题,则应归咎于您的lambda函数。

The other log group to check is the one attached to your endpoint. Any issue raised here indicates that something is incorrect in your Docker image code. Even if it ran fine locally there can be issues running it in AWS. Also there will be a lot of pings shown here, ignore them or try to filter through them.

要检查的另一个日志组是连接到端点的日志组。 此处出现的任何问题均表明您的Docker映像代码中有不正确的地方。 即使它在本地运行良好,在AWS中运行它也会出现问题。 此外,此处还会显示很多ping,请忽略它们或尝试对它们进行过滤。

Hopefully, its running smoothly now and you get your expected output, if not leave a comment and I will help fix it.

希望它现在运行平稳,如果没有发表任何意见,您将获得预期的输出,我会帮助修复它。

Finally, we have a working and accessible model. You can definitely stop there if all you need is the POST URL. However, another useful thing to do might be to make a nice looking front-end for people to input their own data into the model. An easy way to do this is to use S3 to create a static website to access the POST URL.

最后,我们有一个可行且可访问的模型。 如果您只需要POST URL,那么您绝对可以在这里停下来。 但是,另一有用的操作可能是使人们可以将自己的数据输入到模型中,从而使其外观漂亮。 一种简单的方法是使用S3创建一个静态网站来访问POST URL。

使用S3创建静态网站前端 (Creating a Static Website Front-end with S3)

This Medium article does a great job at describing step-by-step how to create a static website with S3. So instead of recreating this myself, check it out and go through all of its steps (Note: you don’t need to set up a CNAME record).

这篇中型文章在逐步描述如何使用S3创建静态网站方面做得很好。 因此,与其亲自重新创建它,不如将其签出并完成所有步骤(请注意:您无需设置CNAME记录)。

Once you are done with that the only thing I would recommend doing is adding a CORS policy by going to “Permissions” -> “CORS configuration” and pasting this:

完成此操作后,我唯一建议做的就是通过转到“权限”->“ CORS配置”并粘贴以下内容来添加CORS策略:

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedHeader>*</AllowedHeader>
<AllowedMethod>POST</AllowedMethod>
</CORSRule>
</CORSConfiguration>

which will deal with CORS issues the same way we dealt with it in API Gateway.

与我们在API网关中处理CORS的方式相同,它将处理CORS问题。

AJAX POST请求建模 (AJAX POST Request to Model)

Lastly, lets hook up the API call with AJAX in our website. The call is simple and will look something like this:

最后,让我们在我们的网站中使用AJAX连接API调用。 调用很简单,看起来像这样:

$.ajax({
    type: "POST",
    url: "https://a3bqc81dv1.execute-api.us-east-2.amazonaws.com/test/myLambdaFunction",
    data: JSON.stringify({'hyperparmeter_1': 0, 'hyperparameter_2': 0,'data': csv_text}),
    contentType: 'text/plain',
    success: function(success){
        console.log(success);
    },
    error: function(error){
        console.log(error)
    }
});

Note: Set the content type as text/plain to avoid it encoding your request data just like with Postman (unless you want that).

注意: 将内容类型设置为text / plain,以避免像Postman一样对请求数据进行编码(除非您希望这样做)。

That about wraps up the end-to-end development chain of deploying a custom model with AWS and setting it up with a nice user-facing front-end. If you want to make changes to your model code then re-make the Docker image and push it back to the ECR. Then redeploy. Also, delete the model endpoint when you don’t want it deployed either from the SageMaker console or from the notebook code.

内容涵盖了端到端开发链,该端到端开发链使用AWS部署自定义模型,并使用面向用户的漂亮前端对其进行设置。 如果要更改模型代码,请重新制作Docker映像并将其推回到ECR。 然后重新部署。 另外,当您不希望从SageMaker控制台或笔记本代码中部署模型端点时,请删除它。

我的应用程序-Google趋势数据的异常检测 (My Application — Anomaly Detection for Google Trends Data)

I created a custom model that takes google trends data (CSV) and analyses the plot for anomalies with the algorithm Density-Based Clustering for Applications with Noise (DBSCAN) or Seasonal-Trend decomposition with Loess (STL).

我创建了一个自定义模型,该模型采用了Google趋势数据(CSV),并使用基于密度的聚类算法对带噪声的应用程序(DBSCAN)或黄土的季节性趋势分解(STL)进行了异常图分析。

Image for post
Wikipedia 维基百科

Briefly, DBSCAN is a machine learning clustering algorithm but can be used for anomaly detection given that unlike K-means it does not cluster all points. These leftover or unclustered points can be considered outliers/anomalies.

简而言之, DBSCAN是一种机器学习聚类算法,但由于与K均值不同,它不能对所有点进行聚类,因此可以用于异常检测。 这些剩余或未聚类的点可以视为异常值/异常。

Image for post
machinelearningmastery 机器学习大师

STL is a statistical decomposition of time-series data for the purpose of detecting anomalies or exploring elements of the data. It decomposes the data into its seasonal pattern, trend and then residual. You can then use tests for outlier detection on the residual such as the Generalized Extreme Studentized Deviate (GESD) test. STL tends to work better than DBSCAN for data with a seasonal pattern, however if there is no seasonal pattern then STL cannot effectively be used.

STL是时间序列数据的统计分解,目的是检测异常或探究数据元素。 它将数据分解为季节性模式,趋势,然后分解为残差。 然后,您可以将测试用于残差的离群值检测,例如广义极端学生偏差(GESD)测试。 对于具有季节性模式的数据,STL往往比DBSCAN更好,但是,如果没有季节性模式,则无法有效地使用STL。

Check out my deployed model front-end here. Search a term on google trends and download the CSV:

在此处查看我部署的模型前端。 搜索有关Google趋势的字词并下载CSV:

Image for post

Then upload it on the website and fill in the hyperparameters given if there is seasonality to the data.

然后将其上传到网站上,如果数据存在季节性,则填写给定的超参数。

Image for post

Plot it, and it will look something like this:

绘制它,它将看起来像这样:

Image for post
Plot of Lakers popularity with anomalous points
得分异常的湖人人气图

Note: The website may not be up all the time

注意: 网站可能并非一直都在打开

Let me know if it's not up and you would like to check it out or if you have any problems with these steps in the comments! And check out the accompanying code here.

让我知道它是否还没有启动,您想检查一下,或者在注释中这些步骤是否有任何问题! 并在此处查看随附的代码。

翻译自: https://towardsdatascience.com/deploying-a-custom-docker-model-with-sagemaker-to-a-serverless-front-end-with-s3-8ee07edc24e6

前端代码部署到docker

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值