Skip to content

Commit bfd72ad

Browse files
Additional edits.
1 parent e25c110 commit bfd72ad

File tree

2 files changed

+77
-70
lines changed

2 files changed

+77
-70
lines changed

articles/machine-learning/how-to-image-processing-batch.md

Lines changed: 62 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ ms.custom: devplatv2, update-code, devx-track-azurecli
1818

1919
[!INCLUDE [ml v2](includes/machine-learning-dev-v2.md)]
2020

21-
Batch model deployments can be used for processing tabular data, but also any other file type like images. Those deployments are supported in both MLflow and custom models. In this article, you learn how to deploy a model that classifies images according to the ImageNet taxonomy.
21+
You can use batch model deployments for processing tabular data, but also any other file types, like images. Those deployments are supported in both MLflow and custom models. In this article, you learn how to deploy a model that classifies images according to the ImageNet taxonomy.
2222

2323
## Prerequisites
2424

@@ -31,7 +31,7 @@ The model that this article works with was built using TensorFlow along with the
3131
- It works with images of size 244x244 (tensors of `(224, 224, 3)`).
3232
- It requires inputs to be scaled to the range `[0,1]`.
3333

34-
The information in this article is based on code samples contained in the [azureml-examples](https://github.com/azure/azureml-examples) repository. To run the commands locally without having to copy/paste YAML and other files, clone the repo. Change directories to `cli/endpoints/batch/deploy-models/imagenet-classifier` if you're using the Azure CLI or `sdk/python/endpoints/batch/deploy-models/imagenet-classifier` if you're using the SDK for Python.
34+
The information in this article is based on code samples contained in the [azureml-examples](https://github.com/azure/azureml-examples) repository. To run the commands locally without having to copy/paste YAML and other files, clone the repo. Change directories to *cli/endpoints/batch/deploy-models/imagenet-classifier* if you're using the Azure CLI or *sdk/python/endpoints/batch/deploy-models/imagenet-classifier* if you're using the SDK for Python.
3535

3636
```azurecli
3737
git clone https://github.com/Azure/azureml-examples --depth 1
@@ -51,7 +51,7 @@ Create the endpoint that hosts the model:
5151

5252
# [Azure CLI](#tab/cli)
5353

54-
1. Decide on the name of the endpoint:
54+
1. Specify the name of the endpoint.
5555

5656
```azurecli
5757
ENDPOINT_NAME="imagenet-classifier-batch"
@@ -61,19 +61,19 @@ Create the endpoint that hosts the model:
6161

6262
:::code language="yaml" source="~/azureml-examples-main/cli/endpoints/batch/deploy-models/imagenet-classifier/endpoint.yml":::
6363

64-
To create the endpoint, run the following code.
64+
To create the endpoint, run the following code:
6565

6666
:::code language="azurecli" source="~/azureml-examples-main/cli/endpoints/batch/deploy-models/imagenet-classifier/deploy-and-run.sh" ID="create_endpoint" :::
6767

6868
# [Python](#tab/python)
6969

70-
1. Decide on the name of the endpoint:
70+
1. Specify the name of the endpoint.
7171

7272
```python
7373
endpoint_name="imagenet-classifier-batch"
7474
```
7575

76-
1. Configure the endpoint:
76+
1. Configure the endpoint.
7777

7878
```python
7979
endpoint = BatchEndpoint(
@@ -94,7 +94,7 @@ Create the endpoint that hosts the model:
9494

9595
Model deployments can only deploy registered models. You need to register the model. You can skip this step if the model you're trying to deploy is already registered.
9696

97-
1. Download a copy of the model:
97+
1. Download a copy of the model.
9898

9999
# [Azure CLI](#tab/cli)
100100

@@ -114,7 +114,7 @@ Model deployments can only deploy registered models. You need to register the mo
114114
model_path = zip.extractall(path="imagenet-classifier")
115115
```
116116

117-
1. Register the model:
117+
1. Register the model.
118118

119119
# [Azure CLI](#tab/cli)
120120

@@ -134,33 +134,35 @@ Model deployments can only deploy registered models. You need to register the mo
134134

135135
### Create a scoring script
136136

137-
Create a scoring script that can read the images provided by the batch deployment and return the scores of the model. The following script:
137+
Create a scoring script that can read the images provided by the batch deployment and return the scores of the model.
138138

139139
> [!div class="checklist"]
140-
> - Indicates an `init` function that load the model using `keras` module in `tensorflow`.
141-
> - Indicates a `run` function that is executed for each mini-batch the batch deployment provides.
142-
> - The `run` function read one image of the file at a time
140+
> - The `init` method loads the model using the `keras` module in `tensorflow`.
141+
> - The `run` method runs for each mini-batch the batch deployment provides.
142+
> - The `run` method reads one image of the file at a time.
143143
> - The `run` method resizes the images to the expected sizes for the model.
144144
> - The `run` method rescales the images to the range `[0,1]` domain, which is what the model expects.
145-
> - It returns the classes and the probabilities associated with the predictions.
145+
> - The script returns the classes and the probabilities associated with the predictions.
146146
147-
**code/score-by-file/batch_driver.py**:
147+
This code is the *code/score-by-file/batch_driver.py* file:
148148

149149
:::code language="python" source="~/azureml-examples-main/cli/endpoints/batch/deploy-models/imagenet-classifier/code/score-by-file/batch_driver.py" :::
150150

151151
> [!TIP]
152-
> Although images are provided in mini-batches by the deployment, this scoring script processes one image at a time. This is a common pattern because trying to load the entire batch and send it to the model at once might result in high-memory pressure on the batch executor (OOM exceptions). However, there are certain cases where doing so enables high throughput in the scoring task. This is the case for batch deployments over GPU hardware where you want to achieve high GPU utilization. For an example of a scoring script that takes advantage of this approach, see [High throughput deployments](#high-throughput-deployments).
152+
> Although images are provided in mini-batches by the deployment, this scoring script processes one image at a time. This is a common pattern because trying to load the entire batch and send it to the model at once might result in high-memory pressure on the batch executor (OOM exceptions).
153+
>
154+
> There are certain cases where doing so enables high throughput in the scoring task. This is the case for batch deployments over GPU hardware where you want to achieve high GPU utilization. For a scoring script that takes advantage of this approach, see [High throughput deployments](#high-throughput-deployments).
153155
154156
> [!NOTE]
155-
> If you are trying to deploy a generative model, which generates files, read how to author a scoring script as explained at [Customize outputs in batch deployments](how-to-deploy-model-custom-output.md).
157+
> If you want to deploy a generative model, which generates files, learn how to author a scoring script: [Customize outputs in batch deployments](how-to-deploy-model-custom-output.md).
156158
157159
### Creating the deployment
158160

159161
After you create the scoring script, create a batch deployment for it. Use the following procedure:
160162

161163
1. Ensure that you have a compute cluster created where you can create the deployment. In this example, use a compute cluster named `gpu-cluster`. Although not required, using GPUs speeds up the processing.
162164

163-
1. Indicate over which environment to run the deployment. In this example, the model runs on `TensorFlow`. Azure Machine Learning already has an environment with the required software installed, so you can reuse this environment. You need to add a couple of dependencies in a `conda.yml` file.
165+
1. Indicate over which environment to run the deployment. In this example, the model runs on `TensorFlow`. Azure Machine Learning already has an environment with the required software installed, so you can reuse this environment. You need to add a couple of dependencies in a *conda.yml* file.
164166

165167
# [Azure CLI](#tab/cli)
166168

@@ -170,7 +172,7 @@ After you create the scoring script, create a batch deployment for it. Use the f
170172

171173
# [Python](#tab/python)
172174

173-
Get a reference to the environment:
175+
Get a reference to the environment.
174176

175177
```python
176178
environment = Environment(
@@ -180,15 +182,15 @@ After you create the scoring script, create a batch deployment for it. Use the f
180182
)
181183
```
182184

183-
1. Create the deployment:
185+
1. Create the deployment.
184186

185187
# [Azure CLI](#tab/cli)
186188

187-
To create a new deployment under the created endpoint, create a `YAML` configuration like the following example. You can check the [full batch endpoint YAML schema](reference-yaml-endpoint-batch.md) for extra properties.
189+
To create a new deployment under the created endpoint, create a `YAML` configuration like the following example. For other properties, see the [full batch endpoint YAML schema](reference-yaml-endpoint-batch.md).
188190

189191
:::code language="yaml" source="~/azureml-examples-main/cli/endpoints/batch/deploy-models/imagenet-classifier/deployment-by-file.yml":::
190192

191-
Then, create the deployment with the following command:
193+
Create the deployment with the following command:
192194

193195
:::code language="azurecli" source="~/azureml-examples-main/cli/endpoints/batch/deploy-models/imagenet-classifier/deploy-and-run.sh" ID="create_deployment" :::
194196

@@ -224,7 +226,9 @@ After you create the scoring script, create a batch deployment for it. Use the f
224226
ml_client.batch_deployments.begin_create_or_update(deployment)
225227
```
226228

227-
1. Although you can invoke a specific deployment inside of an endpoint, you usually want to invoke the endpoint itself, and let the endpoint decide which deployment to use. Such deployment is named the *default* deployment. This approach gives you the possibility of changing the default deployment - and hence changing the model serving the deployment - without changing the contract with the user invoking the endpoint. Use the following instruction to update the default deployment:
229+
1. Although you can invoke a specific deployment inside of an endpoint, you usually want to invoke the endpoint itself, and let the endpoint decide which deployment to use. Such deployment is called the *default* deployment.
230+
231+
This approach lets you change the default deployment and change the model serving the deployment without changing the contract with the user invoking the endpoint. Use the following code to update the default deployment:
228232

229233
# [Azure Machine Learning CLI](#tab/cli)
230234

@@ -243,16 +247,19 @@ Your batch endpoint is ready to be used.
243247

244248
## Test the deployment
245249

246-
For testing the endpoint, use a sample of 1,000 images from the original ImageNet dataset. Batch endpoints can only process data that is located in the cloud and that is accessible from the Azure Machine Learning workspace. Upload it to an Azure Machine Learning data store. Create a data asset that can be used to invoke the endpoint for scoring. However, batch endpoints accept data that can be placed in multiple type of locations.
250+
For testing the endpoint, use a sample of 1,000 images from the original ImageNet dataset. Batch endpoints can only process data that is located in the cloud and that is accessible from the Azure Machine Learning workspace. Upload it to an Azure Machine Learning data store. Create a data asset that can be used to invoke the endpoint for scoring.
247251

248-
1. Download the associated sample data:
252+
> [!NOTE]
253+
> Batch endpoints accept data that can be placed in multiple types of locations.
254+
255+
1. Download the associated sample data.
249256

250257
# [Azure CLI](#tab/cli)
251258

252259
:::code language="azurecli" source="~/azureml-examples-main/cli/endpoints/batch/deploy-models/imagenet-classifier/deploy-and-run.sh" ID="download_sample_data" :::
253260

254261
> [!NOTE]
255-
> If you don't have `wget` installed locally, install it or use a browser to get the .*zip* file.
262+
> If you don't have `wget` installed locally, install it or use a browser to get the *.zip* file.
256263
257264
# [Python](#tab/python)
258265

@@ -267,41 +274,41 @@ For testing the endpoint, use a sample of 1,000 images from the original ImageNe
267274

268275
1. Create a data asset definition in a `YAML` file called *imagenet-sample-unlabeled.yml*:
269276

270-
:::code language="yaml" source="~/azureml-examples-main/cli/endpoints/batch/deploy-models/imagenet-classifier/imagenet-sample-unlabeled.yml":::
277+
:::code language="yaml" source="~/azureml-examples-main/cli/endpoints/batch/deploy-models/imagenet-classifier/imagenet-sample-unlabeled.yml":::
271278

272-
1. Create the data asset:
279+
1. Create the data asset.
273280

274-
:::code language="azurecli" source="~/azureml-examples-main/cli/endpoints/batch/deploy-models/imagenet-classifier/deploy-and-run.sh" ID="create_sample_data_asset" :::
281+
:::code language="azurecli" source="~/azureml-examples-main/cli/endpoints/batch/deploy-models/imagenet-classifier/deploy-and-run.sh" ID="create_sample_data_asset" :::
275282

276283
# [Python](#tab/python)
277284

278285
1. Specify these values:
279286

280-
```python
281-
data_path = "data"
282-
dataset_name = "imagenet-sample-unlabeled"
283-
284-
imagenet_sample = Data(
285-
path=data_path,
286-
type=AssetTypes.URI_FOLDER,
287-
description="A sample of 1000 images from the original ImageNet dataset",
288-
name=dataset_name,
289-
)
290-
```
287+
```python
288+
data_path = "data"
289+
dataset_name = "imagenet-sample-unlabeled"
290+
291+
imagenet_sample = Data(
292+
path=data_path,
293+
type=AssetTypes.URI_FOLDER,
294+
description="A sample of 1000 images from the original ImageNet dataset",
295+
name=dataset_name,
296+
)
297+
```
291298

292299
1. Create the data asset.
293300

294-
```python
295-
ml_client.data.create_or_update(imagenet_sample)
296-
```
301+
```python
302+
ml_client.data.create_or_update(imagenet_sample)
303+
```
297304

298-
To get the newly created data asset, use this code:
305+
To get the newly created data asset, use this code:
299306

300-
```python
301-
imagenet_sample = ml_client.data.get(dataset_name, label="latest")
302-
```
307+
```python
308+
imagenet_sample = ml_client.data.get(dataset_name, label="latest")
309+
```
303310

304-
1. When the data is uploaded and ready to be used, invoke the endpoint:
311+
1. When the data is uploaded and ready to be used, invoke the endpoint.
305312

306313
# [Azure CLI](#tab/cli)
307314

@@ -326,9 +333,9 @@ For testing the endpoint, use a sample of 1,000 images from the original ImageNe
326333
---
327334

328335
> [!TIP]
329-
> You don't indicate the deployment name in the invoke operation. That's because the endpoint automatically routes the job to the default deployment. Since the endpoint only has one deployment, that one is the default one. You can target an specific deployment by indicating the argument/parameter `deployment_name`.
336+
> You don't indicate the deployment name in the invoke operation. That's because the endpoint automatically routes the job to the default deployment. Since the endpoint only has one deployment, that one is the default. You can target an specific deployment by indicating the argument/parameter `deployment_name`.
330337
331-
1. A batch job starts as soon as the command returns. You can monitor the status of the job until it finishes:
338+
1. A batch job starts as soon as the command returns. You can monitor the status of the job until it finishes.
332339

333340
# [Azure CLI](#tab/cli)
334341

@@ -340,7 +347,7 @@ For testing the endpoint, use a sample of 1,000 images from the original ImageNe
340347
ml_client.jobs.get(job.name)
341348
```
342349

343-
1. After the deployment finishes, download the predictions:
350+
1. After the deployment finishes, download the predictions.
344351

345352
# [Azure CLI](#tab/cli)
346353

@@ -380,12 +387,12 @@ On those cases, you might want to do inference on the entire batch of data. That
380387
> [!WARNING]
381388
> Some models have a non-linear relationship with the size of the inputs in terms of the memory consumption. To avoid out-of-memory exceptions, batch again (as done in this example) or decrease the size of the batches created by the batch deployment.
382389

383-
1. Creating the scoring script. *code/score-by-batch/batch_driver.py*:
390+
1. Create the scoring script *code/score-by-batch/batch_driver.py*:
384391

385392
:::code language="python" source="~/azureml-examples-main/cli/endpoints/batch/deploy-models/imagenet-classifier/code/score-by-batch/batch_driver.py" :::
386393

387-
- This script is constructing a tensor dataset from the mini-batch sent by the batch deployment. This dataset is preprocessed to obtain the expected tensors for the model using the `map` operation with the function `decode_img`.
388-
- The dataset is batched again (16) send the data to the model. Use this parameter to control how much information you can load into memory and send to the model at once. If running on a GPU, you need to carefully tune this parameter to achieve the maximum usage of the GPU just before getting an OOM exception.
394+
- This script constructs a tensor dataset from the mini-batch sent by the batch deployment. This dataset is preprocessed to obtain the expected tensors for the model using the `map` operation with the function `decode_img`.
395+
- The dataset is batched again (16) to send the data to the model. Use this parameter to control how much information you can load into memory and send to the model at once. If running on a GPU, you need to carefully tune this parameter to achieve the maximum usage of the GPU just before getting an OOM exception.
389396
- After predictions are computed, the tensors are converted to `numpy.ndarray`.
390397

391398
1. Create the deployment.
@@ -440,8 +447,8 @@ On those cases, you might want to do inference on the entire batch of data. That
440447
MLflow models in Batch Endpoints support reading images as input data. Since MLflow deployments don't require a scoring script, have the following considerations when using them:
441448

442449
> [!div class="checklist"]
443-
> - Image files supported includes: *.png*, *.jpg*, *.jpeg*, *.tiff*, *.bmp* and *.gif*.
444-
> - MLflow models should expect to recieve a `np.ndarray` as input that matches the dimensions of the input image. In order to support multiple image sizes on each batch, the batch executor invokes the MLflow model once per image file.
450+
> - Image files supported include: *.png*, *.jpg*, *.jpeg*, *.tiff*, *.bmp*, and *.gif*.
451+
> - MLflow models should expect to receive a `np.ndarray` as input that matches the dimensions of the input image. In order to support multiple image sizes on each batch, the batch executor invokes the MLflow model once per image file.
445452
> - MLflow models are highly encouraged to include a signature. If they do, it must be of type `TensorSpec`. Inputs are reshaped to match tensor's shape if available. If no signature is available, tensors of type `np.uint8` are inferred.
446453
> - For models that include a signature and are expected to handle variable size of images, include a signature that can guarantee it. For instance, the following signature example allows batches of 3 channeled images.
447454

articles/machine-learning/includes/azureml-batch-prereqs.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,29 @@ Before following the steps in this article, make sure you have the following pre
1717

1818
- You need to install the following software to work with Azure Machine Learning:
1919

20-
# [Azure CLI](#tab/cli)
20+
# [Azure CLI](#tab/cli)
2121

22-
Add the [Azure CLI](/cli/azure/) `ml` [extension for Azure Machine Learning](../how-to-configure-cli.md).
22+
Add the [Azure CLI](/cli/azure/) `ml` [extension for Azure Machine Learning](../how-to-configure-cli.md).
2323

24-
```azurecli
25-
az extension add -n ml
26-
```
24+
```azurecli
25+
az extension add -n ml
26+
```
2727

28-
> [!NOTE]
29-
> Pipeline component deployments for Batch Endpoints were introduced in version 2.7 of the `ml` extension for Azure CLI. Use `az extension update --name ml` to get the last version of it.
28+
> [!NOTE]
29+
> Pipeline component deployments for Batch Endpoints were introduced in version 2.7 of the `ml` extension for Azure CLI. Use `az extension update --name ml` to get the last version of it.
3030
31-
# [Python](#tab/python)
31+
# [Python](#tab/python)
3232

33-
Install the [Azure Machine Learning SDK for Python](https://aka.ms/sdk-v2-install).
33+
Install the [Azure Machine Learning SDK for Python](https://aka.ms/sdk-v2-install).
3434

35-
```python
36-
pip install azure-ai-ml
37-
```
35+
```python
36+
pip install azure-ai-ml
37+
```
3838

39-
> [!NOTE]
40-
> Classes `ModelBatchDeployment` and `PipelineComponentBatchDeployment` were introduced in version 1.7.0 of the SDK. Use `pip install -U azure-ai-ml` to get the last version of it.
39+
> [!NOTE]
40+
> Classes `ModelBatchDeployment` and `PipelineComponentBatchDeployment` were introduced in version 1.7.0 of the SDK. Use `pip install -U azure-ai-ml` to get the last version of it.
4141
42-
---
42+
---
4343

4444
### Connect to your workspace
4545

0 commit comments

Comments
 (0)