(isTrainingData)
| 148 | } |
| 149 | |
| 150 | getData_(isTrainingData) { |
| 151 | let imagesIndex; |
| 152 | let labelsIndex; |
| 153 | if (isTrainingData) { |
| 154 | imagesIndex = 0; |
| 155 | labelsIndex = 1; |
| 156 | } else { |
| 157 | imagesIndex = 2; |
| 158 | labelsIndex = 3; |
| 159 | } |
| 160 | const size = this.dataset[imagesIndex].length; |
| 161 | tf.util.assert( |
| 162 | this.dataset[labelsIndex].length === size, |
| 163 | `Mismatch in the number of images (${size}) and ` + |
| 164 | `the number of labels (${this.dataset[labelsIndex].length})`); |
| 165 | |
| 166 | // Only create one big array to hold batch of images. |
| 167 | const imagesShape = [size, IMAGE_HEIGHT, IMAGE_WIDTH, 1]; |
| 168 | const images = new Float32Array(tf.util.sizeFromShape(imagesShape)); |
| 169 | const labels = new Int32Array(tf.util.sizeFromShape([size, 1])); |
| 170 | |
| 171 | let imageOffset = 0; |
| 172 | let labelOffset = 0; |
| 173 | for (let i = 0; i < size; ++i) { |
| 174 | images.set(this.dataset[imagesIndex][i], imageOffset); |
| 175 | labels.set(this.dataset[labelsIndex][i], labelOffset); |
| 176 | imageOffset += IMAGE_FLAT_SIZE; |
| 177 | labelOffset += 1; |
| 178 | } |
| 179 | |
| 180 | return { |
| 181 | images: tf.tensor4d(images, imagesShape), |
| 182 | labels: tf.oneHot(tf.tensor1d(labels, 'int32'), LABEL_FLAT_SIZE).toFloat() |
| 183 | }; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | module.exports = new MnistDataset(); |
no outgoing calls
no test coverage detected