-
Notifications
You must be signed in to change notification settings - Fork 4
/
approach2.m
189 lines (122 loc) · 3.57 KB
/
approach2.m
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
% 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
%}