Quickstart: Train and Deploy an Image Classification Model with Azure AI Machine Learning

This quickstart guide will walk you through the essential steps to train and deploy a simple image classification model using Azure AI Machine Learning. By the end of this guide, you'll have a working model deployed as a web service.

Prerequisites

Before you begin, ensure you have the following:

Step 1: Set up your Azure ML Workspace

If you haven't already, create an Azure AI Machine Learning workspace. This workspace will be your central hub for managing your ML projects.

Tip: You can also explore and manage your workspace directly in the Azure ML studio.

Step 2: Install and Configure the Azure ML SDK

Install the necessary Python packages using pip:

pip install azure-ai-ml azure-identity

Then, configure your local environment to connect to your workspace. You can do this by creating a config.json file in your project directory or by using environment variables. For local development, a config.json is often the easiest.

Create a file named config.json with the following content, replacing the placeholders with your actual workspace details:

{
    "subscription_id": "YOUR_SUBSCRIPTION_ID",
    "resource_group": "YOUR_RESOURCE_GROUP",
    "workspace_name": "YOUR_WORKSPACE_NAME",
    "location": "YOUR_WORKSPACE_LOCATION"
}

Step 3: Prepare your Data

For this quickstart, we'll use a sample dataset available in Azure ML. If you have your own dataset, you'll need to upload it to your workspace's default datastore or a specified datastore.

You can create an Azure ML data asset representing your dataset:

from azure.ai.ml import MLClient
from azure.ai.ml.entities import Data
from azure.identity import DefaultAzureCredential

credential = DefaultAzureCredential()
ml_client = MLClient.from_config(credential=credential)

# Example for an existing dataset (replace with your actual path)
my_data = Data(
    path="azureml://datastores/workspaceblobstore/paths/datasets/my-image-classification-data",
    type="uri_folder",
    description="My image classification dataset",
    name="image-classification-data"
)

ml_client.data.create_or_update(my_data)
print(f"Data asset created: {my_data.name}")

Step 4: Train the Model

We'll use a simple Python script for training. Create a file named train.py:

import argparse
import os
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
import joblib

def train_model(data_path, model_output_dir):
    # Load data (assuming CSV with features and a 'label' column)
    df = pd.read_csv(data_path)
    
    X = df.drop('label', axis=1)
    y = df['label']
    
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
    
    # Initialize and train the model
    model = RandomForestClassifier(n_estimators=100, random_state=42)
    model.fit(X_train, y_train)
    
    # Evaluate the model
    y_pred = model.predict(X_test)
    accuracy = accuracy_score(y_test, y_pred)
    print(f"Model accuracy: {accuracy:.4f}")
    
    # Save the model
    model_path = os.path.join(model_output_dir, "model.pkl")
    joblib.dump(model, model_path)
    print(f"Model saved to {model_path}")

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--data_path", type=str, help="Path to the training data")
    parser.add_argument("--model_output_dir", type=str, default="./outputs", help="Directory to save the trained model")
    args = parser.parse_args()
    
    os.makedirs(args.model_output_dir, exist_ok=True)
    train_model(args.data_path, args.model_output_dir)

Now, define an Azure ML job to run this training script. Create a YAML file, e.g., job.yml:

$schema: https://azuremlschemas.azureedge.net/latest/commandJob.schema.json
code: .
command: >-
  python train.py 
  --data_path ${{inputs.training_data}} 
  --model_output_dir ${{outputs.model_output}}
inputs:
  training_data:
    type: uri_folder
    path: azureml://datastores/workspaceblobstore/paths/datasets/my-image-classification-data/
environment: azureml://registries/azureml/environments/sklearn-1.0/versions/4
outputs:
  model_output:
    type: uri_folder
    path: azureml://datastores/workspaceblobstore/paths/models/image-classification-quickstart
display_name: image-classification-training-job
experiment_name: image-classification-quickstart
description: Train a simple image classification model.

Submit the job:

from azure.ai.ml import MLClient, command
from azure.identity import DefaultAzureCredential

credential = DefaultAzureCredential()
ml_client = MLClient.from_config(credential=credential)

# Load the job configuration
job = command(yaml_file="job.yml")

# Submit the job
returned_job = ml_client.jobs.create_or_update(job)
print(f"Job submitted: {returned_job.name}")

Step 5: Register the Model

Once the training job completes, register the trained model in your workspace:

