-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller_dataset.js
executable file
·47 lines (40 loc) · 1.75 KB
/
controller_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
import * as tf from '@tensorflow/tfjs';
/**
* A dataset for webcam controls which allows the user to add example Tensors
* for particular labels. This object will concat them into two large xs and ys.
*/
export class ControllerDataset {
constructor(numClasses) {
this.numClasses = numClasses;
}
/**
* Adds an example to the controller dataset.
* @param {Tensor} example A tensor representing the example. It can be an image,
* an activation, or any other type of Tensor.
* @param {number} label The label of the example. Should be a number.
*/
addExample(example, label) {
// One-hot encode the label.
const y = tf.tidy(() => tf.oneHot(tf.tensor1d([label]).toInt(), this.numClasses));
if (this.xs == null) {
// For the first example that gets added, keep example and y so that the
// ControllerDataset owns the memory of the inputs. This makes sure that
// if addExample() is called in a tf.tidy(), these Tensors will not get
// disposed.
this.xs = tf.keep(example);
this.ys = tf.keep(y);
} else {
// all other examples
const oldX = this.xs;
const oldY = this.ys;
// concatenate previous and new examples and keep for later
this.xs = tf.keep(oldX.concat(example, 0));
this.ys = tf.keep(oldY.concat(y, 0));
// specifcally call dispose instead of tidying
// lots of tensors mean lots of memory, js memory management isnt great which is why tensor flow opts to handle alot of gabbage collection explicitly
oldX.dispose();
oldY.dispose();
y.dispose();
}
}
}