Skip to content

Commit a05d24c

Browse files
authored
Merge branch 'main' into atqy/computer-vision
2 parents bba00ba + ddb08d4 commit a05d24c

File tree

10 files changed

+506
-94
lines changed

10 files changed

+506
-94
lines changed

introduction_to_amazon_algorithms/jumpstart_image_classification/Amazon_JumpStart_Image_Classification.ipynb

Lines changed: 85 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@
4141
"4. [Fine-tune the pre-trained model on a custom dataset](#4.-Fine-tune-the-pre-trained-model-on-a-custome-dataset)\n",
4242
" * [Retrieve JumpStart Training artifacts](#4.1.-Retrieve-JumpStart-Training-artifacts)\n",
4343
" * [Set Training parameters](#4.2.-Set-Training-parameters)\n",
44-
" * [Start Training](#4.3.-Start-Training)\n",
45-
" * [Deploy & run Inference on the fine-tuned model](#4.4.-Deploy-&-run-Inference-on-the-fine-tuned-model)"
44+
" * [Train with Automatic Model Tuning (HPO)](#AMT)\n",
45+
" * [Start Training](#4.4.-Start-Training)\n",
46+
" * [Deploy & run Inference on the fine-tuned model](#4.5.-Deploy-&-run-Inference-on-the-fine-tuned-model)"
4647
]
4748
},
4849
{
@@ -407,7 +408,7 @@
407408
"from sagemaker import image_uris, model_uris, script_uris, hyperparameters\n",
408409
"\n",
409410
"model_id, model_version = dropdown.value, \"*\"\n",
410-
"training_instance_type = \"ml.g4dn.xlarge\"\n",
411+
"training_instance_type = \"ml.p3.2xlarge\"\n",
411412
"\n",
412413
"# Retrieve the docker image\n",
413414
"train_image_uri = image_uris.retrieve(\n",
@@ -491,12 +492,66 @@
491492
"print(hyperparameters)"
492493
]
493494
},
495+
{
496+
"cell_type": "markdown",
497+
"id": "19c3a820",
498+
"metadata": {
499+
"collapsed": false
500+
},
501+
"source": [
502+
"### 4.3. Train with Automatic Model Tuning ([HPO](https://docs.aws.amazon.com/sagemaker/latest/dg/automatic-model-tuning.html)) <a id='AMT'></a>\n",
503+
"***\n",
504+
"Amazon SageMaker automatic model tuning, also known as hyperparameter tuning, finds the best version of a model by running many training jobs on your dataset using the algorithm and ranges of hyperparameters that you specify. It then chooses the hyperparameter values that result in a model that performs the best, as measured by a metric that you choose. We will use a [HyperparameterTuner](https://sagemaker.readthedocs.io/en/stable/api/training/tuner.html) object to interact with Amazon SageMaker hyperparameter tuning APIs.\n",
505+
"***"
506+
]
507+
},
508+
{
509+
"cell_type": "code",
510+
"execution_count": null,
511+
"id": "1684d6c6",
512+
"metadata": {
513+
"collapsed": false,
514+
"pycharm": {
515+
"name": "#%%\n"
516+
}
517+
},
518+
"outputs": [],
519+
"source": [
520+
"from sagemaker.tuner import ContinuousParameter\n",
521+
"\n",
522+
"# Use AMT for tuning and selecting the best model\n",
523+
"use_amt = True\n",
524+
"\n",
525+
"# Define objective metric per framework, based on which the best model will be selected.\n",
526+
"metric_definitions_per_model = {\n",
527+
" \"tensorflow\": {\n",
528+
" \"metrics\": [{\"Name\": \"val_accuracy\", \"Regex\": \"val_accuracy: ([0-9\\\\.]+)\"}],\n",
529+
" \"type\": \"Maximize\",\n",
530+
" },\n",
531+
" \"pytorch\": {\n",
532+
" \"metrics\": [{\"Name\": \"val_accuracy\", \"Regex\": \"val Acc: ([0-9\\\\.]+)\"}],\n",
533+
" \"type\": \"Maximize\",\n",
534+
" },\n",
535+
"}\n",
536+
"\n",
537+
"# You can select from the hyperparameters supported by the model, and configure ranges of values to be searched for training the optimal model.(https://docs.aws.amazon.com/sagemaker/latest/dg/automatic-model-tuning-define-ranges.html)\n",
538+
"hyperparameter_ranges = {\n",
539+
" \"adam-learning-rate\": ContinuousParameter(0.0001, 0.1, scaling_type=\"Logarithmic\")\n",
540+
"}\n",
541+
"\n",
542+
"# Increase the total number of training jobs run by AMT, for increased accuracy (and training time).\n",
543+
"max_jobs = 6\n",
544+
"# Change parallel training jobs run by AMT to reduce total training time, constrained by your account limits.\n",
545+
"# if max_jobs=max_parallel_jobs then Bayesian search turns to Random.\n",
546+
"max_parallel_jobs = 2"
547+
]
548+
},
494549
{
495550
"cell_type": "markdown",
496551
"id": "336871f4",
497552
"metadata": {},
498553
"source": [
499-
"### 4.3. Start Training\n",
554+
"### 4.4. Start Training\n",
500555
"***\n",
501556
"We start by creating the estimator object with all the required assets and then launch the training job.\n",
502557
"***"
@@ -511,6 +566,7 @@
511566
"source": [
512567
"from sagemaker.estimator import Estimator\n",
513568
"from sagemaker.utils import name_from_base\n",
569+
"from sagemaker.tuner import HyperparameterTuner\n",
514570
"\n",
515571
"training_job_name = name_from_base(f\"jumpstart-example-{model_id}-transfer-learning\")\n",
516572
"\n",
@@ -526,18 +582,38 @@
526582
" max_run=360000,\n",
527583
" hyperparameters=hyperparameters,\n",
528584
" output_path=s3_output_location,\n",
585+
" base_job_name=training_job_name,\n",
529586
")\n",
530587
"\n",
531-
"# Launch a SageMaker Training job by passing s3 path of the training data\n",
532-
"ic_estimator.fit({\"training\": training_dataset_s3_path}, logs=True)"
588+
"if use_amt:\n",
589+
" metric_definitions = next(\n",
590+
" value for key, value in metric_definitions_per_model.items() if model_id.startswith(key)\n",
591+
" )\n",
592+
"\n",
593+
" hp_tuner = HyperparameterTuner(\n",
594+
" ic_estimator,\n",
595+
" metric_definitions[\"metrics\"][0][\"Name\"],\n",
596+
" hyperparameter_ranges,\n",
597+
" metric_definitions[\"metrics\"],\n",
598+
" max_jobs=max_jobs,\n",
599+
" max_parallel_jobs=max_parallel_jobs,\n",
600+
" objective_type=metric_definitions[\"type\"],\n",
601+
" base_tuning_job_name=training_job_name,\n",
602+
" )\n",
603+
"\n",
604+
" # Launch a SageMaker Tuning job to search for the best hyperparameters\n",
605+
" hp_tuner.fit({\"training\": training_dataset_s3_path})\n",
606+
"else:\n",
607+
" # Launch a SageMaker Training job by passing s3 path of the training data\n",
608+
" ic_estimator.fit({\"training\": training_dataset_s3_path}, logs=True)"
533609
]
534610
},
535611
{
536612
"cell_type": "markdown",
537613
"id": "cc67f26b",
538614
"metadata": {},
539615
"source": [
540-
"## 4.4. Deploy & run Inference on the fine-tuned model\n",
616+
"## 4.5. Deploy & run Inference on the fine-tuned model\n",
541617
"***\n",
542618
"A trained model does nothing on its own. We now want to use the model to perform inference. For this example, that means predicting the class label of an image. We follow the same steps as in [3. Run inference on the pre-trained model](#3.-Run-inference-on-the-pre-trained-model). We start by retrieving the jumpstart artifacts for deploying an endpoint. However, instead of base_predictor, we deploy the `ic_estimator` that we fine-tuned.\n",
543619
"***"
@@ -569,7 +645,7 @@
569645
"endpoint_name = name_from_base(f\"jumpstart-example-FT-{model_id}-\")\n",
570646
"\n",
571647
"# Use the estimator from the previous step to deploy to a SageMaker endpoint\n",
572-
"finetuned_predictor = ic_estimator.deploy(\n",
648+
"finetuned_predictor = (hp_tuner if use_amt else ic_estimator).deploy(\n",
573649
" initial_instance_count=1,\n",
574650
" instance_type=inference_instance_type,\n",
575651
" entry_point=\"inference.py\",\n",
@@ -695,4 +771,4 @@
695771
},
696772
"nbformat": 4,
697773
"nbformat_minor": 5
698-
}
774+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
### SageMaker JumpStart Image classification Training & Deployment
2-
This notebook `Amazon_JumpStart_Image_Classification.ipynb` demos how to fine-tune and deploy a pre-trained image classification model using JumpStart API. It shows how to select a pre-trained image classification model from JumpStart and fine-tune it on an example dataset containing raw .jpg/.png images, while varying training hyperparameters such as learning rate, batch-size and number of epochs. Once the training is complete, the notebook shows how to host the trained model for inference. It also shows how to host the pre-trained model as-it-is without first fine-tuning it.
2+
This notebook `Amazon_JumpStart_Image_Classification.ipynb` demos how to fine-tune and deploy a pre-trained image classification model using JumpStart API. It shows how to select a pre-trained image classification model from JumpStart and fine-tune it on an example dataset containing raw .jpg/.png images, while varying training hyperparameters such as learning rate, batch-size and number of epochs. AMT (Automatic Model Tuning) is used to search for the best hyperparameters. Once the training is complete, the notebook shows how to host the trained model for inference. It also shows how to host the pre-trained model as-it-is without first fine-tuning it.

introduction_to_amazon_algorithms/jumpstart_object_detection/Amazon_JumpStart_Object_Detection.ipynb

Lines changed: 90 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@
4444
"3. [Fine-tune the pre-trained model on a custom dataset](#3.-Fine-tune-the-pre-trained-model-on-a-custom-dataset)\n",
4545
" * [Retrieve Training Artifacts](#3.1.-Retrieve-Training-Artifacts)\n",
4646
" * [Set Training parameters](#3.2.-Set-Training-parameters)\n",
47-
" * [Start Training](#3.3.-Start-Training)\n",
48-
" * [Deploy and run inference on the fine-tuned model](#3.4.-Deploy-and-run-inference-on-the-fine-tuned-model)\n"
47+
" * [Train with Automatic Model Tuning (HPO)](#AMT)\n",
48+
" * [Start Training](#3.4.-Start-Training)\n",
49+
" * [Deploy and run inference on the fine-tuned model](#3.5.-Deploy-and-run-inference-on-the-fine-tuned-model)\n"
4950
]
5051
},
5152
{
@@ -506,11 +507,11 @@
506507
"# Currently, not all the object detection models in jumpstart support finetuning. Thus, we manually select a model\n",
507508
"# which supports finetuning.\n",
508509
"train_model_id, train_model_version, train_scope = (\n",
509-
" \"mxnet-od-ssd-512-vgg16-atrous-coco\",\n",
510+
" \"mxnet-od-ssd-512-vgg16-atrous-coco\", # \"pytorch-od1-fasterrcnn-resnet50-fpn\"\n",
510511
" \"*\",\n",
511512
" \"training\",\n",
512513
")\n",
513-
"training_instance_type = \"ml.p2.xlarge\"\n",
514+
"training_instance_type = \"ml.p3.2xlarge\"\n",
514515
"\n",
515516
"# Retrieve the docker image\n",
516517
"train_image_uri = image_uris.retrieve(\n",
@@ -598,12 +599,66 @@
598599
"print(hyperparameters)"
599600
]
600601
},
602+
{
603+
"cell_type": "markdown",
604+
"id": "8108dfae",
605+
"metadata": {
606+
"collapsed": false
607+
},
608+
"source": [
609+
"### 3.3. Train with Automatic Model Tuning ([HPO](https://docs.aws.amazon.com/sagemaker/latest/dg/automatic-model-tuning.html)) <a id='AMT'></a>\n",
610+
"***\n",
611+
"Amazon SageMaker automatic model tuning, also known as hyperparameter tuning, finds the best version of a model by running many training jobs on your dataset using the algorithm and ranges of hyperparameters that you specify. It then chooses the hyperparameter values that result in a model that performs the best, as measured by a metric that you choose. We will use a [HyperparameterTuner](https://sagemaker.readthedocs.io/en/stable/api/training/tuner.html) object to interact with Amazon SageMaker hyperparameter tuning APIs.\n",
612+
"***"
613+
]
614+
},
615+
{
616+
"cell_type": "code",
617+
"execution_count": null,
618+
"id": "be0a1097",
619+
"metadata": {
620+
"collapsed": false,
621+
"pycharm": {
622+
"name": "#%%\n"
623+
}
624+
},
625+
"outputs": [],
626+
"source": [
627+
"from sagemaker.tuner import ContinuousParameter\n",
628+
"\n",
629+
"# Use AMT for tuning and selecting the best model\n",
630+
"use_amt = True\n",
631+
"\n",
632+
"# Define objective metric per framework, based on which the best model will be selected.\n",
633+
"metric_definitions_per_model = {\n",
634+
" \"mxnet\": {\n",
635+
" \"metrics\": [{\"Name\": \"val_cross_entropy\", \"Regex\": \"Val_CrossEntropy=([0-9\\\\.]+)\"}],\n",
636+
" \"type\": \"Minimize\",\n",
637+
" },\n",
638+
" \"pytorch\": {\n",
639+
" \"metrics\": [{\"Name\": \"val_loss\", \"Regex\": \"val_loss: ([0-9\\\\.]+)\"}],\n",
640+
" \"type\": \"Minimize\",\n",
641+
" },\n",
642+
"}\n",
643+
"\n",
644+
"# You can select from the hyperparameters supported by the model, and configure ranges of values to be searched for training the optimal model.(https://docs.aws.amazon.com/sagemaker/latest/dg/automatic-model-tuning-define-ranges.html)\n",
645+
"hyperparameter_ranges = {\n",
646+
" \"adam-learning-rate\": ContinuousParameter(0.0001, 0.1, scaling_type=\"Logarithmic\")\n",
647+
"}\n",
648+
"\n",
649+
"# Increase the total number of training jobs run by AMT, for increased accuracy (and training time).\n",
650+
"max_jobs = 6\n",
651+
"# Change parallel training jobs run by AMT to reduce total training time, constrained by your account limits.\n",
652+
"# if max_jobs=max_parallel_jobs then Bayesian search turns to Random.\n",
653+
"max_parallel_jobs = 2"
654+
]
655+
},
601656
{
602657
"cell_type": "markdown",
603658
"id": "70a010d7",
604659
"metadata": {},
605660
"source": [
606-
"### 3.3. Start Training"
661+
"### 3.4. Start Training"
607662
]
608663
},
609664
{
@@ -626,6 +681,7 @@
626681
"source": [
627682
"from sagemaker.estimator import Estimator\n",
628683
"from sagemaker.utils import name_from_base\n",
684+
"from sagemaker.tuner import HyperparameterTuner\n",
629685
"\n",
630686
"training_job_name = name_from_base(f\"jumpstart-example-{train_model_id}-transfer-learning\")\n",
631687
"\n",
@@ -641,26 +697,40 @@
641697
" max_run=360000,\n",
642698
" hyperparameters=hyperparameters,\n",
643699
" output_path=s3_output_location,\n",
644-
")"
645-
]
646-
},
647-
{
648-
"cell_type": "code",
649-
"execution_count": null,
650-
"id": "540de4ae",
651-
"metadata": {},
652-
"outputs": [],
653-
"source": [
654-
"# Launch a SageMaker Training job by passing s3 path of the training data\n",
655-
"od_estimator.fit({\"training\": training_dataset_s3_path}, logs=True, job_name=training_job_name)"
700+
" base_job_name=training_job_name,\n",
701+
")\n",
702+
"\n",
703+
"if use_amt:\n",
704+
" metric_definitions = next(\n",
705+
" value\n",
706+
" for key, value in metric_definitions_per_model.items()\n",
707+
" if train_model_id.startswith(key)\n",
708+
" )\n",
709+
"\n",
710+
" hp_tuner = HyperparameterTuner(\n",
711+
" od_estimator,\n",
712+
" metric_definitions[\"metrics\"][0][\"Name\"],\n",
713+
" hyperparameter_ranges,\n",
714+
" metric_definitions[\"metrics\"],\n",
715+
" max_jobs=max_jobs,\n",
716+
" max_parallel_jobs=max_parallel_jobs,\n",
717+
" objective_type=metric_definitions[\"type\"],\n",
718+
" base_tuning_job_name=training_job_name,\n",
719+
" )\n",
720+
"\n",
721+
" # Launch a SageMaker Tuning job to search for the best hyperparameters\n",
722+
" hp_tuner.fit({\"training\": training_dataset_s3_path})\n",
723+
"else:\n",
724+
" # Launch a SageMaker Training job by passing s3 path of the training data\n",
725+
" od_estimator.fit({\"training\": training_dataset_s3_path}, logs=True)"
656726
]
657727
},
658728
{
659729
"cell_type": "markdown",
660730
"id": "99a147f2",
661731
"metadata": {},
662732
"source": [
663-
"### 3.4. Deploy and run inference on the fine-tuned model\n",
733+
"### 3.5. Deploy and run inference on the fine-tuned model\n",
664734
"\n",
665735
"---\n",
666736
"\n",
@@ -695,7 +765,7 @@
695765
"endpoint_name = name_from_base(f\"jumpstart-example-FT-{train_model_id}-\")\n",
696766
"\n",
697767
"# Use the estimator from the previous step to deploy to a SageMaker endpoint\n",
698-
"finetuned_predictor = od_estimator.deploy(\n",
768+
"finetuned_predictor = (hp_tuner if use_amt else od_estimator).deploy(\n",
699769
" initial_instance_count=1,\n",
700770
" instance_type=inference_instance_type,\n",
701771
" entry_point=\"inference.py\", # entry point file in source_dir and present in deploy_source_uri\n",
@@ -800,17 +870,8 @@
800870
"nbconvert_exporter": "python",
801871
"pygments_lexer": "ipython3",
802872
"version": "3.6.13"
803-
},
804-
"pycharm": {
805-
"stem_cell": {
806-
"cell_type": "raw",
807-
"metadata": {
808-
"collapsed": false
809-
},
810-
"source": []
811-
}
812873
}
813874
},
814875
"nbformat": 4,
815876
"nbformat_minor": 5
816-
}
877+
}

0 commit comments

Comments
 (0)