Code viewer for World: Assignment 2 : Character R...

//Name: Raj Vibhute
// Cloned by Raj Vibhute on 2 Dec 2022 from World "CharRecognition_UsingCNN (clone by Dheera)" by "Dheera" 
// Please leave this clone trail here.



// --- defined by MNIST - do not change these ---------------------------------------
const PIXELS = 28, // images in data set are tiny 
    PIXELSSQUARED = PIXELS * PIXELS,


    // number of training and test exemplars in the data set:
    NOTRAIN = 124800,
    NOTEST = 20800,



    //--- can modify all these --------------------------------------------------

    // no of nodes in network 
    noinput = PIXELSSQUARED,
    nohidden = 64,
    nooutput = 10,

    learningrate = 0.1; // default 0.1


// should we train every timestep or not
let do_training = true;


// how many to train and test per timestep 
const TRAINPERSTEP = 15,
    TESTPERSTEP = 5,

    // multiply it by this to magnify for display 
    ZOOMFACTOR = 7,
    ZOOMPIXELS = 7 * PIXELS,


    // 3 rows of
    // large image + 50 gap + small image    
    // 50 gap between rows 
    canvaswidth = PIXELS + ZOOMPIXELS + 100,
    canvasheight = 3 * ZOOMPIXELS + 60,



    DOODLE_THICK = 18, // thickness of doodle lines
    DOODLE_BLUR = 3; // blur factor applied to doodles



let mnist, mycnn, mycnnTrain, mycnnModel, doodle, demo,
    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"],


    trainrun = 1,
    train_index = 0,


    testrun = 1,
    test_index = 0,
    total_tests = 0,
    total_correct = 0,


    // images in LHS:
    doodle_exists = false,
    demo_exists = false,

    mousedrag = false // are we in the middle of a mouse drag drawing?


// save inputs to global var to inspect
// type these names in console 
var train_inputs, test_inputs, demo_inputs, doodle_inputs, thehtml;


// Matrix.randomize() is changed to point to this. Must be defined by user of Matrix. 

function randomWeight() {
    return (AB.randomFloatAtoB(-0.5, 0.5));
    // Coding Train default is -1 to 1
}


// make run header bigger
AB.headerCSS({
        "max-height": "95vh"
    }),



    //--- start of AB.msgs structure: ---------------------------------------------------------
    // We output a serious of AB.msgs to put data at various places in the run header 


    // 1 Doodle header 
    thehtml = "<hr> <h1> 1. Doodle </h1> Top row: Doodle (left) and shrunk (right). <br>  Draw your doodle in top LHS. <br> <button onclick='wipeDoodle();' class='normbutton' >Clear</button> <br> ",
    AB.msg(thehtml, 1),


    // 3 Training header
    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>  <button onclick='do_training = true;' class='normbutton' >Resume training</button> <br> ",
    AB.msg(thehtml, 3),


    // 5 Testing header
    thehtml = "<h3> Hidden tests </h3> ",
    AB.msg(thehtml, 5),



    // 7 Demo header 
    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 bluespan = "<span style='font-weight:bold; font-size:large; color:darkblue'> ";
//--- end of AB.msgs structure: ---------------------------------------------------------



function setup() {
    createCanvas(canvaswidth, canvasheight),
        (doodle = createGraphics(ZOOMPIXELS, ZOOMPIXELS)).pixelDensity(1),
        wipeDoodle(), AB.loadingScreen(),
        $.getScript("/uploads/codingtrain/matrix.js",
            function() {
                $.getScript("/uploads/rajv261/convnet.js",
                    function() {
                        $.getScript("/uploads/rajv261/mnistDhe.js",
                            function() {
                                setup
                                console.log("All JS Files 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: 26
                                    }),
                                    (mycnnModel = new convnetjs.Net).makeLayers(t),
                                    mycnnTrain = new convnetjs.SGDTrainer(mycnnModel, {
                                        method: "adadelta",
                                        momentum: 0.9,
                                        batch_size: 10,
                                        l2_decay: 0.001
                                    }),
                                    lData();
                            });
                    });
            });
}




// load data set from local file (on this server)
function lData() {
    loadMNIST(function(img) {
        mnist = img;
        let e = 0;
        for (; e < NOTRAIN; e++) rotateImage(mnist.train_images[e]);
        for (e = 0; e < NOTEST; e++) rotateImage(mnist.test_images[e]);
        console.log("All data loaded into Emnist object."),
            console.log(mnist),
            AB.removeLoading();
    });
}




