-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimpleMobileNetScript.html
117 lines (89 loc) · 3.25 KB
/
simpleMobileNetScript.html
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
<html>
<head>
<!-- Load TensorFlow.js -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]"> </script>
<!-- Place your code in the script tag below. You can also use an external .js file -->
<script>
// Notice there is no 'import' statement. 'tf' is available on the index-page
// because of the script tag above.
// Define a model for linear regression.
//console.log(model)
const LEARNING_RATE = 0.15;
const optimizer = tf.train.sgd(LEARNING_RATE);
// How many examples the model should "see" before making a parameter update.
const BATCH_SIZE = 64;
// How many batches to train the model for.
const TRAIN_BATCHES = 100;
// Every TEST_ITERATION_FREQUENCY batches, test accuracy over TEST_BATCH_SIZE examples.
// Ideally, we'd compute accuracy over the whole test set, but for performance
// reasons we'll use a subset.
const TEST_BATCH_SIZE = 1000;
const TEST_ITERATION_FREQUENCY = 5;
async function loadNetwork(num_classes) {
//get Prelaoded model of MobileNet
const preLoadedmodel = await tf.loadModel("https://raw.githubusercontent.com/cytoai/models/master/MobileNetJS/model.json");
//get some intermediate layer
const layer = preLoadedmodel.getLayer('conv_pw_8_relu');
tmpModel = tf.model({inputs: preLoadedmodel.inputs, outputs: layer.output});
for(i = 0; i<tmpModel.layers.length; i++){
tmpModel.layers[i].trainable = false;
}
//create denselayer for class prediction
const dense = tf.layers.dense({
units: num_classes,
kernelInitializer: 'VarianceScaling',
activation: 'softmax'
});
//apply the layers
const output = dense.apply(tmpModel.outputs)
//create the model
const model = tf.model({inputs: preLoadedmodel.inputs, outputs: output});
//compile the model
model.compile({
optimizer: optimizer,
loss: 'categoricalCrossentropy',
metrics: ['accuracy'],
});
return model;
}
async function train(model, imagewidth, imageheight){
for (let i = 0; i < TRAIN_BATCHES; i++) {
// TODO: Change function for getting trainign batch
const batch = data.nextTrainBatch(BATCH_SIZE);
let testBatch;
let validationData;
// Every few batches test the accuracy of the mode.
if (i % TEST_ITERATION_FREQUENCY === 0) {
//TODO: get a new function to get validation batch
testBatch = data.nextTestBatch(TEST_BATCH_SIZE);
validationData = [
testBatch.xs.reshape([TEST_BATCH_SIZE, imagewidth, imageheight, 1]), testBatch.labels
];
}
// The entire dataset doesn't fit into memory so we call fit repeatedly
// with batches.
const history = await model.fit(
batch.xs.reshape([BATCH_SIZE, imagewidth, imageheight, 1]),
batch.labels,
{
batchSize: BATCH_SIZE,
validationData,
epochs: 1
});
const loss = history.history.loss[0];
const accuracy = history.history.acc[0];
// ... plotting code ...
}
}
async function run() {
const model = await loadNetwork(10);
console.log(model);
//await train(model,28,28);
}
// Initialize the application.
run();
</script>
</head>
<body>
</body>
</html>