-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSURF_HOG.m
More file actions
206 lines (136 loc) · 5.57 KB
/
SURF_HOG.m
File metadata and controls
206 lines (136 loc) · 5.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
%% Train SVM
% SVM and MLP require images to be grayscale and not RGB
% Code taken and adapted from Lab 06 - posted by Dr Giacomo Tarroni and
% from https://uk.mathworks.com/help/vision/examples/image-category-classification-using-bag-of-features.html
% Model script - 2
directory_path = '/Users/hishorajanathan/Desktop/CV/CroppedFacesAugment';
%% Create an image datastore
faceImages = imageDatastore(directory_path,'LabelSource','foldernames','IncludeSubfolders',true);
%% Balance the dataset
%Not all labels have the same amount of files so balance the number of
%images. The minimum number is 200 hence total will be 9,600 images
countLabel = countEachLabel(faceImages);
minCountImages = min(countLabel.Count);
faceImageMin = splitEachLabel(faceImages,minCountImages,'randomize');
%% Train split
% 90% for training and 10% for validation
[imagesTrain,imagesValidation] = splitEachLabel(faceImageMin,0.9,'randomize');
%% Display some of the training images
numTrainImages = numel(imagesTrain.Labels);
idx = randperm(numTrainImages,16);
figure
for i = 1:16
subplot(4,4,i)
I = readimage(imagesTrain,idx(i));
imshow(I)
end
%% Create SURF Features
% bagofFeatures extracts SURF features from all images in all image
% categories
bag = bagOfFeatures(imagesTrain);
%% Extract SURF
% use encode method
trainFeatureVector = encode(bag, imagesTrain);
validationFeatureVector = encode(bag, imagesValidation);
%% Create labels
%% Test an individual image
% Extract hogfeatures for a single image to test.
img_1 = readimage(imagesTrain,1000);
cellSize = [8,8];
[hog_8x8, vis8x8] = extractHOGFeatures(img_1,'CellSize',cellSize);
imshow(img_1)
plot(vis8x8)
%% SVM
% Find the labels for the training and validation set
trainingSVM = categorical(imagesTrain.Labels);
validationSVM = categorical(imagesValidation.Labels);
%% Extract HOG Features
% Find the total number of images for the training and validation set
numTrainImages = numel(imagesTrain.Labels);
numValidationImages = numel(imagesValidation.Labels);
% create empty matrix to store the hog features for each image
hogFeatureSize = length(hog_8x8);
trainingFeatures = zeros(numTrainImages,hogFeatureSize,'single');
validationFeatures = zeros(numValidationImages,hogFeatureSize,'single');
% Code taken from and adapted : https://uk.mathworks.com/help/vision/examples/digit-classification-using-hog-features.html
for i = 1:numTrainImages
img = readimage(imagesTrain, i);
%convert image to grayscale to extract hog features
img = rgb2gray(img);
trainingFeatures(i, :) = extractHOGFeatures(img,'CellSize',cellSize);
end
for i = 1:numValidationImages
img = readimage(imagesValidation, i);
%convert image to grayscale to extract hog features
img = rgb2gray(img);
validationFeatures(i, :) = extractHOGFeatures(img,'CellSize',cellSize);
end
%% SURF - SVM
% Train the SVM model
SURF_SVM = fitcecoc(trainFeatureVector,trainingSVM);
% Test model on validation set
SURF_SVM_pred = predict(SURF_SVM,validationFeatureVector);
% calculate accuracy score for SVM SURF
accuracy_SURF_SVM = sum(SURF_SVM_pred == validationSVM ) / length(validationSVM);
%% HOG - SVM
% Train the SVM model using HOG features
HOG_SVM = fitcecoc(trainingFeatures,trainingSVM);
HOG_SVM_pred = predict(HOG_SVM,validationFeatures);
% accuracy SVM HOG
accuracy_HOG_SVM = sum(HOG_SVM_pred == validationSVM) / length(validationSVM);
% Apply one hot encoding to the training and validation labels
training_number = categories(trainingSVM);
validation_number = categories(validationSVM);
% create empty matrix
SURF_Training_Labels = zeros(numel(training_number),numTrainImages);
SURF_Validation_Labels = zeros(numel(validation_number),numValidationImages);
for i = 1:numTrainImages
t_labels = trainingSVM(i);
SURF_Training_Labels(strcmp(training_number,cellstr(t_labels)),i) =1;
end
for i = 1:numValidationImages
t_labels = validationSVM(i);
SURF_Validation_Labels(strcmp(validation_number,cellstr(t_labels)),i) =1;
end
%% SURF - MLP
% train MLP network
hiddenSizes = 80;
trainFcn = 'trainscg';
net_SURF = patternnet(hiddenSizes,trainFcn);
net_SURF = configure(net_SURF,trainFeatureVector',SURF_Training_Labels);
net_SURF = train(net_SURF,trainFeatureVector',SURF_Training_Labels);
% predict on validation set
SURF_MLP_pred = net_SURF(validationFeatureVector');
SURF_MLP_pred2 = [];
for i = 1:numValidationImages
[value, number] = max(SURF_MLP_pred(:,i));
SURF_MLP_pred2 = [SURF_MLP_pred2; validation_number(number)];
end
SURF_MLP_pred2 = categorical(str2num(cell2mat(SURF_MLP_pred2)));
% accuracy score
accuracy_SURF_MLP = sum(SURF_MLP_pred2 == validationSVM) / length(validationSVM);
%% HOG - MLP
% train MLP network
hiddenSizesHog = 80;
trainFcnHog = 'trainscg';
% Define network architecture
net_HOG = patternnet(hiddenSizesHog,trainFcnHog);
net_HOG = configure(net_HOG,trainingFeatures',SURF_Training_Labels);
net_HOG = train(net_HOG,trainingFeatures',SURF_Training_Labels);
% Find the predictions for MLP network
mlp_HOG_predict = net_HOG(validationFeatures');
% create empty cell
mlp_HOG_predict2 = [];
for i = 1:numValidationImages
[value, number] = max(mlp_HOG_predict(:,i));
mlp_HOG_predict2 = [mlp_HOG_predict2; validation_number(number)];
end
mlp_HOG_predict2 = categorical(str2num(cell2mat(mlp_HOG_predict2)));
% accuracy score for MLP HOG
accuracy_HOG_MLP = sum(mlp_HOG_predict2 == validationSVM) / length(validationSVM);
%% Save Models
save('SVM_SURF.mat','SURF_SVM');
save('SVM_HOG.mat','HOG_SVM');
save('MLP_HOG.mat','net_HOG');
save('MLP_SURF.mat','net_SURF');
save('SURF_bagFeatures.mat', 'bag');