(baseURL, destDir, filename)
| 126 | } |
| 127 | |
| 128 | async function loadLabels(baseURL, destDir, filename) { |
| 129 | const buffer = |
| 130 | await fetchOnceAndSaveToDiskWithBuffer(baseURL, destDir, filename); |
| 131 | |
| 132 | const headerBytes = LABEL_HEADER_BYTES; |
| 133 | const recordBytes = LABEL_RECORD_BYTE; |
| 134 | |
| 135 | const headerValues = loadHeaderValues(buffer, headerBytes); |
| 136 | tf.util.assert( |
| 137 | headerValues[0] === LABEL_HEADER_MAGIC_NUM, |
| 138 | () => `Label file header doesn't match expected magic num.`); |
| 139 | |
| 140 | const labels = []; |
| 141 | let index = headerBytes; |
| 142 | while (index < buffer.byteLength) { |
| 143 | const array = new Int32Array(recordBytes); |
| 144 | for (let i = 0; i < recordBytes; i++) { |
| 145 | array[i] = buffer.readUInt8(index++); |
| 146 | } |
| 147 | labels.push(array); |
| 148 | } |
| 149 | |
| 150 | tf.util.assert( |
| 151 | labels.length === headerValues[1], |
| 152 | () => `Actual labels length (${images.length} doesn't match ` + |
| 153 | `value in header (${headerValues[1]})`); |
| 154 | return labels; |
| 155 | } |
| 156 | |
| 157 | /** Helper class to handle loading training and test data. */ |
| 158 | export class MnistDataset { |
no test coverage detected