Skip to content

Commit

Permalink
Added code to classify handwritten digits using nearest neighbor with…
Browse files Browse the repository at this point in the history
… MNIST dataset as the training set
  • Loading branch information
jervisfm committed Dec 12, 2011
1 parent eab1233 commit ce3ae12
Show file tree
Hide file tree
Showing 12 changed files with 1,078 additions and 0 deletions.
189 changes: 189 additions & 0 deletions approach1.m
@@ -0,0 +1,189 @@
% Jervis Muindi
% Biometrics Final Project
% 11th December 2011

% Does classification on test data set.
% By applying nearest neighbor directly
% on extracted features.
function approach1()
% 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);
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};
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);
sizeTest = 200;
%Do a quick nearest neighbor test classification.
errors = 0;
for i = 1:sizeTest
fprintf('test %d - ', i);
tv = test_data{i};
tv = tv(:);
minIdx = 0;
minDist = Inf;
arr(1) = 0;
arr(2) = 0;
arr(3) = 0;
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;
prev_first = arr(1);
prev_sec = arr(2);
arr(1) = minIdx;
arr(2) = prev_first;
arr(3) = prev_sec;
end
%disp('running');
%disp(dist);
end
num = [train_labels{arr(1)};train_labels{arr(2)} ;train_labels{arr(3)}];
indices = zeros(10,1);
for ii = 1:3
indices(num(ii)+1) = indices(num(ii)+1) + 1;
end
[x,number] = max(indices);
%error('s');
%save result;
result(i) = number - 1;
%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;
continue;
end
fprintf('Curr Accuracy: %f.\n', curr_acc );
end
end
%}
189 changes: 189 additions & 0 deletions approach2.m
@@ -0,0 +1,189 @@
% Jervis Muindi
% Does classification on test data set.
% By applying nearest neighbor directly
% on extracted digits that have been bounded to
% a box.
function approach2()
% Does a test for classifying the handwritten images.

disp('reading data');
% assume, we have loaded
[train_data, train_labels ] = readDATA();
train_data = procTD(train_data);% Process the trainig data to bound image to a bounding box.

[test_data, test_labels] = loadHndDATA();

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

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

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(:);

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);
sizeTest = 200;
%Do a quick nearest neighbor test classification.
errors = 0;
for i = 1:sizeTest
fprintf('test %d - ', i);
tv = test_data{i};
tv = tv(:);
minIdx = 0;
minDist = Inf;
arr(1) = 0;
arr(2) = 0;
arr(3) = 0;
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;
prev_first = arr(1);
prev_sec = arr(2);
arr(1) = minIdx;
arr(2) = prev_first;
arr(3) = prev_sec;
end
%disp('running');
%disp(dist);
end
num = [train_labels{arr(1)};train_labels{arr(2)} ;train_labels{arr(3)}];
indices = zeros(10,1);
for ii = 1:3
indices(num(ii)+1) = indices(num(ii)+1) + 1;
end
[x,number] = max(indices);
%error('s');
%save result;
result(i) = number - 1;
%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;
continue;
end
fprintf('Curr Accuracy: %f.\n', curr_acc );
end
end
%}

0 comments on commit ce3ae12

Please sign in to comment.