Automating Model Deployment with Azure ML Pipelines
Automating Model Deployment with Azure ML Pipelines 🚀
To fully automate your ML model training, registration, and deployment in Azure ML using Azure ML Pipelines, follow these structured steps:
1. Setup and Configuration
a. Install Required Libraries
pip install azureml-sdk mlflow azureml-mlflow azureml-pipeline-core azureml-pipeline-steps azureml-train-automl-client
b. Create and Connect to Azure ML Workspace
from azureml.core import Workspace
ws = Workspace.from_config() # Load existing workspace
print(ws.name, ws.resource_group, ws.location)
2. Define Azure ML Pipeline Components
a. Create a Compute Cluster for Training
from azureml.core.compute import AmlCompute, ComputeTarget
compute_name = "cpu-cluster"
compute_config = AmlCompute.provisioning_configuration(vm_size="STANDARD_DS3_V2", max_nodes=4)
if compute_name not in ws.compute_targets:
compute_target = ComputeTarget.create(ws, compute_name, compute_config)
compute_target.wait_for_completion(show_output=True)
b. Define Data Input and Output for Pipeline
from azureml.data import DataReference
from azureml.core import Dataset
datastore = ws.get_default_datastore()
data_ref = DataReference(datastore=datastore, path_on_datastore="training-data/")
c. Create Training Step Using MLflow
Training Script (train.py)
import mlflow
import mlflow.sklearn
from sklearn.linear_model import LogisticRegression
import argparse
import os
# Parse arguments
parser = argparse.ArgumentParser()
parser.add_argument("--output_dir", type=str, required=True)
args = parser.parse_args()
# Enable MLflow logging
mlflow.sklearn.autolog()
# Train model
model = LogisticRegression()
mlflow.log_param("solver", "liblinear")
mlflow.log_metric("accuracy", 0.92)
# Save model
mlflow.sklearn.log_model(model, args.output_dir)
Pipeline Training Step
from azureml.pipeline.steps import PythonScriptStep
from azureml.pipeline.core import PipelineData
model_output = PipelineData("model_output", datastore=datastore)
train_step = PythonScriptStep(
script_name="train.py",
arguments=["--output_dir", model_output],
inputs=[data_ref],
outputs=[model_output],
compute_target=compute_target,
source_directory="./scripts",
)
3. Register Model in Azure ML
from azureml.core.model import Model
register_step = Model.register(
workspace=ws,
model_path=model_output, # Path from pipeline output
model_name="my_model",
)
4. Define Deployment Step
a. Create Inference Configuration
Scoring Script (score.py)
import json
import mlflow.sklearn
from azureml.core.model import Model
def init():
global model
model_path = Model.get_model_path("my_model")
model = mlflow.sklearn.load_model(model_path)
def run(data):
input_data = json.loads(data)["data"]
prediction = model.predict(input_data)
return json.dumps({"prediction": prediction.tolist()})
Inference Environment
from azureml.core.environment import Environment
from azureml.core.model import InferenceConfig
env = Environment("inference-env")
env.python.conda_dependencies.add_pip_package("scikit-learn")
inference_config = InferenceConfig(entry_script="score.py", environment=env)
Deploy to Azure Container Instance (ACI)
from azureml.core.webservice import AciWebservice
deployment_config = AciWebservice.deploy_configuration(cpu_cores=1, memory_gb=1)
deploy_step = Model.deploy(
workspace=ws,
name="my-mlflow-service",
models=[register_step],
inference_config=inference_config,
deployment_config=deployment_config,
)
5. Build and Run the Pipeline
from azureml.pipeline.core import Pipeline
from azureml.pipeline.core import PipelineRun
from azureml.core.experiment import Experiment
pipeline = Pipeline(workspace=ws, steps=[train_step, register_step, deploy_step])
pipeline_run = Experiment(ws, "mlflow-pipeline").submit(pipeline)
pipeline_run.wait_for_completion(show_output=True)
6. Test the Deployed Model
import requests
input_data = json.dumps({"data": [[0.1, 0.2, 0.3, 0.4]]})
headers = {"Content-Type": "application/json"}
service = ws.webservices["my-mlflow-service"]
response = requests.post(service.scoring_uri, data=input_data, headers=headers)
print(response.json())
7. Automate with Azure ML Pipelines in CI/CD
To automate deployments in CI/CD, integrate this script into GitHub Actions or Azure DevOps Pipelines using az ml CLI commands.
✅ Summary of Automation:
1️⃣ Set up workspace & compute cluster
2️⃣ Define ML pipeline for training & registration
3️⃣ Deploy model using MLflow
4️⃣ Test API and automate pipeline execution
This ensures a fully automated ML pipeline from development to deployment in Azure ML! 🚀
Comments