Skip to content

Commit 67bf9bb

Browse files
authored
Batch Endpoints scenarios (Azure#1777)
* batch scenarios remove model * black * comments * typo * typos * formatting * build * batch.sh fix
1 parent 994f623 commit 67bf9bb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+80860
-45
lines changed

.github/workflows/sdk-endpoints-batch-mnist-nonmlflow.yml

+9-9
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ jobs:
3737
run: bash setup.sh
3838
working-directory: cli
3939
continue-on-error: true
40-
- name: run endpoints/batch/mnist-nonmlflow.ipynb
40+
- name: run endpoints/batch/mnist-batch.ipynb
4141
run: |
42-
sed -i -e "s/<SUBSCRIPTION_ID>/6560575d-fa06-4e7d-95fb-f962e74efd7a/g" mnist-nonmlflow.ipynb
43-
sed -i -e "s/<RESOURCE_GROUP>/azureml-examples/g" mnist-nonmlflow.ipynb
44-
sed -i -e "s/<AML_WORKSPACE_NAME>/main/g" mnist-nonmlflow.ipynb
45-
sed -i -e "s/DefaultAzureCredential/AzureCliCredential/g" mnist-nonmlflow.ipynb
42+
sed -i -e "s/<SUBSCRIPTION_ID>/6560575d-fa06-4e7d-95fb-f962e74efd7a/g" mnist-batch.ipynb
43+
sed -i -e "s/<RESOURCE_GROUP>/azureml-examples/g" mnist-batch.ipynb
44+
sed -i -e "s/<AML_WORKSPACE_NAME>/main/g" mnist-batch.ipynb
45+
sed -i -e "s/DefaultAzureCredential/AzureCliCredential/g" mnist-batch.ipynb
4646
47-
papermill -k python mnist-nonmlflow.ipynb mnist-nonmlflow.output.ipynb
47+
papermill -k python mnist-batch.ipynb mnist-batch.output.ipynb
4848
working-directory: sdk/python/endpoints/batch
4949
- name: upload notebook's working folder as an artifact
5050
if: ${{ always() }}
@@ -63,11 +63,11 @@ jobs:
6363
private_key: ${{ secrets.AZUREML_ICM_CONNECTOR_PRIVATE_KEY }}
6464
args: |
6565
incident:
66-
Title: "[azureml-examples] Notebook validation failed on branch '${{ github.ref_name }}' for notebook 'endpoints/batch/mnist-nonmlflow.ipynb'"
66+
Title: "[azureml-examples] Notebook validation failed on branch '${{ github.ref_name }}' for notebook 'endpoints/batch/mnist-batch.ipynb'"
6767
Summary: |
68-
Notebook 'endpoints/batch/mnist-nonmlflow.ipynb' is failing on branch '${{ github.ref_name }}': ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
68+
Notebook 'endpoints/batch/mnist-batch.ipynb' is failing on branch '${{ github.ref_name }}': ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
6969
Severity: 4
7070
RoutingId: "github://azureml-examples"
7171
Status: Active
7272
Source:
73-
IncidentId: "endpoints/batch/mnist-nonmlflow.ipynb[${{ github.ref_name }}]"
73+
IncidentId: "endpoints/batch/mnist-batch.ipynb[${{ github.ref_name }}]"

cli/batch-score.sh

+20-20
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ set -e
44

55
# <set_variables>
66
export ENDPOINT_NAME="<YOUR_ENDPOINT_NAME>"
7+
export DEPLOYMENT_NAME="<YOUR_DEPLOYMENT_NAME>"
78
# </set_variables>
89

910
export ENDPOINT_NAME=endpt-`echo $RANDOM`
11+
export DEPLOYMENT_NAME="mnist-torch-dpl"
1012

1113
echo "Creating compute"
1214
# <create_compute>
@@ -20,7 +22,7 @@ az ml batch-endpoint create --name $ENDPOINT_NAME
2022

2123
echo "Creating batch deployment nonmlflowdp for endpoint $ENDPOINT_NAME"
2224
# <create_batch_deployment_set_default>
23-
az ml batch-deployment create --name nonmlflowdp --endpoint-name $ENDPOINT_NAME --file endpoints/batch/nonmlflow-deployment.yml --set-default
25+
az ml batch-deployment create --file endpoints/batch/mnist-torch-deployment.yml --endpoint-name $ENDPOINT_NAME --set-default
2426
# </create_batch_deployment_set_default>
2527

2628
echo "Showing details of the batch endpoint"
@@ -30,7 +32,7 @@ az ml batch-endpoint show --name $ENDPOINT_NAME
3032

3133
echo "Showing details of the batch deployment"
3234
# <check_batch_deployment_detail>
33-
az ml batch-deployment show --name nonmlflowdp --endpoint-name $ENDPOINT_NAME
35+
az ml batch-deployment show --name $DEPLOYMENT_NAME --endpoint-name $ENDPOINT_NAME
3436
# </check_batch_deployment_detail>
3537

3638
echo "Invoking batch endpoint with public URI (MNIST)"
@@ -67,9 +69,15 @@ fi
6769
echo "Invoke batch endpoint with specific output file name"
6870
# <start_batch_scoring_job_configure_output_settings>
6971
export OUTPUT_FILE_NAME=predictions_`echo $RANDOM`.csv
70-
JOB_NAME=$(az ml batch-endpoint invoke --name $ENDPOINT_NAME --input https://pipelinedata.blob.core.windows.net/sampledata/mnist --input-type uri_folder --output-path azureml://datastores/workspaceblobstore/paths/$ENDPOINT_NAME --set output_file_name=$OUTPUT_FILE_NAME --mini-batch-size 20 --instance-count 5 --query name -o tsv)
72+
JOB_NAME=$(az ml batch-endpoint invoke --name $ENDPOINT_NAME --input https://pipelinedata.blob.core.windows.net/sampledata/mnist --input-type uri_folder --output-path azureml://datastores/workspaceblobstore/paths/$ENDPOINT_NAME --set output_file_name=$OUTPUT_FILE_NAME --query name -o tsv)
7173
# </start_batch_scoring_job_configure_output_settings>
7274

75+
echo "Invoke batch endpoint with specific overwrites"
76+
# <start_batch_scoring_job_overwrite>
77+
export OUTPUT_FILE_NAME=predictions_`echo $RANDOM`.csv
78+
JOB_NAME=$(az ml batch-endpoint invoke --name $ENDPOINT_NAME --input https://pipelinedata.blob.core.windows.net/sampledata/mnist --input-type uri_folder --mini-batch-size 20 --instance-count 5 --query name -o tsv)
79+
# </start_batch_scoring_job_overwrite>
80+
7381
echo "Stream job detail"
7482
# <stream_job_logs_to_console>
7583
az ml job stream -n $JOB_NAME
@@ -93,17 +101,19 @@ fi
93101

94102
echo "List all jobs under the batch deployment"
95103
# <list_all_jobs>
96-
az ml batch-deployment list-jobs --name nonmlflowdp --endpoint-name $ENDPOINT_NAME --query [].name
104+
az ml batch-deployment list-jobs --name $DEPLOYMENT_NAME --endpoint-name $ENDPOINT_NAME --query [].name
97105
# </list_all_jobs>
98106

99-
echo "Create a new batch deployment (mlflow-nyctaxi), not setting it as default this time"
107+
echo "Create a new batch deployment (mnist-keras-dpl), not setting it as default this time"
100108
# <create_new_deployment_not_default>
101-
az ml batch-deployment create --name mlflowdp --endpoint-name $ENDPOINT_NAME --file endpoints/batch/mlflow-deployment.yml
109+
az ml batch-deployment create --file endpoints/batch/mnist-keras-deployment.yml --endpoint-name $ENDPOINT_NAME
102110
# </create_new_deployment_not_default>
103111

104112
echo "Invoke batch endpoint with public data"
105113
# <test_new_deployment>
106-
JOB_NAME=$(az ml batch-endpoint invoke --name $ENDPOINT_NAME --deployment-name mlflowdp --input https://pipelinedata.blob.core.windows.net/sampledata/nytaxi/taxi-tip-data.csv --input-type uri_file --query name -o tsv)
114+
DEPLOYMENT_NAME="mnist-keras-dpl"
115+
JOB_NAME=$(az ml batch-endpoint invoke --name $ENDPOINT_NAME --deployment-name $DEPLOYMENT_NAME --input https://pipelinedata.blob.core.windows.net/sampledata/mnist --input-type uri_folder --query name -o tsv)
116+
# </test_new_deployment>
107117

108118
echo "Show job detail"
109119
# <show_job_in_studio>
@@ -130,30 +140,20 @@ else
130140
exit 2
131141
fi
132142
# </check_job_status>
133-
# </test_new_deployment>
134143

135144
echo "Update the batch deployment as default for the endpoint"
136145
# <update_default_deployment>
137-
az ml batch-endpoint update --name $ENDPOINT_NAME --set defaults.deployment_name=mlflowdp
146+
az ml batch-endpoint update --name $ENDPOINT_NAME --set defaults.deployment_name=$DEPLOYMENT_NAME
138147
# </update_default_deployment>
139148

140149
echo "Verify default deployment. In this example, it should be mlflowdp."
141150
# <verify_default_deployment>
142151
az ml batch-endpoint show --name $ENDPOINT_NAME --query "{Name:name, Defaults:defaults}"
143-
144-
DEFAULT_DEPL_NAME=$(az ml batch-endpoint show --name $ENDPOINT_NAME --query defaults.deployment_name)
145-
if [[ $DEFAULT_DEPL_NAME == \"mlflowdp\" ]]
146-
then
147-
echo "mlflowdp is set as default successfully"
148-
else
149-
echo "default deployment set failed"
150-
exit 1
151-
fi
152152
# </verify_default_deployment>
153153

154154
echo "Invoke batch endpoint with the new default deployment with public URI"
155155
# <test_new_default_deployment>
156-
JOB_NAME=$(az ml batch-endpoint invoke --name $ENDPOINT_NAME --input https://pipelinedata.blob.core.windows.net/sampledata/nytaxi/taxi-tip-data.csv --input-type uri_file --query name -o tsv)
156+
JOB_NAME=$(az ml batch-endpoint invoke --name $ENDPOINT_NAME --input https://pipelinedata.blob.core.windows.net/sampledata/mnist --input-type uri_folder --query name -o tsv)
157157
# </test_new_default_deployment>
158158

159159
echo "Stream job logs to console"
@@ -196,7 +196,7 @@ RESPONSE=$(curl --location --request POST "$SCORING_URI" \
196196
\"properties\": {
197197
\"dataset\": {
198198
\"dataInputType\": \"DataUrl\",
199-
\"Path\": \"https://pipelinedata.blob.core.windows.net/sampledata/nytaxi/taxi-tip-data.csv\"
199+
\"Path\": \"https://pipelinedata.blob.core.windows.net/sampledata/mnist\"
200200
}
201201
}
202202
}")
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
$schema: https://azuremlschemas.azureedge.net/latest/batchEndpoint.schema.json
2-
name: mybatchedp
3-
description: my sample batch endpoint
2+
name: mnist-batch
3+
description: A batch endpoint for scoring images from the MNIST dataset.
44
auth_mode: aad_token
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
$schema: https://azuremlschemas.azureedge.net/latest/batchDeployment.schema.json
2+
name: mnist-keras-dpl
3+
description: A deployment using Keras with TensorFlow to solve the MNIST classification dataset.
4+
endpoint_name: mnist-batch
5+
model:
6+
path: ./mnist-keras/model/
7+
code_configuration:
8+
code: ./mnist-keras/code/
9+
scoring_script: batch_driver.py
10+
environment:
11+
conda_file: ./mnist-keras/environment/conda.yml
12+
image: mcr.microsoft.com/azureml/openmpi3.1.2-ubuntu18.04:latest
13+
compute: azureml:batch-cluster
14+
resources:
15+
instance_count: 1
16+
max_concurrency_per_instance: 2
17+
mini_batch_size: 10
18+
output_action: append_row
19+
output_file_name: predictions.csv
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import os
2+
import numpy as np
3+
import pandas as pd
4+
import tensorflow as tf
5+
from os.path import basename
6+
from PIL import Image
7+
from tensorflow.keras.models import load_model
8+
9+
10+
def init():
11+
global model
12+
13+
# AZUREML_MODEL_DIR is an environment variable created during deployment
14+
model_path = os.path.join(os.environ["AZUREML_MODEL_DIR"], "model")
15+
16+
# load the model
17+
model = load_model(model_path)
18+
19+
20+
def run(mini_batch):
21+
results = []
22+
23+
for image in mini_batch:
24+
data = Image.open(image)
25+
data = np.array(data)
26+
data_batch = tf.expand_dims(data, axis=0)
27+
28+
# perform inference
29+
pred = model.predict(data_batch)
30+
31+
# Compute probabilities, classes and labels
32+
pred_prob = tf.math.reduce_max(tf.math.softmax(pred, axis=-1)).numpy()
33+
pred_class = tf.math.argmax(pred, axis=-1).numpy()
34+
35+
results.append([basename(image), pred_class[0], pred_prob])
36+
37+
return pd.DataFrame(results)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name: tensorflow-env
2+
channels:
3+
- conda-forge
4+
dependencies:
5+
- python=3.7
6+
- pip
7+
- pip:
8+
- pandas
9+
- tensorflow
10+
- pillow
11+
- azureml-core
12+
- azureml-dataset-runtime[fuse]

0 commit comments

Comments
 (0)