Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
APPLIES TO:
Azure CLI ml extension v2 (current)
Python SDK azure-ai-ml v2 (current)
In this tutorial, you learn how to train an object detection model by using Azure Machine Learning automated ML with the Azure Machine Learning CLI extension v2 or the Azure Machine Learning Python SDK v2. This object detection model identifies whether an image contains objects, such as a can, carton, milk bottle, or water bottle.
Automated ML accepts training data and configuration settings and automatically iterates through combinations of different feature normalization/standardization methods, models, and hyperparameter settings to arrive at the best model.
You write code by using the Python SDK in this tutorial and learn the following tasks:
- Download and transform data
- Train an automated machine learning object detection model
- Specify hyperparameter values for your model
- Perform a hyperparameter sweep
- Deploy your model
- Visualize detections
Prerequisites
-
To use Azure Machine Learning, you need a workspace. If you don't have one, complete Create resources you need to get started to create a workspace and learn more about using it.
Important
If your Azure Machine Learning workspace is configured with a managed virtual network, you might need to add outbound rules to allow access to the public Python package repositories. For more information, see Scenario: Access public machine learning packages.
Use Python 3.10 or later.
Download and unzip the odFridgeObjects.zip data file. The dataset is annotated in Pascal VOC format, where each image corresponds to an XML file. Each XML file contains information on where its corresponding image file is located and also contains information about the bounding boxes and the object labels. To use this data, you first need to convert it to the required JSONL format, as shown in the Convert the downloaded data to JSONL section of the automl-image-object-detection-task-fridge-items.ipynb notebook.
Use a compute instance to complete this tutorial without further installation. (See Create a compute instance.) Or install the CLI or SDK to use your own local environment.
APPLIES TO:
Azure CLI ml extension v2 (current)This tutorial is also available in the azureml-examples repository on GitHub. If you want to run it in your local environment, install and set up CLI (v2) and make sure you install the
mlextension.
Compute target setup
Note
To try serverless compute, skip this step and go to Experiment setup.
You first need to set up a compute target to use for your automated ML model training. Automated ML models for image tasks require GPU SKUs.
This tutorial uses the NCsv3-series (with V100 GPUs) because this type of compute target uses multiple GPUs to speed up training. Additionally, you can set up multiple nodes to take advantage of parallelism when tuning hyperparameters for your model.
The following code creates a GPU compute of size Standard_NC24s_v3 with four nodes.
APPLIES TO:
Azure CLI ml extension v2 (current)
Create a .yml file with the following configuration.
$schema: https://azuremlschemas.azureedge.net/latest/amlCompute.schema.json
name: gpu-cluster
type: amlcompute
size: Standard_NC24s_v3
min_instances: 0
max_instances: 4
idle_time_before_scale_down: 120
To create the compute, you run the following CLI v2 command with the path to your .yml file, workspace name, resource group, and subscription ID.
az ml compute create -f [PATH_TO_YML_FILE] --workspace-name [YOUR_AZURE_WORKSPACE] --resource-group [YOUR_AZURE_RESOURCE_GROUP] --subscription [YOUR_AZURE_SUBSCRIPTION]
Experiment setup
You can use an experiment to track your model training jobs.
APPLIES TO:
Azure CLI ml extension v2 (current)
You can provide the experiment name by using the experiment_name key:
experiment_name: dpv2-cli-automl-image-object-detection-experiment
Visualize input data
After you have the input image data prepared in JSONL (JSON Lines) format, you can visualize the ground truth bounding boxes for an image. To do so, be sure you have matplotlib installed.
%pip install --upgrade matplotlib
%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import matplotlib.patches as patches
from PIL import Image as pil_image
import numpy as np
import json
import os
def plot_ground_truth_boxes(image_file, ground_truth_boxes):
# Display the image
plt.figure()
img_np = mpimg.imread(image_file)
img = pil_image.fromarray(img_np.astype("uint8"), "RGB")
img_w, img_h = img.size
fig,ax = plt.subplots(figsize=(12, 16))
ax.imshow(img_np)
ax.axis("off")
label_to_color_mapping = {}
for gt in ground_truth_boxes:
label = gt["label"]
xmin, ymin, xmax, ymax = gt["topX"], gt["topY"], gt["bottomX"], gt["bottomY"]
topleft_x, topleft_y = img_w * xmin, img_h * ymin
width, height = img_w * (xmax - xmin), img_h * (ymax - ymin)
if label in label_to_color_mapping:
color = label_to_color_mapping[label]
else:
# Generate a random color. If you want to use a specific color, you can use something like "red."
color = np.random.rand(3)
label_to_color_mapping[label] = color
# Display bounding box
rect = patches.Rectangle((topleft_x, topleft_y), width, height,
linewidth=2, edgecolor=color, facecolor="none")
ax.add_patch(rect)
# Display label
ax.text(topleft_x, topleft_y - 10, label, color=color, fontsize=20)
plt.show()
def plot_ground_truth_boxes_jsonl(image_file, jsonl_file):
image_base_name = os.path.basename(image_file)
ground_truth_data_found = False
with open(jsonl_file) as fp:
for line in fp.readlines():
line_json = json.loads(line)
filename = line_json["image_url"]
if image_base_name in filename:
ground_truth_data_found = True
plot_ground_truth_boxes(image_file, line_json["label"])
break
if not ground_truth_data_found:
print("Unable to find ground truth information for image: {}".format(image_file))
By using the preceding helper functions, for any given image, you can run the following code to display the bounding boxes.
image_file = "./odFridgeObjects/images/31.jpg"
jsonl_file = "./odFridgeObjects/train_annotations.jsonl"
plot_ground_truth_boxes_jsonl(image_file, jsonl_file)
Upload data and create an MLTable
To use data for training, upload it to the default blob storage of your Azure Machine Learning workspace and register it as an asset. The benefits of registering data are:
- Easy to share with other members of the team.
- Versioning of the metadata (location, description, and so on).
- Lineage tracking.
APPLIES TO:
Azure CLI ml extension v2 (current)
Create a .yml file with the following configuration.
$schema: https://azuremlschemas.azureedge.net/latest/data.schema.json
name: fridge-items-images-object-detection
description: Fridge-items images Object detection
path: ./data/odFridgeObjects
type: uri_folder
To upload the images as a data asset, run the following CLI v2 command with the path to your .yml file, workspace name, resource group, and subscription ID.
az ml data create -f [PATH_TO_YML_FILE] --workspace-name [YOUR_AZURE_WORKSPACE] --resource-group [YOUR_AZURE_RESOURCE_GROUP] --subscription [YOUR_AZURE_SUBSCRIPTION]
The next step is to create an MLTable from your data in JSONL format, as shown in the following example. An MLTable packages your data into a consumable object for training.
paths:
- file: ./train_annotations.jsonl
transformations:
- read_json_lines:
encoding: utf8
invalid_lines: error
include_path_column: false
- convert_column_types:
- columns: image_url
column_type: stream_info
APPLIES TO:
Azure CLI ml extension v2 (current)
The following configuration creates training and validation data from the MLTable.
target_column_name: label
training_data:
path: data/training-mltable-folder
type: mltable
validation_data:
path: data/validation-mltable-folder
type: mltable
Configure your object detection experiment
To configure automated ML jobs for image-related tasks, create a task-specific AutoML job.
APPLIES TO:
Azure CLI ml extension v2 (current)
Note
To use serverless compute, replace the line compute: azureml:gpu-cluster with this code:
resources:
instance_type: Standard_NC24s_v3
instance_count: 4
task: image_object_detection
primary_metric: mean_average_precision
compute: azureml:gpu-cluster
Automatic hyperparameter sweeping for image tasks (AutoMode)
In your AutoML job, you can perform an automatic hyperparameter sweep to find the optimal model. (This functionality is called AutoMode). You only specify the number of trials. The hyperparameter search space, sampling method, and early termination policy aren't needed. The system automatically determines the region of the hyperparameter space to sweep based on the number of trials. A value between 10 and 20 will probably work well on many datasets.
APPLIES TO:
Azure CLI ml extension v2 (current)
limits:
max_trials: 10
max_concurrent_trials: 2
You can then submit the job to train an image model.
APPLIES TO:
Azure CLI ml extension v2 (current)
To submit your AutoML job, you run the following CLI v2 command with the path to your .yml file, workspace name, resource group, and subscription ID.
az ml job create --file ./hello-automl-job-basic.yml --workspace-name [YOUR_AZURE_WORKSPACE] --resource-group [YOUR_AZURE_RESOURCE_GROUP] --subscription [YOUR_AZURE_SUBSCRIPTION]
Manual hyperparameter sweeping for image tasks
In your AutoML job, you can specify the model architectures by using the model_name parameter and configure the settings to perform a hyperparameter sweep over a defined search space to find the optimal model.
In this example, you train an object detection model with YOLOv5 and FasterRCNN ResNet50 FPN, both of which are pretrained on COCO, a large-scale object detection, segmentation, and captioning dataset that contains thousands of labeled images with more than 80 label categories.
You can perform a hyperparameter sweep over a defined search space to find the optimal model.
Job limits
You can control the resources spent on your AutoML image training job by specifying the timeout_minutes, max_trials, and max_concurrent_trials for the job in limit settings. For more information, see the description of job limits parameters.
APPLIES TO:
Azure CLI ml extension v2 (current)
limits:
timeout_minutes: 60
max_trials: 10
max_concurrent_trials: 2
The following code defines the search space in preparation for the hyperparameter sweep for each defined architecture, YOLOv5 and FasterRCNN ResNet50 FPN. In the search space, specify the range of values for learning_rate, optimizer, lr_scheduler, and so on, for AutoML to choose from as it attempts to generate a model with the optimal primary metric. If hyperparameter values aren't specified, default values are used for each architecture.
For the tuning settings, use random sampling to pick samples from this parameter space by using the random sampling algorithm. The job limits specified in the preceding code configure automated ML to try a total of 10 trials with these different samples, running two trials at a time on the compute target, which is set up using four nodes. The more parameters the search space has, the more trials you need to find optimal models.
The Bandit early termination policy is also used. This policy terminates poorly performing trials. That is, trials that aren't within 20% slack of the best performing trial. This termination significantly saves compute resources.
APPLIES TO:
Azure CLI ml extension v2 (current)
sweep:
sampling_algorithm: random
early_termination:
type: bandit
evaluation_interval: 2
slack_factor: 0.2
delay_evaluation: 6
search_space:
- model_name:
type: choice
values: [yolov5]
learning_rate:
type: uniform
min_value: 0.0001
max_value: 0.01
model_size:
type: choice
values: [small, medium]
- model_name:
type: choice
values: [fasterrcnn_resnet50_fpn]
learning_rate:
type: uniform
min_value: 0.0001
max_value: 0.001
optimizer:
type: choice
values: [sgd, adam, adamw]
min_size:
type: choice
values: [600, 800]
After you define the search space and sweep settings, you can submit the job to train an image model by using your training dataset.
APPLIES TO:
Azure CLI ml extension v2 (current)
To submit your AutoML job, run the following CLI v2 command with the path to your .yml file, the workspace name, the resource group, and the subscription ID.
az ml job create --file ./hello-automl-job-basic.yml --workspace-name [YOUR_AZURE_WORKSPACE] --resource-group [YOUR_AZURE_RESOURCE_GROUP] --subscription [YOUR_AZURE_SUBSCRIPTION]
When you do a hyperparameter sweep, it can be useful to visualize the different trials that were tried by using the Hyperdrive UI. You can get to this UI by going to the Child jobs tab in the UI of the main AutoML image job that appears earlier, which is the Hyperdrive parent job. You can then go to the Child jobs tab of this one.
Alternatively, here you can directly see the Hyperdrive parent job and navigate to its Child jobs tab:
APPLIES TO:
Azure CLI ml extension v2 (current)
CLI example not available. Use the the Python SDK.
Register and deploy the model
After the job finishes, you can register the model that was created from the best trial (the configuration that resulted in the best primary metric). You can either register the model after downloading or by specifying the azureml path with a corresponding jobid.
Get the best trial
APPLIES TO:
Azure CLI ml extension v2 (current)
CLI example not available. Use the Python SDK.
Register the model
Register the model by using either the azureml path or your locally downloaded path.
APPLIES TO:
Azure CLI ml extension v2 (current)
az ml model create --name od-fridge-items-mlflow-model --version 1 --path azureml://jobs/$best_run/outputs/artifacts/outputs/mlflow-model/ --type mlflow_model --workspace-name [YOUR_AZURE_WORKSPACE] --resource-group [YOUR_AZURE_RESOURCE_GROUP] --subscription [YOUR_AZURE_SUBSCRIPTION]
After you register the model you want to use, you can deploy it by using the managed online endpoint.
Configure online endpoint
APPLIES TO:
Azure CLI ml extension v2 (current)
$schema: https://azuremlschemas.azureedge.net/latest/managedOnlineEndpoint.schema.json
name: od-fridge-items-endpoint
auth_mode: key
Create the endpoint
By using the MLClient created earlier, you'll now create the endpoint in the workspace. This command starts the endpoint creation and returns a confirmation response while the endpoint creation continues.
APPLIES TO:
Azure CLI ml extension v2 (current)
az ml online-endpoint create --file .\create_endpoint.yml --workspace-name [YOUR_AZURE_WORKSPACE] --resource-group [YOUR_AZURE_RESOURCE_GROUP] --subscription [YOUR_AZURE_SUBSCRIPTION]
You can also create a batch endpoint for batch inferencing on large volumes of data over a period of time. See the object detection batch scoring notebook for an example of batch inferencing using the batch endpoint.
Configure online deployment
A deployment is a set of resources required for hosting the model that does the actual inferencing. The following code creates a deployment for the endpoint. You can use either GPU or CPU VM SKUs for your deployment cluster.
APPLIES TO:
Azure CLI ml extension v2 (current)
name: od-fridge-items-mlflow-deploy
endpoint_name: od-fridge-items-endpoint
model: azureml:od-fridge-items-mlflow-model@latest
instance_type: Standard_DS3_v2
instance_count: 1
liveness_probe:
failure_threshold: 30
success_threshold: 1
timeout: 2
period: 10
initial_delay: 2000
readiness_probe:
failure_threshold: 10
success_threshold: 1
timeout: 10
period: 10
initial_delay: 2000
Create the deployment
Using the MLClient created earlier, you'll create the deployment in the workspace. This command starts the deployment creation and returns a confirmation response while the deployment creation continues.
APPLIES TO:
Azure CLI ml extension v2 (current)
az ml online-deployment create --file .\create_deployment.yml --workspace-name [YOUR_AZURE_WORKSPACE] --resource-group [YOUR_AZURE_RESOURCE_GROUP] --subscription [YOUR_AZURE_SUBSCRIPTION]
Update traffic
By default, the current deployment is set to receive 0% traffic. You can set the traffic percentage that the current deployment should receive. The sum of the traffic percentages of all the deployments with one endpoint shouldn't exceed 100%.
APPLIES TO:
Azure CLI ml extension v2 (current)
az ml online-endpoint update --name 'od-fridge-items-endpoint' --traffic 'od-fridge-items-mlflow-deploy=100' --workspace-name [YOUR_AZURE_WORKSPACE] --resource-group [YOUR_AZURE_RESOURCE_GROUP] --subscription [YOUR_AZURE_SUBSCRIPTION]
Test the deployment
APPLIES TO:
Azure CLI ml extension v2 (current)
CLI example not available. Use the Python SDK.
Visualize detections
Now that you've scored a test image, you can visualize the bounding boxes for the image. To do so, you need to have Matplotlib installed.
APPLIES TO:
Azure CLI ml extension v2 (current)
CLI example not available. Use the Python SDK.
Clean up resources
Don't complete this section if you plan to complete other Azure Machine Learning tutorials.
If you don't plan to use the resources you created, delete them so that you don't incur any charges.
- In the Azure portal, select Resource groups in the left pane.
- In the list of resource groups, select the resource group that you created.
- Select Delete resource group.
- Enter the resource group name. Then select Delete.
You can also keep the resource group but delete a single workspace. Go to the workspace page, and then select Delete.
Next steps
In this automated machine learning tutorial, you completed the following tasks:
- Configured a workspace and prepared data for an experiment
- Trained an automated object detection model
- Specified hyperparameter values for your model
- Performed a hyperparameter sweep
- Deployed your model
- Visualized detections
Learn how to set up AutoML to train computer vision models by using Python.
Learn how to configure incremental training on computer vision models.
See what hyperparameters are available for computer vision tasks.
View code examples:
APPLIES TO:
Azure CLI ml extension v2 (current)- Review detailed code examples and use cases in the azureml-examples repository for automated machine learning samples. See the folders that have the cli-automl-image- prefix for samples that are specific to creating computer vision models.
Note
Use of the fridge objects dataset is available through the license under the MIT License.