How to use a bag of features object to train a Random fores classifier?
1 view (last 30 days)
Uttiya Ghosh on 14 Jul 2020
Edited: Uttiya Ghosh on 15 Jul 2020
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',...
[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.