-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparse_mnist_dataset.js
66 lines (51 loc) · 2.19 KB
/
parse_mnist_dataset.js
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
const fs = require('fs');
function parseMNISTDataset(setSize, imagesPath, labelsPath, outputPath = "") {
const dataFileBuffer = fs.readFileSync(imagesPath);
const labelFileBuffer = fs.readFileSync(labelsPath);
const pixelValues = [];
for (let image = 0; image < setSize; image++) {
const pixels = [];
for (let x = 0; x < 28; x++) {
for (let y = 0; y < 28; y++) {
pixels.push(parseInt(dataFileBuffer[(image * 28 * 28) + (x + (y * 28)) + 16]));
}
}
const label = parseInt(labelFileBuffer[image + 8]);
// const binary_classifiers = [];
// for (var i = 0; i < 10; i++)
// binary_classifiers.push(label === i ? 1 : 0);
const imageData = [label, pixels];
pixelValues.push(imageData);
}
//console.log(pixelValues);
if (outputPath)
fs.writeFileSync(outputPath, JSON.stringify(pixelValues));
else
return pixelValues;
}
// The parsed data looks like:
// [ 5,
// [0, 0, 0, 0, 0, 0, 0, 0, ... ] -- image data
// ]
const trainingSetSize = 60000; // Number of examples in the training set
const testSetSize = 10000; // Number of examples in the test set
const trainingImagesPath = __dirname + '/training_sets/train-images.idx3-ubyte';
const trainingLabelsPath = __dirname + '/training_sets/train-labels.idx1-ubyte';
const testImagesPath = __dirname + '/training_sets/t10k-images.idx3-ubyte';
const testLabelsPath = __dirname + '/training_sets/t10k-labels.idx1-ubyte';
function writeDataToFile() {
// Generate parsed JSON for training set
parseMNISTDataset(trainingSetSize, trainingImagesPath, trainingLabelsPath, __dirname + '/training_sets/parsed_mnist_training_set.json');
// Generate parsed JSON for test set
parseMNISTDataset(testSetSize, testImagesPath, testLabelsPath, __dirname + '/training_sets/parsed_mnist_test_set.json');
}
function loadTrainingData() {
return parseMNISTDataset(trainingSetSize, trainingImagesPath, trainingLabelsPath);
}
function loadTestingData() {
return parseMNISTDataset(testSetSize, testImagesPath, testLabelsPath);
}
module.exports = {
loadTrainingData: loadTrainingData,
loadTestingData: loadTestingData,
};