-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
1214 lines (997 loc) · 32.6 KB
/
index.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// require("./abs");
const express = require("express");
const bodyParser = require("body-parser");
var cors = require("cors");
const {
writeToJSON,
appendToJSON,
readFromJSON,
prepData,
} = require("./utils.js");
// const writeToJSON = require("./utils.js").writeToJSON;
// const appendToJSON = require("./utils.js").appendToJSON;
// const readFromJSON = require("./utils.js").readFromJSON;
// const prepData = require("./utils.js").prepData;
const fs = require("fs-extra");
const csv1 = require("csv-parser");
const fft = require("fft-js").fft;
const fftUtil = require("fft-js").util;
// const tfvis = require("@tensorflow/tfjs-vis");
const Papa = require("papaparse");
const app = express();
const port = 3232;
// const server = require("http").createServer();
// const io = require("socket.io")(server);
// io.on("connection", (client) => {
// client.on("event", (data) => {
// /* … */
// });
// client.on("disconnect", () => {
// /* … */
// });
// });
// server.listen(6464);
app.use(bodyParser.json({ limit: "50mb" }));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors());
// create webhook to send training data to client
// socket.on("trainingData", (data) => {
// console.log(data);
// res.send(200, { data: data });
// });
app.get("/", cors(), (req, res) => {
// res.send("Hello World!");
//redirect to the client at localhost:3000
res.redirect("http://localhost:3000");
});
app.get("/test", (req, res) => {
console.log("Got body:", req.body);
writeToJSON(req.body);
res.sendStatus(200);
});
app.post("/test", (req, res) => {
console.log("Got body:", req.body);
writeToJSON(req.body);
res.sendStatus(200);
});
app.post("/api/eeg", (req, res) => {
// console.log("Got body:", req.body);
// appendToJSON(req.body);
res.sendStatus(200);
});
app.get("/api/trainNewAIModel", (req, res) => {
console.log("Got body:", req.body);
res.sendStatus(200);
});
app.get("/api/data.csv", (req, res) => {
console.log("Got body:", req.body);
// appendToJSON(req.body);
res.sendFile("./data.csv", { root: __dirname });
});
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});
//TODO = require tensorflow and firebase
const tf = require("@tensorflow/tfjs-node-gpu");
// import * as tf from "@tensorflow/tfjs-node-gpu";
var admin = require("firebase-admin");
console.log(tf.getBackend());
const logdir = "logs";
const summaryWriter = tf.node.summaryFileWriter(logdir);
// Fetch the service account key JSON file contents
var serviceAccount = require("./isistr-db-firebase-adminsdk-gzboz-fecbf1a908.json");
// const { tensor } = require("@tensorflow/tfjs-node");
// const {
// computeOutShape,
// } = require("@tensorflow/tfjs-core/dist/ops/segment_util.js");
// Initialize the app with a service account, granting admin privileges
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
// The database URL depends on the location of the database
databaseURL: "https://isistr-db-default-rtdb.firebaseio.com/",
});
// As an admin, the app has access to read and write all data, regardless of Security Rules
var db = admin.database();
var ref = db.ref("restricted_access/secret_document");
ref.once("value", function (snapshot) {
console.log(snapshot.val());
});
let resultsData;
app.get("/api/modelTrain", (req, res) => {
// console.log("Got body:", req.body);
resultsData = getDataFromDB();
res.send(200, { data: "Training started" });
});
app.get("/api/modelTrain_Audio", (req, res) => {
// console.log("Got body:", req.body);
resultsData = getDataFromDB("results_new/toneAI_begin");
res.send(200, { data: "Training started" });
});
app.post("/api/newClientDataPush", (req, res) => {
// console.log("Got body:", req.body);
// push data to firebase
pushDataToDB(req.body);
res.send(200, { data: "Data pushed to DB" });
});
app.post("/api/toneAIPredict", async (req, res) => {
console.log("Got body:", req.body);
// predict data
let preproccessedData = preprocessSample(req.body);
let prediction = await predict(preproccessedData);
console.log(prediction);
res.send(200, { data: prediction });
});
const devMode = true;
function log(data) {
if (devMode) {
console.log(data);
}
}
// const io = require("socket.io")(3000, {
// cors: {
// origin: "http://localhost:3000",
// methods: ["GET", "POST"],
// },
// });
// Load the trained model from disk
// const model = await tf.loadLayersModel("file://path/to/model.json");
// io.on("connection", (socket) => {
// console.log("Connected");
// socket.on("sampleData", (data) => {
// // Preprocess the sample data
// const sample = preprocessSample(data);
// // Make a prediction using the model
// const prediction = model.predict(sample);
// // Convert the prediction to a JSON object
// const predictionJSON = prediction.arraySync();
// // Send the prediction back to the client
// socket.emit("prediction", predictionJSON);
// });
// });
function preprocessSample(sampleData) {
// Convert the sample data to a tensor
const tensor = tf.tensor2d([sampleData], [1, 48]);
// Normalize the tensor
const normalizedTensor = tensor.div(2048);
return normalizedTensor;
}
// retry at EEG Model Training:
function beginEEGTraining() {
// get data from firebase
jsonData = getDataFromDB();
// Extract the samples and labels arrays from the json data
let samplesArray = [];
let labelsArray = [];
jsonData.forEach((data) => {
const key = Object.keys(data)[0];
samplesArray.push(data[key].eeg.samples);
labelsArray.push(data[key].promptName);
});
// Normalize the samples array
const samplesNormalized = samplesArray.map((samples) => {
const min = Math.min(...samples);
const max = Math.max(...samples);
return samples.map((val) => (val - min) / (max - min));
});
// One-hot encode the labels array
const labelsEncoded = labelsArray.map((label) => {
const labels = new Set(labelsArray);
const encoded = Array.from(labels).reduce((encoding, l) => {
encoding[l] = label === l ? 1 : 0;
return encoding;
}, {});
return encoded;
});
console.log(samplesNormalized);
console.log(labelsEncoded);
// Assume that samplesNormalized and labelsEncoded are the preprocessed data obtained from previous step
const testSplit = 0.2;
const samplesCount = samplesNormalized.length;
const testCount = Math.floor(samplesCount * testSplit);
const trainCount = samplesCount - testCount;
const samplesNormalizedShuffled = shuffle(samplesNormalized);
const labelsEncodedShuffled = shuffle(labelsEncoded);
const X_train = samplesNormalizedShuffled.slice(0, trainCount);
const y_train = labelsEncodedShuffled.slice(0, trainCount);
const X_test = samplesNormalizedShuffled.slice(trainCount);
const y_test = labelsEncodedShuffled.slice(trainCount);
console.log(X_train);
console.log(y_train);
console.log(X_test);
console.log(y_test);
function shuffle(array) {
let currentIndex = array.length,
temporaryValue,
randomIndex;
while (0 !== currentIndex) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
// Assume that X_train, y_train, X_test, and y_test are the preprocessed and split data
const model = tf.sequential();
model.add(
tf.layers.dense({
inputShape: [X_train[0].length],
units: 16,
activation: "relu",
})
);
model.add(
tf.layers.dense({
units: Object.keys(y_train[0]).length,
activation: "softmax",
})
);
model.compile({
optimizer: "adam",
loss: "categoricalCrossentropy",
metrics: ["accuracy"],
});
const batchSize = 32;
const epochs = 50;
async function trainModel() {
const history = await model.fit(
tf.tensor2d(X_train),
tf.tensor2d(y_train),
{ batchSize, epochs }
);
console.log(history.history.acc);
}
async function testModel() {
const result = model.evaluate(tf.tensor2d(X_test), tf.tensor2d(y_test));
console.log(result[1].dataSync());
}
trainModel();
testModel();
}
// Import TensorFlow.js
// const tf = require("@tensorflow/tfjs");
// require("@tensorflow/tfjs-node");
// require tfjs node gpu
require("@tensorflow/tfjs-node-gpu");
const path = require("path");
async function predict(newSample) {
console.log(path.join(__dirname, "toneAI_model_highEpoch"));
const modelSavePath = path.join(__dirname, "toneAI_model_highEpoch"); // Path to saved model
// Load the saved model
// const model = await tf.loadLayersModel(modelSavePath);
const model = await tf.loadLayersModel(
"file://./toneAI_model_highEpoch/model.json"
);
// Make a prediction on the new sample
const prediction = model.predict(newSample);
// Extract the index of the highest probability
// console.log(prediction);
const index = prediction.argMax(-1).dataSync()[0];
console.log(index);
let label;
// Get the label corresponding to the index
switch (index) {
case 0:
label = "airTone_440";
break;
case 1:
label = "earthTone_45";
break;
case 2:
label = "fireTone_880";
break;
case 3:
label = "waterTone_220";
break;
default:
label = -1;
break;
}
console.log(`Predicted label: ${label}, index: ${index}`);
return label;
}
app.post("/api/predictEEG", async (req, res) => {
// console.log("Got body:", req.body);
const prediction1 = await predict(req.body);
res.send(prediction1);
});
app.get("/api/trainEEG", (req, res) => {
// console.log("Got body:", req.body);
// appendToJSON(req.body);
beginEEGTraining();
res.sendStatus(200);
});
// train the model data from groupDataByElectrode
// {
// electrode,
// labelName: promptName,
// samples,
// }
// create a function that uses the best algorithm for training the model of eeg data
async function trainModel(data) {
const { formattedData, lengthOfDatasets } = formatData(data);
// Use sigmoid activation function for binary classification
let csv = Papa.unparse(formattedData, {
header: true,
complete: function (results) {
fs.writeFile("./data.csv", results, function (err) {
if (err) {
return console.log(err);
}
console.log("The file was saved!");
});
},
});
// Load the EEG data from the CSV file
let eegData = [];
let headers = [];
fs.createReadStream("./data.csv")
.pipe(csv1())
.on("headers", (row) => {
headers = row;
})
.on("data", (row) => {
eegData.push(row);
})
.on("end", () => {
// Convert the EEG data to the frequency domain using the FFT
const fftData = fft(eegData.map((row) => Object.values(row).map(Number)));
// Save the FFT data to a new CSV file
const fftDataString =
headers.join(",") +
"\n" +
fftData.map((row) => row.join(",")).join("\n");
fs.writeFileSync("./fft_data.csv", fftDataString);
});
// // save csv to local file
// fs.writeFile("./data.csv", csv, function (err) {
// if (err) {
// return console.log(err);
// }
// console.log("The file was saved!");
// });
// get all column names from the data
const columnNames = Object.keys(formattedData[0]);
// console.log(formattedData[0]);
// console.log(columnNames);
// get the label column name
const labelColumnName = "promptName";
// get the features column names
const featuresColumnNames = columnNames.filter(
(columnName) => columnName !== labelColumnName
);
console.log(featuresColumnNames.length, "featuresColumnNames");
// create a model
const model = tf.sequential();
// add a input layer with 10 neurons
model.add(
tf.layers.dense({
inputShape: [featuresColumnNames.length],
activation: "relu",
units: 36,
})
);
tf.layers.dense({
activation: "sigmoid",
units: 4,
});
// add a output layer with 1 neuron
model.add(
tf.layers.dense({
activation: "sigmoid",
units: 4,
})
);
model.add(
tf.layers.dense({
activation: "sigmoid",
units: 1,
})
);
// compile the model for eeg data training using categorical crossentropy loss
model.compile({
// loss: "absoluteDifference",
// loss: "categoricalCrossentropy",
// loss: "meanSquaredError",
// loss: "meanAbsoluteError",
// loss: "meanAbsolutePercentageError",
// loss: "meanSquaredLogarithmicError",
// loss: "squaredHinge",
// loss: "hinge",
// loss: "categoricalHinge",
loss: "logcosh",
// loss: "huberLoss",
// loss: "cosineProximity",
metrics: ["accuracy", "mse"],
// set an optimizer that is not tf.train.adam or sgd because they don't work with categorical crossentropy
// optimizer: tf.train.adagrad(0.6),
optimizer: tf.train.adam(0.6),
});
// convert the data to a form we can use for training
function convertToTensors() {
// Wrapping these calculations in a tidy will dispose any
// intermediate tensors.
return tf.tidy(() => {
// Step 1. Shuffle the data
tf.util.shuffle(formattedData);
// Step 2. Convert data to Tensor
const inputs = formattedData.map((d) =>
featuresColumnNames.map((name) => d[name])
);
console.log(inputs, "inputs");
const labels = formattedData.map((d) => d[labelColumnName]);
const inputTensor = tf.tensor2d(inputs, [
inputs.length,
inputs[0].length,
]);
const labelTensor = tf.tensor2d(labels, [labels.length, 1]);
// Step 3. Normalize the data to the range 0 - 1 using min-max scaling
const inputMax = inputTensor.max();
const inputMin = inputTensor.min();
const labelMax = labelTensor.max();
const labelMin = labelTensor.min();
const normalizedInputs = inputTensor
.sub(inputMin)
.div(inputMax.sub(inputMin));
const normalizedLabels = labelTensor
.sub(labelMin)
.div(labelMax.sub(labelMin));
return {
inputs: normalizedInputs,
labels: normalizedLabels,
// Return the min/max bounds so we can use them later.
inputMax,
inputMin,
labelMax,
labelMin,
};
});
}
// convert the data to a form we can use for training
const { inputs, labels } = convertToTensors(formattedData);
// train the model
await model.fit(inputs, labels, {
batchSize: 32,
epochs: 1000,
// callbacks: tf.node.tensorBoard("/tmp/tfjs_logs"),
});
// evaluate the model
const evalOutput = model.evaluate(inputs, labels);
console.log(`Accuracy: ${(evalOutput[1].dataSync()[0] * 100).toFixed(1)}%`);
// make some predictions using the model and compare them to the
// labels
const preds = model.predict(inputs).dataSync();
labels.dataSync().forEach((val, i) => {
console.log(`Label: ${val}, Prediction: ${preds[i]}`);
});
// save the model
await model.save("file://model");
}
async function trainAudioModel_Dynamic(data, identifier) {
console.log(data, "data");
console.log(identifier, "identifier");
let resultsArray = [];
Object.keys(data).forEach((key) => {
for (let i = 0; i < data[key].length; i++) {
resultsArray.push(data[key][i]);
}
});
const numClasses = 4;
if (!Array.isArray(resultsArray)) {
console.error("Results is not an array.");
}
function applyFFT(inputTensor) {
const fftOutput = [];
for (let i = 0; i < 12; i++) {
const signal = inputTensor.slice([i], [1]);
const signalLength = signal.shape[0];
const signalLengthTensor = tf.scalar(signalLength);
// Apply FFT to the signal
const signalFFT = tf.spectral.rfft(signal);
// Compute the power spectrum of the signal
const powerSpectrum = tf.abs(signalFFT).square().div(signalLengthTensor);
// Convert power spectrum to a regular array
const powerSpectrumData = powerSpectrum.dataSync();
// Store the power spectrum for this signal in the output array
fftOutput.push(powerSpectrumData);
// Dispose of the tensors we created
signal.dispose();
}
return fftOutput;
}
const newResultsArray = resultsArray.map(([result, label], index) => {
const tempArray = [];
// NEED TO FLATTEN FFT data array prior to passing to the model to train on
// let result = [];
// for (var electrode in arr) {
// console.log(arr[electrode], "electrode");
// for (var entry in electrode) {
// console.log(electrode[entry], "entry");
// }
// }
// loop the result array and split and create 4 new arrays of 12 each
// resultsArray.length
if (index < resultsArray.length) {
for (let j = 0; j < result.length; j += 12) {
const newResult = tf.tensor1d(result.slice(j, j + 12), "float32");
const fftResult = applyFFT(newResult);
console.log("Electrode processed");
tempArray.push(fftResult);
}
}
return [tempArray, label];
});
pushDataToDB(newResultsArray, "/fftData/");
log("Finished processing data.");
const newData = newResultsArray.map(([tempArray, label]) => {
const arr = [];
for (let i = 0; i < tempArray.length; i++) {
const subArr = tempArray[i].map((item) => [...item]);
while (subArr.length < 12) {
subArr.push(new Array(12).fill(0));
}
arr.push(subArr);
}
return [arr, label];
});
const dataset = tf.data.array(newData);
dataset.forEachAsync((element) => {
console.log(element.shape); // output: [3, 4]
return Promise.resolve(); // required to avoid warning message
}, this);
console.log(newData[1], "newData");
// Shuffle the data and split into training, validation, and test sets
const numExamples = newResultsArray.length;
const numTrainExamples = Math.floor(numExamples * 0.7);
const numValExamples = Math.floor(numExamples * 0.15);
const numTestExamples = numExamples - numTrainExamples - numValExamples;
const batchSize = 32;
// // Reshape input dataset to match the input shape of the model
// const reshapedData = tf.tensor4d(dataset, [numExamples, 4, 12, 12]);
const trainDataset = dataset.take(numTrainExamples).batch(batchSize);
const valDataset = dataset
.skip(numTrainExamples)
.take(numValExamples)
.batch(batchSize);
const testDataset = dataset
.skip(numTrainExamples + numValExamples)
.batch(batchSize);
const model = tf.sequential({
layers: [
tf.layers.dense({
units: 128,
activation: "relu",
inputShape: [4, 12, 1],
}),
tf.layers.flatten(),
tf.layers.dense({ units: 64, activation: "relu" }),
tf.layers.dense({ units: numClasses, activation: "softmax" }),
],
});
model.compile({
optimizer: "adam",
loss: "categoricalCrossentropy",
metrics: ["accuracy"],
});
// Set up TensorBoard callback
const tensorBoardCallback = tf.node.tensorBoard(logdir, {
updateFreq: "epoch",
histogramFreq: 1,
});
// Train the model on the reshaped data
await model.fit(trainDataset, {
epochs: 10,
validationData: valDataset,
callbacks: [tensorBoardCallback],
});
// Evaluate the model on the test dataset
const evalOutput = model.evaluate(testDataset);
// Log the evaluation accuracy
console.log(`Test Accuracy: ${(await evalOutput[1].data())[0]}`);
// Train the model on the reshaped data
// trainDataset
// await model.fit(trainDataset, {
// epochs: 25,
// validationData: valDataset,
// callbacks: tfvis.show.fitCallbacks(
// { name: "Training Performance" },
// ["loss", "val_loss", "acc", "val_acc"],
// { callbacks: ["onEpochEnd"] }
// ),
// });
// Save the model
await model.save("file://./model");
}
async function trainAudioMo_del_Dynamic(data, identifier) {
console.log(data, "data");
console.log(identifier, "identifier");
let resultsArray = [];
Object.keys(data).forEach((key) => {
for (let i = 0; i < data[key].length; i++) {
resultsArray.push(data[key][i]);
}
});
// console.log(resultsArray);
const numClasses = 4;
if (!Array.isArray(resultsArray)) {
console.error("Results is not an array.");
}
function applyFFT(inputTensor) {
// console.log("inputArray", inputTensor, inputTensor.shape);
const fftOutput = [];
for (let i = 0; i < 12; i++) {
const signal = inputTensor.slice([i], [1]);
const signalLength = signal.shape[0];
const signalLengthTensor = tf.scalar(signalLength);
// Apply FFT to the signal
const signalFFT = tf.spectral.rfft(signal);
// Compute the power spectrum of the signal
const powerSpectrum = tf.abs(signalFFT).square().div(signalLengthTensor);
// Convert power spectrum to a regular array
const powerSpectrumData = powerSpectrum.dataSync();
// Store the power spectrum for this signal in the output array
fftOutput.push(powerSpectrumData);
// Dispose of the tensors we created
signal.dispose();
}
// log("finished, 301");
return fftOutput;
}
const newResultsArray = [];
for (let i = 0; i < resultsArray.length; i++) {
const result = resultsArray[i][0];
const label = resultsArray[i][1];
const tempArray = [];
// loop the result array and split and create 4 new arrays of 12 each
for (let j = 0; j < result.length; j += 12) {
// const newResult = result.slice(j, j + 12);
const newResult = tf.tensor1d(result.slice(j, j + 12), "float32");
const fftResult = applyFFT(newResult);
// console.log(fftResult, "fftResult");
tempArray.push(fftResult);
}
// log("finished, 620");
log([tempArray, label], "tempArray");
newResultsArray.push([tempArray, label]);
}
console.log(newResultsArray, "newResultsArray");
const dataset = tf.data.array(newResultsArray);
// Shuffle the data and split into training, validation, and test sets
const model = tf.sequential({
layers: [
tf.layers.dense({ units: 128, activation: "relu", inputShape: [12, 4] }),
tf.layers.flatten(),
tf.layers.dense({ units: 64, activation: "relu" }),
tf.layers.dense({ units: 3, activation: "softmax" }),
],
});
model.compile({
optimizer: "adam",
loss: "categoricalCrossentropy",
metrics: ["accuracy"],
});
// Train the model on the training set
const epochs = 10;
const batchSize = 32;
const trainBatch = trainDataset.batch(batchSize);
const valBatch = valDataset.batch(batchSize);
const testBatch = testDataset.batch(batchSize);
await model.fit(
dataset.shuffle(inputData[0].length).batch(batchSize).take(numTrainSamples),
{
epochs: numEpochs,
validationData: dataset
.skip(numTrainSamples)
.batch(batchSize)
.take(numValSamples),
}
);
console.log("Finished training the model");
console.log(history);
console.log("Evaluating model on test data...");
const result = model.evaluate(testBatch);
console.log(`Test loss: ${result[0]}, Test accuracy: ${result[1]}`);
// Train the model on the training set
// await model.fit(trainDataset.batch(batchSize), {
// epochs,
// validationData: valDataset.batch(batchSize),
// });
// const [testLoss, testAcc] = model.evaluate(testDataset.batch(batchSize));
// console.log(
// `Test loss: ${testLoss.toFixed(4)}, Test accuracy: ${testAcc.toFixed(4)}`
// );
// const [testLoss, testAcc] = model.evaluate(testDataset.batch(batchSize));
// console.log(
// `Test loss: ${testLoss.toFixed(4)}, Test accuracy: ${testAcc.toFixed(4)}`
// );
const modelSavePath = "file://toneAI_model_500Epoch"; // Path to save the model
// await model.save(modelSavePath);
console.log("Model saved successfully!");
// Use the model to make predictions
// const predictions = model.predict(x);
// Convert the predictions to labels
// const predictedLabels = Array.from(tf.argMax(predictions, 1).dataSync());
}
// backup:
async function trainAudioModel(data) {
console.warn(
"trainAudioModel is deprecated. Use trainAudioModel_Dnyamic instead."
);
console.log(data, "data");
const resultsArray = prepData(data);
console.log(resultsArray);
const numClasses = 4;
if (!Array.isArray(resultsArray)) {
console.error("Results is not an array.");
}
// // Define the model architecture
const model = tf.sequential();
model.add(
tf.layers.dense({ inputShape: [48], units: 64, activation: "relu" })
);
model.add(tf.layers.dense({ units: 64, activation: "relu" }));
model.add(tf.layers.dense({ units: numClasses, activation: "softmax" }));
// Compile the model
model.compile({
optimizer: "adam",
loss: "categoricalCrossentropy",
metrics: ["accuracy"],
});
// Overly complex model below
// Define the model architecture
// const model = tf.sequential();
// model.add(
// tf.layers.dense({
// inputShape: [48],
// units: 128,
// activation: "relu",
// kernel_regularizer: tf.regularizers.l2({ l2: 0.005 }),
// })
// );
// model.add(tf.layers.dropout({ rate: 0.5 }));
// model.add(
// tf.layers.dense({
// units: 64,
// activation: "relu",
// kernel_regularizer: tf.regularizers.l2({ l2: 0.05 }),
// })
// );
// model.add(tf.layers.dropout({ rate: 0.5 }));
// model.add(
// tf.layers.dense({
// units: numClasses,
// activation: "softmax",
// })
// );
// // Compile the model
// model.compile({
// optimizer: "adam",
// loss: "categoricalCrossentropy",
// metrics: ["accuracy"],
// });
const features = [];
const labels = [];
// shuffle the data
let shuffled = resultsArray.sort(() => 0.5 - Math.random());
// Prepare the data
for (let i = 0; i < resultsArray.length; i++) {
features.push(resultsArray[i][0]);
labels.push(resultsArray[i][1]);
}
const fLength = features.length;
console.log(features, "features");
console.log(labels, "labels");
const x = tf.tensor2d(features, [fLength, 48]);
const y = tf.oneHot(tf.tensor1d(labels, "int32"), numClasses);
const history = await model.fit(x, y, { epochs: 500 });
const modelSavePath = "file://toneAI_model_rework"; // Path to save the model
await model.save(modelSavePath);
console.log("Model saved successfully!");
// Use the model to make predictions
const predictions = model.predict(x);
// Convert the predictions to labels
const predictedLabels = Array.from(tf.argMax(predictions, 1).dataSync());
}
function formatData(data) {
let dataArr = [];
let groupObj = {};
// for each test Object.keys(data).length)
for (let i = 0; i < Object.keys(data).length - 1; i++) {
console.log(Object.keys(data)[3]);
// for each index of the test
for (const property in data[Object.keys(data)[i]]) {
let promptName =
data[Object.keys(data)[i]][property][
Object.keys(data[Object.keys(data)[i]][property])[0]
].promptName;
const { electrode, index, samples } =
data[Object.keys(data)[i]][property][
Object.keys(data[Object.keys(data)[i]][property])[0]
].eeg;
// flatten the data
let feature = {};
// feature.electrode = electrode;
// feature.index = index;
samples.forEach((sample, i) => {
feature["sample" + electrode + i] = samples[i];
if (groupObj[index] == undefined) {
groupObj[index] = {};
}