-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
157 lines (127 loc) · 4.17 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
import 'bootstrap/dist/css/bootstrap.css';
import * as tf from '@tensorflow/tfjs';
import {MnistData} from './data';
import { deflateRaw } from 'zlib';
var model;
function createLogEntry(entry) {
document.getElementById('log').innerHTML += '<br>' + entry;
}
function createModel() {
createLogEntry('Create model ...');
model = tf.sequential();
createLogEntry('Model created');
createLogEntry('Add layers ...');
model.add(tf.layers.conv2d({
inputShape: [28, 28, 1],
kernelSize: 5,
filters: 8,
strides: 1,
activation: 'relu',
kernelInitializer: 'VarianceScaling'
}));
model.add(tf.layers.maxPooling2d({
poolSize: [2,2],
strides: [2,2]
}));
model.add(tf.layers.conv2d({
kernelSize: 5,
filters: 16,
strides: 1,
activation: 'relu',
kernelInitializer: 'VarianceScaling'
}));
model.add(tf.layers.maxPooling2d({
poolSize: [2,2],
strides: [2,2]
}));
model.add(tf.layers.flatten());
model.add(tf.layers.dense({
units: 10,
kernelInitializer: 'VarianceScaling',
activation: 'softmax'
}));
createLogEntry('Layers created');
createLogEntry('Start compiling ...');
model.compile({
optimizer: tf.train.sgd(0.15),
loss: 'categoricalCrossentropy'
});
createLogEntry('Compiled');
}
let data;
async function load() {
createLogEntry('Loading MNIST data ...');
data = new MnistData();
await data.load();
createLogEntry('Data loaded successfully');
}
const BATCH_SIZE = 64;
const TRAIN_BATCHES = 150;
async function train() {
createLogEntry('Start training ...');
for (let i = 0; i < TRAIN_BATCHES; i++) {
const batch = tf.tidy(() => {
const batch = data.nextTrainBatch(BATCH_SIZE);
batch.xs = batch.xs.reshape([BATCH_SIZE, 28, 28, 1]);
return batch;
});
await model.fit(
batch.xs, batch.labels, {batchSize: BATCH_SIZE, epochs: 1}
);
tf.dispose(batch);
await tf.nextFrame();
}
createLogEntry('Training complete');
}
async function main() {
createModel();
await load();
await train();
document.getElementById('selectTestDataButton').disabled = false;
document.getElementById('selectTestDataButton').innerText = "Ramdomly Select Test Data And Predict";
}
async function predict(batch) {
tf.tidy(() => {
const input_value = Array.from(batch.labels.argMax(1).dataSync());
const div = document.createElement('div');
div.className = 'prediction-div';
const output = model.predict(batch.xs.reshape([-1, 28, 28, 1]));
const prediction_value = Array.from(output.argMax(1).dataSync());
const image = batch.xs.slice([0, 0], [1, batch.xs.shape[1]]);
const canvas = document.createElement('canvas');
canvas.className = 'prediction-canvas';
draw(image.flatten(), canvas);
const label = document.createElement('div');
label.innerHTML = 'Original Value: ' + input_value;
label.innerHTML += '<br>Prediction Value: ' + prediction_value;
if (prediction_value - input_value == 0) {
label.innerHTML += '<br>Value recognized successfully';
} else {
label.innerHTML += '<br>Recognition failed!'
}
div.appendChild(canvas);
div.appendChild(label);
document.getElementById('predictionResult').appendChild(div);
});
}
function draw(image, canvas) {
const [width, height] = [28, 28];
canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext('2d');
const imageData = new ImageData(width, height);
const data = image.dataSync();
for (let i = 0; i < height * width; ++i) {
const j = i * 4;
imageData.data[j + 0] = data[i] * 255;
imageData.data[j + 1] = data[i] * 255;
imageData.data[j + 2] = data[i] * 255;
imageData.data[j + 3] = 255;
}
ctx.putImageData(imageData, 0, 0);
}
document.getElementById('selectTestDataButton').addEventListener('click', async (el,ev) => {
const batch = data.nextTestBatch(1);
await predict(batch);
});
main();