MCPcopy Create free account
hub / github.com/tensorflow/tfjs-examples / loadImages

Function loadImages

fashion-mnist-vae/data.js:63–95  ·  view source on GitHub ↗

* Load the images from the given file and normalize the data to 0-1 range. * * Input file should be in the MNIST/FashionMNSIT file format * * @param {string} filepath * * @returns {Float32Array[]} an array of images represented as typed arrays.

(filepath)

Source from the content-addressed store, hash-verified

61 * @returns {Float32Array[]} an array of images represented as typed arrays.
62 */
63async function loadImages(filepath) {
64 if (!fs.existsSync(filepath)) {
65 console.log(`Data File: ${filepath} does not exist.
66 Please see the README for instructions on how to download it`);
67 process.exit(1);
68 }
69
70 const buffer = await readFile(filepath)
71
72 const headerBytes = IMAGE_HEADER_BYTES;
73 const recordBytes = IMAGE_HEIGHT * IMAGE_WIDTH;
74
75 const headerValues = loadHeaderValues(buffer, headerBytes);
76 assert.equal(headerValues[0], IMAGE_HEADER_MAGIC_NUM);
77 assert.equal(headerValues[2], IMAGE_HEIGHT);
78 assert.equal(headerValues[3], IMAGE_WIDTH);
79
80 const images = [];
81 let index = headerBytes;
82 while (index < buffer.byteLength) {
83 const array = new Float32Array(recordBytes);
84 for (let i = 0; i < recordBytes; i++) {
85 // Normalize the pixel values into the 0-1 interval, from
86 // the original 0-255 interval.
87 array[i] = buffer.readUInt8(index++) / 255;
88 }
89 images.push(array);
90 }
91
92 assert.equal(images.length, headerValues[1]);
93 tf.util.shuffle(images);
94 return images;
95}
96
97/**
98 * Take an array of images (represented as typedarrays) and return

Callers 1

runFunction · 0.70

Calls 1

loadHeaderValuesFunction · 0.70

Tested by

no test coverage detected