How to use a bag of features object to train a Random fores classifier?

1 view (last 30 days)
Hello, I'm using categoryClassifier = trainImageCategoryClassifier(trainingSet, bag) to train ans SVM model. bag is a bag of visual words. How can I derive a TreeBagger model instead of SVM?

Accepted Answer

Uttiya Ghosh
Uttiya Ghosh on 14 Jul 2020
Edited: Uttiya Ghosh on 15 Jul 2020
Hi Shai,
From my understanding, you want to know how to convert a bag of features into a table of features so that it can be used to train a TreeBagger model. PFB the code that will help you in the conversion process. Here I have used a datatore of 12 images. I have randomly selected 75% of the images for training and remaining for testing my model. I have used 5 trees in my tree bagger model.
setDir = fullfile(toolboxdir('vision'),'visiondata','imageSets');
imds = imageDatastore(setDir,'IncludeSubfolders',true,'LabelSource',...
'foldernames');
[imdsTrain,imdsTest] = splitEachLabel(imds,0.75,'randomize');
bagTrain = bagOfFeatures(imdsTrain);
bagTest = bagOfFeatures(imdsTest);
featureVectorTrain = encode(bagTrain, imdsTrain);
featureVectorTest = encode(bagTest, imdsTest);
B = TreeBagger(5,featureVectorTrain,imdsTrain.Labels);
imdsPred = predict(B,featureVectorTest);
acc = (nnz(imdsPred == imdsTest.Labels))/length(imdsTest.Labels);
For more information, refer to the following links.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!