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

Method getData_

quantization/data_mnist.js:206–240  ·  view source on GitHub ↗
(isTrainingData)

Source from the content-addressed store, hash-verified

204 }
205
206 getData_(isTrainingData) {
207 let imagesIndex;
208 let labelsIndex;
209 if (isTrainingData) {
210 imagesIndex = 0;
211 labelsIndex = 1;
212 } else {
213 imagesIndex = 2;
214 labelsIndex = 3;
215 }
216 const size = this.dataset[imagesIndex].length;
217 tf.util.assert(
218 this.dataset[labelsIndex].length === size,
219 `Mismatch in the number of images (${size}) and ` +
220 `the number of labels (${this.dataset[labelsIndex].length})`);
221
222 // Only create one big array to hold batch of images.
223 const imagesShape = [size, IMAGE_HEIGHT, IMAGE_WIDTH, 1];
224 const images = new Float32Array(tf.util.sizeFromShape(imagesShape));
225 const labels = new Int32Array(tf.util.sizeFromShape([size, 1]));
226
227 let imageOffset = 0;
228 let labelOffset = 0;
229 for (let i = 0; i < size; ++i) {
230 images.set(this.dataset[imagesIndex][i], imageOffset);
231 labels.set(this.dataset[labelsIndex][i], labelOffset);
232 imageOffset += IMAGE_FLAT_SIZE;
233 labelOffset += 1;
234 }
235
236 return {
237 images: tf.tensor4d(images, imagesShape),
238 labels: tf.oneHot(tf.tensor1d(labels, 'int32'), LABEL_FLAT_SIZE).toFloat()
239 };
240 }
241}
242
243export class FashionMnistDataset extends MnistDataset {

Callers 2

getTrainDataMethod · 0.95
getTestDataMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected