How to build a classifier with Custom Vision
To use the Custom Vision Service for image classification, you must first build a classifier model. In this guide, you'll learn how to build a classifier through the Custom Vision website.
- A valid Azure subscription. Create an account for free.
- A set of images with which to train your classifier. See below for tips on choosing images.
Create Custom Vision resources in the Azure Portal
To use Custom Vision Service, you will need to create Custom Vision Training and Prediction resources in the in the Azure portal. This will create both a Training and Prediction resource.
Create a new project
In your web browser, navigate to the Custom Vision web page and select Sign in. Sign in with the same account you used to sign into the Azure Portal.
To create your first project, select New Project. The Create new project dialog box will appear.
Enter a name and a description for the project. Then select a Resource Group. If your signed-in account is associated with an Azure account, the Resource Group dropdown will display all of your Azure Resource Groups that include a Custom Vision Service Resource.
If no resource group is available, please confirm that you have logged into customvision.ai with the same account as you used to log into the Azure Portal. Also, please confirm you have selected the same “Directory” in the Custom Vision portal as the directory in the Azure portal where your Custom Vision resources are located. In both sites, you may select your directory from the drop down account menu at the top right corner of the screen.
Select Classification under Project Types. Then, under Classification Types, choose either Multilabel or Multiclass, depending on your use case. Multilabel classification applies any number of your tags to an image (zero or more), while multiclass classification sorts images into single categories (every image you submit will be sorted into the most likely tag). You will be able to change the classification type later if you wish.
Next, select one of the available domains. Each domain optimizes the classifier for specific types of images, as described in the following table. You will be able to change the domain later if you wish.
Domain Purpose Generic Optimized for a broad range of image classification tasks. If none of the other domains are appropriate, or you are unsure of which domain to choose, select the Generic domain. Food Optimized for photographs of dishes as you would see them on a restaurant menu. If you want to classify photographs of individual fruits or vegetables, use the Food domain. Landmarks Optimized for recognizable landmarks, both natural and artificial. This domain works best when the landmark is clearly visible in the photograph. This domain works even if the landmark is slightly obstructed by people in front of it. Retail Optimized for images that are found in a shopping catalog or shopping website. If you want high precision classifying between dresses, pants, and shirts, use this domain. Adult Optimized to better define adult content and non-adult content. For example, if you want to block images of people in bathing suits, this domain allows you to build a custom classifier to do that. Compact domains Optimized for the constraints of real-time classification on mobile devices. The models generated by compact domains can be exported to run locally.
Finally, select Create project.
Choose training images
As a minimum, we recommend you use at least 30 images per tag in the initial training set. You'll also want to collect a few extra images to test your model once it is trained.
In order to train your model effectively, use images with visual variety. Select images with that vary by:
- camera angle
- visual style
- individual/grouped subject(s)
Additionally, make sure all of your training images meet the following criteria:
- .jpg, .png, or .bmp format
- no greater than 6MB in size (4MB for prediction images)
- no less than 256 pixels on the shortest edge; any images shorter than this will be automatically scaled up by the Custom Vision Service
Upload and tag images
In this section you will upload and manually tag images to help train the classifier.
To add images, click the Add images button and then select Browse local files. Select Open to move to tagging. Your tag selection will be applied to the entire group of images you've selected to upload, so it is easier to upload images in separate groups according to their desired tags. You can also change the tags for individual images after they have been uploaded.
To create a tag, enter text in the My Tags field and press Enter. If the tag already exists, it will appear in a dropdown menu. In a multilabel project, you can add more than one tag to your images, but in a multiclass project you can add only one. To finish uploading the images, use the Upload [number] files button.
Select Done once the images have been uploaded.
To upload another set of images, return to the top of this section and repeat the steps. At some point in your project, you may need to add negative samples to help make your classifier more accurate. Negative samples are those which do not match any of the other tags. When you upload these images, apply the special Negative label to them.
The Custom Vision Service supports some automatic negative image handling. For example, if you are building a grape vs. banana classifier and submit an image of a shoe for prediction, the classifier should score that image as close to 0% for both grape and banana.
On the other hand, in cases where the negative images are just a variation of the images used in training, it is likely that the model will classify the negative images as a labeled class due to the great similarities. For example, if you have an orange vs. grapefruit classifier, and you feed in an image of a clementine, it may score the clementine as an orange because many features of the clementine resemble those of oranges. If your negative images are of this nature, we recommend you create one or more additional tags (such as Other) and label the negative images with this tag during training to allow the model to better differentiate between these classes.
Train the classifier
To train the classifier, select the Train button. The classifier uses all of the current images to create a model that identifies the visual qualities of each tag.
The training process should only take a few minutes. During this time, information about the training process is displayed in the Performance tab.
Evaluate the classifier
After training has completed, the model's performance is estimated and displayed. The Custom Vision Service uses the images that you submitted for training to calculate precision and recall, using a process called k-fold cross validation. Precision and recall are two different measurements of the effectiveness of a classifier:
- Precision indicates the fraction of identified classifications that were correct. For example, if the model identified 100 images as dogs, and 99 of them were actually of dogs, then the precision would be 99%.
- Recall indicates the fraction of actual classifications that were correctly identified. For example, if there were actually 100 images of apples, and the model identified 80 as apples, the recall would be 80%.
Note the Probability Threshold slider on the left pane of the Performance tab. This is the threshold for a predicted probability to be considered correct when computing precision and recall.
Interpreting prediction calls with a high probability threshold tends to return results with high precision at the expense of recall (the found classifications are correct, but many were not found); a low probability threshold does the opposite (most of the actual classifications were found, but there are false positives within that set). With this in mind, you should set the probability threshold according to the specific needs of your project. Later, on the client side, you should use the same probability threshold value as a filter when receiving prediction results from the model.
Manage training iterations
Each time you train your classifier, you create a new iteration with its own updated performance metrics. You can view all of your iterations in the left pane of the Performance tab. When you select one, you have the option of making it the default iteration by clicking the Make default button at the top. The default iteration is the model that will be used by default when you query it through the Prediction API (from an app, for instance). If you decline to update the default iteration, you can continue to train your model without affecting your app's current behavior; then, once you are satisfied with the improved model, you can update the default.
In the left pane you will also find the Delete button, which you can use to delete an iteration if it's obsolete. When you delete an iteration, you delete any images that are uniquely associated with it.
In this guide, you learned how to create and train an image classification model using the Custom Vision website. Next, get more information on the iterative process of improving your model.
We'd love to hear your thoughts. Choose the type you'd like to provide:
Our feedback system is built on GitHub Issues. Read more on our blog.