$schema: https://azuremlschemas.azureedge.net/latest/batchEndpoint.schema.json
name: imagenet-classifier-batch
description: A batch endpoint for performing image classification using a TFHub model ImageNet model.
auth_mode: aad_token
运行以下代码以创建终结点。
az ml batch-endpoint create --file endpoint.yml --name $ENDPOINT_NAME
确定终结点的名称:
endpoint_name="imagenet-classifier-batch"
配置终结点:
endpoint = BatchEndpoint(
name=endpoint_name,
description="An batch service to perform ImageNet image classification",
)
MODEL_NAME='imagenet-classifier'
az ml model create --name $MODEL_NAME --path "model"
model_name = 'imagenet-classifier'
model = ml_client.models.create_or_update(
Model(name=model_name, path=model_path, type=AssetTypes.CUSTOM_MODEL)
)
创建评分脚本
我们需要创建评分脚本,该脚本应能读取批处理部署提供的映像并返回模型的分数。 以下脚本:
表示init使用tensorflow中模块keras加载模型的函数。
表示run为批处理部署提供的每个小型批处理执行的函数。
函数run一次读取文件的一个图像
该run方法可将图像大小调整为模型的预期大小。
该run方法将图像重新缩放到范围[0,1]域,即模型的预期内容。
它会返回与预测有关的类和概率。
code/score-by-file/batch_driver.py
import os
import numpy as np
import pandas as pd
import tensorflow as tf
from os.path import basename
from PIL import Image
from tensorflow.keras.models import load_model
def init():
global model
global input_width
global input_height
# AZUREML_MODEL_DIR is an environment variable created during deployment
model_path = os.path.join(os.environ["AZUREML_MODEL_DIR"], "model")
# load the model
model = load_model(model_path)
input_width = 244
input_height = 244
def run(mini_batch):
results = []
for image in mini_batch:
data = Image.open(image).resize(
(input_width, input_height)
) # Read and resize the image
data = np.array(data) / 255.0 # Normalize
data_batch = tf.expand_dims(
data, axis=0
) # create a batch of size (1, 244, 244, 3)
# perform inference
pred = model.predict(data_batch)
# Compute probabilities, classes and labels
pred_prob = tf.math.reduce_max(tf.math.softmax(pred, axis=-1)).numpy()
pred_class = tf.math.argmax(pred, axis=-1).numpy()
results.append([basename(image), pred_class[0], pred_prob])
return pd.DataFrame(results)
$schema: https://azuremlschemas.azureedge.net/latest/data.schema.json
name: imagenet-sample-unlabeled
description: A sample of 1000 images from the original ImageNet dataset. Download content from https://azuremlexampledata.blob.core.chinacloudapi.cn/data/imagenet-1000.zip.
type: uri_folder
path: data
然后,创建数据资产:
az ml data create -f imagenet-sample-unlabeled.yml
data_path = "data"
dataset_name = "imagenet-sample-unlabeled"
imagenet_sample = Data(
path=data_path,
type=AssetTypes.URI_FOLDER,
description="A sample of 1000 images from the original ImageNet dataset",
name=dataset_name,
)
import os
import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow.keras.models import load_model
def init():
global model
global input_width
global input_height
# AZUREML_MODEL_DIR is an environment variable created during deployment
model_path = os.path.join(os.environ["AZUREML_MODEL_DIR"], "model")
# load the model
model = load_model(model_path)
input_width = 244
input_height = 244
def decode_img(file_path):
file = tf.io.read_file(file_path)
img = tf.io.decode_jpeg(file, channels=3)
img = tf.image.resize(img, [input_width, input_height])
return img / 255.0
def run(mini_batch):
images_ds = tf.data.Dataset.from_tensor_slices(mini_batch)
images_ds = images_ds.map(decode_img).batch(64)
# perform inference
pred = model.predict(images_ds)
# Compute probabilities, classes and labels
pred_prob = tf.math.reduce_max(tf.math.softmax(pred, axis=-1)).numpy()
pred_class = tf.math.argmax(pred, axis=-1).numpy()
return pd.DataFrame(
[mini_batch, pred_prob, pred_class], columns=["file", "probability", "class"]
)
> [!TIP]
> * Notice that 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`.
> * 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 will need to carefully tune this parameter to achieve the maximum utilization of the GPU just before getting an OOM exception.
> * Once predictions are computed, the tensors are converted to `numpy.ndarray`.