Skip to content

๐ŸŽจ Image Classification Solution (Azure AI Custom Vision)

๐Ÿ” Problem

You need to build a computer vision solution that can classify images into categories (e.g., identify products in a grocery checkout, classify fruit images) using your own training data.


โ˜๏ธ Solution with Azure

Use Azure AI Custom Vision, which enables:

  • ๐Ÿ“Š Image classification: Predict a single class (multiclass) or multiple classes (multilabel) for an image
  • ๐ŸŽฏ Object detection (not detailed in this material)

You train your own model using labeled images, then publish the model for prediction.


๐Ÿงฉ Components Required

  • ๐Ÿ‹๏ธ Azure AI Custom Vision training resource Used to train custom models with your image data

  • ๐Ÿ”ฎ Azure AI Custom Vision prediction resource Used to generate predictions from new images using your trained model

  • ๐ŸŒ Azure AI Custom Vision portal Web interface at https://www.customvision.ai for managing projects, uploading images, training, publishing, and testing models

  • ๐Ÿ“ฑ SDK / REST API

    • Python: azure-cognitiveservices-vision-customvision
    • .NET: Microsoft.Azure.CognitiveServices.Vision.CustomVision.Training and ...Prediction
  • ๐Ÿ†” Project ID + resource endpoints/keys Required for training and prediction API calls


๐Ÿ—๏ธ Architecture / Development

๐Ÿ‹๏ธ Training a model

  • Create an image classification project in the Custom Vision portal
  • Associate the project with a training resource
  • Upload images and assign class label tags
  • Train and evaluate the model in the portal
  • Publish the trained model to a prediction resource

๐Ÿ’ก Both multiclass (single label per image) and multilabel (multiple labels per image) classification are supported.

๐Ÿ”ฎ Example: Classify images (Python SDK)

from msrest.authentication import ApiKeyCredentials
from azure.cognitiveservices.vision.customvision.prediction import CustomVisionPredictionClient

# Authenticate a client for the prediction API
credentials = ApiKeyCredentials(in_headers={"Prediction-key": "<YOUR_PREDICTION_RESOURCE_KEY>"})
prediction_client = CustomVisionPredictionClient(
    endpoint="<YOUR_PREDICTION_RESOURCE_ENDPOINT>",
    credentials=credentials
)

# Get classification predictions for an image
image_data = open("<PATH_TO_IMAGE_FILE>", "rb").read()
results = prediction_client.classify_image(
    "<YOUR_PROJECT_ID>",
    "<YOUR_PUBLISHED_MODEL_NAME>",
    image_data
)

# Process predictions
for prediction in results.predictions:
    if prediction.probability > 0.5:
        print(image, ': {} ({:.0%})'.format(prediction.tag_name, prediction.probability))

โญ Best Practice / Considerations

๐Ÿ”„ Separate training and prediction resources

Enables flexibility (e.g., train in one region, deploy prediction resources in multiple regions)

๐Ÿš€ DevOps integration

Use SDKs / REST API to automate training and publishing as part of a CI/CD pipeline

๐Ÿ› ๏ธ Resource management

  • Each resource has its own endpoint and authentication key
  • Each project has a unique project ID used by client applications

๐Ÿ“ Sample Exam-Like Questions

1๏ธโƒฃ What is required to train and publish an image classification model with Azure AI Custom Vision?

โœ… Answer: A training resource for model training and a prediction resource for generating predictions


2๏ธโƒฃ How can you classify images using a trained Custom Vision model?

โœ… Answer: Submit images to the prediction resource using the SDK, REST API, or Custom Vision portal


3๏ธโƒฃ What type of image classification is supported by Azure AI Custom Vision?

โœ… Answer: Multiclass (one label per image) and multilabel (multiple labels per image) classification


4๏ธโƒฃ What advantage does separating training and prediction resources provide?

โœ… Answer: Flexibility to train in one region and deploy prediction endpoints in others for performance or compliance reasons


5๏ธโƒฃ Which tool allows you to visually upload images, tag them, train, and test models?

โœ… Answer: The Azure AI Custom Vision portal at https://www.customvision.ai