forked from tensorflow/tfjs-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
web-data.js
210 lines (183 loc) · 6.74 KB
/
web-data.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/**
* @license
* Copyright 2018 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
// TODO(cais): Once MNIST dataset is available from the tf.data.* API, use
// that instead and remove this file.
import * as tf from '@tensorflow/tfjs';
export const IMAGE_H = 28;
export const IMAGE_W = 28;
const IMAGE_SIZE = IMAGE_H * IMAGE_W;
const NUM_CLASSES = 10;
const NUM_DATASET_ELEMENTS = 65000;
const NUM_TRAIN_ELEMENTS = 55000;
const NUM_TEST_ELEMENTS = NUM_DATASET_ELEMENTS - NUM_TRAIN_ELEMENTS;
const MNIST_IMAGES_SPRITE_PATH =
'https://storage.googleapis.com/learnjs-data/model-builder/mnist_images.png';
const MNIST_LABELS_PATH =
'https://storage.googleapis.com/learnjs-data/model-builder/mnist_labels_uint8';
/**
* A class that fetches the sprited MNIST dataset and provide data as
* tf.Tensors.
*/
export class MnistData {
constructor() {}
async load() {
// Make a request for the MNIST sprited image.
const img = new Image();
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
const imgRequest = new Promise((resolve, reject) => {
img.crossOrigin = '';
img.onload = () => {
img.width = img.naturalWidth;
img.height = img.naturalHeight;
const datasetBytesBuffer =
new ArrayBuffer(NUM_DATASET_ELEMENTS * IMAGE_SIZE * 4);
const chunkSize = 5000;
canvas.width = img.width;
canvas.height = chunkSize;
for (let i = 0; i < NUM_DATASET_ELEMENTS / chunkSize; i++) {
const datasetBytesView = new Float32Array(
datasetBytesBuffer, i * IMAGE_SIZE * chunkSize * 4,
IMAGE_SIZE * chunkSize);
ctx.drawImage(
img, 0, i * chunkSize, img.width, chunkSize, 0, 0, img.width,
chunkSize);
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
for (let j = 0; j < imageData.data.length / 4; j++) {
// All channels hold an equal value since the image is grayscale, so
// just read the red channel.
datasetBytesView[j] = imageData.data[j * 4] / 255;
}
}
this.datasetImages = new Float32Array(datasetBytesBuffer);
resolve();
};
img.src = MNIST_IMAGES_SPRITE_PATH;
});
const labelsRequest = fetch(MNIST_LABELS_PATH);
const [imgResponse, labelsResponse] =
await Promise.all([imgRequest, labelsRequest]);
this.datasetLabels = new Uint8Array(await labelsResponse.arrayBuffer());
// Slice the the images and labels into train and test sets.
this.trainImages =
this.datasetImages.slice(0, IMAGE_SIZE * NUM_TRAIN_ELEMENTS);
this.testImages = this.datasetImages.slice(IMAGE_SIZE * NUM_TRAIN_ELEMENTS);
this.trainLabels =
this.datasetLabels.slice(0, NUM_CLASSES * NUM_TRAIN_ELEMENTS);
this.testLabels =
this.datasetLabels.slice(NUM_CLASSES * NUM_TRAIN_ELEMENTS);
}
/**
* Get all training data as a data tensor and a labels tensor.
*
* @returns
* xs: The data tensor, of shape `[numTrainExamples, 28, 28, 1]`.
* labels: The one-hot encoded labels tensor, of shape
* `[numTrainExamples, 10]`.
*/
getTrainData() {
const xs = tf.tensor4d(
this.trainImages,
[this.trainImages.length / IMAGE_SIZE, IMAGE_H, IMAGE_W, 1]);
const labels = tf.tensor2d(
this.trainLabels, [this.trainLabels.length / NUM_CLASSES, NUM_CLASSES]);
return {xs, labels};
}
/**
* Get all test data as a data tensor a a labels tensor.
*
* @param {number} numExamples Optional number of examples to get. If not
* provided,
* all test examples will be returned.
* @returns
* xs: The data tensor, of shape `[numTestExamples, 28, 28, 1]`.
* labels: The one-hot encoded labels tensor, of shape
* `[numTestExamples, 10]`.
*/
getTestData(numExamples) {
let xs = tf.tensor4d(
this.testImages,
[this.testImages.length / IMAGE_SIZE, IMAGE_H, IMAGE_W, 1]);
let labels = tf.tensor2d(
this.testLabels, [this.testLabels.length / NUM_CLASSES, NUM_CLASSES]);
if (numExamples != null) {
xs = xs.slice([0, 0, 0, 0], [numExamples, IMAGE_H, IMAGE_W, 1]);
labels = labels.slice([0, 0], [numExamples, NUM_CLASSES]);
}
return {xs, labels};
}
}
let mnistImages;
let mnistLabels;
let mnistNumExamples;
let mnistIndices;
/** Load MNIST data. */
export async function loadMnistData() {
const mnistData = new MnistData();
await mnistData.load();
const mnistSamples = mnistData.getTrainData();
mnistImages = mnistSamples.xs;
mnistLabels = await mnistSamples.labels.argMax(-1).data();
mnistNumExamples = mnistLabels.length;
mnistIndices = [];
for (let i = 0; i < mnistNumExamples; ++i) {
mnistIndices.push(i);
}
}
/**
* Sample a number of examples from each class of the MNIST dataset.
*
* @param {number} numExamplesPerClass Number of examples per class.
* @returns {tf.Tensor} A 4D tensor of shape
* [numExamplesPerClass * 10, 28, 28, 1].
*/
export function sampleFromMnistData(numExamplesPerClass) {
tf.util.assert(
numExamplesPerClass <= mnistNumExamples / 10,
`Requested too many examples per class ` +
`(${numExamplesPerClass} > ${mnistNumExamples / 10})`);
tf.util.shuffle(mnistIndices);
const indicesByClass = [];
for (let i = 0; i < NUM_CLASSES; ++i) {
indicesByClass.push([]);
}
for (let i = 0; i < mnistIndices.length; ++i) {
if (indicesByClass[mnistLabels[mnistIndices[i]]].length >=
numExamplesPerClass) {
continue;
}
indicesByClass[mnistLabels[mnistIndices[i]]].push(mnistIndices[i]);
let minLength = Infinity;
indicesByClass.forEach(indicesArray => {
if (indicesArray.length < minLength) {
minLength = indicesArray.length;
}
});
if (minLength >= numExamplesPerClass) {
break;
}
}
return tf.tidy(() => {
let rowsToCombine = [];
indicesByClass.forEach(classIndices => {
const classImages = tf.gather(mnistImages, classIndices);
const rowOfExamples = tf.concat(classImages.unstack(), 0);
rowsToCombine.push(rowOfExamples);
});
return tf.concat(rowsToCombine, 1);
});
}