MLOps项目实战:使用Azure ML将模型部署为生产环境Web服务
【免费下载链接】MLOps MLOps examples 项目地址: https://gitcode.com/gh_mirrors/ml/MLOps
前言
在现代机器学习项目中,模型训练只是整个流程的一部分。将训练好的模型部署到生产环境,使其能够为实际业务提供预测服务,才是真正创造价值的关键环节。本文将基于MLOps项目中的实践案例,详细介绍如何使用Azure ML将糖尿病预测模型部署为生产环境可用的Web服务。
准备工作
在开始部署之前,我们需要确保已完成以下准备工作:
- 已创建Azure ML工作区
- 已将糖尿病数据集(diabetes.csv)上传到工作区的默认数据存储
- 已在工作区注册了"Diabetes Dataset"数据集
- 已训练并注册了至少一个"diabetes_model"模型
如果这些准备工作尚未完成,建议先完成模型训练和数据准备的相关步骤。
连接工作区
首先,我们需要连接到Azure ML工作区。确保已安装最新版的Azure ML SDK:
!pip install --upgrade azureml-sdk[notebooks]
import azureml.core
print("Ready to use Azure ML", azureml.core.VERSION)
然后从保存的配置文件加载工作区:
from azureml.core import Workspace
# 从保存的配置文件加载工作区
ws = Workspace.from_config()
print('Ready to work with', ws.name)
模型部署为Web服务
查看已注册模型
在部署前,我们先查看工作区中已注册的模型:
from azureml.core import Model
for model in Model.list(ws):
print(model.name, 'version:', model.version)
for tag_name in model.tags:
tag = model.tags[tag_name]
print ('\t',tag_name, ':', tag)
for prop_name in model.properties:
prop = model.properties[prop_name]
print ('\t',prop_name, ':', prop)
print('\n')
获取目标模型
获取我们要部署的糖尿病预测模型(默认返回最新版本):
model = ws.models['diabetes_model']
print(model.name, 'version', model.version)
创建服务文件夹
为Web服务文件创建专用文件夹:
import os
folder_name = 'diabetes_service'
# 创建Web服务文件文件夹
experiment_folder = './' + folder_name
os.makedirs(folder_name, exist_ok=True)
print(folder_name, 'folder created.')
创建评分脚本
Web服务需要Python代码来加载输入数据、获取模型并生成预测。我们创建评分文件score_diabetes.py:
%%writefile $folder_name/score_diabetes.py
import json
import numpy as np
import os
import joblib
from azureml.core.model import Model
import azureml.train.automl # AutoML模型需要
# 服务加载时调用
def init():
global model
# 获取模型路径并加载
model_path = Model.get_model_path('diabetes_model')
model = joblib.load(model_path)
# 收到请求时调用
def run(raw_data):
# 获取输入数据 - 待分类患者的特征
data = json.loads(raw_data)['data']
# 从模型获取预测
predictions = model.predict(data)
# 获取每个预测对应的类别名称(0或1)
classnames = ['not-diabetic', 'diabetic']
predicted_classes = []
for prediction in predictions:
predicted_classes.append(classnames[prediction])
# 以JSON格式返回预测
return json.dumps(predicted_classes)
配置环境依赖
Web服务将在容器中运行,需要配置Python依赖环境。我们的评分代码需要scikit-learn:
from azureml.core.conda_dependencies import CondaDependencies
# 添加模型依赖(AzureML默认已包含)
myenv = CondaDependencies()
myenv.add_conda_package("scikit-learn")
myenv.add_pip_package("azureml-sdk[automl]") # AutoML模型需要
# 将环境配置保存为.yml文件
env_file = folder_name + "/diabetes_env.yml"
with open(env_file,"w") as f:
f.write(myenv.serialize_to_string())
print("Saved dependency info in", env_file)
# 打印.yml文件内容
with open(env_file,"r") as f:
print(f.read())
部署Web服务
现在可以部署名为"diabetes-service"的服务了。部署过程包括:
- 定义推理配置(包含评分和环境文件)
- 定义部署配置(指定执行环境)
- 部署模型为Web服务
- 验证部署状态
from azureml.core.webservice import AciWebservice
from azureml.core.model import InferenceConfig
# 配置评分环境
inference_config = InferenceConfig(runtime= "python",
source_directory = folder_name,
entry_script="score_diabetes.py",
conda_file="diabetes_env.yml")
deployment_config = AciWebservice.deploy_configuration(cpu_cores = 1, memory_gb = 1)
service_name = "diabetes-service"
service = Model.deploy(ws, service_name, [model], inference_config, deployment_config)
service.wait_for_deployment(True)
print(service.state)
部署完成后,状态应显示为"Healthy"。
使用Web服务
通过SDK调用服务
部署完成后,我们可以从客户端应用程序使用该服务:
import json
x_new = [[2,180,74,24,21,23.9091702,1.488172308,22]]
print ('Patient: {}'.format(x_new[0]))
# 将数组转换为JSON文档中的可序列化列表
input_json = json.dumps({"data": x_new})
# 调用Web服务,传入输入数据
predictions = service.run(input_data = input_json)
# 获取预测类别
predicted_classes = json.loads(predictions)
print(predicted_classes[0])
也可以一次发送多个患者观察数据:
import json
# 这次我们的输入是两个特征数组
x_new = [[2,180,74,24,21,23.9091702,1.488172308,22],
[0,148,58,11,179,39.19207553,0.160829008,45]]
# 将数组转换为JSON文档中的可序列化列表
input_json = json.dumps({"data": x_new})
# 调用Web服务
predictions = service.run(input_data = input_json)
# 获取预测类别
predicted_classes = json.loads(predictions)
for i in range(len(x_new)):
print ("Patient {}".format(x_new[i]), predicted_classes[i] )
通过HTTP端点调用
在生产环境中,应用程序可能不使用Azure ML SDK,而是直接向Web服务发送HTTP请求。首先获取服务端点:
endpoint = service.scoring_uri
print(endpoint)
然后可以通过HTTP POST请求调用服务:
import requests
import json
x_new = [[2,180,74,24,21,23.9091702,1.488172308,22],
[0,148,58,11,179,39.19207553,0.160829008,45]]
# 将数组转换为JSON文档中的可序列化列表
input_json = json.dumps({"data": x_new})
# 设置内容类型
headers = { 'Content-Type':'application/json' }
predictions = requests.post(endpoint, input_json, headers = headers)
predicted_classes = json.loads(predictions.json())
for i in range(len(x_new)):
print ("Patient {}".format(x_new[i]), predicted_classes[i] )
总结
通过本文,我们完成了以下工作:
- 连接到Azure ML工作区并准备部署环境
- 创建了包含评分逻辑和依赖项的Web服务包
- 将糖尿病预测模型部署为可扩展的Web服务
- 通过SDK和HTTP端点两种方式测试了服务
这种部署方式为实际业务场景提供了可靠的生产级机器学习服务能力,是MLOps实践中模型部署环节的典型实现方案。
【免费下载链接】MLOps MLOps examples 项目地址: https://gitcode.com/gh_mirrors/ml/MLOps
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考



