-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReader.java
More file actions
296 lines (246 loc) · 10.7 KB
/
Reader.java
File metadata and controls
296 lines (246 loc) · 10.7 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
import java.io.FileInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
/**
* A class for reading the MNIST data set from the <b>decompressed</b>
* (unzipped) files that are published at
* <a href="http://yann.lecun.com/exdb/mnist/">
* http://yann.lecun.com/exdb/mnist/</a>.
*
* Original code by @RayDeeA on StackOverflow here:
* https://stackoverflow.com/a/20383900/16755079
*
* I have made edits and comments to that code
*/
public class Reader {
public static final int TYPE_FILE_IDX_IMAGE = 0;
private int fileType;
private FileInputStream file;
private int magicNumber;
private int[] dimensions;
private int currentIndex; //counting through dimensions[0]
public Reader(String path, int fileType) {
this.fileType = fileType;
if (fileType == TYPE_FILE_IDX_IMAGE) {
setupIDXImage(path);
} else {
// throw FileTypeNotFoundException;
}
}
public int getImageSize() {
int size = 1;
for (int i = 1; i < dimensions.length; i++) {
size *= dimensions[i];
}
return size;
}
private void setupIDXImage(String path) {
try {
file = new FileInputStream(path);
/**
* The basic format for idx files is:
* magic number
* size in dimension 0
* size in dimension 1
* size in dimension 2
* .....
* size in dimension N
* data
*/
/**
* The magic number is an integer (MSB first). The first 2 bytes are always 0.
*
* The third byte codes the type of the data:
* 0x08: unsigned byte
* 0x09: signed byte
* 0x0B: short (2 bytes)
* 0x0C: int (4 bytes)
* 0x0D: float (4 bytes)
* 0x0E: double (8 bytes)
*/
this.magicNumber = (file.read() << 24) | (file.read() << 16) | (file.read() << 8) | (file.read());
/**
* The 4-th byte codes the number of dimensions of the vector/matrix: 1 for vectors, 2 for matrices....
*/
int dimension_count = 0xF & magicNumber;
this.dimensions = new int[dimension_count];
/**
* The sizes in each dimension are 4-byte integers (MSB first, high endian, like in most non-Intel processors).
*
* The data is stored like in a C array, i.e. the index in the last dimension changes the fastest.
*/
for (int i = 0; i < dimensions.length; i++) {
dimensions[i] = (file.read() << 24) | (file.read() << 16) | (file.read() << 8) | (file.read());
}
this.currentIndex = 0;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* return the first len sets of data
* @params len : the number of sets, if len would cause currentIndex to surpass dimensions[0], only return the the rest of the file
*
* Commented out code here can be used for creating PNG files of the image
*/
public int[][] read(int len) {
// we can basically read the image as a long line of pixels.
// pixels wrap around to the next line when you reach the end.
int numberOfPixels = 1;
for (int i = 1; i < dimensions.length; i++) {
numberOfPixels *= dimensions[i];
}
System.out.println("Num pxl = " + numberOfPixels);
// int[] imgPixels = new int[numberOfPixels];
// BufferedImage image = new BufferedImage(dimensions[2], dimensions[1], BufferedImage.TYPE_INT_ARGB);
//indexing stuff
int endIndex;
if (currentIndex + len > dimensions[0]) {
endIndex = dimensions[0];
} else {
endIndex = currentIndex + len;
}
int[][] batch = new int[endIndex-currentIndex][numberOfPixels];
for (int i = 0; i < batch.length; i++) {
currentIndex++;
//log every 100 images
if(currentIndex % 100 == 0) {System.out.println("Number of images extracted: " + currentIndex);}
for (int p = 0; p < numberOfPixels; p++) {
try {
int gray = file.read();
batch[i][p] = gray;
// gray = 255 - gray;
// imgPixels[p] = 0xFF000000 | (gray<<16) | (gray<<8) | gray;
} catch (IOException e) {
e.printStackTrace();
}
}
// image.setRGB(0, 0, dimensions[2], dimensions[1], imgPixels, 0, dimensions[2]);
// File outputfile = new File("./Number_Images/" + "_0" + currentIndex + ".png");
// try {
// ImageIO.write(image, "png", outputfile);
// } catch (IOException e) {
// e.printStackTrace();
// }
}
return batch;
}
/**
* read the next line of file
*/
public int[][] read() {
return read(1);
}
/**
* This is the original code written by @RayDeeA (with added comments).
*
* Commented out to avoid accidental overwritting
public static void read() {
* a FileInputStream is basically a stream of bytes.
* We will referrence the file to get the stream from
* an actual file in the file system. This file is
* selected with the `inputImagePath` and `inputLabelPath`.
FileInputStream inImage = null;
FileInputStream inLabel = null;
// change the `.` according to where you put the files
String inputImagePath = "./train-images-idx3-ubyte";
String inputLabelPath = "./train-labels-idx1-ubyte";
String outputPath = "./";
//remember that MNIST is a bunch of images for the integers 0-9
//this will keep track of the frequency of each integer
int[] hashMap = new int[10];
try {
* FileInputStream.read() throws an IOException,
* so this all has to be in a try-catch. Also,
* a few other methods throw various exceptions (see
* the catches below)
inImage = new FileInputStream(inputImagePath);
inLabel = new FileInputStream(inputLabelPath);
* The basic format for idx files is:
* magic number
* size in dimension 0
* size in dimension 1
* size in dimension 2
* .....
* size in dimension N
* data
* The magic number is an integer (MSB first). The first 2 bytes are always 0.
*
* The third byte codes the type of the data:
* 0x08: unsigned byte
* 0x09: signed byte
* 0x0B: short (2 bytes)
* 0x0C: int (4 bytes)
* 0x0D: float (4 bytes)
* 0x0E: double (8 bytes)
*
* The 4-th byte codes the number of dimensions of the vector/matrix: 1 for vectors, 2 for matrices....
int magicNumberImages = (inImage.read() << 24) | (inImage.read() << 16) | (inImage.read() << 8) | (inImage.read());
int numberOfImages = (inImage.read() << 24) | (inImage.read() << 16) | (inImage.read() << 8) | (inImage.read());
* The sizes in each dimension are 4-byte integers (MSB first, high endian, like in most non-Intel processors).
*
* The data is stored like in a C array, i.e. the index in the last dimension changes the fastest.
int numberOfRows = (inImage.read() << 24) | (inImage.read() << 16) | (inImage.read() << 8) | (inImage.read());
int numberOfColumns = (inImage.read() << 24) | (inImage.read() << 16) | (inImage.read() << 8) | (inImage.read());
int magicNumberLabels = (inLabel.read() << 24) | (inLabel.read() << 16) | (inLabel.read() << 8) | (inLabel.read());
int numberOfLabels = (inLabel.read() << 24) | (inLabel.read() << 16) | (inLabel.read() << 8) | (inLabel.read());
//BufferedImage witih size numberOfColumns x numberOfRows using ARGB color space
BufferedImage image = new BufferedImage(numberOfColumns, numberOfRows, BufferedImage.TYPE_INT_ARGB);
// we can basically read the image as a long line of pixels.
// pixels wrap around to the next line when you reach the end.
int numberOfPixels = numberOfRows * numberOfColumns;
int[] imgPixels = new int[numberOfPixels];
for (int i = 0; i < numberOfImages; i++) {
//log every 100 images
if(i % 100 == 0) {System.out.println("Number of images extracted: " + i);}
for (int p = 0; p < numberOfPixels; p++) {
//MNIST uses 0 as white -> 255 as black
//ARGB uses 255 as white -> 0 as black
int gray = 255 - inImage.read(); //flip the byte
//fully opaque with repeating `gray` bytes
//basically, gray scale the pixel
imgPixels[p] = 0xFF000000 | (gray<<16) | (gray<<8) | gray;
}
//fill the entire `image` with the values dictated by `imgPixels`
image.setRGB(0, 0, numberOfColumns, numberOfRows, imgPixels, 0, numberOfColumns);
int label = inLabel.read();
hashMap[label]++;
File outputfile = new File(outputPath + label + "_0" + hashMap[label] + ".png");
//This would write the image to your root directory
//Remember that this happens a couple tens of thousands of times
//It is generally not a good idea
// ImageIO.write(image, "png", outputfile);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// Clean up the files
if (inImage != null) {
try {
inImage.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (inLabel != null) {
try {
inLabel.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
*/
}