(预览)无代码模型部署(Preview) No-code model deployment

无代码模型部署目前处于预览阶段,支持以下机器学习框架:No-code model deployment is currently in preview and supports the following machine learning frameworks:

TensorFlow SavedModel 格式TensorFlow SavedModel format

需要以 SavedModel 格式注册 TensorFlow 模型,才能进行无代码模型部署。TensorFlow models need to be registered in SavedModel format to work with no-code model deployment.

请查看此链接,了解如何创建 SavedModel。See this link for information on how to create a SavedModel.

我们支持 TensorFlow Serving DockerHub 的“标记”下列出的任何 TensorFlow 版本。We support any TensorFlow version that is listed under "Tags" at the TensorFlow Serving DockerHub.

from azureml.core import Model

model = Model.register(workspace=ws,
                       model_name='flowers',                        # Name of the registered model in your workspace.
                       model_path='./flowers_model',                # Local Tensorflow SavedModel folder to upload and register as a model.
                       model_framework=Model.Framework.TENSORFLOW,  # Framework used to create the model.
                       model_framework_version='1.14.0',            # Version of Tensorflow used to create the model.
                       description='Flowers model')

service_name = 'tensorflow-flower-service'
service = Model.deploy(ws, service_name, [model])

ONNX 模型ONNX models

任何 ONNX 推理图都支持 ONNX 模型注册和部署。ONNX model registration and deployment is supported for any ONNX inference graph. 当前不支持预处理和后续处理步骤。Preprocess and postprocess steps are not currently supported.

下面是有关如何注册和部署 MNIST ONNX 模型的示例:Here is an example of how to register and deploy an MNIST ONNX model:

from azureml.core import Model

model = Model.register(workspace=ws,
                       model_name='mnist-sample',                  # Name of the registered model in your workspace.
                       model_path='mnist-model.onnx',              # Local ONNX model to upload and register as a model.
                       model_framework=Model.Framework.ONNX ,      # Framework used to create the model.
                       model_framework_version='1.3',              # Version of ONNX used to create the model.
                       description='Onnx MNIST model')

service_name = 'onnx-mnist-service'
service = Model.deploy(ws, service_name, [model])

若要对模型进行评分,请参阅使用部署为 Web 服务的 Azure 机器学习模型To score a model, see Consume an Azure Machine Learning model deployed as a web service. 许多 ONNX 项目使用 protobuf 文件来紧凑存储训练和验证数据,这样就可能难以知道服务所需的数据格式。Many ONNX projects use protobuf files to compactly store training and validation data, which can make it difficult to know what the data format expected by the service. 作为模型开发人员,你应该为开发人员提供文档:As a model developer, you should document for your developers:

  • 输入格式(JSON 或二进制)Input format (JSON or binary)
  • 输入数据形状和类型(例如,形状为 [100,100,3] 的浮点数组)Input data shape and type (for example, an array of floats of shape [100,100,3])
  • 域信息(例如,有关图像、颜色空间、组件顺序以及值是否已规范化的信息)Domain information (for instance, for an image, the color space, component order, and whether the values are normalized)

如果使用的是 Pytorch,请阅读将模型从 PyTorch 导出到 ONNX,详细了解转换和限制。If you're using Pytorch, Exporting models from PyTorch to ONNX has the details on conversion and limitations.

Scikit-learn 模型Scikit-learn models

所有内置 scikit-learn 模型类型都支持无代码模型部署。No code model deployment is supported for all built-in scikit-learn model types.

下面的示例演示如何在不使用额外代码的情况下注册和部署 sklearn 模型:Here is an example of how to register and deploy a sklearn model with no extra code:

from azureml.core import Model
from azureml.core.resource_configuration import ResourceConfiguration

model = Model.register(workspace=ws,
                       model_name='my-sklearn-model',                # Name of the registered model in your workspace.
                       model_path='./sklearn_regression_model.pkl',  # Local file to upload and register as a model.
                       model_framework=Model.Framework.SCIKITLEARN,  # Framework used to create the model.
                       model_framework_version='0.19.1',             # Version of scikit-learn used to create the model.
                       resource_configuration=ResourceConfiguration(cpu=1, memory_in_gb=0.5),
                       description='Ridge regression model to predict diabetes progression.',
                       tags={'area': 'diabetes', 'type': 'regression'})
                       
service_name = 'my-sklearn-service'
service = Model.deploy(ws, service_name, [model])

备注

默认情况下,支持 predict_proba 的模型会使用该方法。Models that support predict_proba will use that method by default. 要重写此内容以使用预测,可以修改 POST 正文,如下所示:To override this to use predict you can modify the POST body as below:

import json


input_payload = json.dumps({
    'data': [
        [ 0.03807591,  0.05068012,  0.06169621, 0.02187235, -0.0442235,
         -0.03482076, -0.04340085, -0.00259226, 0.01990842, -0.01764613]
    ],
    'method': 'predict'  # If you have a classification model, the default behavior is to run 'predict_proba'.
})

output = service.run(input_payload)

print(output)

备注

这些依赖项包含在预建的 scikit-learn 推理容器中:These dependencies are included in the prebuilt scikit-learn inference container:

    - dill
    - azureml-defaults
    - inference-schema[numpy-support]
    - scikit-learn
    - numpy
    - joblib
    - pandas
    - scipy
    - sklearn_pandas

后续步骤Next steps