function getImage(img) {
    let e = createImage(PIXELS, PIXELS);
    e.loadPixels();
    for (let n = 0; n < PIXELSSQUARED; n++) {
        let o = img[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(img) {
    let e = [];
    for (let n = 0; n < PIXELSSQUARED; n++) {
        let o = img[n];
        e[n] = o / 255;
    }
    return e;
}




function rotateImage(img) {
    for (let e = 0; e < PIXELS; e++)
        for (let n = e; n < PIXELS; n++) {
            let o = e * PIXELS + n,
                s = n * PIXELS + e,
                i = img[o];
            img[o] = img[s], img[s] = i;
        }
}




function trainit(img) {
    let e = mnist.train_images[train_index],
        n = mnist.train_labels[train_index];
    if (img) {
        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 img = getmycnnInputs(s);
        mycnnTrain.train(img, 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 getmycnnInputs(img) {
    for (var e = new convnetjs.Vol(28, 28, 1, 0), n = 0; n < PIXELSSQUARED; n++) e.w[n] = img[n];
    return e;
}




function testit() {
    let img = mnist.test_images[test_index],
        e = mnist.test_labels[test_index],
        n = getInputs(img),
        o = getmycnnInputs(n);
    test_inputs = n;
    let s = findMax(mycnnModel.forward(o).w);
    var i = getImage(img);
    image(i, 0, ZOOMPIXELS + 50, ZOOMPIXELS, ZOOMPIXELS),
        image(i, ZOOMPIXELS + 50, ZOOMPIXELS + 50, PIXELS, PIXELS),
        total_tests++, s == e && total_correct++;
    let a = total_correct / total_tests * 100;
    thehtml = " testrun: " + testrun + "<br> no: " + total_tests + " <br>  correct: " + total_correct + "<br>  score: " + bluespan + a.toFixed(2) + "</span>", AB.msg(thehtml, 6),
        ++test_index == NOTEST && (console.log("finished testrun: " + testrun + " score: " + a.toFixed(2)), testrun++, test_index = 0, total_tests = 0, total_correct = 0);
}




//--- find no.1 (and maybe no.2) output nodes ---------------------------------------
// (restriction) assumes array values start at 0 (which is true for output nodes) 


function find12(a) // return array showing indexes of no.1 and no.2 values in array
{
    let e = 0,
        n = 0,
        o = 0,
        s = 0;
    for (let i = 0; i < a.length; i++)
        a[i] > o ? (n = e, s = o, e = i, o = a[i]) : a[i] > s && (n = i, s = a[i]);
    return [e, n];
}



// just get the maximum - separate function for speed - done many times 
// find our guess - the max of the output nodes array

function findMax(a) {
    let e = 0,
        n = 0;
    for (let o = 0; o < a.length; o++) a[o] > n && (e = o, n = a[o]);
    return e;
}




// --- the draw function -------------------------------------------------------------
// every step:

function draw() {
    if (void 0 !== mnist) {
        if (background("black"), strokeWeight(1), rect(0, 0, ZOOMPIXELS, ZOOMPIXELS), textSize(10), textAlign(CENTER), 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"), strokeJoin(ROUND), doodle.strokeWeight(DOODLE_THICK), doodle.line(mouseX, mouseY, pmouseX, pmouseY));
        } else mousedrag && (mousedrag = false, doodle.filter(BLUR, DOODLE_BLUR));
    }
}



//--- demo -------------------------------------------------------------
// demo some test image and predict it
// get it from test set so have not used it in training


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 = getmycnnInputs(t),
        n = findMax(mycnnModel.forward(e).w);
    thehtml = " We classify it as: " + bluespan + alphabets[n - 1] + "</span>", AB.msg(thehtml, 9);
}




//--- doodle -------------------------------------------------------------

function drawDoodle() {
    let t = doodle.get();
    image(t, 0, 0, ZOOMPIXELS, ZOOMPIXELS),
        image(t, ZOOMPIXELS + 20, 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 = e;
    let n = getmycnnInputs(e),
        o = find12(mycnnModel.forward(n).w);
    thehtml = " Our 1st Guess is: " + bluespan + alphabets[o[0] - 1] + "</span> <br> Our 2nd Guess is: " + bluespan + alphabets[o[1] - 1] + "</span>", AB.msg(thehtml, 2);
}




function wipeDoodle() {
    doodle_exists = false,
        doodle.background("black");
}



// --- debugging --------------------------------------------------
// in console
// showInputs(demo_inputs);
// showInputs(doodle_inputs);


function showInputs(inputs) {
    var e = "";
    for (let n = 0; n < inputs.length; n++) {
        n % PIXELS == 0 && (e += "\n"),
            e = e + " " + inputs[n].toFixed(2);
    }
    console.log(e);
}