-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.js
119 lines (108 loc) · 3.07 KB
/
dataset.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
'use strict';
const fs = require('fs');
const es = require('event-stream');
const zlib = require('zlib');
const path = require('path');
const https = require('https');
const progress = require('progress');
const numjs = require('numjs');
const SOURCE_URL = 'https://storage.googleapis.com/cvdf-datasets/mnist/';
const TRAIN_IMAGES = 'train-images-idx3-ubyte';
const TRAIN_LABELS = 'train-labels-idx1-ubyte';
const TEST_IMAGES = 't10k-images-idx3-ubyte';
const TEST_LABELS = 't10k-labels-idx1-ubyte';
function _download(filename, callback) {
const local = path.join(__dirname, './data', filename);
const exists = fs.existsSync(local);
if (!exists) {
const sourceUrl = SOURCE_URL + filename + '.gz';
const dest = fs.createWriteStream(local);
https.get(sourceUrl).on('response', (res) => {
const bufs = [];
const len = parseInt(res.headers['content-length'], 10);
const bar = new progress(`${filename}: [:bar] :rate/bps :percent :etas`, {
complete: '=',
incomplete: ' ',
width: 20,
total: len,
});
const barCount = es.map((chunk, callback) => {
bar.tick(chunk.length);
callback(null, chunk);
});
const chunkCollector = es.map((chunk, callback) => {
bufs.push(chunk);
callback(null, chunk);
});
res.pipe(barCount)
.pipe(zlib.createUnzip())
.pipe(dest)
.on('finish', () => {
callback(null, Buffer.concat(bufs));
});
});
} else {
fs.readFile(local, callback);
}
}
function download(filename) {
return new Promise((resolve, reject) => {
_download(filename, (err, data) => {
err ? reject(err) : resolve(data);
});
});
}
function _extract(buffer, block) {
let offset = 0;
function _read32() {
const r = buffer.readUInt32BE(offset);
offset += 4;
return r;
}
function _read(len) {
const ab = buffer.buffer.slice(offset, offset + len);
const r = Buffer.from(ab);
offset += len;
return r;
}
return block(_read32, _read);
}
function extractImages(buffer) {
return _extract(buffer, (read32, read) => {
if (read32() !== 2051)
throw new Error('Invalid magic number in image file');
const count = read32();
const rows = read32();
const cols = read32();
const data = read(count * rows * cols);
console.log(
`images: count=${count} shape=${rows}x${cols} length=${data.byteLength}`
);
return data;
});
}
function extractLabels(buffer) {
return _extract(buffer, (read32, read) => {
if (read32() !== 2049)
throw new Error('Invalid magic number in label file');
const count = read32();
const data = read(count);
console.log(
`labels: count=${count} length=${data.byteLength}`
);
return data;
});
}
exports.load = function() {
return Promise.all([
download(TRAIN_IMAGES),
download(TRAIN_LABELS),
download(TEST_IMAGES),
download(TEST_LABELS)
]).then((buffers) => {
return buffers.map((data, index) => {
return index % 2 === 0 ?
extractImages(data) : extractLabels(data);
});
});
};