{ "cells": [ { "cell_type": "markdown", "id": "1b502ffc", "metadata": {}, "source": [ "## Lab 1. Object detection\n", "\n", "This notebook is intended to be run along with the [Workshop document](https://catalog.workshops.aws/panorama-immersion-day/en-US/20-lab1-object-detection/21-lab1)." ] }, { "cell_type": "markdown", "id": "2a8942ed", "metadata": {}, "source": [ "## Preparation" ] }, { "cell_type": "code", "execution_count": null, "id": "4ca08917", "metadata": {}, "outputs": [], "source": [ "# Import libraries\n", "\n", "import sys\n", "import os\n", "import json\n", "import glob\n", "import tarfile\n", "\n", "import boto3\n", "import sagemaker\n", "import IPython\n", "import gluoncv\n", "\n", "sys.path.insert( 0, os.path.abspath( \"../common/test_utility\" ) )\n", "import panorama_test_utility" ] }, { "cell_type": "code", "execution_count": null, "id": "fb4b9b77", "metadata": {}, "outputs": [], "source": [ "# Initialize variables and configurations\n", "\n", "boto3_session = boto3.session.Session()\n", "sm_session = sagemaker.Session()\n", "\n", "account_id = boto3.client(\"sts\").get_caller_identity()[\"Account\"]\n", "region = boto3_session.region_name\n", "s3_bucket = sm_session.default_bucket()\n", "sm_role = sagemaker.get_execution_role()\n", "\n", "print( \"account_id :\", account_id )\n", "print( \"region :\", region )\n", "print( \"s3_bucket :\", s3_bucket )\n", "print( \"sm_role :\", sm_role )" ] }, { "cell_type": "markdown", "id": "ad6a50dc", "metadata": {}, "source": [ "## \"Hello World!\" application" ] }, { "cell_type": "code", "execution_count": null, "id": "827c3043", "metadata": {}, "outputs": [], "source": [ "app_name = \"lab1\"\n", "\n", "!panorama-cli init-project --name {app_name}" ] }, { "cell_type": "code", "execution_count": null, "id": "0990fe4d", "metadata": {}, "outputs": [], "source": [ "code_package_name = f\"{app_name}_code\"\n", "code_package_version = \"1.0\"\n", "source_filename = f\"./{app_name}/packages/{account_id}-{code_package_name}-{code_package_version}/src/app.py\"\n", "\n", "!cd {app_name} && panorama-cli create-package --type Container --name {code_package_name} --version {code_package_version}" ] }, { "cell_type": "markdown", "id": "3c0f2e6b", "metadata": {}, "source": [ "
Manual edit needed: Manually create a one-line python source code referring to the Workshop document.
" ] }, { "cell_type": "markdown", "id": "eda43bb2", "metadata": {}, "source": [ "
Manual edit needed: Manually edit the code node descriptor file, and specify the entry point of the application code, referring to the Workshop document.
" ] }, { "cell_type": "code", "execution_count": null, "id": "ca3e26ca", "metadata": {}, "outputs": [], "source": [ "!cd {app_name} && panorama-cli build-container --container-asset-name code --package-path packages/{account_id}-{code_package_name}-{code_package_version}" ] }, { "cell_type": "code", "execution_count": null, "id": "9d284a86", "metadata": {}, "outputs": [], "source": [ "%run ../common/test_utility/panorama_test_utility_run.py \\\n", "\\\n", "--app-name {app_name} \\\n", "--code-package-name {code_package_name} \\\n", "--py-file {source_filename}" ] }, { "cell_type": "markdown", "id": "3142f3e0", "metadata": {}, "source": [ "## Video pass-through application" ] }, { "cell_type": "code", "execution_count": null, "id": "8f77409b", "metadata": {}, "outputs": [], "source": [ "camera_node_name = f\"{app_name}_camera\"\n", "\n", "!cd {app_name} && panorama-cli add-panorama-package --type camera --name {camera_node_name}" ] }, { "cell_type": "code", "execution_count": null, "id": "9fe53dcd", "metadata": {}, "outputs": [], "source": [ "data_sink_node_name = f\"{app_name}_data_sink\"\n", "\n", "!cd {app_name} && panorama-cli add-panorama-package --type data_sink --name {data_sink_node_name}" ] }, { "cell_type": "markdown", "id": "9564a162", "metadata": {}, "source": [ "
Manual edit needed: Manually edit the graph.json file, and connect code node, camera node, and data sink node, referring to the Workshop document.
" ] }, { "cell_type": "markdown", "id": "4c1e7e2f", "metadata": {}, "source": [ "
Manual edit needed: Manually edit the app.py referring to the Workshop document.
" ] }, { "cell_type": "code", "execution_count": null, "id": "14fd4f26", "metadata": {}, "outputs": [], "source": [ "video_filepath = \"../../videos/TownCentreXVID.avi\"\n", "\n", "%run ../common/test_utility/panorama_test_utility_run.py \\\n", "\\\n", "--app-name {app_name} \\\n", "--code-package-name {code_package_name} \\\n", "--py-file {source_filename} \\\n", "\\\n", "--camera-node-name lab1_camera \\\n", "--video-file {video_filepath} \\\n", "--video-start 0 \\\n", "--video-stop 10 \\\n", "--video-step 1 \\\n", "\\\n", "--output-screenshots ./screenshots/%Y%m%d_%H%M%S" ] }, { "cell_type": "code", "execution_count": null, "id": "c50261a9", "metadata": {}, "outputs": [], "source": [ "# View latest screenshot image\n", "\n", "latest_screenshot_dirname = sorted( glob.glob( \"./screenshots/*\" ) )[-1]\n", "screenshot_filename = sorted( glob.glob( f\"{latest_screenshot_dirname}/*.png\" ) )[-1]\n", "\n", "print(screenshot_filename)\n", "IPython.display.Image( filename = screenshot_filename )" ] }, { "cell_type": "markdown", "id": "0e36094d", "metadata": {}, "source": [ "## People detection application" ] }, { "cell_type": "code", "execution_count": null, "id": "4cf925a3", "metadata": {}, "outputs": [], "source": [ "def export_model_and_create_targz( prefix, name, model ):\n", " os.makedirs( prefix, exist_ok=True )\n", " gluoncv.utils.export_block( os.path.join( prefix, name ), model, preprocess=False, layout=\"CHW\" )\n", "\n", " tar_gz_filename = f\"{prefix}/{name}.tar.gz\"\n", " with tarfile.open( tar_gz_filename, \"w:gz\" ) as tgz:\n", " tgz.add( f\"{prefix}/{name}-symbol.json\", f\"{name}-symbol.json\" )\n", " tgz.add( f\"{prefix}/{name}-0000.params\", f\"{name}-0000.params\" )\n", " \n", " print( f\"Exported : {tar_gz_filename}\" )\n", " \n", "# Export object detection model. Reset the classes for human detection only.\n", "people_detection_model = gluoncv.model_zoo.get_model('yolo3_mobilenet1.0_coco', pretrained=True)\n", "people_detection_model.reset_class([\"person\"], reuse_weights=['person'])\n", "export_model_and_create_targz( \"models\", \"yolo3_mobilenet1.0_coco_person\", people_detection_model )" ] }, { "cell_type": "code", "execution_count": null, "id": "29dccbef", "metadata": {}, "outputs": [], "source": [ "model_package_name = f\"{app_name}_model\"\n", "model_package_version = \"1.0\"\n", "people_detection_model_name = \"people_detection_model\"\n", "\n", "!cd {app_name} && panorama-cli create-package --name {model_package_name} --type Model --version {model_package_version}" ] }, { "cell_type": "markdown", "id": "5af317d7", "metadata": {}, "source": [ "
Manual edit needed: Manually edit the model descriptor file, and specify the name of ML framework (\"MXNET\"), input data name (\"data\") and input data shape ( [1, 3, 480, 600] ), referring to the Workshop document.
\n" ] }, { "cell_type": "code", "execution_count": null, "id": "029fc576", "metadata": {}, "outputs": [], "source": [ "!cd {app_name} && panorama-cli add-raw-model \\\n", " --model-asset-name {people_detection_model_name} \\\n", " --model-local-path ../models/yolo3_mobilenet1.0_coco_person.tar.gz \\\n", " --descriptor-path packages/{account_id}-{model_package_name}-{model_package_version}/descriptor.json \\\n", " --packages-path packages/{account_id}-{model_package_name}-{model_package_version}" ] }, { "cell_type": "code", "execution_count": null, "id": "70c9d006", "metadata": {}, "outputs": [], "source": [ "people_detection_model_data_shape = '{\"data\":[1,3,480,600]}'\n", "\n", "%run ../common/test_utility/panorama_test_utility_compile.py \\\n", "\\\n", "--s3-model-location s3://{s3_bucket}/panorama-workshop/{app_name} \\\n", "\\\n", "--model-node-name {people_detection_model_name} \\\n", "--model-file-basename ./models/yolo3_mobilenet1.0_coco_person \\\n", "--model-data-shape '{people_detection_model_data_shape}' \\\n", "--model-framework MXNET" ] }, { "cell_type": "markdown", "id": "95d4a5d1", "metadata": {}, "source": [ "
Manual edit needed: Manually edit the app.py referring to the Workshop document.
" ] }, { "cell_type": "code", "execution_count": null, "id": "c6a42e84", "metadata": {}, "outputs": [], "source": [ "video_filepath = \"../../videos/TownCentreXVID.avi\"\n", "\n", "%run ../common/test_utility/panorama_test_utility_run.py \\\n", "\\\n", "--app-name {app_name} \\\n", "--code-package-name {code_package_name} \\\n", "--py-file {source_filename} \\\n", "\\\n", "--model-package-name {model_package_name} \\\n", "--model-node-name {people_detection_model_name} \\\n", "--model-file-basename ./models/yolo3_mobilenet1.0_coco_person \\\n", "\\\n", "--camera-node-name lab1_camera \\\n", "\\\n", "--video-file {video_filepath} \\\n", "--video-start 0 \\\n", "--video-stop 10 \\\n", "--video-step 1 \\\n", "\\\n", "--output-screenshots ./screenshots/%Y%m%d_%H%M%S" ] }, { "cell_type": "code", "execution_count": null, "id": "f9502349", "metadata": {}, "outputs": [], "source": [ "# View latest screenshot image\n", "\n", "latest_screenshot_dirname = sorted( glob.glob( \"./screenshots/*\" ) )[-1]\n", "screenshot_filename = sorted( glob.glob( f\"{latest_screenshot_dirname}/*.png\" ) )[-1]\n", "\n", "print(screenshot_filename)\n", "IPython.display.Image( filename = screenshot_filename )" ] }, { "cell_type": "markdown", "id": "9cab1cb0", "metadata": {}, "source": [ "## Run the people detection application on real device" ] }, { "cell_type": "code", "execution_count": null, "id": "48888b27", "metadata": {}, "outputs": [], "source": [ "!aws panorama list-devices" ] }, { "cell_type": "markdown", "id": "7e75ab92", "metadata": {}, "source": [ "
Manual edit needed: Manually edit the Docker file, and add some python libraries referring to the Workshop document.
" ] }, { "cell_type": "code", "execution_count": null, "id": "66c81a0d", "metadata": { "scrolled": true }, "outputs": [], "source": [ "!cd {app_name} && panorama-cli build-container --container-asset-name code --package-path packages/{account_id}-{code_package_name}-{code_package_version}" ] }, { "cell_type": "code", "execution_count": null, "id": "4fab8103", "metadata": {}, "outputs": [], "source": [ "!cd {app_name} && panorama-cli package-application" ] }, { "cell_type": "markdown", "id": "4c44511d", "metadata": {}, "source": [ "
Manual operation needed: Deploy the application using the AWS Management Console referring to the Workshop document.\n", "
" ] }, { "cell_type": "markdown", "id": "6b221ff2", "metadata": {}, "source": [ "
Manual operation needed: After confirming the execution of application, delete the application, referring to the Workshop document.\n", "
" ] } ], "metadata": { "kernelspec": { "display_name": "conda_python3", "language": "python", "name": "conda_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.12" } }, "nbformat": 4, "nbformat_minor": 5 }