I want to classify the Cifar-10 dataset using Hierarchical SVM. I know CNN is best choice but I need to preprocess this data and then use hierarchical SVM. I saw one of the post hierarchical classification with SVM but I am still confused for cifar10. I tried the following code for one level of hierarchy but it doesn't satisfy me as I am getting an accuracy of 90% only. See the code below. Any help would be highly appreciated.
rootFolder = 'cifar10Train';
categories = {'Deer','Dog','Frog','Cat','truck','ship','airplane','horse',...
'bird','automobile'};
imds = imageDatastore(fullfile(rootFolder, categories), 'LabelSource',...
'foldernames');
%Load test data
rootFolder = 'cifar10Test';
imds_test = imageDatastore(fullfile(rootFolder, categories), ...
'LabelSource', 'foldernames');
% Hierarchical SVM
% data generation suffix 'T' is used for test dataset
Y = imds.Labels;
YT = imds_test.Labels;
L = length(Y);
LT = length(YT);
X = zeros(32*32*3,L);
XT = zeros(32*32*3,LT);
% New labels for hierarchy
Y1 = (Y=='Deer');
Y2 = (Y=='Dog');
Y3 = (Y=='Frog');
Y4 = (Y=='Cat');
Y5 = (Y=='truck');
Y6 = (Y=='ship');
Y7 = (Y=='airplane');
Y8 = (Y=='horse');
Y9 = (Y=='bird');
Y10= (Y=='automobile');
% for test dataset
Y1T = (YT=='Deer');
Y2T = (YT=='Dog');
Y3T = (YT=='Frog');
Y4T = (YT=='Cat');
Y5T = (YT=='truck');
Y6T = (YT=='ship');
Y7T = (YT=='airplane');
Y8T = (YT=='horse');
Y9T = (YT=='bird');
Y10T= (YT=='automobile');
% train Samples
for i=1:L
img = readimage(imds,i);
X(:,i) = double(img(:));
end
% test data
for i=1:LT
img = readimage(imds_test,i);
XT(:,i) = double(img(:));
end
%First Linear classification
c1 = fitclinear(X',Y1);
pred1 = predict(c1,XT');%
Acc = sum(pred1==Y1T)/LT;'''
I have written another compressed code for it. But the thing that confuse me is that I will be 9 classifiers not a one. Should I expect one classifier which can classify hierarchically all classes.
function [Classifier Accuracy] = HSVM_mine(Num_of_Classes)
% training data rows consitute samples and column features
[Xtrain,Ytrain,Xtest,Ytest] = data_generate(filename_train,filename_test);
Accuracy = zeros(Num_of_Classes,1)
Classifier = {};
Labels = {'class 1','class 2', 'class 3'...}
Num_of_TrainSamples_per_class= m;
m= size(Xtrain,1)/Num_of_Classes;
Num_of_TestSamples_per_class= n;
n= size(Xtest,1)/Num_of_Classes;
for i=1:Num_of_Classes
X1 = Xtrain((1+(i-1)*m):size(Xtrain,1));
Y1 = (Yrain((1+(i-1)*m):size(Xtrain,1))==Ytrain(Labels(i));
XT = Xtest((1+(i-1)*n):size(Xest,1));
YT = (Yest((1+(i-1)*n):size(Xest,1))==Ytest(Labels(i));
Classifier{i} = fitclinear(X1,Y1)
Pred = predict(Classifier{i},XT);
Accuracy(i) = sum(Pred==YT)/length(YT)
end