diff --git a/program/cohort.ipynb b/program/cohort.ipynb index aa9ae9d..8cb0cc9 100644 --- a/program/cohort.ipynb +++ b/program/cohort.ipynb @@ -5396,7 +5396,7 @@ }, { "cell_type": "code", - "execution_count": 241, + "execution_count": 289, "id": "a1f25973", "metadata": {}, "outputs": [], @@ -5415,7 +5415,7 @@ }, { "cell_type": "code", - "execution_count": 242, + "execution_count": 290, "id": "c480ae02", "metadata": { "tags": [ @@ -5508,8 +5508,7 @@ " \"\"\"\n", "\n", " try:\n", - " response = model.transform(input_data)\n", - " return response\n", + " return model.transform(input_data)\n", " except ValueError as e:\n", " print(\"Error transforming the input data\", e)\n", " return None\n", @@ -5546,7 +5545,7 @@ }, { "cell_type": "code", - "execution_count": 243, + "execution_count": 291, "id": "34721e63", "metadata": { "tags": [ @@ -5701,7 +5700,7 @@ }, { "cell_type": "code", - "execution_count": 244, + "execution_count": 292, "id": "d23798aa", "metadata": { "tags": [ @@ -5748,8 +5747,7 @@ "\n", "def input_fn(input_data, content_type):\n", " if content_type == \"application/json\":\n", - " predictions = json.loads(input_data)[\"predictions\"]\n", - " return predictions\n", + " return json.loads(input_data)[\"predictions\"]\n", " \n", " raise ValueError(f\"{content_type} is not supported.\")\n", "\n", @@ -5802,7 +5800,7 @@ }, { "cell_type": "code", - "execution_count": 245, + "execution_count": 293, "id": "965a6be5", "metadata": { "tags": [ @@ -5884,7 +5882,7 @@ }, { "cell_type": "code", - "execution_count": 246, + "execution_count": 294, "id": "82dafcd0", "metadata": {}, "outputs": [], @@ -5910,7 +5908,7 @@ }, { "cell_type": "code", - "execution_count": 247, + "execution_count": 295, "id": "8c7dda7a", "metadata": {}, "outputs": [], @@ -5937,7 +5935,7 @@ }, { "cell_type": "code", - "execution_count": 248, + "execution_count": 296, "id": "6c31090e", "metadata": {}, "outputs": [], @@ -5962,7 +5960,7 @@ }, { "cell_type": "code", - "execution_count": 249, + "execution_count": 297, "id": "9fa95ab8", "metadata": {}, "outputs": [], @@ -5989,7 +5987,7 @@ }, { "cell_type": "code", - "execution_count": 250, + "execution_count": 298, "id": "7c052332", "metadata": {}, "outputs": [], @@ -6156,14 +6154,14 @@ "id": "7dfe7356-53e8-4ac1-9a7f-3bd51bb739a5", "metadata": {}, "source": [ - "### Step 8 - Testing the Endpoint\n", + "### Step 9 - Testing the Endpoint\n", "\n", "Let's now test the endpoint. Notice that we can now send the raw data to the endpoint, and it will return the penguin's species in a human-readable format.\n" ] }, { "cell_type": "code", - "execution_count": 121, + "execution_count": 300, "id": "3cc966fb-b611-417f-a8b8-0c5d2f95252c", "metadata": { "tags": [] @@ -6181,15 +6179,15 @@ "[\n", " {\n", " \"prediction\": \"Adelie\",\n", - " \"confidence\": 0.988331139\n", + " \"confidence\": 0.98598367\n", " },\n", " {\n", " \"prediction\": \"Adelie\",\n", - " \"confidence\": 0.982327223\n", + " \"confidence\": 0.978753448\n", " },\n", " {\n", " \"prediction\": \"Adelie\",\n", - " \"confidence\": 0.955816865\n", + " \"confidence\": 0.947375655\n", " }\n", "]\n" ] @@ -6225,12 +6223,12 @@ "id": "83fd85c2", "metadata": {}, "source": [ - "We can also test the endpoint by sending a JSON payload:\n" + "We can also test the endpoint by sending a JSON payload. Notice how you can use a deserealizer to automatically decode the response from the model." ] }, { "cell_type": "code", - "execution_count": 127, + "execution_count": 301, "id": "ce3d33f0", "metadata": {}, "outputs": [ @@ -6238,12 +6236,14 @@ "name": "stdout", "output_type": "stream", "text": [ - "{'prediction': 'Gentoo', 'confidence': 0.987513483}\n" + "{'prediction': 'Gentoo', 'confidence': 0.989867449}\n" ] } ], "source": [ "from sagemaker.serializers import JSONSerializer\n", + "from sagemaker.deserializers import JSONDeserializer\n", + "\n", "\n", "sample = {\n", " \"island\": \"Biscoe\",\n", @@ -6257,12 +6257,13 @@ "predictor = Predictor(\n", " endpoint_name=ENDPOINT,\n", " serializer=JSONSerializer(),\n", + " deserializer=JSONDeserializer(),\n", " sagemaker_session=sagemaker_session,\n", ")\n", "\n", "try:\n", " response = predictor.predict(sample)\n", - " print(json.loads(response.decode(\"utf-8\")))\n", + " print(response)\n", "except Exception as e:\n", " print(e)" ] @@ -6277,14 +6278,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 303, "id": "240aaff2", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[['Gentoo', '0.989867449']]\n" + ] + } + ], "source": [ + "from sagemaker.deserializers import CSVDeserializer\n", + "\n", + "predictor = Predictor(\n", + " endpoint_name=ENDPOINT,\n", + " serializer=JSONSerializer(),\n", + " deserializer=CSVDeserializer(),\n", + " sagemaker_session=sagemaker_session,\n", + ")\n", + "\n", "try:\n", " response = predictor.predict(sample, initial_args={\"Accept\": \"text/csv\"})\n", - " print(response.decode(\"utf-8\"))\n", + " print(response)\n", "except Exception as e:\n", " print(e)" ] @@ -6321,7 +6339,7 @@ }, { "cell_type": "code", - "execution_count": 114, + "execution_count": 266, "id": "97b3cd80", "metadata": { "tags": [ @@ -6462,7 +6480,7 @@ }, { "cell_type": "code", - "execution_count": 115, + "execution_count": 267, "id": "91786cc7", "metadata": { "tags": [ @@ -6482,22 +6500,22 @@ "output_type": "stream", "text": [ "Keras version: 2.14.0\n", - "8/8 - 0s - loss: 1.3397 - accuracy: 0.3180 - val_loss: 1.2616 - val_accuracy: 0.2941 - 218ms/epoch - 27ms/step\n", - "2/2 [==============================] - 0s 2ms/step\n" + "8/8 - 0s - loss: 1.3752 - accuracy: 0.0251 - val_loss: 1.3031 - val_accuracy: 0.0196 - 217ms/epoch - 27ms/step\n", + "2/2 [==============================] - 0s 1ms/step\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ - "INFO:tensorflow:Assets written to: /var/folders/4c/v1q3hy1x4mb5w0wpc72zl3_w0000gp/T/tmpsd37udrp/model/001/assets\n" + "INFO:tensorflow:Assets written to: /var/folders/4c/v1q3hy1x4mb5w0wpc72zl3_w0000gp/T/tmpebq8p3w6/model/001/assets\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ - "Validation accuracy: 0.29411764705882354\n", + "Validation accuracy: 0.0196078431372549\n", "Handling endpoint request\n", "Processing input data...\n", "Sending input data to model to make a prediction...\n" @@ -6516,10 +6534,10 @@ "output_type": "stream", "text": [ "1/1 [==============================] - 0s 25ms/step\n", - "Response: {'predictions': [[0.40967607498168945, 0.4577697217464447, 0.13255421817302704]]}\n", + "Response: {'predictions': [[0.08315584808588028, 0.5809251666069031, 0.3359190821647644]]}\n", "Processing prediction received from the model...\n", - "HERE {'predictions': [[0.40967607498168945, 0.4577697217464447, 0.13255421817302704]]}\n", - "{'prediction': 'Chinstrap', 'confidence': 0.4577697217464447}\n", + "HERE {'predictions': [[0.08315584808588028, 0.5809251666069031, 0.3359190821647644]]}\n", + "{'prediction': 'Chinstrap', 'confidence': 0.5809251666069031}\n", "\u001b[32m.\u001b[0m" ] }, @@ -6535,7 +6553,7 @@ "output_type": "stream", "text": [ "Keras version: 2.14.0\n", - "8/8 - 0s - loss: 1.3897 - accuracy: 0.1967 - val_loss: 1.2680 - val_accuracy: 0.2157 - 212ms/epoch - 26ms/step\n", + "8/8 - 0s - loss: 1.0557 - accuracy: 0.3849 - val_loss: 1.0517 - val_accuracy: 0.2941 - 215ms/epoch - 27ms/step\n", "2/2 [==============================] - 0s 1ms/step\n" ] }, @@ -6543,14 +6561,14 @@ "name": "stderr", "output_type": "stream", "text": [ - "INFO:tensorflow:Assets written to: /var/folders/4c/v1q3hy1x4mb5w0wpc72zl3_w0000gp/T/tmp1rvwan40/model/001/assets\n" + "INFO:tensorflow:Assets written to: /var/folders/4c/v1q3hy1x4mb5w0wpc72zl3_w0000gp/T/tmpu3pzk9nz/model/001/assets\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ - "Validation accuracy: 0.21568627450980393\n", + "Validation accuracy: 0.29411764705882354\n", "Handling endpoint request\n", "Processing input data...\n", "Sending input data to model to make a prediction...\n" @@ -6568,11 +6586,11 @@ "name": "stdout", "output_type": "stream", "text": [ - "1/1 [==============================] - 0s 29ms/step\n", - "Response: {'predictions': [[0.00972421932965517, 0.8837649822235107, 0.10651086270809174]]}\n", + "1/1 [==============================] - 0s 26ms/step\n", + "Response: {'predictions': [[0.3565749526023865, 0.21773585677146912, 0.4256891906261444]]}\n", "Processing prediction received from the model...\n", - "HERE {'predictions': [[0.00972421932965517, 0.8837649822235107, 0.10651086270809174]]}\n", - "{'prediction': 'Chinstrap', 'confidence': 0.8837649822235107}\n", + "HERE {'predictions': [[0.3565749526023865, 0.21773585677146912, 0.4256891906261444]]}\n", + "{'prediction': 'Gentoo', 'confidence': 0.4256891906261444}\n", "\u001b[32m.\u001b[0m" ] }, @@ -6588,7 +6606,7 @@ "output_type": "stream", "text": [ "Keras version: 2.14.0\n", - "8/8 - 0s - loss: 0.9700 - accuracy: 0.5063 - val_loss: 0.9822 - val_accuracy: 0.5098 - 215ms/epoch - 27ms/step\n", + "8/8 - 0s - loss: 1.5010 - accuracy: 0.1883 - val_loss: 1.4162 - val_accuracy: 0.1373 - 226ms/epoch - 28ms/step\n", "2/2 [==============================] - 0s 2ms/step\n" ] }, @@ -6596,14 +6614,14 @@ "name": "stderr", "output_type": "stream", "text": [ - "INFO:tensorflow:Assets written to: /var/folders/4c/v1q3hy1x4mb5w0wpc72zl3_w0000gp/T/tmp3msq8crf/model/001/assets\n" + "INFO:tensorflow:Assets written to: /var/folders/4c/v1q3hy1x4mb5w0wpc72zl3_w0000gp/T/tmpj0f8xfh9/model/001/assets\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ - "Validation accuracy: 0.5098039215686274\n", + "Validation accuracy: 0.13725490196078433\n", "Handling endpoint request\n", "Processing input data...\n", "Sending input data to model to make a prediction...\n" @@ -6621,11 +6639,11 @@ "name": "stdout", "output_type": "stream", "text": [ - "1/1 [==============================] - 0s 29ms/step\n", - "Response: {'predictions': [[0.26935648918151855, 0.05234304443001747, 0.6783004999160767]]}\n", + "1/1 [==============================] - 0s 27ms/step\n", + "Response: {'predictions': [[0.7790030837059021, 0.13591648638248444, 0.08508043736219406]]}\n", "Processing prediction received from the model...\n", - "HERE {'predictions': [[0.26935648918151855, 0.05234304443001747, 0.6783004999160767]]}\n", - "{'prediction': 'Gentoo', 'confidence': 0.6783004999160767}\n", + "HERE {'predictions': [[0.7790030837059021, 0.13591648638248444, 0.08508043736219406]]}\n", + "{'prediction': 'Adelie', 'confidence': 0.7790030837059021}\n", "\u001b[32m.\u001b[0m" ] }, @@ -6641,29 +6659,29 @@ "output_type": "stream", "text": [ "Keras version: 2.14.0\n", - "8/8 - 0s - loss: 1.4065 - accuracy: 0.4059 - val_loss: 1.4185 - val_accuracy: 0.3725 - 216ms/epoch - 27ms/step\n", - "2/2 [==============================] - 0s 1ms/step\n" + "8/8 - 0s - loss: 1.3193 - accuracy: 0.1674 - val_loss: 1.3469 - val_accuracy: 0.1569 - 217ms/epoch - 27ms/step\n", + "2/2 [==============================] - 0s 2ms/step\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ - "INFO:tensorflow:Assets written to: /var/folders/4c/v1q3hy1x4mb5w0wpc72zl3_w0000gp/T/tmpscclaeuh/model/001/assets\n" + "INFO:tensorflow:Assets written to: /var/folders/4c/v1q3hy1x4mb5w0wpc72zl3_w0000gp/T/tmp6oxxyllo/model/001/assets\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ - "Validation accuracy: 0.37254901960784315\n", + "Validation accuracy: 0.1568627450980392\n", "Handling endpoint request\n", "Processing input data...\n", "There was an error processing the input data. Expecting value: line 1 column 1 (char 0)\n", "Processing prediction received from the model...\n", "{'prediction': None}\n", "\u001b[32m.\u001b[0m\n", - "\u001b[32m\u001b[32m\u001b[1m4 passed\u001b[0m\u001b[32m in 2.31s\u001b[0m\u001b[0m\n" + "\u001b[32m\u001b[32m\u001b[1m4 passed\u001b[0m\u001b[32m in 2.36s\u001b[0m\u001b[0m\n" ] } ], @@ -6809,7 +6827,7 @@ }, { "cell_type": "code", - "execution_count": 117, + "execution_count": 268, "id": "603cb8f5", "metadata": { "tags": [ @@ -6845,7 +6863,7 @@ }, { "cell_type": "code", - "execution_count": 118, + "execution_count": 269, "id": "b5015c75", "metadata": {}, "outputs": [], @@ -6863,25 +6881,17 @@ }, { "cell_type": "markdown", - "id": "a98b5c0a", + "id": "74ce15c7", "metadata": {}, "source": [ - "### Step 3 - Registering the Model\n", + "### Step 3 - Configuring the Model Package Group\n", "\n", - "We'll modify the registration step to register the custom model in the Model Registry.\n" - ] - }, - { - "cell_type": "markdown", - "id": "f29084d1", - "metadata": {}, - "source": [ - "First, let's define a new group where we'll register the model using the custom `inference.py` script.\n" + "Let's define a new group where we'll register the model using the custom `inference.py` script.\n" ] }, { "cell_type": "code", - "execution_count": 119, + "execution_count": 270, "id": "7ee3cdb6", "metadata": {}, "outputs": [], @@ -6891,15 +6901,17 @@ }, { "cell_type": "markdown", - "id": "a8fb092d", + "id": "a98b5c0a", "metadata": {}, "source": [ - "We can now create the Registration Step using the new model.\n" + "### Step 4 - Registering the Model\n", + "\n", + "We can now modify the registration step to register the custom model in the Model Registry.\n" ] }, { "cell_type": "code", - "execution_count": 120, + "execution_count": 271, "id": "21fb3e2c", "metadata": { "tags": [ @@ -6932,14 +6944,14 @@ "id": "6b9293b0", "metadata": {}, "source": [ - "### Step 4 - Modifying the Deploy Step\n", + "### Step 5 - Modifying the Deploy Step\n", "\n", "Let's now modify the [LambdaStep](https://sagemaker.readthedocs.io/en/stable/workflows/pipelines/sagemaker.workflow.pipelines.html#sagemaker.workflow.lambda_step.LambdaStep) to use the updated Registration Step.\n" ] }, { "cell_type": "code", - "execution_count": 122, + "execution_count": 272, "id": "9e8162b7", "metadata": {}, "outputs": [], @@ -6952,14 +6964,14 @@ "id": "cd1812e7", "metadata": {}, "source": [ - "### Step 5 - Modifying the Condition Step\n", + "### Step 6 - Modifying the Condition Step\n", "\n", "Since we modified the Registration Step, we also need to modify the Condition Step to use the new registration:\n" ] }, { "cell_type": "code", - "execution_count": 123, + "execution_count": 273, "id": "849b379f", "metadata": {}, "outputs": [], @@ -6977,14 +6989,14 @@ "id": "671a5efb", "metadata": {}, "source": [ - "### Step 6 - Creating the Pipeline\n", + "### Step 7 - Creating the Pipeline\n", "\n", "We can now define the SageMaker Pipeline and submit its definition to the SageMaker Pipelines service to create the pipeline if it doesn't exist or update it if it does.\n" ] }, { "cell_type": "code", - "execution_count": 124, + "execution_count": 274, "id": "f64921ee", "metadata": { "tags": [ @@ -6999,8 +7011,6 @@ "INFO:sagemaker.image_uris:image_uri is not presented, retrieving image_uri based on instance_type, framework etc.\n", "INFO:sagemaker.processing:Uploaded None to s3://mlschool/session15-pipeline/code/c90fe2477ad62c58fbc0c004cc5ab07f/sourcedir.tar.gz\n", "INFO:sagemaker.processing:runproc.sh uploaded to s3://mlschool/session15-pipeline/code/f3b7867d7495763812a03744135acb08/runproc.sh\n", - "/Users/svpino/dev/ml.school/.venv/lib/python3.10/site-packages/sagemaker/workflow/pipeline_context.py:332: UserWarning: Running within a PipelineSession, there will be No Wait, No Logs, and No Job being started.\n", - " warnings.warn(\n", "WARNING:sagemaker.workflow._utils:Popping out 'CertifyForMarketplace' from the pipeline definition since it will be overridden in pipeline execution time.\n", "INFO:sagemaker.image_uris:image_uri is not presented, retrieving image_uri based on instance_type, framework etc.\n", "INFO:sagemaker.processing:Uploaded None to s3://mlschool/session15-pipeline/code/c90fe2477ad62c58fbc0c004cc5ab07f/sourcedir.tar.gz\n", @@ -7011,16 +7021,16 @@ "data": { "text/plain": [ "{'PipelineArn': 'arn:aws:sagemaker:us-east-1:325223348818:pipeline/session15-pipeline',\n", - " 'ResponseMetadata': {'RequestId': '016ce20b-84bf-41be-b0c1-3c2f3c2b769a',\n", + " 'ResponseMetadata': {'RequestId': 'a7b456b4-ac91-4100-9f8e-d6fdcb370b55',\n", " 'HTTPStatusCode': 200,\n", - " 'HTTPHeaders': {'x-amzn-requestid': '016ce20b-84bf-41be-b0c1-3c2f3c2b769a',\n", + " 'HTTPHeaders': {'x-amzn-requestid': 'a7b456b4-ac91-4100-9f8e-d6fdcb370b55',\n", " 'content-type': 'application/x-amz-json-1.1',\n", " 'content-length': '86',\n", - " 'date': 'Thu, 28 Mar 2024 16:14:05 GMT'},\n", + " 'date': 'Fri, 29 Mar 2024 19:19:55 GMT'},\n", " 'RetryAttempts': 0}}" ] }, - "execution_count": 124, + "execution_count": 274, "metadata": {}, "output_type": "execute_result" } @@ -7047,14 +7057,14 @@ "id": "0a6f02a4", "metadata": {}, "source": [ - "### Step 7 - Testing the Endpoint\n", + "### Step 8 - Testing the Endpoint\n", "\n", "Let's test the endpoint to make sure it works.\n" ] }, { "cell_type": "code", - "execution_count": 128, + "execution_count": 277, "id": "1d63ce59", "metadata": {}, "outputs": [ @@ -7062,7 +7072,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "{'prediction': 'Adelie', 'confidence': 0.629927337}\n" + "b'{\"prediction\": \"Adelie\", \"confidence\": 0.629927337}'\n" ] } ], @@ -8786,7 +8796,7 @@ }, { "cell_type": "code", - "execution_count": 257, + "execution_count": 299, "id": "59d1e634", "metadata": { "tags": [ @@ -8797,10 +8807,10 @@ { "data": { "text/plain": [ - "_PipelineExecution(arn='arn:aws:sagemaker:us-east-1:325223348818:pipeline/session14-pipeline/execution/h2hoqjmirvv1', sagemaker_session=)" + "_PipelineExecution(arn='arn:aws:sagemaker:us-east-1:325223348818:pipeline/session14-pipeline/execution/zma7heuo2ood', sagemaker_session=)" ] }, - "execution_count": 257, + "execution_count": 299, "metadata": {}, "output_type": "execute_result" } @@ -8882,7 +8892,7 @@ }, { "cell_type": "code", - "execution_count": 166, + "execution_count": 265, "id": "6b32c3a4-312e-473c-a217-33606f77d1e9", "metadata": { "tags": [