|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2018 Google LLC. All Rights Reserved. |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * ============================================================================= |
| 16 | + */ |
| 17 | + |
| 18 | +import * as tf from '@tensorflow/tfjs'; |
| 19 | + |
| 20 | +const IMAGE_SIZE = 784; |
| 21 | +const NUM_CLASSES = 10; |
| 22 | +const NUM_DATASET_ELEMENTS = 65000; |
| 23 | + |
| 24 | +const TRAIN_TEST_RATIO = 5 / 6; |
| 25 | + |
| 26 | +const NUM_TRAIN_ELEMENTS = Math.floor(TRAIN_TEST_RATIO * NUM_DATASET_ELEMENTS); |
| 27 | +const NUM_TEST_ELEMENTS = NUM_DATASET_ELEMENTS - NUM_TRAIN_ELEMENTS; |
| 28 | + |
| 29 | +const MNIST_IMAGES_SPRITE_PATH = |
| 30 | + 'https://storage.googleapis.com/learnjs-data/model-builder/mnist_images.png'; |
| 31 | +const MNIST_LABELS_PATH = |
| 32 | + 'https://storage.googleapis.com/learnjs-data/model-builder/mnist_labels_uint8'; |
| 33 | + |
| 34 | +/** |
| 35 | + * A class that fetches the sprited MNIST dataset and returns shuffled batches. |
| 36 | + * |
| 37 | + * NOTE: This will get much easier. For now, we do data fetching and |
| 38 | + * manipulation manually. |
| 39 | + */ |
| 40 | +export class MnistData { |
| 41 | + constructor() { |
| 42 | + this.shuffledTrainIndex = 0; |
| 43 | + this.shuffledTestIndex = 0; |
| 44 | + } |
| 45 | + |
| 46 | + async load() { |
| 47 | + // Make a request for the MNIST sprited image. |
| 48 | + const img = new Image(); |
| 49 | + const canvas = document.createElement('canvas'); |
| 50 | + const ctx = canvas.getContext('2d'); |
| 51 | + const imgRequest = new Promise((resolve, reject) => { |
| 52 | + img.crossOrigin = ''; |
| 53 | + img.onload = () => { |
| 54 | + img.width = img.naturalWidth; |
| 55 | + img.height = img.naturalHeight; |
| 56 | + |
| 57 | + const datasetBytesBuffer = |
| 58 | + new ArrayBuffer(NUM_DATASET_ELEMENTS * IMAGE_SIZE * 4); |
| 59 | + |
| 60 | + const chunkSize = 5000; |
| 61 | + canvas.width = img.width; |
| 62 | + canvas.height = chunkSize; |
| 63 | + |
| 64 | + for (let i = 0; i < NUM_DATASET_ELEMENTS / chunkSize; i++) { |
| 65 | + const datasetBytesView = new Float32Array( |
| 66 | + datasetBytesBuffer, i * IMAGE_SIZE * chunkSize * 4, |
| 67 | + IMAGE_SIZE * chunkSize); |
| 68 | + ctx.drawImage( |
| 69 | + img, 0, i * chunkSize, img.width, chunkSize, 0, 0, img.width, |
| 70 | + chunkSize); |
| 71 | + |
| 72 | + const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height); |
| 73 | + |
| 74 | + for (let j = 0; j < imageData.data.length / 4; j++) { |
| 75 | + // All channels hold an equal value since the image is grayscale, so |
| 76 | + // just read the red channel. |
| 77 | + datasetBytesView[j] = imageData.data[j * 4] / 255; |
| 78 | + } |
| 79 | + } |
| 80 | + this.datasetImages = new Float32Array(datasetBytesBuffer); |
| 81 | + |
| 82 | + resolve(); |
| 83 | + }; |
| 84 | + img.src = MNIST_IMAGES_SPRITE_PATH; |
| 85 | + }); |
| 86 | + |
| 87 | + const labelsRequest = fetch(MNIST_LABELS_PATH, {mode: 'arraybuffer'}); |
| 88 | + const [imgResponse, labelsResponse] = |
| 89 | + await Promise.all([imgRequest, labelsRequest]); |
| 90 | + |
| 91 | + this.datasetLabels = new Uint8Array(await labelsResponse.arrayBuffer()); |
| 92 | + |
| 93 | + // Create shuffled indices into the train/test set for when we select a |
| 94 | + // random dataset element for training / validation. |
| 95 | + this.trainIndices = tf.util.createShuffledIndices(NUM_TRAIN_ELEMENTS); |
| 96 | + this.testIndices = tf.util.createShuffledIndices(NUM_TEST_ELEMENTS); |
| 97 | + |
| 98 | + // Slice the the images and labels into train and test sets. |
| 99 | + this.trainImages = |
| 100 | + this.datasetImages.slice(0, IMAGE_SIZE * NUM_TRAIN_ELEMENTS); |
| 101 | + this.testImages = this.datasetImages.slice(IMAGE_SIZE * NUM_TRAIN_ELEMENTS); |
| 102 | + this.trainLabels = |
| 103 | + this.datasetLabels.slice(0, NUM_CLASSES * NUM_TRAIN_ELEMENTS); |
| 104 | + this.testLabels = |
| 105 | + this.datasetLabels.slice(NUM_CLASSES * NUM_TRAIN_ELEMENTS); |
| 106 | + } |
| 107 | + |
| 108 | + nextTrainBatch(batchSize) { |
| 109 | + return this.nextBatch( |
| 110 | + batchSize, [this.trainImages, this.trainLabels], () => { |
| 111 | + this.shuffledTrainIndex = |
| 112 | + (this.shuffledTrainIndex + 1) % this.trainIndices.length; |
| 113 | + return this.trainIndices[this.shuffledTrainIndex]; |
| 114 | + }); |
| 115 | + } |
| 116 | + |
| 117 | + nextTestBatch(batchSize) { |
| 118 | + return this.nextBatch(batchSize, [this.testImages, this.testLabels], () => { |
| 119 | + this.shuffledTestIndex = |
| 120 | + (this.shuffledTestIndex + 1) % this.testIndices.length; |
| 121 | + return this.testIndices[this.shuffledTestIndex]; |
| 122 | + }); |
| 123 | + } |
| 124 | + |
| 125 | + nextBatch(batchSize, data, index) { |
| 126 | + const batchImagesArray = new Float32Array(batchSize * IMAGE_SIZE); |
| 127 | + const batchLabelsArray = new Uint8Array(batchSize * NUM_CLASSES); |
| 128 | + |
| 129 | + for (let i = 0; i < batchSize; i++) { |
| 130 | + const idx = index(); |
| 131 | + |
| 132 | + const image = |
| 133 | + data[0].slice(idx * IMAGE_SIZE, idx * IMAGE_SIZE + IMAGE_SIZE); |
| 134 | + batchImagesArray.set(image, i * IMAGE_SIZE); |
| 135 | + |
| 136 | + const label = |
| 137 | + data[1].slice(idx * NUM_CLASSES, idx * NUM_CLASSES + NUM_CLASSES); |
| 138 | + batchLabelsArray.set(label, i * NUM_CLASSES); |
| 139 | + } |
| 140 | + |
| 141 | + const xs = tf.tensor2d(batchImagesArray, [batchSize, IMAGE_SIZE]); |
| 142 | + const labels = tf.tensor2d(batchLabelsArray, [batchSize, NUM_CLASSES]); |
| 143 | + |
| 144 | + return {xs, labels}; |
| 145 | + } |
| 146 | +} |
0 commit comments