-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.js
68 lines (57 loc) · 1.69 KB
/
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
const tf = require('@tensorflow/tfjs-node');
const fs = require('fs');
const path = require('path');
const TRAIN_IMAGES_DIR = './data/train';
const TEST_IMAGES_DIR = './data/test';
function loadImages(dataDir) {
const images = [];
const labels = [];
var folders = fs.readdirSync(dataDir)
folders.map((folder, index) => {
var files = fs.readdirSync(path.join(dataDir,folder));
for (let i = 0; i < files.length; i++) {
var filePath = path.join(dataDir, folder, files[i]);
var buffer = fs.readFileSync(filePath);
var imageTensor = tf.node.decodeImage(buffer)
.resizeNearestNeighbor([96,96])
.mean(2)
.toFloat()
.div(tf.scalar(255.0))
.expandDims()
.expandDims(-1);
images.push(imageTensor);
labels.push(index);
}
});
console.log('Labels are');
console.log(labels);
return [images, labels];
}
/** Helper class to handle loading training and test data. */
class DogDataset {
constructor() {
this.trainData = [];
this.testData = [];
}
/** Loads training and test data. */
loadData() {
console.log('Loading images...');
this.trainData = loadImages(TRAIN_IMAGES_DIR);
this.testData = loadImages(TEST_IMAGES_DIR);
console.log('Images loaded successfully.')
}
getTrainData() {
return {
images: tf.concat(this.trainData[0]),
labels: tf.oneHot(tf.tensor1d(this.trainData[1], 'int32'), 5).toFloat() // here 5 is class
}
}
getTestData() {
return {
images: tf.concat(this.testData[0]),
labels: tf.oneHot(tf.tensor1d(this.testData[1], 'int32'), 5).toFloat()
}
}
}
module.exports = new DogDataset();
console.log('All done.')