-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMain.java
203 lines (173 loc) · 4.67 KB
/
Main.java
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
/**
@author Akshay Mattoo
*/
package NNClassifier;
import NNClassifier.*;
import NNClassifier.Classifier.*;
import NNClassifier.Distance.*;
import NNClassifier.VPTree.*;
import java.util.*;
import java.io.*;
public class Main
{
public static void main(String[] args)
{
//Set Path to Train and Test dataset
String trainImagesFile = "NNClassifier/Data/train_images";
String trainLabelsFile = "NNClassifier/Data/train_labels";
String testImagesFile = "NNClassifier/Data/test_images";
String testLabelsFile = "NNClassifier/Data/test_labels";
// Read Train Images
System.out.print("Reading train images...");
FileInputStream fin = null;
ArrayList <Image> trainImages = null;
try
{
fin = new FileInputStream (trainImagesFile);
trainImages = readImages (fin);
System.out.print("\tRead!\n");
}
catch (FileNotFoundException e)
{
System.out.print("\tERROR! File not found\n");
}
// Read Train Labels
System.out.print("Reading train labels...");
fin = null;
ArrayList <Integer> trainLabels = null;
try
{
fin = new FileInputStream (trainLabelsFile);
trainLabels = readLabels (fin);
System.out.print("\tRead!\n");
}
catch (FileNotFoundException e)
{
System.out.print("\tERROR! File not found\n");
}
OneNNClassifier classifier = null;
System.out.print("Setting distance metric as...");
if (args.length==0 || ((String)args[0]).equals("l2"))
{
l2Distance dist = new l2Distance ();
classifier = new OneNNClassifier (dist);
System.out.print("\tEuclidean distance\n");
}
else if (((String)(args[0])).equals("l1"))
{
l1Distance dist = new l1Distance ();
classifier = new OneNNClassifier (dist);
System.out.print("\tManhattan distance\n");
}
else if (((String)(args[0])).equals("linf"))
{
linfDistance dist = new linfDistance ();
classifier = new OneNNClassifier (dist);
System.out.print("\tChebyshev distance\n");
}
else
{
System.out.print("\tInput not recognized! Using Euclidean distance by default\n");
l2Distance dist = new l2Distance ();
classifier = new OneNNClassifier (dist);
}
// Train Classifier
System.out.print("Training classifier...");
classifier.train(trainImages, trainLabels);
System.out.print("\tDone!\n");
// Read Test Images
System.out.print("Reading test images...");
fin = null;
ArrayList <Image> testImages = null;
try
{
fin = new FileInputStream (testImagesFile);
testImages = readImages (fin);
System.out.print("\tRead!\n");
}
catch (FileNotFoundException e)
{
System.out.print("\tERROR! File not found\n");
}
// Read Test Labels
System.out.print("Reading test labels...");
fin = null;
ArrayList <Integer> testLabels = null;
try
{
fin = new FileInputStream (testLabelsFile);
testLabels = readLabels (fin);
System.out.print("\tRead!\n");
}
catch (FileNotFoundException e)
{
System.out.print("\tERROR! File not found\n");
}
System.out.print("Running classifier over test data...");
classifier.test (testImages);
System.out.print("\tPredictions stored!\n");
System.out.println("Calculating Accuracy...");
double acc = classifier.getAccuracy(testLabels);
System.out.println("Accuracy = " + acc);
}
private static ArrayList <Image> readImages (FileInputStream fin)
{
DataInputStream din = new DataInputStream (fin);
int magicNum = readInt(din);
int numItems = readInt(din);
int row = readInt(din);
int col = readInt(din);
ArrayList <Image> data = new ArrayList <Image> ();
for (int i=0; i<numItems; ++i)
{
ArrayList <Integer> pixels = new ArrayList <Integer> ();
for (int j=0; j<row*col; ++j)
{
int p = 0;
try
{
p = fin.read();
}
catch (IOException e)
{
System.out.println("ERROR! Could not read images");
}
pixels.add(p);
}
Image img = new Image (pixels);
data.add(img);
}
return data;
}
private static ArrayList <Integer> readLabels (FileInputStream fin)
{
DataInputStream din = new DataInputStream (fin);
int magicNum = readInt(din);
int numItems = readInt(din);
ArrayList <Integer> labels = new ArrayList <Integer> ();
for (int i=0; i<numItems; ++i)
{
int label = 0;
try
{
label = fin.read();
}
catch (IOException e)
{
System.out.println("ERROR! Could not read labels");
}
labels.add(label);
}
return labels;
}
private static int readInt (DataInputStream din)
{
try
{
return din.readInt();
}
catch (IOException e)
{ }
return 0;
}
}