Skip to content

Commit

Permalink
Finished A3 Q1 roughly, might revisit
Browse files Browse the repository at this point in the history
  • Loading branch information
MaiHameed committed Apr 4, 2021
1 parent fa1507e commit 185a924
Show file tree
Hide file tree
Showing 539 changed files with 98,613 additions and 18 deletions.
45 changes: 43 additions & 2 deletions generate_cropped_notfaces.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

% Crop image
croppedImage = img(startRow:startRow+dim-1, ...
startCol:startCol+dim-1);
startCol:startCol+dim-1, :);

% Save image
imgPath = strcat(new_imageDir, '/', int2str(n_have), '.jpg');
Expand All @@ -42,4 +42,45 @@
% list of uncropped images
n_have = n_have + 1;
i = i + 1;
end
end

%% Split training images into a training set and validation set
percentTraining = 80;
numOfTraining = floor((percentTraining/100)*n_have);

% Making the directories
facesDir = 'cropped_training_images_faces';
facesList = dir(sprintf('%s/*.jpg',facesDir));

notFacesDir = 'cropped_training_images_notfaces';
notFacesList = dir(sprintf('%s/*.jpg',notFacesDir));

[~,~,~] = mkdir(strcat(facesDir,'/validation'));
[~,~,~] = mkdir(strcat(facesDir,'/training'));
[~,~,~] = mkdir(strcat(notFacesDir,'/validation'));
[~,~,~] = mkdir(strcat(notFacesDir,'/training'));

for i = 1:numOfTraining
% Copy faces training images
source = strcat(facesDir,'/',facesList(i).name);
dest = strcat(facesDir,'/training/',facesList(i).name);
copyfile(source, dest);

% Copy not faces training images
source = strcat(notFacesDir,'/',notFacesList(i).name);
dest = strcat(notFacesDir,'/training/',notFacesList(i).name);
copyfile(source, dest);
end

for i = numOfTraining+1:n_have
% Copy faces validation images
source = strcat(facesDir,'/',facesList(i).name);
dest = strcat(facesDir,'/validation/',facesList(i).name);
copyfile(source, dest);

% Copy not faces validation images
source = strcat(notFacesDir,'/',notFacesList(i).name);
dest = strcat(notFacesDir,'/validation/',notFacesList(i).name);
copyfile(source, dest);
end

22 changes: 14 additions & 8 deletions get_features.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
close all
clear
run('../vlfeat-0.9.20/toolbox/vl_setup')
%% Run if VLFeat is not set up
run('vlfeat-0.9.21/toolbox/vl_setup');

%% Get Features
close all;
clear;

pos_imageDir = 'cropped_training_images_faces';
pos_imageList = dir(sprintf('%s/*.jpg',pos_imageDir));
Expand All @@ -10,15 +13,18 @@
neg_imageList = dir(sprintf('%s/*.jpg',neg_imageDir));
neg_nImages = length(neg_imageList);

cellSize = 6;
featSize = 31*cellSize^2;
% cellSize needs to be a factor of 36
cellSize = 4;
im = imread(sprintf('%s/%s',pos_imageDir,pos_imageList(1).name));
[imRows, imCols, ~] = size(im);
featSize = 31*(imRows/cellSize)*(imCols/cellSize);

pos_feats = zeros(pos_nImages,featSize);
for i=1:pos_nImages
im = im2single(imread(sprintf('%s/%s',pos_imageDir,pos_imageList(i).name)));
feat = vl_hog(im,cellSize);
pos_feats(i,:) = feat(:);
fprintf('got feat for pos image %d/%d\n',i,pos_nImages);
% fprintf('got feat for pos image %d/%d\n',i,pos_nImages);
% imhog = vl_hog('render', feat);
% subplot(1,2,1);
% imshow(im);
Expand All @@ -32,7 +38,7 @@
im = im2single(imread(sprintf('%s/%s',neg_imageDir,neg_imageList(i).name)));
feat = vl_hog(im,cellSize);
neg_feats(i,:) = feat(:);
fprintf('got feat for neg image %d/%d\n',i,neg_nImages);
% fprintf('got feat for neg image %d/%d\n',i,neg_nImages);
% imhog = vl_hog('render', feat);
% subplot(1,2,1);
% imshow(im);
Expand All @@ -41,4 +47,4 @@
% pause;
end

