Code viewer for World:
Not
const PIXELS = 28, PIXELSSQUARED = PIXELS * PIXELS, NOTRAIN = 6e4, NOTEST = 1e4, 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"], noinput = PIXELSSQUARED, nohidden = 64, nooutput = 10, learningrate = 0.1;
let do_training = true;
const TRAINPERSTEP = 30, TESTPERSTEP = 5, ZOOMFACTOR = 7, ZOOMPIXELS = 7 * PIXELS, canvaswidth = PIXELS + ZOOMPIXELS + 50, canvasheight = 3 * ZOOMPIXELS + 100, DOODLE_THICK = 18, DOODLE_BLUR = 3;
let mnist, nn, mycnnTrain, mycnnModel, doodle, demo, trainrun = 1, train_index = 0, testrun = 1, test_index = 0, total_tests = 0, total_correct = 0, doodle_exists = false, demo_exists = false, mousedrag = false;
var train_inputs, test_inputs, demo_inputs, doodle_inputs, thehtml;
AB.headerCSS({"max-height": "95vh"}), thehtml = "<hr> <h1> 1. Doodle </h1> Top row: Doodle (left) and shrunk (right). <br> Draw your doodle in top LHS. <button onclick='wipeDoodle();' class='normbutton' >Clear doodle</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 = false;' class='normbutton' >Stop training</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);
const greenspan = "<span style='font-weight:bold; font-size:x-large; color:darkgreen'> ";
function setup() {
createCanvas(canvaswidth, canvasheight), (doodle = createGraphics(ZOOMPIXELS, ZOOMPIXELS)).pixelDensity(1), wipeDoodle(), AB.loadingScreen(), $.getScript("/uploads/codingtrain/matrix.js", function () {
$.getScript("/uploads/jagtaps2/convnet-min.js", function () {
console.log("All JS loaded");
var t = [];
t.push({type: "input", out_sx: 24, out_sy: 24, 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: 26}), (mycnnModel = new convnetjs.Net).makeLayers(t), nn = new convnetjs.SGDTrainer(mycnnModel, {method: "adadelta", momentum: 0.9, batch_size: 20, l2_decay: 0.001}), loadData();
});
});
}
function loadData() {
loadMNIST(function (t) {
mnist = t, console.log("All data loaded into mnist object:");
for (var e = 0; e < NOTRAIN; e++) rotateImageBy90(mnist.train_images[e]);
for (e = 0; e < NOTEST; e++) rotateImageBy90(mnist.test_images[e]);
AB.removeLoading();
});
}
function rotateImageBy90(t) {
for (var e = 0; e < PIXELS; e++) for (var n = e; n < PIXELS; n++) {
var o = e * PIXELS + n, s = n * PIXELS + e, i = t[o];
t[o] = t[s], t[s] = i;
}
}
function getmycnnInputs(t) {
for (var e = new convnetjs.Vol(28, 28, 1, 0), n = 0; n < PIXELSSQUARED; n++) e.w[n] = t[n];
return e;
}
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 trainit(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 = getmycnnInputs(s), nn.train(train_inputs, n), thehtml = " trainrun: " + trainrun + "<br> no: " + train_index, AB.msg(thehtml, 4), ++train_index == NOTRAIN && (train_index = 0, console.log("finished trainrun: " + trainrun), trainrun++);
}
function testit() {
let t = mnist.test_images[test_index], e = mnist.test_labels[test_index], n = getInputs(t);
test_inputs = getmycnnInputs(n);
let o = findMax(mycnnModel.forward(test_inputs).w);
total_tests++, o == e && total_correct++;
let s = total_correct / total_tests * 100;
thehtml = " testrun: " + testrun + "<br> no: " + total_tests + " <br> correct: " + total_correct + "<br> score: " + greenspan + s.toFixed(2) + "</span>", AB.msg(thehtml, 6), ++test_index == NOTEST && (console.log("finished testrun: " + testrun + " score: " + s.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 i = 0; i < t.length; i++) t[i] >= o ? (n = e, s = o, e = i, o = t[i]) : t[i] >= s && (n = i, s = t[i]);
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"), strokeWeight(1), stroke("yellow"), rect(0, 0, ZOOMPIXELS, ZOOMPIXELS), textSize(10), textAlign(CENTER), text("You can draw DOODLE here", ZOOMPIXELS / 2, ZOOMPIXELS / 2), do_training) {
for (let t = 0; t < TRAINPERSTEP; t++) trainit(0 == t);
for (let t = 0; t < TESTPERSTEP; t++) testit();
}
if (demo_exists && (drawDemo(), guessDemo()), doodle_exists && (drawDoodle(), guessDoodle()), 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: " + ALPHABETS[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 = findMax(mycnnModel.forward(demo_inputs).w);
thehtml = " We classify it as: " + greenspan + ALPHABETS[e - 1] + "</span>", AB.msg(thehtml, 9);
}
function drawDoodle() {
let t = doodle.get();
image(t, 0, 0, ZOOMPIXELS, ZOOMPIXELS), image(t, ZOOMPIXELS + 50, 0, PIXELS, PIXELS);
}
function guessDoodle() {
let t = doodle.get();
t.resize(PIXELS, PIXELS), t.loadPixels();
let e = [];
for (let n = 0; n < PIXELSSQUARED; n++) e[n] = t.pixels[4 * n] / 255;
doodle_inputs = getmycnnInputs(e);
let n = find12(mycnnModel.forward(doodle_inputs).w);
thehtml = " We classify it as: " + greenspan + ALPHABETS[n[0] - 1] + "</span> <br> No.2 guess is: " + greenspan + ALPHABETS[n[1] - 1] + "</span>", AB.msg(thehtml, 2);
}
function wipeDoodle() {
doodle_exists = false, doodle.background("black");
}
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);
}
function loadMNIST(t) {
let e = {}, n = {train_images: "/uploads/dheera0704/emnist-letters-train-images-idx3-ubyte.bin", train_labels: "/uploads/jagtaps2/emnist-letters-train-labels-idx1-ubyte.bin", test_images: "/uploads/jagtaps2/emnist-letters-test-images-idx3-ubyte.bin", test_labels: "/uploads/jagtaps2/emnist-letters-test-labels-idx1-ubyte.bin"};
return Promise.all(Object.keys(n).map(async t => {
e[t] = await loadFile(n[t]);
})).then(() => t(e));
}
async function loadFile(t) {
let e, n, o = await fetch(t).then(t => t.arrayBuffer()), s = 4, i = new DataView(o, 0, 4 * s), a = new Array(s).fill().map((t, e) => i.getUint32(4 * e, false));
if (2049 == a[0]) e = "label", n = 1, s = 2; else {
if (2051 != a[0]) throw new Error("Unknown file type " + a[0]);
e = "image", n = a[2] * a[3];
}
let r = new Uint8Array(o, 4 * s);
if ("image" == e) {
dataArr = [];
for (let t = 0; t < a[1]; t++) dataArr.push(r.subarray(n * t, n * (t + 1)));
return dataArr;
}
return r;
}