{ "cells": [ { "cell_type": "markdown", "id": "1540e9a4-128d-4a8b-b56c-450d2f7f44eb", "metadata": {}, "source": [ "# Notebook - Invoke SageMaker Inference Endpoint" ] }, { "cell_type": "markdown", "id": "d889b717-a81a-4068-9c94-b0c18092ab84", "metadata": {}, "source": [ "**NOTICE: Before running this notebook, please track and wait for the run of the pipeline created by the accompanied notebook `kfp_with_sagemaker_training_s3.ipynb` to complete. Once it completes, use\n", "this notebook to involke a SageMaker Endpoint and submit a sample image for inference** \n" ] }, { "cell_type": "markdown", "id": "0e4c35a6-c776-46c9-9bae-e4eb484d473b", "metadata": {}, "source": [ "Before running this notebok, \n", "- Make sure that a sample image is uploaded to the notebook instance (below `1000_dog.png` is put in the folder where this notebook is located).\n", "- Update SageMaker Endpoint name below (`Endpoint-cnn-dist-job-2023-05-04-05-47-05-124` for example). There are two ways to retrieve the Endpoint name: \n", " 1. In the left pane of SageMaker Console, go to `Inference > Endpoint`\n", " 2. Go to the graph visualization of the run of the pipeline, click the component for `SageMaker - Deploy Model`, and go to `Input/Output > endpoint_name`" ] }, { "cell_type": "markdown", "id": "c19c19ae-285f-4566-8552-c5151e944197", "metadata": {}, "source": [ "## Preparation" ] }, { "cell_type": "code", "execution_count": null, "id": "9e8ed44e-6959-4b6b-8f2a-557ac143bdf0", "metadata": {}, "outputs": [], "source": [ "import json\n", "import boto3\n", "import numpy as np\n" ] }, { "cell_type": "code", "execution_count": null, "id": "a6ec76cf-4329-4009-9fa5-acc8456632b7", "metadata": {}, "outputs": [], "source": [ "# create a boto3 client for SageMaker\n", "client = boto3.client('runtime.sagemaker')\n" ] }, { "cell_type": "code", "execution_count": null, "id": "fbd42f21-8c71-4156-b41e-f8ab8d187d79", "metadata": {}, "outputs": [], "source": [ "# load a sample image\n", "file_name = '1000_dog.png'\n", "with open(file_name, 'rb') as f:\n", " payload = f.read()\n" ] }, { "cell_type": "code", "execution_count": null, "id": "c17d6599-64ab-4319-9c99-9febf9c9991d", "metadata": {}, "outputs": [], "source": [ "# map between label and index for CIFAR-10 (index 1 corresponds to 'automobile')\n", "labels = [\n", " 'airplane',\n", " 'automobile',\n", " 'bird',\n", " 'cat',\n", " 'deer',\n", " 'dog',\n", " 'frog',\n", " 'horse',\n", " 'ship',\n", " 'truck'\n", "]\n" ] }, { "cell_type": "markdown", "id": "0e801a31-5487-4098-a9f5-d8774ae9525d", "metadata": {}, "source": [ "## Invole SageMaker Endpoint for inference" ] }, { "cell_type": "code", "execution_count": null, "id": "297da926-cd3f-4519-85be-5b4ae09e35f0", "metadata": {}, "outputs": [], "source": [ "# NOTE: ensure you update the endpoint name\n", "endpoint_name = 'Endpoint-cnn-dist-job-2023-05-04-05-47-05-124'\n", "\n", "response = client.invoke_endpoint(\n", " EndpointName=endpoint_name, \n", " ContentType='application/x-image', \n", " Body=payload\n", ")\n" ] }, { "cell_type": "code", "execution_count": null, "id": "847c1535-23f7-402e-b70a-61fe66697b5a", "metadata": {}, "outputs": [], "source": [ "# show the prediction result\n", "\n", "pred_raw = json.loads(response['Body'].read().decode())\n", "pred_scores = pred_raw['score'][0]\n", "pred_index = np.argmax(pred_scores)\n", "pred_label = labels[pred_index]\n", "\n", "print(f\"prediction result: index {pred_index}, label {pred_label}\")\n" ] }, { "cell_type": "markdown", "id": "a0a0da04-6d66-44d0-90b1-4c2df38be438", "metadata": {}, "source": [ "### Delete Endpoint" ] }, { "cell_type": "code", "execution_count": null, "id": "75520f74-52e6-4cfe-ab19-d00adc3f8269", "metadata": {}, "outputs": [], "source": [ "# # delete endpoint\n", "# sagemaker_client = boto3.client('sagemaker')\n", "# sagemaker_client.delete_endpoint(EndpointName=endpoint_name)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "6f31fd76-6a6f-4417-8b1c-b2db656927e0", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "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.8.13" } }, "nbformat": 4, "nbformat_minor": 5 }