{ "cells": [ { "cell_type": "markdown", "id": "1540e9a4-128d-4a8b-b56c-450d2f7f44eb", "metadata": {}, "source": [ "## Notebook 2 - Invoke SageMaker Inference Endpoint" ] }, { "cell_type": "markdown", "id": "d889b717-a81a-4068-9c94-b0c18092ab84", "metadata": {}, "source": [ "## Track and wait for the notebook 'STEP1_create_pipeline_k8s_sagemaker.ipynb' to complete. After it completes, invoke the SageMaker Endpoint using sample image " ] }, { "cell_type": "code", "execution_count": null, "id": "a6ec76cf-4329-4009-9fa5-acc8456632b7", "metadata": {}, "outputs": [], "source": [ "#Read a sample input image\n", "import json, boto3, numpy as np\n", "client = boto3.client('runtime.sagemaker')\n", "\n", "file_name = '1000_dog.png'\n", "with open(file_name, 'rb') as f:\n", " payload = f.read()" ] }, { "cell_type": "markdown", "id": "4fc2e096-4afc-4b4a-826e-33b8f39add5e", "metadata": {}, "source": [ "## Ensure you update the EndpointName" ] }, { "cell_type": "code", "execution_count": null, "id": "297da926-cd3f-4519-85be-5b4ae09e35f0", "metadata": {}, "outputs": [], "source": [ "# Invoke SageMaker Endpoint. * Ensure you update the endpoint\n", "# You can grab the SageMaker Endpoint name by either 1) going to the pipeline visualization of Kubeflow console and click the component for deployment, or 2) Go to SageMaker console and go to the list of endpoints, and then substitute the name to the EndpointName='...' in this cell.\n", "\n", "response = client.invoke_endpoint(EndpointName='Endpoint-cnn-dist-job-2022-10-05-14-55-15-278', \n", " ContentType='application/x-image', \n", " Body=payload)\n", "\n", "pred = json.loads(response['Body'].read().decode())\n", "\n", "output_vector_list=pred['score']\n", "\n", "# Get outout vector of 10 classes \n", "output_vector = output_vector_list[0]\n", "\n", "# Find the class with highest probability\n", "max=output_vector[0]\n", "index = 0\n", "for i in range(1,len(output_vector)):\n", " if output_vector[i] > max:\n", " max = output_vector[i]\n", " index = i\n", " \n", "print(f'Index of the maximum value is : {index}') \n", "\n", "labels = ['airplane','automobile','bird','cat','deer','dog','frog','horse','ship','truck']\n", "\n", "print(labels[index])" ] }, { "cell_type": "code", "execution_count": null, "id": "847c1535-23f7-402e-b70a-61fe66697b5a", "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 }