const PIXELS = 28, PIXELSSQUARED = PIXELS * PIXELS, NOTRAIN = 124800, NOTEST = 20800, noinput = PIXELSSQUARED, nohidden = 64, nooutput = 10, learningrate = 0.1;
let do_training = false;
const TRAINPERSTEP = 12, TESTPERSTEP = 2, ZOOMFACTOR = 10, ZOOMPIXELS = 8 * PIXELS, canvaswidth = PIXELS + ZOOMPIXELS + 150, canvasheight = 3 * ZOOMPIXELS + 200, DOODLE_THICK = 15, DOODLE_BLUR = 1, redspan = "<span style='font-weight:bold; font-size:x-large; color:red'> ";;
let mnist, cnn, cnn_mod, cnn_train, doodle, demo, DOODLE_TOTAL_GUESS = 1, DOODLE_TOTAL_WRONG = 0, trainrun = 1, train_index = 0, testrun = 1, test_index = 0, total_tests = 0, total_correct = 0, doodle_exists = false, demo_exists = false, mousedrag = false, alphabets = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"];
var train_inputs, test_inputs, demo_inputs, doodle_inputs, thehtml;
AB.headerCSS({"max-height": "95vh"}), thehtml = "Draw your doodle in top LHS. <button onclick='wipeDoodle();' class='normbutton' >Clear</button> <button class='normbutton' onclick='guessDoodle();'>Guess</button> <br>", AB.msg(thehtml, 1), thehtml = "<hr> <h1> 2. Training </h1> Middle row: Training image magnified (left) and original (right). <br> <button onclick='do_training = !do_training;' class='normbutton' >start or end train</button> <br> ", AB.msg(thehtml, 3), thehtml = "<h3> Hidden tests </h3> ", AB.msg(thehtml, 5), thehtml = "<hr> <h1> 3. Demo </h1> Bottom row: Test image magnified (left) and original (right). <br> The network is <i>not</i> trained on any of these images. <br> <button onclick='makeDemo();' class='normbutton' >Demo test image</button> <br> ", AB.msg(thehtml, 7);
function setup() {
createCanvas(canvaswidth, canvasheight), (doodle = createGraphics(ZOOMPIXELS, ZOOMPIXELS)).pixelDensity(1), AB.loadingScreen(), $.getScript("/uploads/codingtrain/matrix.js", function () {
$.getScript("/uploads/chinya07/convnet-min.js", function () { //dataset uploaded by user chinmay
$.getScript("/uploads/chinya07/mymnist.js", function () { //dataset uploaded by user chinmay
console.log("All JS loaded");
let t = [];
t.push({type: "input", out_sx: 28, out_sy: 28, out_depth: 1}), t.push({type: "conv", sx: 5, filters: 8, stride: 1, pad: 2, activation: "relu"}), t.push({type: "pool", sx: 2, stride: 2}), t.push({type: "conv", sx: 5, filters: 16, stride: 1, pad: 2, activation: "relu"}), t.push({type: "pool", sx: 3, stride: 3}), t.push({type: "softmax", num_classes: 27}), cnn_mod = new convnetjs.Net, AB.restoreData(function (t) {
if (console.log(t), void 0 !== t) {
cnn_mod.fromJSON(t.cnn), DOODLE_TOTAL_GUESS = t.doodle_total_guess, DOODLE_TOTAL_WRONG = t.doodle_total_wrong;
let e = "Doodle score:" + ((DOODLE_TOTAL_GUESS - DOODLE_TOTAL_WRONG) / DOODLE_TOTAL_GUESS * 100).toFixed(2);
AB.msg(e, 2);
}
}), cnn_mod.makeLayers(t), cnn_train = new convnetjs.SGDTrainer(cnn_mod, {method: "adadelta", batch_size: 32, l2_decay: 0.001}), loadData();
});
});
});
}
function loadData() {
loadMNIST(function (t) {
mnist = t;
for (var e = 0; e < NOTRAIN; e++) imageRotate(mnist.train_images[e]);
for (e = 0; e < NOTEST; e++) imageRotate(mnist.test_images[e]);
console.log("All data loaded into mnist object:"), AB.removeLoading();
});
}
function imageRotate(t) {
for (let e = 0; e < PIXELS; e++) for (let n = e; n < PIXELS; n++) {
let o = e * PIXELS + n, s = n * PIXELS + e, a = t[o];
t[o] = t[s], t[s] = a;
}
}
function getImage(t) {
let e = createImage(PIXELS, PIXELS);
e.loadPixels();
for (let n = 0; n < PIXELSSQUARED; n++) {
let o = t[n], s = 4 * n;
e.pixels[s + 0] = o, e.pixels[s + 1] = o, e.pixels[s + 2] = o, e.pixels[s + 3] = 255;
}
return e.updatePixels(), e;
}
function getInputs(t) {
let e = [];
for (let n = 0; n < PIXELSSQUARED; n++) {
let o = t[n];
e[n] = o / 255;
}
return e;
}
function trainData(t) {
let e = mnist.train_images[train_index], n = mnist.train_labels[train_index];
if (t) {
var o = getImage(e);
image(o, 0, ZOOMPIXELS + 50, ZOOMPIXELS, ZOOMPIXELS), image(o, ZOOMPIXELS + 50, ZOOMPIXELS + 50, PIXELS, PIXELS);
}
let s = getInputs(e);
train_inputs = s;
{
let t = getCnnInputs(s);
cnn_train.train(t, n);
}
thehtml = " trainrun: " + trainrun + "<br> no: " + train_index, AB.msg(thehtml, 4), ++train_index == NOTRAIN && (train_index = 0, dataSavedOnAB(), console.log("finished trainrun: " + trainrun), trainrun++);
}
function getCnnInputs(t) {
for (var e = new convnetjs.Vol(PIXELS, PIXELS, 1, 0), n = 0; n < PIXELSSQUARED; n++) e.w[n] = t[n];
return e;
}
function testData() {
let t = mnist.test_images[test_index], e = mnist.test_labels[test_index], n = getInputs(t), o = getCnnInputs(n);
test_inputs = n;
let s = findMax(cnn_mod.forward(o).w);
var a = getImage(t);
image(a, 0, ZOOMPIXELS + 50, ZOOMPIXELS, ZOOMPIXELS), image(a, ZOOMPIXELS + 50, ZOOMPIXELS + 50, PIXELS, PIXELS), total_tests++, s == e && total_correct++;
let i = total_correct / total_tests * 100;
thehtml = " testrun: " + testrun + "<br> no: " + total_tests + " <br> correct: " + total_correct + "<br> score: " + redspan + i.toFixed(2) + "</span>", AB.msg(thehtml, 6), ++test_index == NOTEST && (console.log("finished testrun: " + testrun + " score: " + i.toFixed(2)), testrun++, test_index = 0, total_tests = 0, total_correct = 0);
}
function find12(t) {
let e = 0, n = 0, o = 0, s = 0;
for (let a = 0; a < t.length; a++) t[a] > o ? (n = e, s = o, e = a, o = t[a]) : t[a] > s && (n = a, s = t[a]);
return [e, n];
}
function findMax(t) {
let e = 0, n = 0;
for (let o = 0; o < t.length; o++) t[o] > n && (e = o, n = t[o]);
return e;
}
function draw() {
if (void 0 !== mnist) {
if (background("black"), do_training) {
for (let t = 0; t < TRAINPERSTEP; t++) trainData(false);
for (let t = 0; t < TESTPERSTEP; t++) testData();
}
if (demo_exists && (drawDemo(), guessDemo()), doodle_exists && drawDoodle(), mouseIsPressed) {
var t = ZOOMPIXELS + 20;
mouseX < t && mouseY < t && pmouseX < t && pmouseY < t && (mousedrag = true, doodle_exists = true, doodle.stroke("white"), doodle.strokeWeight(DOODLE_THICK), doodle.line(mouseX, mouseY, pmouseX, pmouseY));
} else mousedrag && (mousedrag = false, doodle.filter(BLUR, DOODLE_BLUR));
}
}
function makeDemo() {
demo_exists = true;
var t = AB.randomIntAtoB(0, NOTEST - 1);
demo = mnist.test_images[t];
var e = mnist.test_labels[t];
thehtml = "Test image no: " + t + "<br>Classification: " + letters[e - 1] + "<br>", AB.msg(thehtml, 8);
}
function drawDemo() {
var t = getImage(demo);
image(t, 0, canvasheight - ZOOMPIXELS, ZOOMPIXELS, ZOOMPIXELS), image(t, ZOOMPIXELS + 50, canvasheight - ZOOMPIXELS, PIXELS, PIXELS);
}
function guessDemo() {
let t = getInputs(demo);
demo_inputs = t;
let e = getCnnInputs(t), n = findMax(cnn_mod.forward(e).w);
thehtml = " We classify it as: " + redspan + letters[n - 1] + "</span>", AB.msg(thehtml, 9);
}
function dataSavedOnAB() {
let t = {};
t.doodle_total_guess = DOODLE_TOTAL_GUESS, t.doodle_total_wrong = DOODLE_TOTAL_WRONG, t.cnn = cnn_mod.toJSON(), AB.saveData(t);
}
function drawDoodle() {
let t = doodle.get();
image(t, 0, 0, ZOOMPIXELS, ZOOMPIXELS), image(t, ZOOMPIXELS + 50, 0, PIXELS, PIXELS);
}
function guessWrong() {
let t = "Doodle score:" + ((DOODLE_TOTAL_GUESS - ++DOODLE_TOTAL_WRONG) / DOODLE_TOTAL_GUESS * 100).toFixed(2);
AB.msg(t, 2), dataSavedOnAB();
}
function guessDoodle() {
let t = doodle.get();
DOODLE_TOTAL_GUESS++, t.resize(PIXELS, PIXELS), t.loadPixels();
let e = [];
for (let n = 0; n < PIXELSSQUARED; n++) e[n] = t.pixels[4 * n] / 255;
doodle_inputs = e;
let n = getCnnInputs(e), o = findMax(cnn_mod.forward(n).w);
thehtml = "Doodle score:" + ((DOODLE_TOTAL_GUESS - DOODLE_TOTAL_WRONG) / DOODLE_TOTAL_GUESS * 100).toFixed(2) + "<br>We classify it as: " + redspan + letters[o - 1] + "</span><button class='normbutton' onclick='guessWrong()'>wrong</button>", AB.msg(thehtml, 2), dataSavedOnAB();
}
function wipeDoodle() {
doodle_exists = false, doodle.background("black");
let t = "Doodle score:" + ((DOODLE_TOTAL_GUESS - DOODLE_TOTAL_WRONG) / DOODLE_TOTAL_GUESS * 100).toFixed(2);
AB.msg(t, 2);
}
function showInputs(t) {
var e = "";
for (let n = 0; n < t.length; n++) n % PIXELS == 0 && (e += "\n"), e = e + " " + t[n].toFixed(2);
console.log(e);
}