-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPreProcess.java
More file actions
326 lines (265 loc) · 10.9 KB
/
PreProcess.java
File metadata and controls
326 lines (265 loc) · 10.9 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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
* Mark Agib
* 4/28/24
* Final
*/
package Final;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class PreProcess {
public static double[][] processImages(int numImagesToRead) throws FileNotFoundException, IOException {
return processImages("train-images.idx3-ubyte", numImagesToRead);
}
public static double[][] processImages(String imagesFileName, int numImagesToRead) throws FileNotFoundException, IOException {
String filePath = resolveDatasetPath(imagesFileName);
try (DataInputStream inputStream = new DataInputStream(new FileInputStream(filePath))) {
int magicNumber = inputStream.readInt();
if (magicNumber != 0x00000803) {
System.err.println("Invalid magic number. This may not be a valid image file.");
return null;
}
inputStream.readInt();
int numRows = inputStream.readInt();
int numColumns = inputStream.readInt();
System.out.println("Processing " + numImagesToRead + " " + numRows + "x" + numColumns + " images");
int[][] images = new int[numImagesToRead][numRows * numColumns];
for (int i = 0; i < numImagesToRead; i++) {
for (int j = 0; j < images[i].length; j++) {
images[i][j] = inputStream.readUnsignedByte();
}
if (i % 10 == 0) {
updateProgress(i, numImagesToRead);
}
}
double[][] orderedImages = new double[numImagesToRead][numRows * numColumns];
for (int i = 0; i < numImagesToRead; i++) {
orderedImages[i] = minMaxNormalization(images[i]);
}
System.out.println("");
System.out.println("Finished processing images!");
return transpose(orderedImages);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static double[][] processLabels(int numLabelsToRead) {
return processLabels("train-labels.idx1-ubyte", numLabelsToRead);
}
public static double[][] processLabels(String labelsFileName, int numLabelsToRead) {
String labelFilePath = resolveDatasetPath(labelsFileName);
try (DataInputStream inputStream = new DataInputStream(new FileInputStream(labelFilePath))) {
int magicNumber = inputStream.readInt();
if (magicNumber != 0x00000801) {
System.err.println("Invalid magic number. This may not be a valid labels file.");
return null;
}
int numLabels = inputStream.readInt(); // Read the number of labels
if (numLabels < numLabelsToRead) {
System.err.println("Requested more labels than available.");
return null;
}
byte[] labels = new byte[numLabelsToRead];
inputStream.readFully(labels);
System.out.println("Processing " + numLabelsToRead + " labels");
double[] orderedLabels = new double[numLabelsToRead];
for (int i = 0; i < numLabelsToRead; i++) {
orderedLabels[i] = (double) labels[i];
}
double[] categories = {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0};
System.out.println("Finished processing labels!");
return oneHotEncode(orderedLabels, categories);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private static String resolveDatasetPath(String filename) {
// Prefer repo-local dataset folder; fall back to CWD.
Path candidate = Paths.get("mnist-dataset", filename);
if (Files.exists(candidate)) {
return candidate.toString();
}
return Paths.get(filename).toString();
}
public static byte[] flatten(byte[][] image, int numRows, int numColumns) {
byte[] temp = new byte[numColumns * numRows];
int i = 0;
for (int row = 0; row < numRows; row++) {
for (int col = 0; col < numColumns; col++) {
temp[i] = image[row][col];
i++;
}
}
return temp;
}
public static double[] minMaxNormalization(byte[] image) {
// MNIST is already in [0,255], so global scaling works better than per-image min/max.
double[] newImages = new double[image.length];
for (int i = 0; i < image.length; i++) {
int pixel = (image[i] & 0xFF); // treat byte as unsigned
newImages[i] = pixel / 255.0;
}
return newImages;
}
public static double[] minMaxNormalization(int[] image) {
// MNIST is already in [0,255], so global scaling works better than per-image min/max.
double[] newImages = new double[image.length];
for (int i = 0; i < image.length; i++) {
newImages[i] = image[i] / 255.0;
}
return newImages;
}
public static double[][] oneHotEncode(double[] labels, double[] categories) {
double[][] result = new double[categories.length][labels.length];
for (int i = 0; i < categories.length; i++) {
for (int j = 0; j < labels.length; j++) {
if (labels[j] == categories[i]) {
result[i][j] = 1.0;
}
else {
result[i][j] = 0.0;
}
}
}
return result;
}
public static void updateProgress(int currentStep, int totalSteps) {
double progress = (double) currentStep / totalSteps;
int barLength = 100;
System.out.print("\r[");
int progressChars = (int) (progress * barLength);
for (int i = 0; i < barLength; i++) {
if (i < progressChars) {
System.out.print("=");
} else {
System.out.print(" ");
}
}
System.out.printf("] %.2f%%", progress * 100);
}
// public static void accuracy() {
// }
public static double[][] dot(double[][] matrixA, double[][] matrixB) {
double[][] newMatrix = new double[matrixA.length][matrixB[0].length];
for (int i = 0; i < newMatrix.length; i++) {
for (int j = 0; j < newMatrix[i].length; j++) {
for (int k = 0; k < matrixB.length; k++) {
newMatrix[i][j] += matrixA[i][k] * matrixB[k][j];
}
}
}
return newMatrix;
}
public static double[][] matrixCoefficientMultiplication(double[][] matrix, double coefficent) {
double[][] newMatrix = new double[matrix.length][matrix[0].length];
for (int i = 0; i < newMatrix.length; i++) {
for (int j = 0; j < newMatrix[i].length; j++) {
newMatrix[i][j] = matrix[i][j] * coefficent;
}
}
return newMatrix;
}
public static double[][] matrixOperations(double[][] matrixA, double[][] matrixB, boolean subtraction) {
double[][] newMatrix = new double[matrixA.length][matrixA[0].length];
double[][] newMatrixB;
if (subtraction) {
newMatrixB = matrixCoefficientMultiplication(matrixB, -1.0);
}
else {
newMatrixB = matrixB;
}
for (int i = 0; i < newMatrix.length; i++) {
for (int j = 0; j < newMatrix[i].length; j++) {
newMatrix[i][j] = matrixA[i][j] + newMatrixB[i][j];
}
}
return newMatrix;
}
public static double[][] matrixExp(double[][] matrix) {
double[][] newMatrix = new double[matrix.length][matrix[0].length];
for (int i = 0; i < newMatrix.length; i++) {
for (int j = 0; j < newMatrix[i].length; j++) {
newMatrix[i][j] = Math.exp(matrix[i][j]);
}
}
return newMatrix;
}
public static double sum(double[][] matrix) {
double sum = 0;
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
sum += matrix[i][j];
}
}
return sum;
}
public static double[][] sumSecondAxis(double[][] array) {
double[][] sums = new double[array.length][1];
for (int i = 0; i < array.length; i++) {
double sum = 0.0;
for (int j = 0; j < array[i].length; j++) {
sum += array[i][j];
}
sums[i][0] = sum;
}
return sums;
}
public static double[][] reshape(double[][] matrix) {
double[][] newMatrix = new double[matrix.length * matrix[0].length][1];
int k = 0;
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
newMatrix[k][0] = matrix[i][j];
k++;
}
}
return newMatrix;
}
public static double[] reshape(double[][] matrix, int numColumns) {
double[] newMatrix = new double[matrix.length * matrix[0].length];
int k = 0;
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
newMatrix[k] = matrix[i][j];
k++;
}
}
return newMatrix;
}
public static double[][] transpose(double[][] array) {
int length = array.length;
int imageSize = array[0].length;
double[][] transposedMatrix = new double[imageSize][length];
for (int i = 0; i < imageSize; i++) {
for (int j = 0; j < length; j++) {
transposedMatrix[i][j] = array[j][i];
}
}
return transposedMatrix;
}
public static double[][] copyAcross(double[][] matrix, int numColumns) {
double[][] result = new double[matrix.length][numColumns];
for (int i = 0; i < matrix.length; i++) {
double value = matrix[i][0];
for (int j = 0; j < numColumns; j++) {
result[i][j] = value;
}
}
return result;
}
public static double[][] elementWiseMult(double[][] matrixA, double[][] matrixB) {
double[][] newMatrix = new double[matrixA.length][matrixA[0].length];
for (int i = 0; i < matrixA.length; i++) {
for (int j = 0; j < matrixA[i].length; j++) {
newMatrix[i][j] = matrixA[i][j] * matrixB[i][j];
}
}
return newMatrix;
}
}