简体   繁体   English

具有不受支持的内置算法的 Sagemaker 多模型端点

[英]Sagemaker multi-model endpoints with unsupported built-in algorithms

I am aware that Sagemaker does not support multi-model endpoints for their built-in image classification algorithm .我知道 Sagemaker 的内置图像分类算法不支持多模型端点 However, in the documentation they hint at building a custom container to use "any other framework or algorithm" with the multi-model endpoint functionality:但是,在文档中,他们暗示构建自定义容器以使用具有多模型端点功能的“任何其他框架或算法”:

To use any other framework or algorithm, use the SageMaker inference toolkit to build a container that supports multi-model endpoints.要使用任何其他框架或算法,请使用 SageMaker 推理工具包构建支持多模型端点的容器。 For information, see Build Your Own Container with Multi Model Server .有关信息,请参阅使用多 Model 服务器构建您自己的容器

Ideally, I would like to deploy many (20+) image classification models I have already trained to a single endpoint to save on costs.理想情况下,我想将我已经训练过的许多(20 多个)图像分类模型部署到单个端点以节省成本。 However, after reading the "Build Your Own Container" guide it is still not exactly clear to me how to build a custom inference container for the models produced by a non-custom algorithm.但是,在阅读了“构建您自己的容器”指南之后,我仍然不清楚如何为非自定义算法生成的模型构建自定义推理容器。 Most of the tutorials and example notebooks refer to using Pytorch or Sklearn.大多数教程和示例笔记本都提到使用 Pytorch 或 Sklearn。 It is not clear to me that I could make inferences using these libraries on the models I've created with the built-in image classification algorithm.我不清楚我是否可以在使用内置图像分类算法创建的模型上使用这些库进行推断。

Is it possible to create a container to support multi-model endpoints for unsupported built-in Sagemaker algorithms?是否可以创建一个容器来支持不受支持的内置 Sagemaker 算法的多模型端点? If so, would somebody be able to hint at how this might be done?如果是这样,是否有人能够暗示如何做到这一点?

yes, it is possible to deploy the built in image classification models as a SageMaker multi model endpoint.是的,可以将内置图像分类模型部署为 SageMaker 多 model 端点。 The key is that the image classification uses Apache MXNet .关键是图像分类使用了 Apache MXNet You can extract the model artifacts (SageMaker stores them in a zip file named model.tar.gz in S3), then load them in to MXNet.您可以提取 model 工件(SageMaker 将它们存储在 S3 中名为 model.tar.gz 的 zip 文件中),然后将它们加载到 MXNet。 The SageMaker MXNet container supports multi model endpoints, so you can use that to deploy the model. SageMaker MXNet 容器支持多个 model 端点,因此您可以使用它来部署 model。

If you unzip the model.tar.gz from this algorithm, you'll find three files:如果你从这个算法中解压 model.tar.gz,你会发现三个文件:

image-classification-****.params图像分类-****.params

image-classification-symbol.json图像分类符号.json

model-shapes.json模型形状.json

The MxNet container expects these files to be named image-classification-0000.params, model-symbol.json, and model-shapes.json . MxNet 容器需要将这些文件命名为image-classification-0000.params、model-symbol.json 和 model-shapes.json So I unzipped the zip file, renamed the files and rezipped them.所以我解压缩了 zip 文件,重命名文件并重新压缩它们。 For more information on the MXNet container check out the GitHub repository .有关 MXNet 容器的更多信息,请查看GitHub 存储库

After that you can deploy the model as a single MXNet endpoint using the SageMaker SDK with the following code:之后,您可以使用 SageMaker SDK 和以下代码将 model 部署为单个 MXNet 端点:

from sagemaker import get_execution_role
from sagemaker.mxnet.model import MXNetModel

role = get_execution_role()

mxnet_model = MXNetModel(model_data=s3_model, role=role, 
                         entry_point='built_in_image_classifier.py', 
                         framework_version='1.4.1',
                         py_version='py3')

predictor = mxnet_model.deploy(instance_type='ml.c4.xlarge', initial_instance_count=1)

The entry point Python script can be an empty Python file for now.入口点 Python 脚本现在可以是一个空的 Python 文件。 We will be using the default inference handling provided by the MXNet container.我们将使用 MXNet 容器提供的默认推理处理。

The default MXNet container only accepts JSON, CSV, and Numpy arrays as valid input.默认 MXNet 容器仅接受 JSON、CSV 和 Numpy ZA3CBC3F9D0CE2F2C1554E1B6D 作为有效输入。 So you will have to format your input in to one of these three formats.因此,您必须将输入格式化为这三种格式之一。 The code below demonstrates how I did it with Numpy arrays:下面的代码演示了我是如何使用 Numpy arrays 做到的:

import cv2
import io

np_array = cv2.imread(filename=img_filename)
np_array = np_array.transpose((2,0,1))
np_array = np.expand_dims(np_array, axis=0)

buffer = io.BytesIO()
np.save(buffer, np_array)

response = sm.invoke_endpoint(EndpointName='Your_Endpoint_name', Body=buffer.getvalue(), ContentType='application/x-npy')

Once you have a single endpoint working with MXNet container, you should be able to get it running in multi model endpoint using the SageMaker MultiDataModel constructor .一旦您有一个使用 MXNet 容器的端点,您应该能够使用SageMaker MultiDataModel 构造函数在多个 model 端点中运行它。

If you want to use a different input data type so you don't have to do the preprocessing in your application code, you can overwrite the input_fn method in the MxNet container by providing it in the entry_point script.如果您想使用不同的输入数据类型以便不必在应用程序代码中进行预处理,您可以通过在 entry_point 脚本中提供它来覆盖 MxNet 容器中的 input_fn 方法。 See here for more information. 请参阅此处了解更多信息。 If you do this, you could pass the image bytes directly to SageMaker, without formatting the numpy arrays.如果这样做,您可以将图像字节直接传递给 SageMaker,而无需格式化 numpy arrays。

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

相关问题 带有 Scikit Learn 的 AWS Sagemaker 多模型终端节点:使用训练脚本时出现 UnexpectedStatusException - AWS Sagemaker Multi-Model Endpoint with Scikit Learn: UnexpectedStatusException whilst using a training script 在Django中创建多模型 - Creating a multi-model in Django 通过 Sagemaker 的 XGBoost 内置算法和训练容器拟合 model 进行预测的数据格式 - data format to predict with model fitted via Sagemaker's XGBoost built-in algorithm and training container Django中复杂多模型查询的优化 - Optimization of complex multi-model query in Django Django 中的多模型 RSS 提要 - Multi-model RSS Feed in Django 如何从 AWS Sagemaker 内置容器加载 model 构件? - How do I load the model artifact from AWS Sagemaker built-in container? 机器学习中多模型融合的输入缺失如何处理? - How to deal with the input lack of multi-model fusion in machine learning? Django链接到现有项目,或者以多模型形式创建(如果唯一) - Django link to existing item or create if unique in multi-model form 使用 Python SDK 的内置算法的 Amazon SageMaker 超参数调整错误 - Amazon SageMaker hyperparameter tuning error for built-in algorithm using the Python SDK Python中的多参数null cowellce和内置“or”函数 - Multi-argument null coalesce and built-in “or” function in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM