-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.js
61 lines (51 loc) · 1.59 KB
/
model.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
// Desc: Create and train a model for classifying images of poses.
const tf = require('@tensorflow/tfjs-node');
const IMAGE_WIDTH = 224;
const IMAGE_HEIGHT = 224;
const NUM_CLASSES = 5;
const createModel = async () => {
const model = tf.sequential();
model.add(tf.layers.conv2d({
inputShape: [IMAGE_WIDTH, IMAGE_HEIGHT, 3],
kernelSize: [3,3],
padding: 'same',
filters: 32,
activation: 'relu'
}));
model.add(tf.layers.maxPooling2d({poolSize: 2, strides: 2})); // 2x2 pool size, 2x2 stride
model.add(tf.layers.dropout({rate: 0.25}));
model.add(tf.layers.conv2d({
kernelSize: [3,3],
padding: 'same',
filters: 64,
activation: 'relu'
}));
model.add(tf.layers.maxPooling2d({poolSize: 2, strides: 2}));
model.add(tf.layers.dropout({rate: 0.25}));
model.add(tf.layers.conv2d({
kernelSize: [3,3],
padding: 'same',
filters: 128,
activation: 'relu'
}));
model.add(tf.layers.maxPooling2d({poolSize: 2, strides: 2}));
model.add(tf.layers.dropout({rate: 0.25}));
model.add(tf.layers.flatten({}));
model.add(tf.layers.dense({units: 512, activation: 'relu'}));
model.add(tf.layers.dropout({rate: 0.25}));
model.add(tf.layers.dense({
units: NUM_CLASSES,
activation: 'softmax'
}));
const learningRate = 0.001;
const optimizer = tf.train.adam(learningRate);
model.compile({
optimizer,
loss: 'categoricalCrossentropy',
metrics: ['accuracy']
});
return model;
};
module.exports = {
createModel
};