Using Naive Bayes for image classification in Matlab

40 Views Asked by At

I am trying to use Naive Bayes fitcnb(Tbl,ResponseVarName) to classify images.

To start I followed the Image Category Classification Using Bag of Features guide provided by MathWorks. I am using the same data set that is used in that example, which is the caltech 101 data set. In that guide they use trainImageCategoryClassifier(imds,bag) which trains a SVM on the data using a bag of words approach.

Using trainImageCategoryClassifier is easy because it takes parameters of type ImageDatastore and bagOfFeatures, as opposed to fitcnb which takes a table and labels.

I tried passing fitcnb the histogram of training data and the labels but when I do that I get an error message as you can see below. The feature vector should have all the images in their reduced state, but maybe I don't exactly understand how bag of visual words works.

imgSets = [ imageSet(('101_ObjectCategories/airplanes')), ...
imageSet(('101_ObjectCategories/ferry')), ...
imageSet(('101_ObjectCategories/laptop')) ];
% 
{ imgSets.Description };
[imgSets.Count];
% 

minSetCount = min([imgSets.Count]); 
imgSets = partition(imgSets, minSetCount, 'randomize');
 
[trainingSets, validationSets] = partition(imgSets, 0.3, 'randomize'); 
trainingSets.Description;
 
% airplanes = read(trainingSets(1),1);
% ferry = read(trainingSets(2),1);
% laptop = read(trainingSets(3),1);
bag = bagOfFeatures(trainingSets);
% img = read(imgSets(1), 1);
% featureVector = encode(bag, img)
featureVector = encode(bag,imgSets);

labels    = cell(1, 201);  % rudimentary I know
labels(1:67) = {'airplanes'};
labels(67:134) = {'ferry'};
labels(134:201) = {'laptop'};

Mdl = fitcnb(featureVector, labels)

% categoryClassifier = trainImageCategoryClassifier(trainingSets, bag);
% 
% confMatrix = evaluate(categoryClassifier, trainingSets);
% 
% confMatrix = evaluate(categoryClassifier, validationSets);

Error using ClassificationNaiveBayes/findNoDataCombos A normal distribution cannot be fit for the combination of class ferry and predictor x179. The data has zero variance.

My question is how can I get the features extracted in Bag of Features into a form that I can pass to fitcnb? I think that the correct thing to do is pass the feature vector because that contains all of the image feature right? Anyway I'm really confused at this point and any insights would be greatly appreciated.

Thanks.

EDIT: it appears that for every ferry image predictor 179 in my feature vector is 0, which I guess means the images don't contain that predictor. How would I deal with a class that doesn't have a particular feature?

0

There are 0 best solutions below