-
Notifications
You must be signed in to change notification settings - Fork 11
/
train.js
166 lines (134 loc) · 5.5 KB
/
train.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
const lineReader = require('line-reader');
var fs = require('fs');
const tf = require('@tensorflow/tfjs-node');
let justFeatures = [];
let justLabels = [];
const gestureClasses = ['expelliarmus', 'lumos'];
let numClasses = gestureClasses.length;
let numSamplesPerGesture = 21;
let totalNumDataFiles = numSamplesPerGesture * numClasses;
let numPointsOfData = 6;
let numLinesPerFile = 50;
let totalNumDataPerFile = numPointsOfData * numLinesPerFile;
function readFile(file) {
let allFileData = [];
return new Promise((resolve, reject) => {
fs.readFile(`data/hp/${file}`, "utf8", (err, data) => {
if (err){
reject(err);
} else{
lineReader.eachLine(`data/hp/${file}`, function(line) {
let dataArray = line.split(" ").map(arrayItem => parseFloat(arrayItem));
allFileData.push(...dataArray);
let concatArray = [...allFileData];
if(concatArray.length === totalNumDataPerFile){
let label = file.split("_")[1];
let labelIndex = gestureClasses.indexOf(label)
resolve({features: concatArray, label: labelIndex })
}
});
}
});
});
}
const readDir = () =>
new Promise((resolve, reject) => fs.readdir(`data/hp/`, "utf8", (err, data) => err ? reject(err) : resolve(data)));
(async () => {
const filenames = await readDir();
let allData = [];
filenames.map(async file => { // 75 times
let originalContent = await readFile(file);
allData.push(originalContent);
if(allData.length === totalNumDataFiles){
format(allData)
}
})
})();
const format = (allData) => {
// sort all data by label to get [{label: 0, features: ...}, {label: 1, features: ...}];
let sortedData = allData.sort((a, b) => (a.label > b.label) ? 1 : -1);
sortedData.map(item => {
createMultidimentionalArrays(justLabels, item.label, item.label);
createMultidimentionalArrays(justFeatures, item.label, item.features);
})
const [trainingFeatures, trainingLabels, testingFeatures, testingLabels] = transformToTensor(justFeatures, justLabels);
createModel(trainingFeatures, trainingLabels, testingFeatures, testingLabels);
}
function createMultidimentionalArrays(dataArray, index, item){
!dataArray[index] && dataArray.push([]);
dataArray[index].push(item);
}
const transformToTensor = (features, labels) => {
return tf.tidy(() => {
const xTrains = [];
const yTrains = [];
const xTests = [];
const yTests = [];
for (let i = 0; i < gestureClasses.length; ++i) {
const [xTrain, yTrain, xTest, yTest] = convertToTensors(features[i], labels[i], 0.20);
xTrains.push(xTrain);
yTrains.push(yTrain);
xTests.push(xTest);
yTests.push(yTest);
}
const concatAxis = 0;
return [
tf.concat(xTrains, concatAxis), tf.concat(yTrains, concatAxis),
tf.concat(xTests, concatAxis), tf.concat(yTests, concatAxis)
];
})
}
function convertToTensors(featuresData, labelData, testSplit) {
if (featuresData.length !== labelData.length) {
throw new Error('features set and labels set have different numbers of examples');
}
const [shuffledFeatures, shuffledLabels] = shuffleData(featuresData, labelData);
const featuresTensor = tf.tensor2d(shuffledFeatures, [numSamplesPerGesture, totalNumDataPerFile]);
// Create a 1D `tf.Tensor` to hold the labels, and convert the number label
// from the set {0, 1, 2} into one-hot encoding (.e.g., 0 --> [1, 0, 0]).
const labelsTensor = tf.oneHot(tf.tensor1d(shuffledLabels).toInt(), numClasses);
return split(featuresTensor, labelsTensor, testSplit);
}
const shuffleData = (features, labels) => {
const indices = [...Array(numSamplesPerGesture).keys()];
tf.util.shuffle(indices);
const shuffledFeatures = [];
const shuffledLabels = [];
features.map((featuresArray, index) => {
shuffledFeatures.push(features[indices[index]])
shuffledLabels.push(labels[indices[index]]);
})
return [shuffledFeatures, shuffledLabels];
}
const split = (featuresTensor, labelsTensor, testSplit) => {
// Split the data into a training set and a test set, based on `testSplit`.
const numTestExamples = Math.round(numSamplesPerGesture * testSplit);
const numTrainExamples = numSamplesPerGesture - numTestExamples;
const trainingFeatures = featuresTensor.slice([0, 0], [numTrainExamples, totalNumDataPerFile]);
const testingFeatures = featuresTensor.slice([numTrainExamples, 0], [numTestExamples, totalNumDataPerFile]);
const trainingLabels = labelsTensor.slice([0, 0], [numTrainExamples, numClasses]);
// was [0,0] and still worked....
// const testingLabels = labelsTensor.slice([0, 0], [numTestExamples, numClasses]);
const testingLabels = labelsTensor.slice([numTrainExamples, 0], [numTestExamples, numClasses]);
return [trainingFeatures, trainingLabels, testingFeatures, testingLabels];
}
const createModel = async(xTrain, yTrain, xTest, yTest) => {
const params = {learningRate: 0.1, epochs: 40};
// Define the topology of the model: two dense layers.
const model = tf.sequential();
model.add(tf.layers.dense({units: 10, activation: 'sigmoid', inputShape: [xTrain.shape[1]]}));
model.add(tf.layers.dense({units: numClasses, activation: 'softmax'}));
model.summary();
const optimizer = tf.train.adam(params.learningRate);
model.compile({
optimizer: optimizer,
loss: 'categoricalCrossentropy',
metrics: ['accuracy'],
});
await model.fit(xTrain, yTrain, {
epochs: params.epochs,
validationData: [xTest, yTest],
});
await model.save('file://model');
return model;
}