Repository navigation
Expand file tree
/
Copy pathClient.java
More file actions
257 lines (231 loc) · 6.65 KB
/
Copy pathClient.java
File metadata and controls
257 lines (231 loc) · 6.65 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package imgrec;
import java.io.Console;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.logging.ConsoleHandler;
import javax.imageio.IIOException;
import javax.swing.JApplet;
import javax.swing.JPanel;
public class Client {
/**
* k-Nearest Neighbor Classifier
* Ryan Taylor, June 11 2019
*
*/
static ArrayList<FileImage> images = new ArrayList<FileImage>();
static ArrayList<double[][]> imageDecomps = new ArrayList<double[][]>();
//testDecomps was exclusively used for 7 heatmap, not necessary for function
static ArrayList<double[][]> testDecomps = new ArrayList<double[][]>();
static FileImage testImg;
static FileImage trainImg;
static double[][] testDecomp;
static FileImage[] results = new FileImage[10];
static ArrayList<String> rests = new ArrayList<String>();
static File f;
static PrintWriter idents;
static PrintWriter dataPoints;
//see above comment
static PrintWriter testDecompArray;
static int ops;
public static void main(String[] args){
//creates FileImages from filepath and constructs 2D double arrays for each, stored in imageDecomps
initImages();
//only used for 7s output (not central to code)
try {
testDecompArray = new PrintWriter("test-decomps.txt", "UTF-8");
} catch (Throwable e2) {
e2.printStackTrace();
}
//printTestDecomps();
//
/*
*
* Failed set (L1, k=1):
* 29, 76, 77*, 129*, 139*, 148*, 178, 215*, 224*, 276, 306, 329*, 342
* [96.28571429% accurate]
*
* Failed set (L2, k=1):
* 77*, 129*, 139*, 148*, 215*, 224*, 227, 329*
* [97.7142857143% accurate]
*
*/
try {
idents = new PrintWriter("test-set-identifiers", "UTF-8");
} catch (FileNotFoundException | UnsupportedEncodingException e1) {
e1.printStackTrace();
}
//iterates through all test images, specifying k-nearest neighbor through "runImgs" algorithm
//prints results to idents > test-set-identifiers
int numImages = 1;
for(int i = 0; i < numImages; i++){
if(i == 148)
i++;
try {
// testImg = new FileImage(new File("./testSample/img_" + (i+1) + ".jpg"), "Unknown");
testImg = new FileImage(new File("./testSample/5y65.jpg"), "Unknown");
} catch (NullPointerException e){
i++;
}
trainImg = images.get(0);
try {
testDecomp = testImg.genPixArray();
} catch (IOException e) {
e.printStackTrace();
}
//idents.println(testImg.pixVals + ",");
runImgs(testDecomp);
//idents.println(rests.get(rests.size() - 1));
}
idents.close();
// System.out.println(images.get(1).file.getAbsolutePath() + ": " + results[0].file.getAbsolutePath() + ", " + results[9]);
}
private static void printTestDecomps() {
System.out.println(testDecomps.size());
// TODO Auto-generated method stub
double[][] testPixs = new double[28][28];
double sum = 0;
for(int r = 0; r < 28; r++){
for(int c = 0; c < 28; c++){
for(double[][] d : testDecomps){
sum += d[r][c];
System.out.println(sum);
}
testPixs[r][c] = (sum/testDecomps.size());
testDecompArray.println(testPixs[r][c]);
sum = 0;
}
}
testDecompArray.close();
}
public static void initImages() {
f = new File("./IMAGES/0");
for(File p : f.listFiles()){
images.add(new FileImage(p, "0"));
ops++;
}
f = new File("./IMAGES/1");
for(File p : f.listFiles()){
images.add(new FileImage(p, "1"));
ops++;
}
f = new File("./IMAGES/2");
for(File p : f.listFiles()){
images.add(new FileImage(p, "2"));
++ops;
}
f = new File("./IMAGES/3");
for(File p : f.listFiles()){
images.add(new FileImage(p, "3"));
++ops;
}
f = new File("./IMAGES/4");
for(File p : f.listFiles()){
images.add(new FileImage(p, "4"));
++ops;
}
f = new File("./IMAGES/5");
for(File p : f.listFiles()){
images.add(new FileImage(p, "5"));
++ops;
}
f = new File("./IMAGES/6");
for(File p : f.listFiles()){
images.add(new FileImage(p, "6"));
++ops;
}
f = new File("./IMAGES/7");
for(File p : f.listFiles()){
images.add(new FileImage(p, "7"));
++ops;
}
f = new File("./IMAGES/8");
for(File p : f.listFiles()){
images.add(new FileImage(p, "8"));
++ops;
}
f = new File("./IMAGES/9");
for(File p : f.listFiles()){
images.add(new FileImage(p, "9"));
++ops;
}
images.remove(0);
Collections.shuffle(images);
images.remove(0);
try {
dataPoints = new PrintWriter("datapoints", "UTF-8");
} catch (FileNotFoundException | UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
for(FileImage i : images){
try {
double[][] pixArray = i.genPixArray();
dataPoints.println(i.pixVals + "," + i.identifier);
imageDecomps.add(pixArray);
if(i.identifier == "7"){
testDecomps.add(pixArray);
}
System.out.println("Indexing " + (++ops/34185559.0)*100 + "%");
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void runImgs(double[][] test){
//declare 2D-double array to compare with -test-
double[][] curs;
//initialize variables
double min = Double.MAX_VALUE;
double max = Double.MIN_VALUE;
int mIndex = 0;
int xIndex = 0;
ArrayList<Double> totals = new ArrayList<Double>();
//iterate through training images
for(int c = 0; c < imageDecomps.size()-1; c++){
//assign second array to given training FileImage pixels
curs = imageDecomps.get(c);
//check that indices of two arrays are not equal (testing image is not training image)
if(!curs.equals(test)){
//assign value of compare (0.0 means identical images), using L2 distance
double total = compareArrayL2(curs, test);
trainImg = images.get(c);
totals.add(total);
if(total < min){
min = total;
mIndex = c;
}
if(total > max){
max = total;
xIndex = c;
}
}
}
//total iterations is equal to -num- * size of imageDecomps (~43,000) * size of images (1456) * k iteration (10)
// = num * 617,480,000
rests.add(images.get(mIndex).identifier);
System.out.println(rests.get(rests.size()-1));
}
public static double compareArrayL1(double[][] comp, double[][] orig){
double sum = 0.0;
for(int x = 0; x < 28; x++){
for(int y = 0; y < 28; y++){
sum += Math.abs(orig[x][y] - comp[x][y]);
}
}
return sum;
}
public static double compareArrayL2(double[][] comp, double[][] orig){
double sum = 0.0;
for(int x = 0; x < 28; x++){
for(int y = 0; y < 28; y++){
sum += (orig[x][y] - comp[x][y]) * (orig[x][y] - comp[x][y]);
}
}
return Math.sqrt(sum);
}
}