{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Download YOLOv3 and SimplePose model from MXNET model zoo\n", "- Choose Data Science kernel and ml.t3.medium instance\n", "- Run following code to have models download\n", "- The downloaded models are saved in ~/.mxnet/models folder. You should be able to see them from the Image Terminal\n", "- Go into the ~/.mxnet/models folder and use following command to create a tar ball file including these two models \n", "`\n", "cd ~/.mxnet/models\n", "tar -cvzf model.tar.gz simple_pose_resnet18_v1b-f63d42ac.params yolo3_mobilenet1.0_coco-66dbbae6.params\n", "`\n", "- Upload the model.tar.gz file into an S3 bucket with following command. Modify S3_BUCKET_NAME and MODEL_FILE_KEY_PREFIX to your own choice. \n", "`aws s3 cp model.tar.gz s3://{S3_BUCKET_NAME}/{MODEL_FILE_KEY_PREFIX}/model.tar.gz`\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from gluoncv import model_zoo, data, utils\n", "detector = model_zoo.get_model('yolo3_mobilenet1.0_coco', pretrained=True)\n", "pose_net = model_zoo.get_model('simple_pose_resnet18_v1b', pretrained=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Build with original MXNet Docker Image\n", "- Run following code to create a SageMaker model with the 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 numpy as np\n", "import json\n", "import boto3\n", "from time import gmtime, strftime\n", "client = boto3.client('sagemaker-runtime')\n", "import sagemaker\n", "from sagemaker.mxnet import MXNetModel\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',\n", " role=sagemaker.get_execution_role(),\n", " name='Extract-pose-keypoints-yolo3-simplepose-'+ strftime('%Y-%m-%d-%H-%M-%S', gmtime())\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ep_name = 'Extract-pose-keypoints-yolo3-simplepose-'+ strftime('%Y-%m-%d-%H-%M-%S', gmtime())\n", "predictor = model.deploy(initial_instance_count=1, \n", " instance_type='ml.m5.4xlarge',\n", " endpoint_name=ep_name)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Inference sample code" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "# Test with SageMaker runtime client\n", "test_image = 'images/soccer.png'\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": { "scrolled": true }, "outputs": [], "source": [ "# Test with SageMaker predictor\n", "from sagemaker.predictor import Predictor\n", "\n", "object_detector = Predictor(endpoint_name=ep_name)\n", "results = object_detector.predict(img,initial_args={\"ContentType\": \"image/jpeg\"})\n", "detections = json.loads(results)\n", "detections" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Show keypoints" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from matplotlib import pyplot as plt\n", "img = plt.imread(test_image)\n", "print(img.shape)\n", "fig,ax = plt.subplots(1, dpi=120)\n", "ax.imshow(img)\n", "for dots in detections['1']['qcresult']:\n", " for idx, dot in enumerate(dots):\n", " if idx==0: continue\n", " x,y,xb,yb = dot\n", " x = x*img.shape[1]\n", " y = y*img.shape[0]\n", " \n", " rect = plt.plot(x,y,'b.')\n", "ax.axis('off')\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Delete the endpoint after use" ] }, { "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 }