未知数据源 2024年10月02日
Reduce the time taken to deploy your models to Amazon SageMaker for testing
index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html

 

本文介绍了如何使用SageMaker Migration Toolkit将本地训练的模型部署到Amazon SageMaker,以简化模型部署过程。该工具库提供了高层抽象,使用户无需深入了解SageMaker API即可快速部署模型。它支持Scikit-learn、PyTorch和TensorFlow模型,并允许用户选择实时或无服务器端点进行部署。文章还详细介绍了模型部署的步骤,包括准备训练模型和推理脚本,以及如何使用不同的推理脚本函数(如model_fn、input_fn、predict_fn和output_fn)来定制模型的输入、输出和推理过程。

😊 **SageMaker Migration Toolkit** 是一款Python库,旨在简化将本地训练的模型部署到Amazon SageMaker的过程。它提供高层抽象,使用户无需深入了解SageMaker API即可快速部署模型。

🤩 **支持多种框架** 该库支持Scikit-learn、PyTorch和TensorFlow模型,并允许用户选择实时或无服务器端点进行部署。实时端点适合实时、交互式、低延迟的推理工作负载,而无服务器端点适合间歇性流量和容忍冷启动的工作负载。

🤯 **模型部署步骤** 部署模型到SageMaker需要准备训练模型和推理脚本。推理脚本通常包括四个函数:model_fn(加载模型)、input_fn(解析输入数据)、predict_fn(执行推理)和output_fn(处理输出数据)。这些函数可以根据不同的框架和模型类型进行调整,以满足特定的需求。

🧐 **定制推理脚本** 推理脚本允许用户定制模型的输入、输出和推理过程。例如,可以使用input_fn函数将输入数据转换为模型所需的格式,可以使用predict_fn函数执行模型推理,可以使用output_fn函数将模型输出转换为所需的格式。

🤔 **SageMaker优势** 通过将模型部署到SageMaker,用户可以利用云平台的可扩展性、专用训练工具(分布式训练、超参数调整)、实验管理、模型管理、偏差检测、模型可解释性等功能,简化机器学习生命周期的各个阶段。

🚀 **快速部署模型** SageMaker Migration Toolkit使数据科学家能够快速将模型部署到SageMaker,并充分利用云平台的优势。

✨ **灵活选择** 用户可以根据自己的需求选择不同的框架、计算类型和推理选项,以满足各种机器学习场景。

🎉 **简化部署过程** 该库简化了模型部署过程,使数据科学家能够专注于模型开发和训练,而不是繁琐的部署步骤。

🏆 **提高效率** 通过使用SageMaker Migration Toolkit,用户可以快速、轻松地将模型部署到云平台,提高机器学习项目的开发效率。

🤖 **未来发展** 该库将继续发展,以支持更多框架和功能,进一步简化模型部署过程。

🎉 **结论** SageMaker Migration Toolkit是一个强大的工具,能够简化将本地训练的模型部署到SageMaker的过程,为数据科学家提供了一种快速、轻松、灵活的解决方案。