save('pos_neg_feats.mat','pos_feats','neg_feats','pos_nImages','neg_nImages')
save('pos_neg_feats.mat','pos_feats','neg_feats','pos_nImages','neg_nImages');
36 changes: 28 additions & 8 deletions train_svm.m
Original file line number Diff line number Diff line change
@@ -1,13 +1,33 @@
run('../vlfeat-0.9.20/toolbox/vl_setup')
load('pos_neg_feats.mat')
%% Run if VLFeat is not set up
run('vlfeat-0.9.20/toolbox/vl_setup');

feats = cat(1,pos_feats,neg_feats);
labels = cat(1,ones(pos_nImages,1),-1*ones(neg_nImages,1));
%% Split into training and validation
close all;
clear;

lambda = 0.1;
[w,b] = vl_svmtrain(feats',labels',lambda);
load('pos_neg_feats.mat');

percentTraining = 80;
numOfTraining = floor((percentTraining/100)*pos_nImages);

trainFeats = cat(1,pos_feats(1:numOfTraining,:), ...
neg_feats(1:numOfTraining,:));
validFeats = cat(1,pos_feats(1+numOfTraining:end,:), ...
neg_feats(1+numOfTraining:end,:));
trainLabels = cat(1,ones(numOfTraining,1), ...
-1*ones(numOfTraining,1));
validLabels = cat(1,ones(pos_nImages-numOfTraining,1), ...
-1*ones(pos_nImages-numOfTraining,1));

%% Train

lambda = 0.06;
[w,b] = vl_svmtrain(trainFeats',trainLabels',lambda);

fprintf('Classifier performance on train data:\n')
confidences = [pos_feats; neg_feats]*w + b;
confidences = [pos_feats(1:numOfTraining,:); neg_feats(1:numOfTraining,:)]*w + b;
[tp_rate, fp_rate, tn_rate, fn_rate] = report_accuracy(confidences, trainLabels);

[tp_rate, fp_rate, tn_rate, fn_rate] = report_accuracy(confidences, labels);
fprintf('Classifier performance on validation data:\n')
confidences = [pos_feats(1+numOfTraining:end,:); neg_feats(1+numOfTraining:end,:)]*w + b;
[tp_rate, fp_rate, tn_rate, fn_rate] = report_accuracy(confidences, validLabels);
3 changes: 3 additions & 0 deletions vlfeat-0.9.21/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.manifest -crlf -diff -merge
*.vcproj binary
*.sln binary
57 changes: 57 additions & 0 deletions vlfeat-0.9.21/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Mac OS X Finder
.DS_Store

# MEX files
*.mexglx
*.mexmac
*.mexmaci
*.mexmaci64
*.mexa64
*.mex

# Python
*.pyc

# Other generated files and directories
VERSION
docsrc/version.html
bin
results
doc
build
toolbox/mexw32
toolbox/mexmaci
toolbox/mexmaci64
toolbox/mexmac
toolbox/mexglx
toolbox/mexa64
toolbox/mexw64
toolbox/mex
toolbox/noprefix

# Xcode
vlfeat.xcodeproj/*.mode1
vlfeat.xcodeproj/*.mode1v3
vlfeat.xcodeproj/*.mode2v3
vlfeat.xcodeproj/*.pbxuser
vlfeat.xcodeproj/*.perspective*
vlfeat.xcodeproj/*.xcworkspace
vlfeat.xcodeproj/xcuserdata

# Visual C++
vlfeat.suo
vlfeat.ncb
vlfeat.vcproj.*.user
vc/*

# Emacs backups
*~

# Vim swap files
.*.swp

# Opt directory
opt/*

# iDraw files
*.idraw
25 changes: 25 additions & 0 deletions vlfeat-0.9.21/COPYING
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Copyright (C) 2007-11, Andrea Vedaldi and Brian Fulkerson
Copyright (C) 2012-13, The VLFeat Team
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the
distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Loading

0 comments on commit 185a924

Please sign in to comment.