Skip to content

Commit

Permalink
Merge branch 'master' of github.com:jervisfm/Digit-Recognizer
Browse files Browse the repository at this point in the history
  • Loading branch information
vrdabomb5717 committed Dec 13, 2011
2 parents 78491a9 + 46734f7 commit df98ac3
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 130 deletions.
49 changes: 43 additions & 6 deletions README
Original file line number Diff line number Diff line change
@@ -1,8 +1,45 @@
Jervis Muindi
Varun Ravishankar
Biometric Project
28th November 2011

Jervis Muindi (jjm2190)
Varun Ravishankar (vr2263)
Biometric Final Project
12th December 2011
---------------------------
About
=======
This is a handwritten digit recognizer that uses a nearest neighbor classifier to recognize handwritten single digits.
This is a handwritten digit recognizer that uses a nearest neighbor classifier to recognize handwritten single digits.


Files:
=======
readDATA.m -> reads in the MNIST dataset. The data should be in a subdirectory called 'MNIST'
loadHndDATA.m -> reads in the manually collected images. These images should in a subdirectory called 'img'
procTD.m -> Processes the MNIST training set and bounds the images to 28x28 bounding box.
proc.m -> Processes a test image and bounds it in a 28x28 bounding box.
approach1.m -> Does classification using raw features
approach2.m -> Binds digits to a bounding box and then applies nearest neighbor classifer
approach3.m -> Does nearest neighbor classification with K = 3.
extract.m -> Finds the bounding box in an image with a digit and returns that bounding box.
binarze.m -> Converts an image to a binary image of 0s and 1s.
pca.m -> Implements PCA
dopca.m -> Tests PCA apporach to see how well it does.
darken.m -> Converts white background in a image to a black background.



Before Running
==============
Make sure that the directory structure is as follows:

1) MNIST : This directory should contain all of the MNIST raw data set and it should be in extracted form. You can download the dataset from http://yann.lecun.com/exdb/mnist/
2) img : This should contain the 200 scanned images.

If you do not have this structure, you will get a file not found error as the scripts expects the data to be in these locations.


Independent Dataset
===================
For the classification of the independently gathered dataset, the relevant files are :
approach1.m ; approach2.m and approach3.m and dopca.m.

When you run these you scripts you will get a current estimate of how well the classifier is doing on test data. Also note that
the scripts output estimated time of completion when carrying out computationally intensive task that can take a while to run.
Refers to the files description above to infer what each file does and for more details please see the report of Jervis Muindi.
6 changes: 3 additions & 3 deletions approach1.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function approach1()

sizeTrain = size(train_data,2);
sizeTest = size(test_data,2);
sizeTest = 50;
sizeTest = 200;

%Do a quick nearest neighbor test classification.
errors = 0;
Expand Down Expand Up @@ -64,9 +64,9 @@ function approach1()
if( test_labels(i) ~= result(i) )

fprintf('Curr Accuracy: %f | %d,%d.\n', curr_acc, test_labels(i),result(i) );
figure;
%figure;
im = [test_data{i} train_data{minIdx} ];
imshow(im);
%imshow(im);
continue;
end

Expand Down
103 changes: 6 additions & 97 deletions approach3.m
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
% Jervis Muindi
% Does classification on test data set.
% By applying nearest neighbor directly
% on extracted digits that have been bounded to
% a box and also thickening the test images by 2 pixels.
%Jervis Muindi
% Biometrics
% K-nearest neighbor with K = 3.
function approach3()
% Does a test for classifying the handwritten images.

Expand All @@ -13,94 +11,6 @@ function approach3()

[test_data, test_labels] = loadHndDATA();

sizeTrain = size(train_data,2);
sizeTest = size(test_data,2);
sizeTest = 50;

%Do a quick nearest neighbor test classification.
errors = 0;
for i = 1:sizeTest

fprintf('test %d - ', i);
tv = test_data{i};
%process the test image so that it's also bound to a box of 28 x 28 and also thicken it by adding 2 pixels to digit outline..
tv = proc2(tv);
tv = tv(:);

minIdx = 0;
minDist = Inf;

for j = 1:sizeTrain

av = train_data{j};
av = av(:);
diff = tv - av;
dist = norm(diff,2);

if(dist < minDist)
minDist = dist;
%disp(j);
minIdx = j;
end
%disp('running');
%disp(dist);

end
%error('s');

%save result;

result(i) = train_labels{minIdx};


%do accuracy checking in line
if( test_labels(i) ~= result(i) )
errors = errors + 1;

end

tot = i;
curr_acc = (tot - errors) / tot;

if( test_labels(i) ~= result(i) )

fprintf('Curr Accuracy: %f | %d,%d.\n', curr_acc, test_labels(i),result(i) );
figure;
im = [test_data{i} train_data{minIdx} ];
%imshow(im);
continue;
end

fprintf('Curr Accuracy: %f.\n', curr_acc);

end



end











