{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# SageMaker Model Monitor - visualizing monitoring results\n" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "\n", "This notebook's CI test result for us-west-2 is as follows. CI test results in other regions can be found at the end of the notebook. \n", "\n", "![This us-west-2 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable](https://h75twx4l60.execute-api.us-west-2.amazonaws.com/sagemaker-nb/us-west-2/sagemaker_model_monitor|visualization|SageMaker-Model-Monitor-Visualize.ipynb)\n", "\n", "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "The prebuilt container from SageMaker computes a variety of statistics and evaluates constraints out of the box. This notebook demonstrates how you can visualize them. You can grab the ProcessingJob arn from the executions behind a MonitoringSchedule and use this notebook to visualize the results.\n", "\n", "Let's import some python libraries that will be helpful for visualization" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from IPython.display import HTML, display\n", "import json\n", "import os\n", "import boto3\n", "\n", "import sagemaker\n", "from sagemaker import session\n", "from sagemaker.model_monitor import MonitoringExecution\n", "from sagemaker.s3 import S3Downloader" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Get Utilities for Rendering\n", "\n", "The functions for plotting and rendering distribution statistics or constraint violations are implemented in a `utils` file so let's grab that." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!wget https://raw.githubusercontent.com/awslabs/amazon-sagemaker-examples/main/sagemaker_model_monitor/visualization/utils.py\n", "\n", "import utils as mu" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Get Execution and Baseline details from Processing Job Arn\n", "\n", "Enter the ProcessingJob arn for an execution of a MonitoringSchedule below to get the result files associated with that execution" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "processing_job_arn = \"FILL-IN-PROCESSING-JOB-ARN\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "execution = MonitoringExecution.from_processing_arn(\n", " sagemaker_session=session.Session(), processing_job_arn=processing_job_arn\n", ")\n", "exec_inputs = {inp[\"InputName\"]: inp for inp in execution.describe()[\"ProcessingInputs\"]}\n", "exec_results = execution.output.destination" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "baseline_statistics_filepath = (\n", " exec_inputs[\"baseline\"][\"S3Input\"][\"S3Uri\"] if \"baseline\" in exec_inputs else None\n", ")\n", "execution_statistics_filepath = os.path.join(exec_results, \"statistics.json\")\n", "violations_filepath = os.path.join(exec_results, \"constraint_violations.json\")\n", "\n", "baseline_statistics = (\n", " json.loads(S3Downloader.read_file(baseline_statistics_filepath))\n", " if baseline_statistics_filepath is not None\n", " else None\n", ")\n", "execution_statistics = json.loads(S3Downloader.read_file(execution_statistics_filepath))\n", "violations = json.loads(S3Downloader.read_file(violations_filepath))[\"violations\"]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Overview\n", "\n", "The code below shows the violations and constraint checks across all features in a simple table." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mu.show_violation_df(\n", " baseline_statistics=baseline_statistics,\n", " latest_statistics=execution_statistics,\n", " violations=violations,\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Distributions\n", "\n", "This section visualizes the distribution and renders the distribution statistics for all features" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "features = mu.get_features(execution_statistics)\n", "feature_baselines = mu.get_features(baseline_statistics)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mu.show_distributions(features)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Execution Stats vs Baseline" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mu.show_distributions(features, feature_baselines)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Notebook CI Test Results\n", "\n", "This notebook was tested in multiple regions. The test results are as follows, except for us-west-2 which is shown at the top of the notebook.\n", "\n", "![This us-east-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable](https://h75twx4l60.execute-api.us-west-2.amazonaws.com/sagemaker-nb/us-east-1/sagemaker_model_monitor|visualization|SageMaker-Model-Monitor-Visualize.ipynb)\n", "\n", "![This us-east-2 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable](https://h75twx4l60.execute-api.us-west-2.amazonaws.com/sagemaker-nb/us-east-2/sagemaker_model_monitor|visualization|SageMaker-Model-Monitor-Visualize.ipynb)\n", "\n", "![This us-west-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable](https://h75twx4l60.execute-api.us-west-2.amazonaws.com/sagemaker-nb/us-west-1/sagemaker_model_monitor|visualization|SageMaker-Model-Monitor-Visualize.ipynb)\n", "\n", "![This ca-central-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable](https://h75twx4l60.execute-api.us-west-2.amazonaws.com/sagemaker-nb/ca-central-1/sagemaker_model_monitor|visualization|SageMaker-Model-Monitor-Visualize.ipynb)\n", "\n", "![This sa-east-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable](https://h75twx4l60.execute-api.us-west-2.amazonaws.com/sagemaker-nb/sa-east-1/sagemaker_model_monitor|visualization|SageMaker-Model-Monitor-Visualize.ipynb)\n", "\n", "![This eu-west-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable](https://h75twx4l60.execute-api.us-west-2.amazonaws.com/sagemaker-nb/eu-west-1/sagemaker_model_monitor|visualization|SageMaker-Model-Monitor-Visualize.ipynb)\n", "\n", "![This eu-west-2 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable](https://h75twx4l60.execute-api.us-west-2.amazonaws.com/sagemaker-nb/eu-west-2/sagemaker_model_monitor|visualization|SageMaker-Model-Monitor-Visualize.ipynb)\n", "\n", "![This eu-west-3 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable](https://h75twx4l60.execute-api.us-west-2.amazonaws.com/sagemaker-nb/eu-west-3/sagemaker_model_monitor|visualization|SageMaker-Model-Monitor-Visualize.ipynb)\n", "\n", "![This eu-central-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable](https://h75twx4l60.execute-api.us-west-2.amazonaws.com/sagemaker-nb/eu-central-1/sagemaker_model_monitor|visualization|SageMaker-Model-Monitor-Visualize.ipynb)\n", "\n", "![This eu-north-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable](https://h75twx4l60.execute-api.us-west-2.amazonaws.com/sagemaker-nb/eu-north-1/sagemaker_model_monitor|visualization|SageMaker-Model-Monitor-Visualize.ipynb)\n", "\n", "![This ap-southeast-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable](https://h75twx4l60.execute-api.us-west-2.amazonaws.com/sagemaker-nb/ap-southeast-1/sagemaker_model_monitor|visualization|SageMaker-Model-Monitor-Visualize.ipynb)\n", "\n", "![This ap-southeast-2 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable](https://h75twx4l60.execute-api.us-west-2.amazonaws.com/sagemaker-nb/ap-southeast-2/sagemaker_model_monitor|visualization|SageMaker-Model-Monitor-Visualize.ipynb)\n", "\n", "![This ap-northeast-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable](https://h75twx4l60.execute-api.us-west-2.amazonaws.com/sagemaker-nb/ap-northeast-1/sagemaker_model_monitor|visualization|SageMaker-Model-Monitor-Visualize.ipynb)\n", "\n", "![This ap-northeast-2 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable](https://h75twx4l60.execute-api.us-west-2.amazonaws.com/sagemaker-nb/ap-northeast-2/sagemaker_model_monitor|visualization|SageMaker-Model-Monitor-Visualize.ipynb)\n", "\n", "![This ap-south-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable](https://h75twx4l60.execute-api.us-west-2.amazonaws.com/sagemaker-nb/ap-south-1/sagemaker_model_monitor|visualization|SageMaker-Model-Monitor-Visualize.ipynb)\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.7.3" } }, "nbformat": 4, "nbformat_minor": 4 }