简体   繁体   English

如何将 .tar.gz 格式的模型存储到 AWS SageMaker 并将其用作已部署的模型?

[英]How to store a .tar.gz formatted model to AWS SageMaker and use it as a deployed model?

I have a pre-trained BERT model which was trained on Google Cloud Platform, and the model is stored in a .tar.gz formatted file, I wanted to deploy this model to SageMaker and also be able to trigger the model via API, how can I achieve this?我有一个在 Google Cloud Platform 上训练过的预训练 BERT 模型,该模型存储在 .tar.gz 格式的文件中,我想将此模型部署到 SageMaker 并且还能够通过 API 触发模型,如何我能做到吗?

I found this question is a little bit related to what I'm asking here, but it's for a scikit-learn model, I'm new to this area, can someone give me some guidance regarding this?我发现这个问题与我在这里提出的问题有点相关,但它是针对 scikit-learn 模型的,我是这个领域的新手,有人能给我一些指导吗? Many thanks.非常感谢。

To use an existing model object this just needs to be packaged in a .tar.gz and stored in S3.要使用现有的模型对象,只需将其打包在 .tar.gz 中并存储在 S3 中。

Once that is packaged then you can deploy it using the sage maker API through sage maker notebooks \\studio or terraform.打包后,您可以使用 sage maker API 通过 sage maker notebooks \\studio 或 terraform 部署它。

Packaging the code打包代码

File Structure文件结构

Here is the structure of what the .tar.gz should follow.这是 .tar.gz 应该遵循的结构。

|----- preprocessing.joblib # multiple model objects included here
|----- model.joblib
|----- code
    |----- inference.py # used for custom transformation \ loading model etc. 
    |----- requirements.txt # can be ran to include dependencies not in the docker image

Zipping your files压缩文件

ex.前任。

tar czvf model.tar.gz. model_xgb.joblib preprocessor.joblib code

Deploying Endpoint部署端点

Once you have the model packaged then you can create an endpoint by loading the model into one of the model classes and then deploying it.打包模型后,您可以通过将模型加载到模型类之一然后部署它来创建端点。

from sagemaker.amazon.amazon_estimator import get_image_uri
from sagemaker.model import Model
from sagemaker.pipeline import PipelineModel

container=sagemaker.image_uris.retrieve("xgboost", boto3.Session().region_name, "0.90-1")
print(container)

model_name = 'some_model_123'
model_location= "s3://somebucket/somemodelpath/someversion/model.tar.gz"

env = {
    'SAGEMAKER_REQUIREMENTS': 'requirements.txt',
    'SAGEMAKER_PROGRAM': 'inference.py',
    'SAGEMAKER_SUBMIT_DIRECTORY' : model_location
    }

model= Model(
    model_data=model_location, 
    image_uri =container,
    env = env
)

endpoint_name = model_name

pipeline_model = PipelineModel(name=model_name,
                               role=role,
                               models=[
                                    model
                               ])

pm = pipeline_model.deploy(initial_instance_count=1, instance_type="ml.c4.xlarge", endpoint_name=endpoint_name)

References参考

Here is a reference of a bring your own model which helped me a bunch.这是一个自带模型的参考,它帮助了我很多。 https://medium.com/geekculture/84af8989d065 https://medium.com/geekculture/84af8989d065

Here is a reference to a similar question that I posted while struggling with as well.这是对我在挣扎时发布的类似问题的参考。 How to handle custom transformation/ inference and requirements in sagemaker endpoints 如何处理 sagemaker 端点中的自定义转换/推理和要求

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM