Microsoft Azure’s AI Services enhances your applications with smart features such as seeing, hearing, and translating. Custom Vision is a fantastic tool that allows you to create your own image recognition system. This blog focuses on Azure AI Services and Custom Vision, demonstrating how to utilize them through real-world examples and step-by-step instructions using C#.
Azure AI Services
Azure AI Services has various machine learning algorithms to effortlessly address multiple AI tasks through simple API requests. Custom Vision is one such tool that provides specialized features for building, deploying, and enhancing custom image classifiers. It allows users to train their models to recognize particular images, which is highly adaptable when developing personalized image recognition projects.
Azure Custom Vision: Use Cases
You can build your own image identifier model across a wide range of businesses using Custom Vision, an Azure AI service.
- Retail: Automate furniture inventory by identifying products on shelves.
- Manufacturing: Detect defects in products that are passing through the assembly lines.
- Agriculture: Monitor the health of crops and the activity of pests.
- Healthcare: Help diagnose diseases through medical imaging.
Get Started
To begin using Azure Custom Vision, please create an Azure account.
• Register or login to your Microsoft Azure portal.
• Go to create a resource, select AI + Machine Learning, and then Custom Vision.
Step 1: Create a Custom Vision Project and Install the Necessary Packages
The first step is to install the Azure AI Services Custom Vision SDK in your project. Use NuGet in your Visual Studio IDE to install the packages listed below, which offer the functionalities required to communicate with the Custom Vision Service.
Install-Package Microsoft.Azure.CognitiveServices.Vision.CustomVision.Training
Install-Package Microsoft.Azure.CognitiveServices.Vision.CustomVision.Prediction
Step 2: Initialize the Custom Vision Training Client
To configure the training client, you can use your Custom Vision training key and endpoint. This client will handle all interactions with the Custom Vision service, including project creation and model training.
using Microsoft.Azure.CognitiveServices.Vision.CustomVision.Training;
using Microsoft.Azure.CognitiveServices.Vision.CustomVision.Prediction;
using Microsoft.Azure.CognitiveServices.Vision.CustomVision.Training.Models;
var trainingClient = new CustomVisionTrainingClient()
{
Endpoint = "Your Custom Vision Endpoint",
ApiKey = "Your training key"
};
// Create a new project
Console.WriteLine("Creating new project:");
var project = trainingClient.CreateProjectAsync("My New Project").Result;
The Custom Vision service initiates a new project. This newly created project now includes all of our models, tags, and photos.
Step 3: Create a Tag and Upload Images
Tags are labels for your images that the model will learn to recognize. Once you’ve created a tag, upload the images to the project along with their corresponding tags.
var tag = trainingClient.CreateTagAsync(project.Id, "My Tag").Result;
using (var stream = new FileStream("path_to_image.jpg", FileMode.Open))
{
trainingClient.CreateImagesFromDataAsync(project.Id, stream, new List() { tag.Id }).Wait();
}
Step 4: Training the Custom Vision Model
In this step, we use the uploaded images to train a custom vision model. The training method uses these images and their tags to determine how to label the uploaded images. To begin training, we utilize the TrainProjectAsync function, which makes an asynchronous request to the training client.
var iteration = trainingClient.TrainProjectAsync(project.Id).Result;
Console.WriteLine("Training started...");
Step 5: Publishing the Model
After completing the training and evaluating the model, publish it.
trainingClient.PublishIterationAsync(project.Id, iteration.Id, "modelname", "predictionResourceId").Wait();
Console.WriteLine("Model published!");
Step 6: Test and Predict Using the Published Model
Once published, we can use the model to predict the classification of new images. This step demonstrates how to apply the model in real-world circumstances.
var predictionClient = new CustomVisionPredictionClient()
{
Endpoint = "Your Custom Vision Endpoint",
ApiKey = "Your prediction key"
};
using (var stream = new FileStream("path_to_test_image.jpg", FileMode.Open))
{
var result = predictionClient.ClassifyImageAsync(project.Id, "modelname", stream).Result;
foreach (var prediction in result.Predictions)
{
Console.WriteLine($"Tag: {prediction.TagName} Probability: {prediction.Probability:P1}");
}
}
Considerations While Using Custom Vision
When integrating Azure Custom Vision into your applications, there are several critical factors to consider to ensure optimal performance and effective use:
Data Quality and Quantity: The success of the custom model largely depends on the quality and diversity of the images you use for training. Furthermore, the quantity of data is important; in general, more data leads to improved model performance.
Model Performance: It’s essential to continuously monitor and analyze your model’s performance to make sure it meets the requirements of your application.. This includes evaluating the model’s accuracy, precision, and recall, as well as knowing how well it operates with new data.
Operational Costs: Running AI models on the cloud incurs expenses associated with processing power, storage, and API calls. To balance performance and cost, you must optimize your model and its usage.
Compliance and Security Considerations
When building Custom Vision Services with Azure AI Services, it is important to ensure compliance and security. Some of the key considerations include implementing strong encryption and access controls, adhering to data privacy regulations such as GDPR and HIPAA, utilizing Azure’s network security features, and implementing rigorous monitoring and logging.
Integration with Other Azure Services
Combining Custom Vision with other Azure services, such as Azure Logic Apps for workflow automation, Azure IoT for edge computing, and Azure Machine Learning for advanced AI model management, can greatly improve capabilities and develop more comprehensive solutions.
Additional References
- Azure Custom Vision Documentation
- Custom Vision SDK for .NET Reference
- Azure Samples GitHub Repository
- Microsoft Learn