%{
K NEaREST NEIGHTBOR
function testhnd()
% Does a test for classifying the handwritten images.
disp('reading data');
% assume, we have loaded
[train_data, train_labels ] = readDATA();
[test_data, test_labels] = loadHndDATA();
sizeTrain = size(train_data,2);
%sizeTrain = 10000;
sizeTest = size(test_data,2);
Expand All @@ -114,6 +24,7 @@ function testhnd()

fprintf('test %d - ', i);
tv = test_data{i};
tv = proc(tv); %process the test image so that it's also bound to a box of 28 x 28.
tv = tv(:);


Expand Down Expand Up @@ -175,8 +86,8 @@ function testhnd()

if( test_labels(i) ~= result(i) )

fprintf('Curr Accuracy: %f | %d,%d.\n', curr_acc, test_labels(i),result(i) );
figure;
fprintf('Curr Accuracy: %f | %d,%d.\n', curr_acc, test_labels(i),result(i) ); % print out actual number | classified number.
%figure;
continue;
end
fprintf('Curr Accuracy: %f.\n', curr_acc );
Expand All @@ -186,5 +97,3 @@ function testhnd()


end
%}
4 changes: 3 additions & 1 deletion darken.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

% Jervis Muindi
% Biometrics
% Dec 2011

function r = darken(A)
%convert from white background to black background
Expand Down
113 changes: 113 additions & 0 deletions dopca.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
function [r,pc] = dopca()
%UNTITLED9 Summary of this function goes here
% Detailed explanation goes here



% with 60,000 samples , its too much. don't have enough memory
% on hand to compute (need 24gb).
%
% so just look at 10,000 samples -> this needs 0.8Gb which we have.
%

[train_data, train_labels, test_data, test_labels] = readDATA();
%train_data = procTD(train_data);
[test_data, test_labels] = loadHndDATA();


length = size(train_data,2);
length = 2000;
data = zeros(784,length);


for i =1:length
if(mod(i,1000) == 0)
disp(i);
end

img = train_data{i};
img = img(:);
data(:,i) = img;


end

disp('doing pca...');

[r, pc] = pca(data);
%[r, pc] = pca2(data');
disp('pca done');
disp('proj data');
size(r)
disp('principal comp')
size(pc)

%error('s');
disp('doiing testing of pca results...');

train_size = length;
train_data = r;
errors=0;

for i = 1:200


tv = test_data{i};
%tv = proc(tv);
tv = tv(:);
%tv = tv'
ptv = pc' * tv;
tv = ptv;

minIdx = 0;
minDist = Inf;
for k = 1:train_size
j=k;
av = train_data(j);
av = av(:);
diff = tv - av;
dist = norm(diff,2);

if(dist < minDist)
minDist = dist;
%disp(j);
minIdx = j;
end

end


result(i) = train_labels{minIdx};


%do accuracy checking in line
if( test_labels(i) ~= result(i) )
errors = errors + 1;

end
tot = i;
curr_acc = (tot - errors) / tot;


if( test_labels(i) ~= result(i) )

fprintf('Curr Accuracy: %f | %d,%d.\n', curr_acc, test_labels(i),result(i) );
%figure;
%im = [test_data{i} train_data{minIdx} ];
%imshow(im);
continue;
end

fprintf('Curr Accuracy: %f.\n', curr_acc);


end






end


27 changes: 8 additions & 19 deletions loadHndDATA.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
img = img / max(img(:)); % normalize values to have maximum of 1.
img = darken(img); %binarize image and convert to a black background.

%do extraction and make it fill up entire 28x28
%img = extract(img);
%img = imresize(img, [28 28]);

test_images{i} = img;
test_labels(i) = mod(i,10);

Expand All @@ -48,23 +52,8 @@

end

% Converts from from white background to black background and
% Binarizes the image in the process.
function r = darken(A)

rows = size(A,1);
cols = size(A,2);

for i = 1:rows
for j = 1:cols
if(A(i,j) == 1)
A(i,j) = 0;
else
A(i,j) = 1; %now we're binarizing img.
end
end
end

r = A;
end





9 changes: 6 additions & 3 deletions pca.m
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
% Jervis Muindi
% Biometrics
% Dec 2011
function [ signals, PC, V ] = pca( data )
% Perform PCA.
% Data is the data matrix having dimensions in the columns and each image vector in the rows
Expand All @@ -24,7 +27,7 @@
size(data)
disp('computing conv');

covariance = 1 / (N-1) * (data * data') ;
covariance = 1 / (N-1) * data * data' ;

disp('finding eig vals');
%find the eigen vectors and eigen values
Expand All @@ -39,8 +42,8 @@

V = sorted;
disp(' e v');
V
rindices = rindices(1:10, :);
%V
rindices = rindices(1:20, :);
%size(rindices)

%PC = PC(:, rindices);
Expand Down
Loading

0 comments on commit df98ac3

Please sign in to comment.