Skip to content

Instantly share code, notes, and snippets.

@venil7
Last active January 31, 2021 01:42
Show Gist options
  • Save venil7/534dfedcff23b0e8fbb5626e7d169677 to your computer and use it in GitHub Desktop.
Save venil7/534dfedcff23b0e8fbb5626e7d169677 to your computer and use it in GitHub Desktop.
MNIST: IDX ubyte to JSON, for JavaScript Machine Learning
const fs = require('fs');
const dataFileBuffer = fs.readFileSync('./train-images-idx3-ubyte');
const labelFileBuffer = fs.readFileSync('./train-labels-idx1-ubyte');
const pixelValues = [];
for (let image = 0; image <= 59999; image++) {
const pixels = [];
for (let x = 0; x <= 27; x++) {
for (let y = 0; y <= 27; y++) {
pixels.push(dataFileBuffer[(image * 28 * 28) + (x + (y * 28)) + 15]);
}
}
const label = JSON.stringify(labelFileBuffer[image + 8]);
const data = pixels;
const imageData = {
label,
data
};
pixelValues.push(imageData);
}
console.log(JSON.stringify(pixelValues));
//node script.js > mnist.json
//produces: [{"label":"5","data":[28,0,0,0,0,0,0,0,...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment