-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcnn.js
More file actions
130 lines (115 loc) · 3.93 KB
/
Copy pathcnn.js
File metadata and controls
130 lines (115 loc) · 3.93 KB
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
let label = ''; // Contains the label of each class
let cnn; // Contains our model
let consoleOutput = document.getElementById('console'); // Displays information related to the model
let input = document.getElementById('video'); // Contains the video stream
let NbEpochs = 20; // Number of epochs
let class_1 = document.getElementById("class_1");
let class_2 = document.getElementById("class_2");
let class_3 = document.getElementById("class_3");
let train = document.getElementById("train");
let saveData = document.getElementById("saveData");
let loadData = document.getElementById("loadData");
let saveModel = document.getElementById("saveModel");
let loadModel = document.getElementById("loadModel");
// Configuration of our model
let options = {
task: 'imageClassification', // Regression | classification | imageClassification, determine the task of our model
debug: true, // Display the training interface
inputs: [input.width, input.height, 4], // Determine the shape of our input array, 4 for 'rgba' colors
layers: [{ // Neural network architecture
type: 'conv2d',
filters: 8,
kernelSize: 5,
strides: 1,
activation: 'relu',
kernelInitializer: 'varianceScaling'
},
{
type: 'maxPooling2d',
poolSize: [2, 2],
strides: [2, 2]
},
{
type: 'conv2d',
filters: 16,
kernelSize: 5,
strides: 1,
activation: 'relu',
kernelInitializer: 'varianceScaling'
},
{
type: 'maxPooling2d',
poolSize: [2, 2],
strides: [2, 2],
},
{
type: 'flatten'
},
{
type: 'dense',
kernelInitializer: 'varianceScaling',
activation: 'softmax'
}
]
};
cnn = ml5.neuralNetwork(options); // Assigning options to our model
// Callback function after training the model
function finishedTraining() {
consoleOutput.textContent = 'training complete';
classifyVideo();
}
// Starts the classification which takes video as input
function classifyVideo() {
let inputImage = {
image: input,
};
cnn.classify(inputImage, gotResults);
}
// Displays the results contained in the results object
function gotResults(error, results) {
if (error) {
return;
}
label = results[0].label;
consoleOutput.innerHTML = "";
for (let i = 0; i < results.length; i++) {
consoleOutput.insertAdjacentHTML('afterbegin', "<p>" + results[i].label + ": " + results[i].confidence * 100 + "</p><br>");
}
classifyVideo(); // We relaunch the classification
}
//We standardize the data and train our model
function training() {
cnn.normalizeData();
cnn.train({
epochs: NbEpochs,
},
finishedTraining
);
}
// We add an example with the assigned label
function addExample(labelValue) {
console.log("ok")
consoleOutput.textContent = 'Adding example: ' + labelValue;
cnn.addData({ image: input }, { label: labelValue });
}
train.addEventListener('click', () => { training() })
class_1.addEventListener('click', () => { addExample('class_1') });
class_2.addEventListener('click', () => { addExample('class_2') });
class_3.addEventListener('click', () => { addExample('class_3') });
saveData.addEventListener('click', () => { cnn.saveData() }); // Saving data
saveModel.addEventListener('click', () => { cnn.save() }); // Saving the model
// Loading data
loadData.addEventListener('click', () => {
cnn.loadData('data/data.json', () => {
console.log("data loaded");
});
})
// Loading the model
loadModel.addEventListener('click', () => {
const modelDetails = {
model: 'model/model.json',
metadata: 'model/model_meta.json',
weights: 'model/model.weights.bin'
}
cnn.load(modelDetails, () => { classifyVideo() }); // We launch the classification
})