-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
96 lines (85 loc) · 2.63 KB
/
sketch.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
// Initial Credits:
// The Coding Train / Daniel Shiffman
// https://thecodingtrain.com/CodingChallenges/158-shape-classifier.html
// https://youtu.be/3MqJzMvHE3E
let circles = [];
let squares = [];
let triangles = [];
let pentagons = [];
let hexagons = [];
let lenses = [];
// Currently available: 100 | 750 | 500
let samples = "100";
// Currently available: 25 | 50
let percentage = "50";
let numClasses = 6;
let epochs = 35;
function preload() {
let filename = `data${samples}_warp${percentage}percent`;
for (let i = 0; i < (parseInt(samples)); i++) {
let index = nf(i + 1, 4, 0);
circles[i] = loadImage(`data/${filename}/circle${index}.png`);
squares[i] = loadImage(`data/${filename}/square${index}.png`);
triangles[i] = loadImage(`data/${filename}/triangle${index}.png`);
pentagons[i] = loadImage(`data/${filename}/pentagon${index}.png`);
hexagons[i] = loadImage(`data/${filename}/hexagon${index}.png`);
lenses[i] = loadImage(`data/${filename}/lens${index}.png`);
}
}
let shapeClassifier;
function setup() {
createCanvas(400, 400);
//background(0);
//image(squares[0], 0, 0, width, height);
let options = {
inputs: [64, 64, 4],
task: 'imageClassification',
debug: true,
layers: [
{ // Add convolutional layers
type: 'conv2d',
filters: 16,
kernelSize: 3,
activation: 'relu'
},
{
type: 'conv2d',
filters: 32,
kernelSize: 3,
activation: 'relu'
},
{ // Add pooling layers
type: 'maxPooling2d',
poolSize: 2
},
{ // Flatten the output for fully connected layers
type: 'flatten'
},
{ // Add fully connected layers
type: 'dense',
units: 64,
activation: 'relu'
},
{ // Output layer with the number of classes
type: 'dense',
units: numClasses,
activation: 'softmax'
},
]
};
shapeClassifier = ml5.neuralNetwork(options);
for (let i = 0; i < circles.length; i++) {
shapeClassifier.addData({ image: circles[i] }, { label: 'circle' });
shapeClassifier.addData({ image: squares[i] }, { label: 'square' });
shapeClassifier.addData({ image: triangles[i] }, { label: 'triangle' });
shapeClassifier.addData({ image: pentagons[i] }, { label: 'pentagon' });
shapeClassifier.addData({ image: hexagons[i] }, { label: 'hexagon' });
shapeClassifier.addData({ image: lenses[i] }, { label: 'lens' });
}
shapeClassifier.normalizeData();
shapeClassifier.train({ epochs: epochs }, finishedTraining);
}
function finishedTraining() {
console.log('finished training!');
shapeClassifier.save();
}