<p>Data scientists often train their models locally and look for a proper hosting service to deploy their models. Unfortunately, there’s no one set mechanism or guide to deploying pre-trained models to the cloud. In this post, we look at deploying trained models to <a href="https://aws.amazon.com/sagemaker/&quot; target="_blank" rel="noopener noreferrer">Amazon SageMaker</a> hosting to reduce your deployment time.</p><p>SageMaker is a fully managed machine learning (ML) service. With SageMaker, you can quickly build and train ML models and directly deploy them into a production-ready hosted environment. Additionally, you don’t need to manage servers. You get an integrated Jupyter notebook environment with easy access to your data sources. You can perform data analysis, train your models, and test them using your own algorithms or use SageMaker-provided ML algorithms that are optimized to run efficiently against large datasets spread across multiple machines. Training and hosting are billed by minutes of usage, with no minimum fees and no upfront commitments.</p><h2>Solution overview</h2><p>Data scientists sometimes train models locally using their IDE and either ship those models to the ML engineering team for deployment or just run predictions locally on powerful machines. In this post, we introduce a Python library that simplifies the process of deploying models to SageMaker for <a href="https://docs.aws.amazon.com/sagemaker/latest/dg/deploy-model.html&quot; target="_blank" rel="noopener noreferrer">hosting</a> on real-time or serverless endpoints.</p><p>This Python library gives data scientists a simple interface to quickly get started on SageMaker without needing to know any of the low-level SageMaker functionality.</p><p>If you have models trained locally using your preferred IDE and want to benefit from the scale of the cloud, you can use this library to deploy your model to SageMaker. With SageMaker, in addition to all the scaling benefits of a cloud-based ML platform, you have access to purpose-built training tools (distributed training, hyperparameter tuning), experiment management, model management, bias detection, model explainability, and many other capabilities that can help you in any aspect of the ML lifecycle. You can choose from the three most popular frameworks for ML: Scikit-learn, PyTorch, and TensorFlow, and can pick the type of compute you want. Defaults are provided along the way so users of this library can deploy their models without needing to make complex decisions or learn new concepts. In this post, we show you how to get started with this library and optimize deploying your ML models on SageMaker hosting.</p><p>The library can be found in the <a href="https://github.com/aws-samples/sagemaker-migration-toolkit&quot; target="_blank" rel="noopener noreferrer">GitHub repository</a>.</p><h2>The SageMaker Migration Toolkit</h2><p>The <code>SageMakerMigration</code> class is available through a Python library published to GitHub. Instructions to install this library are provided in the repository; make sure that you follow the README to properly set up your environment. After you install this library, the rest of this post talks about how you can use it.</p><p>The <code>SageMakerMigration</code> class consists of high-level abstractions over SageMaker APIs that significantly reduce the steps needed to deploy your model to SageMaker, as illustrated in the following figure. This is intended for experimentation so developers can quickly get started and test SageMaker. It is not intended for production migrations.</p><p><a href="https://d2908q01vomqb2.cloudfront.net/f1f836cb4ea6efb2a0b1b99f41ad8b103eff4b59/2022/09/12/ML-10462-image001.jpg&quot;&gt;&lt;img class="alignnone size-full wp-image-42338" src="https://d2908q01vomqb2.cloudfront.net/f1f836cb4ea6efb2a0b1b99f41ad8b103eff4b59/2022/09/12/ML-10462-image001.jpg&quot; alt="" width="1287" height="720" /></a></p><p>For Scikit-learn, PyTorch, and TensorFlow models, this library supports deploying trained models to a SageMaker real-time endpoint or serverless endpoint. To learn more about the inference options in SageMaker, refer to <a href="https://docs.aws.amazon.com/sagemaker/latest/dg/deploy-model.html&quot; target="_blank" rel="noopener noreferrer">Deploy Models for Inference</a>.</p><h2>Real-time vs. serverless endpoints</h2><p>Real-time inference is ideal for inference workloads where you have real-time, interactive, low latency requirements. You can deploy your model to SageMaker hosting services and get an endpoint that can be used for inference. These endpoints are fully managed and support auto scaling.</p><p>SageMaker Serverless Inference is a purpose-built inference option that makes it easy for you to deploy and scale ML models. Serverless Inference is ideal for workloads that have idle periods between traffic spurts and can tolerate cold starts. Serverless endpoints automatically launch compute resources and scale them in and out depending on traffic, eliminating the need to choose instance types or manage scaling policies. This takes away the undifferentiated heavy lifting of selecting and managing servers.</p><p>Depending on your use case, you may want to quickly host your model on SageMaker without actually having an instance always on and incurring costs, in which case a serverless endpoint is a great solution.</p><h2>Prepare your trained model and inference script</h2><p>After you’ve identified the model you want to deploy on SageMaker, you must ensure the model is presented to SageMaker in the right format. SageMaker endpoints generally consist of two components: the trained model artifact (.pth, .pkl, and so on) and an inference script. The inference script is not always mandatory, but if not provided, the default handlers for the serving container that you’re using are applied. It’s essential to provide this script if you need to customize your input/output functionality for inference.</p><p>The trained model artifact is simply a saved Scikit-learn, PyTorch, or TensorFlow model. For Scikit-learn, this is typically a pickle file, for PyTorch this is a .pt or .pth file, and for TensorFlow this is a folder with assets, .pb files, and other variables.</p><p>Generally, you need to able to control how your model processes input and performs inference, and control the output format for your response. With SageMaker, you can provide an <a href="https://docs.aws.amazon.com/sagemaker/latest/dg/adapt-inference-container.html&quot; target="_blank" rel="noopener noreferrer">inference script</a> to add this customization. Any inference script used by SageMaker must have one or more of the following four handler functions: <code>model_fn</code>, <code>input_fn</code>, <code>predict_fn</code>, and <code>output_fn</code>.</p><p>Note that these four functions apply to <a href="https://github.com/aws/sagemaker-pytorch-inference-toolkit&quot; target="_blank" rel="noopener noreferrer">PyTorch</a> and <a href="https://github.com/aws/sagemaker-scikit-learn-container&quot; target="_blank" rel="noopener noreferrer">Scikit-learn</a> containers specifically. TensorFlow has slightly different handlers because it’s integrated with <a href="https://www.tensorflow.org/tfx/guide/serving&quot; target="_blank" rel="noopener noreferrer">TensorFlow Serving</a>. For an inference script with TensorFlow, you have two <a href="https://github.com/aws/sagemaker-tensorflow-serving-container&quot; target="_blank" rel="noopener noreferrer">model handlers</a>: input_handler and output_handler. Again, these have the same preprocessing and postprocessing purpose that you can work with, but they’re configured slightly differently to integrate with <a href="https://github.com/aws/sagemaker-tensorflow-serving-container&quot; target="_blank" rel="noopener noreferrer">TensorFlow Serving</a>. For PyTorch models, model_fn is a compulsory function to have in the inference script.</p><h3>model_fn</h3><p>This is the function that is first called when you invoke your SageMaker endpoint. This is where you write your code to load the model. For example:</p><p>Depending on the framework and type of model, this code may change, but the function must return an initialized model.</p><h3>input_fn</h3><p>This is the second function that is called when your endpoint is invoked. This function takes the data sent to the endpoint for inference and parses it into the format required for the model to generate a prediction. For example:</p><p>The <code>request_body</code> contains the data to be used for generating inference from the model and is parsed in this function so that it’s in the required format.</p><h3>predict_fn</h3><p>This is the third function that is called when your model is invoked. This function takes the preprocessed input data returned from <code>input_fn</code> and uses the model returned from <code>model_fn</code> to make the prediction. For example:</p><p>You can optionally add <code>output_fn</code> to parse the output of <code>predict_fn</code> before returning it to the client. The function signature is <code>def output_fn(prediction, content_type)</code>.</p><h2>Move your pre-trained model to SageMaker</h2><p>After you have your trained model file and inference script, you must put these files in a folder as follows:</p><p>After your model and inference script have been prepared and saved in this folder structure, your model is ready for deployment on SageMaker. See the following code:</p><p>After deployment of your endpoint, make sure to clean up any resources you won’t utilize via the SageMaker console or through the <a href="https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sagemaker.html#SageMaker.Client.delete_endpoint&quot; target="_blank" rel="noopener noreferrer">delete_endpoint</a> Boto3 API call.</p><h2>Conclusion</h2><p>The goal of the SageMaker Migration Toolkit project is to make it easy for data scientists to onboard their models onto SageMaker to take advantage of cloud-based inference. The repository will continue to evolve and support more options for migrating workloads to SageMaker. The code is open source and we welcome community contributions through pull requests and issues.</p><p>Check out the <a href="https://github.com/aws-samples/sagemaker-migration-toolkit&quot; target="_blank" rel="noopener noreferrer">GitHub repository</a> to explore more on utilizing the SageMaker Migration Toolkit, and feel free to also contribute examples or feature requests to add to the project!</p><h3>About the authors</h3><p class="c4"><img class="size-full wp-image-27201 alignleft" src="https://d2908q01vomqb2.cloudfront.net/f1f836cb4ea6efb2a0b1b99f41ad8b103eff4b59/2021/08/12/Kirit-Thadaka.jpg&quot; alt="" width="100" height="133" /><strong>Kirit Thadaka</strong> is an ML Solutions Architect working in the Amazon SageMaker Service SA team. Prior to joining AWS, Kirit spent time working in early stage AI startups followed by some time in consulting in various roles in AI research, MLOps, and technical leadership.</p><p class="c4"><strong><img class="size-full wp-image-28445 alignleft" src="https://d2908q01vomqb2.cloudfront.net/f1f836cb4ea6efb2a0b1b99f41ad8b103eff4b59/2021/09/23/Ram-Vegiraju.jpg&quot; alt="" width="99" height="115" />Ram Vegiraju</strong> is a ML Architect with the SageMaker Service team. He focuses on helping customers build and optimize their AI/ML solutions on Amazon SageMaker. In his spare time, he loves traveling and writing.</p>

Fish AI Reader

Fish AI Reader

AI辅助创作,多种专业模板,深度分析,高质量内容生成。从观点提取到深度思考,FishAI为您提供全方位的创作支持。新版本引入自定义参数,让您的创作更加个性化和精准。

FishAI

FishAI

鱼阅,AI 时代的下一个智能信息助手,助你摆脱信息焦虑

联系邮箱 441953276@qq.com

相关标签

SageMaker 模型部署 机器学习 云计算 人工智能
相关文章