from azure.ai.ml import MLClient
            from azure.ai.ml.entities import Model
            from azure.identity import DefaultAzureCredential

            credential = DefaultAzureCredential()
            ml_client = MLClient.from_config(credential=credential)

            # Assuming the model artifact is in 'outputs/model.pkl' after training job
            model_path = "azureml://datastores/workspaceblobstore/paths/models/image-classification-quickstart/model.pkl"

            model = Model(
                path=model_path,
                name="image-classification-rf",
                description="Random Forest model for image classification."
            )

            ml_client.models.create_or_update(model)
            print(f"Model registered: {model.name}")

Step 6: Create an Inference Script

Create a script that will be used to load the model and make predictions. Save this as score.py:

import os
import joblib
import pandas as pd
import json

def init():
    """This method is called when the code is loaded for inference."""
    global model
    # AZUREML_MODEL_DIR is an environment variable created during deployment.
    # It points to the directory where the model file is located.
    model_path = os.path.join(os.getenv('AZUREML_MODEL_DIR'), 'model.pkl')
    model = joblib.load(model_path)
    print("Model loaded successfully.")

def run(raw_data):
    """
    This method is called for every inference request.
    The input data is received as a string in raw_data.
    """
    try:
        data = json.loads(raw_data)['data']
        # Convert input data to a DataFrame matching training data format
        input_df = pd.DataFrame(data)
        
        predictions = model.predict(input_df)
        
        # Convert predictions to a JSON serializable format
        result = {"predictions": predictions.tolist()}
        return json.dumps(result)
    except Exception as e:
        error_message = str(e)
        return json.dumps({"error": error_message})

Step 7: Deploy the Model

Define the deployment configuration and then deploy the model as a managed online endpoint. Create endpoint.yml and deployment.yml.

endpoint.yml:

$schema: https://azuremlschemas.azureedge.net/latest/managedOnlineEndpoint.schema.json
name: image-classification-endpoint
description: Endpoint for image classification model
auth_mode: key

deployment.yml:

$schema: https://azuremlschemas.azureedge.net/latest/managedOnlineDeployment.schema.json
name: blue
endpoint_name: image-classification-endpoint
model: azureml:image-classification-rf:1  # Use the latest version of your registered model
code_configuration:
  code: .
  scoring_script: score.py
environment: azureml://registries/azureml/environments/sklearn-1.0/versions/4
instance_type: Standard_DS3_v2
instance_count: 1

Deploy the endpoint and deployment:

from azure.ai.ml import MLClient
            from azure.ai.ml.entities import ManagedOnlineEndpoint, ManagedOnlineDeployment
            from azure.identity import DefaultAzureCredential

            credential = DefaultAzureCredential()
            ml_client = MLClient.from_config(credential=credential)

            # Create the endpoint
            endpoint = ManagedOnlineEndpoint(
                name="image-classification-endpoint",
                description="Endpoint for image classification model",
                auth_mode="key",
            )
            ml_client.online_endpoints.begin_create_or_update(endpoint).result()

            # Create the deployment
            deployment = ManagedOnlineDeployment(
                name="blue",
                endpoint_name="image-classification-endpoint",
                model="azureml:image-classification-rf:1", # Replace with your actual model name and version
                code_configuration={
                    "code": ".",
                    "scoring_script": "score.py",
                },
                environment="azureml://registries/azureml/environments/sklearn-1.0/versions/4",
                instance_type="Standard_DS3_v2",
                instance_count=1,
            )
            ml_client.online_deployments.begin_create_or_update(deployment).result()

            print(f"Endpoint deployed: {endpoint.name}")
            print(f"Deployment created: {deployment.name}")

Step 8: Test the Deployed Endpoint

Once the deployment is complete, you can test your endpoint. Get the endpoint's scoring URI and primary key.

from azure.ai.ml import MLClient
            from azure.identity import DefaultAzureCredential

            credential = DefaultAzureCredential()
            ml_client = MLClient.from_config(credential=credential)

            endpoint = ml_client.online_endpoints.get(name="image-classification-endpoint")
            scoring_uri = endpoint.scoring_uri
            primary_key = ml_client.online_endpoints.get_keys(name="image-classification-endpoint").primary_key

            print(f"Scoring URI: {scoring_uri}")
            print(f"Primary Key: {primary_key}")

You can then use tools like curl or Python's requests library to send test data to the endpoint. Make sure your test data matches the format expected by your score.py script.

Congratulations!

You have successfully trained and deployed an image classification model using Azure AI Machine Learning. This quickstart provides a foundation for building more complex ML solutions on Azure.

What's Next?

  • Explore advanced training scenarios with Azure ML.
  • Learn about different deployment options, including batch endpoints.
  • Discover MLOps best practices for managing your ML lifecycle.
Explore Tutorials