{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Host a custom built BYOC inference image \n", "- Choose Python3 Data Science kernel in SageMaker Studio \n", "- Create a dummy model.tar.gz with nothing in it, and upload it to S3(Make sure SageMaker role has access to the bucket)\n", "- Use the script in the folder to built a docker image and push it to Amazon ECR\n", "- Run following code to create a SageMaker model with the dummy model file\n", "- A SageMaker endpoint will be created based on the MXNet model file" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import sagemaker\n", "from sagemaker.mxnet import MXNetModel\n", "from time import gmtime, strftime\n", "bucket_name = 'S3_BUCKET_NAME'\n", "s3_prefix = 'MODEL_FILE_KEY_PREFIX'\n", "model = MXNetModel(\n", " entry_point='entrypoint.py',\n", " model_data='s3://{}/{}/model.tar.gz'.format(bucket_name, s3_prefix),\n", " framework_version='1.6.0',\n", " py_version='py3',\n", " source_dir='inference_src',#'source_directory'\n", " image_uri='ACCOUNT.dkr.ecr.AWS_REGION.amazonaws.com/IMAGE_NAME',\n", " role=sagemaker.get_execution_role(),\n", " name='audio-embedding-model'+ strftime('%Y-%m-%d-%H-%M-%S', gmtime())\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ep_name = 'audio-embedding-endpoint'+ strftime('%Y-%m-%d-%H-%M-%S', gmtime())\n", "predictor = model.deploy(initial_instance_count=1, \n", " instance_type='ml.m5.xlarge',\n", " endpoint_name=ep_name)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Test the endpoint" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import boto3\n", "import json\n", "client = boto3.client('sagemaker-runtime')\n", "test_image = 'test_audio.mp3'\n", "img = open(test_image, 'rb').read()\n", "response = client.invoke_endpoint(\n", " EndpointName=ep_name,\n", " Body=img,\n", " ContentType='application/x-image')\n", "\n", "result = response[\"Body\"].read().decode(\"utf-8\")\n", "detections = json.loads(result)\n", "print (detections)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "boto3.client('sagemaker').delete_endpoint(EndpointName=ep_name)" ] } ], "metadata": { "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.10" } }, "nbformat": 4, "nbformat_minor": 4 }