Main Content

trainImageCategoryClassifier

Train an image category classifier

Description

classifier = trainImageCategoryClassifier(imds,bag) returns an image category classifier. The classifier contains the number of categories and the category labels for the input imds images. The function trains a support vector machine (SVM) multiclass classifier using the input bag, a bagOfFeatures object.

You must have a Statistics and Machine Learning Toolbox™ license to use this function.

This function supports parallel computing using multiple MATLAB® workers. Enable parallel computing using the Computer Vision Toolbox Preferences dialog. To open Computer Vision Toolbox™ preferences, on the Home tab, in the Environment section, click Preferences. Select Computer Vision Toolbox.

example

classifier = trainImageCategoryClassifier(imds,bag,Name,Value) returns a classifier object with optional input properties specified by one or more Name,Value pair arguments.

Examples

collapse all

Load two image categories.

setDir  = fullfile(toolboxdir('vision'),'visiondata','imageSets');
imds = imageDatastore(setDir,'IncludeSubfolders',true,'LabelSource',...
    'foldernames');

Split the data set into a training and test data. Pick 30% of images from each set for the training data and the remainder 70% for the test data.

[trainingSet,testSet] = splitEachLabel(imds,0.3,'randomize');

Create bag of visual words.

bag = bagOfFeatures(trainingSet);
Creating Bag-Of-Features.
-------------------------
* Image category 1: books
* Image category 2: cups
* Selecting feature point locations using the Grid method.
* Extracting SURF features from the selected feature point locations.
** The GridStep is [8 8] and the BlockWidth is [32 64 96 128].

* Extracting features from 4 images...done. Extracted 76800 features.

* Keeping 80 percent of the strongest features from each category.

* Creating a 500 word visual vocabulary.
* Number of levels: 1
* Branching factor: 500
* Number of clustering steps: 1

* [Step 1/1] Clustering vocabulary level 1.
* Number of features          : 61440
* Number of clusters          : 500
* Initializing cluster centers...100.00%.
* Clustering...completed 55/100 iterations (~0.61 seconds/iteration)...converged in 55 iterations.

* Finished creating Bag-Of-Features

Train a classifier with the training sets.

categoryClassifier = trainImageCategoryClassifier(trainingSet,bag);
Training an image category classifier for 2 categories.
--------------------------------------------------------
* Category 1: books
* Category 2: cups

* Encoding features for 4 images...done.

* Finished training the category classifier. Use evaluate to test the classifier on a test set.

Evaluate the classifier using test images. Display the confusion matrix.

confMatrix = evaluate(categoryClassifier,testSet)
Evaluating image category classifier for 2 categories.
-------------------------------------------------------

* Category 1: books
* Category 2: cups

* Evaluating 8 images...done.

* Finished evaluating all the test sets.

* The confusion matrix for this test set is:


             PREDICTED
KNOWN    | books   cups   
--------------------------
books    | 0.75    0.25   
cups     | 0.25    0.75   

* Average Accuracy is 0.75.
confMatrix = 2×2

    0.7500    0.2500
    0.2500    0.7500

Find the average accuracy of the classification.

mean(diag(confMatrix))
ans = 
0.7500

Apply the newly trained classifier to categorize new images.

img = imread(fullfile(setDir,'cups','bigMug.jpg'));
[labelIdx, score] = predict(categoryClassifier,img);
Encoding images using Bag-Of-Features.
--------------------------------------
* Encoding an image...done.

Display the classification label.

categoryClassifier.Labels(labelIdx)
ans = 1x1 cell array
    {'cups'}

Input Arguments

collapse all

Images specified as an imageDatastore object.

Bag of features, specified as a bagOfFeatures object. The object contains a visual vocabulary of extracted feature descriptors from representative images of each image category.

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: 'Verbose',true sets 'Verbose' to the logical true.

Enable progress display to screen, specified as the comma-separated pair consisting of 'Verbose' and the logical true or false.

Classifier options, specified as the comma-separated pair consisting of 'LearnerOptions' and the learner options output returned by the templateSVM (Statistics and Machine Learning Toolbox) function.

Example 2. Example

To adjust the regularization parameter of templateSVM (Statistics and Machine Learning Toolbox) and to set a custom kernel function, use the following syntax:

opts = templateSVM('BoxConstraint',1.1,'KernelFunction','gaussian');
classifier = trainImageCategoryClassifier(imds,bag,'LearnerOptions',opts);

Output Arguments

collapse all

Image category classifier, returned as an imageCategoryClassifier object. The function trains a support vector machine (SVM) multiclass classifier using the error correcting output codes (ECOC) framework.

References

[1] Csurka, G., C. R. Dance, L. Fan, J. Willamowski, and C. Bray Visual Categorization with Bag of Keypoints, Workshop on Statistical Learning in Computer Vision, ECCV 1 (1-22), 1-2.

Extended Capabilities

Version History

Introduced in R2014b

See Also

| | | (Statistics and Machine Learning Toolbox) | (Statistics and Machine Learning Toolbox)