Hi, I am Foteini Savvidou, a Beta Microsoft Learn Student Ambassador!
I am an undergraduate Electrical and Computer Engineering student at Aristotle University of Thessaloniki (Greece) interested in AI, cloud technologies and biomedical engineering. Always passionate about teaching and learning new things, I love helping people expand their technical skills through organizing workshops and sharing articles on my blog. My goal is to use technology to promote accessibility, digital and social inclusion.
In this article, we will explore the pre-trained models of the Azure Custom Vision service for image classification. We will build and deploy a custom computer vision model for flower classification. You will learn how to:
To complete the exercise, you will need an Azure subscription. If you don’t have one, you can sign up for an Azure free account. If your a Student you can apply for a Azure for Student Subscription.
Azure Custom Vision is an Azure Cognitive Services service that lets you build and deploy your own image classification and object detection models. Image classification models apply labels to an image, while object detection models return the bounding box coordinates in the image where the applied labels can be found.
Study the following sketch note to learn how Azure Custom Vision works.
You can find more information and how-to-guides about Custom Vision on Microsoft Learn and Microsoft Docs.
Every machine learning project starts with a question. Our question is, can we identify a flower’s category from an image of a flower, to help document the different types of flowers in our city?
Now that we know what to ask the model, we want to find data that would help us answer the question that we're interested in. To build and train our machine learning model, we will use the 17 Category Flower Dataset from the Visual Geometry Group (University of Oxford). We will use 3 out of 17 flower categories: Iris, Tigerlily and Tulip. You can download the 3 Category Flower Dataset from my GitHub repository. You can download the full dataset from the Visual Geometry Group’s website.
To use the Custom Vision service, you can either create a Custom Vision resource or a Cognitive Services resource. If you plan to use Custom Vision along with other cognitive services, you can create a Cognitive Services resource.
In this exercise, you will create a Custom Vision resource.
You can build and train your model by using the web portal or the Custom Vision SDKs and your preferred programming language. In this article, I will show you how to build a computer vision model using the Custom Vision web portal.
Before publishing our model, let’s test it and see how it performs on new data. We will use the flower images in the Test folder you extracted previously.
Once your model is performing at a satisfactory level, you can deploy it.
In the Custom Vision portal, click the settings icon (⚙) at the top toolbar to view the project settings. Then, under General, copy the Project ID.
Navigate to the Custom Vision portal homepage and select the settings icon (⚙) at the top right. Expand your prediction resource and save the Key and the Endpoint, because you will need these values to build the Python app.
To create an image classification app with Custom Vision for Python, you'll need to install the Custom Vision client library. Install the Azure Cognitive Services Custom Vision SDK for Python package with pip:
pip install azure-cognitiveservices-vision-customvision
Then, use the following code to call the prediction API in Python (code source: Microsoft Docs – Python Quickstart).
from azure.cognitiveservices.vision.customvision.prediction import CustomVisionPredictionClient
from msrest.authentication import ApiKeyCredentials
import os
# Get path to images folder
dirname = os.path.dirname(__file__)
images_folder = os.path.join(dirname, 'images/Test')
# Create variables for your project
publish_iteration_name = "Iteration1"
project_id = "<YOUR_PROJECT_ID>"
# Create variables for your prediction resource
prediction_key = "<YOUR_KEY>"
endpoint = "<YOUR_ENDPOINT>"
prediction_credentials = ApiKeyCredentials(in_headers={"Prediction-key": prediction_key})
predictor = CustomVisionPredictionClient(endpoint, prediction_credentials)
# Open an image and make a prediction
with open(os.path.join(images_folder, "tigerlily4.jpg"), "rb") as image_contents:
results = predictor.classify_image(project_id, publish_iteration_name, image_contents.read())
# Display the results
for prediction in results.predictions:
print(f"{prediction.tag_name}: {prediction.probability * 100 :.2f}%")
Replace <YOUR_PROJECT_ID>
, <YOUR_KEY>
and <YOUR_ENDPOINT>
with the ID of your project, the Key and the Endpoint of your prediction resource, respectively.
Use the following code to display the predicted class of all the images in the Test folder:
images = os.listdir(images_folder)
for i in range(len(images)):
# Open the image, and use the custom vision model to classify it
image_contents = open(os.path.join(images_folder, images[i]), "rb")
results = predictor.classify_image(project_id, publish_iteration_name, image_contents.read())
# Print the predicted class
print(f"Image {images[i]}: {results.predictions[0].tag_name} {results.predictions[0].probability * 100 :.2f}%")
In this article, you learned how to use Azure Custom Vision service to build and deploy an image classification model. If you are interested in learning more about Azure Custom Vision, check out these Microsoft Learn modules:
Share your awesome Custom Vision projects and feel free to reach out to me on LinkedIn or Twitter.
If you have finished learning, you can delete the resource group from your Azure subscription:
I am very grateful to Lee Stott for giving me the opportunity to share my articles here.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.