Code viewer for World: *FAILED - CNN

// Cloned by Laura Campbell on 25 Nov 2022 from World "Character recognition neural network" by "Coding Train" project 
// Please leave this clone trail here.
 

// Port of Character recognition neural network from here:
// https://github.com/CodingTrain/Toy-Neural-Network-JS/tree/master/examples/mnist
// with many modifications 


// --- defined by MNIST - do not change these ---------------------------------------

const PIXELS        = 28;                       // images in data set are tiny 
const PIXELSSQUARED = PIXELS * PIXELS;

// number of training and test exemplars in the data set:
const NOTRAIN = 60000;
const NOTEST  = 10000;



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

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

const learningrate = 0.1;   // default 0.1  

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

// how many to train and test per timestep 
const TRAINPERSTEP = 30;
const TESTPERSTEP  = 10;

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

// 3 rows of
// large image + 50 gap + small image    
// 50 gap between rows 

const canvaswidth = ( PIXELS + ZOOMPIXELS ) + 50;
const canvasheight = ( ZOOMPIXELS * 3 ) + 100;


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


let mnist;      
// all data is loaded into this 
// mnist.train_images
// mnist.train_labels
// mnist.test_images
// mnist.test_labels


let cnn;
let cnnTrain;
let cnnModel;
let letters=["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"];

let trainrun = 1;
let train_index = 0;

let testrun = 1;
let test_index = 0;
let total_tests = 0;
let total_correct = 0;

// images in LHS:
let doodle, demo;
let doodle_exists =!0;
let demo_exists =!0;

let mousedrag =!0;      // are we in the middle of a mouse drag drawing?  


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


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

function randomWeight()
{
        return ( AB.randomFloatAtoB ( -0.5, 0.5 ) );
        //return ( AB.randomFloatAtoB ( -1, 1 ) );
            // 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 
var thehtml;

  // 1 Doodle header 
  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 );

  // 2 Doodle variable data (guess)
  
  // 3 Training header
  thehtml = "<hr> <h1> 2. Training </h1> Middle row: Training image magnified (left) and original (right). <br>  " +
        " <button onclick='do_training =!1;' class='normbutton' >Stop training</button> <br> ";
  AB.msg ( thehtml, 3 );
     
  // 4 variable training data 
  
  // 5 Testing header
  thehtml = "<h3> Hidden tests </h3> " ;
  AB.msg ( thehtml, 5 );
           
  // 6 variable testing data 
  
  // 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 );
   
  // 8 Demo variable data (random demo ID)
  // 9 Demo variable data (changing guess)
  
const greenspan = "<span style='font-weight:bold; font-size:x-large; color:darkgreen'> "  ;

//--- end of AB.msgs structure: ---------------------------------------------------------



function setup() 
{
  createCanvas ( canvaswidth, canvasheight );

  doodle = createGraphics ( ZOOMPIXELS, ZOOMPIXELS );       // doodle on larger canvas 
  doodle.pixelDensity(1);
  
// JS load other JS 
// maybe have a loading screen while loading the JS and the data set 
    
    wipeDoodle();
    AB.loadingScreen();
  //$.getScript("/uploads/codingtrain/matrix.js",function()
  $.getScript("/uploads/lauracampbell26/matrix.js",function()
  { 
   $.getScript ( "/uploads/lauracampbell26/convnet.js", function()
   { 
       $.getScript ( "/uploads/lauracampbell26/mnist.js", function()
        {
            
//https://cs.stanford.edu/people/karpathy/convnetjs/demo/mnist.html
            console.log ("All JS loaded");
            let layer_defs = [];
            layer_defs.push({type:'input', out_sx:28, out_sy:28, out_depth:1});
            layer_defs.push({type:'conv', sx:5, filters:8, stride:1, pad:2, activation:'relu'});
            layer_defs.push({type:'pool', sx:2, stride:2});
            layer_defs.push({type:'conv', sx:5, filters:16, stride:1, pad:2, activation:'relu'});
            layer_defs.push({type:'pool', sx:3, stride:3});
            layer_defs.push({type:'softmax', num_classes:10});
            
            cnnMode = new convnetjs.Net();
            cnnMode.makeLayers(layer_defs);
            
            cnnTrain= new convnetjs.SGDTrainer(cnnMode, {method:'adadelta', batch_size:20, l2_decay:0.001});
            
            loadData();
        });
   });
  });

}


// load data set from local file (on this server)

function loadData() {
    loadMNIST(function(t) {
        mnist = t;
        for (e = 0; 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();      // if no loading screen exists, this does nothing 
    })
}



function getImage( t )      // make a P5 image object from a raw data array   
{
    let e  = createImage (PIXELS, PIXELS);    // make blank image, then populate it 
    e.loadPixels();        
    
    for (let n = 0; n < PIXELSSQUARED ; n++) 
    {
        let o = t[n];
        s = n * 4;
        e.pixels[s + 0] = o;
        e.pixels[s + 1] = o;
        e.pixels[s + 2] = o;
        e.pixels[s + 3] = 255;
    }
    
    //e.updatePixels();
    return e.updatePixels,e;
}


function getInputs( t )      // convert t array into normalised input array 
{
    let e= [];
    for (let n = 0; n < PIXELSSQUARED ; n++)          
    {
        let o = t[n];
        e[n] = o / 255;       // normalise to 0 to 1
    } 
    return (e);
}

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

function trainit (t)        // train the network with a single exemplar, from global var "train_s", show visual on or off 
{
  let e = mnist.train_images[train_index];
  let n = mnist.train_labels[train_index];
  
  // optional - show visual of the image 
  if (t)                
  {
    var o = getImage ( e );    // get image from data array 
    image ( o,   0,                ZOOMPIXELS+50,    ZOOMPIXELS,     ZOOMPIXELS  );      // magnified 
    image ( o,   ZOOMPIXELS+50,    ZOOMPIXELS+50,    PIXELS,         PIXELS      );      // original
  }

  // set up the inputs
  let s= getInputs( e );       // getefrom data array 
    train_inputs = s;
    {
        let t = getcnnInputs(s);
        cnnTrain.train(t, n);
    }
 
  thehtml = " trainrun: " + trainrun + "<br> no: " + train_index ;
  AB.msg ( thehtml, 4 );

  train_index++;
  if ( train_index == NOTRAIN ) 
  {
    train_index = 0;
    console.log( "finished trainrun: " + trainrun );
    trainrun++;
  }
}

function getcnnInputs(t)
{
  for (var e = new convnetjs.Vol(28,28,1,0),n=0;n<PIXELSSQUARED;n++)
  e.w[n]=t[n];
  return e;
}

function testit()    // test the network with a single exemplar, from global var "test_s"
{ 
  let t = mnist.test_images[test_index];
  let e = mnist.test_labels[test_index];

  // set up the inputs
  let n = getInputs(t);
  let o = getcnnInputs(n);  
  
  test_inputs = n;        // can inspect in console 
  
  //let prediction    = nn.predict(inputs);       // array of outputs 
  //let guess         = findMax(prediction);      // the top output 
  let s = findMax(cnnModel.forward(o).w);
  var i = getImage(t);
  image(i,  0,  ZOOMPIXELS+50,  ZOOMPIXELS, ZOOMPIXELS);
  image(i,  ZOOMPIXELS+50,  ZOOMPIXELS+50,  PIXELS, PIXELS);
  total_tests++;
  if (s == e) 
  {
      total_correct++;
  }

  let a = (total_correct / total_tests) * 100 ;
  
  thehtml =  " testrun: " + testrun + "<br> no: " + total_tests + " <br> " +
        " correct: " + total_correct + "<br>" +
        "  score: " + greenspan + percent.toFixed(2) + "</span>";
  AB.msg ( thehtml, 6 );

++test_index;
  if ( test_index == NOTEST ) 
  {
    console.log( "finished testrun: " + testrun + " score: " + percent.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 (t)         // return array showing ses of no.1 and no.2 values in array 
{

  let e=0;
  let n=0;
  let o=0;
  let s=0;
  
  for (let i = 0; i < t.length; i++) 
  {
    if (t[i] > o)   // new no1
    {
      // old no1 becomes no2
      n = e;
      s = o;
      // now put in the new no1
      e = i;
      o = t[i];
    }
    else if (t[i] > s)  // new no2 
    {
      n = i;
      s = t[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 (t)        
{
  let e = 0;
  let n = 0;     
  
  for (let o = 0; o < t.length; o++) 
  {
    if (t[o] > n) 
    {
      e = o;
      n = t[o];
    }
  }
  
  return e;
}




// --- the draw function -------------------------------------------------------------
// every step:
 
function draw() 
{
  // check if libraries and data loaded yet:
    if(void 0!==mnist) {
        if (background("black"),


// how can we get white doodle on black background on yellow canvas?
//        background('#ffffcc');    doodle.background('black');
      strokeWeight(1),
      stroke("pink"),
      rect( 0,  0,  ZOOMPIXELS, ZOOMPIXELS  ),
      textSize(10),
      textAlign(CENTER),
      do_training) 
      {
          // do some training per step 
            for (let t = 0; t < TRAINPERSTEP; t++) 
            {
              //if (t == 0)    trainit(true);    // show only one per step - still flashes by  
             // else           trainit(!0);
               trainit(0===t);
        
            }
          // do some testing per step 
            for (let t = 0; t < TESTPERSTEP; t++) 
              testit();
          
      }
          // keep drawing demo and doodle images 
          // and keep guessing - we will update our guess as time goes on 
  
      if ( demo_exists )
      {
        drawDemo();
        guessDemo();
      }
      if ( doodle_exists ) 
      {
        drawDoodle();
        guessDoodle();
      }


// detect doodle drawing 
// (restriction) the following assumes doodle starts at 0,0 

      if ( mouseIsPressed )         // gets called when we click buttons, as well as if in doodle corner  
      {
         // console.log ( mouseX + " " + mouseY + " " + pmouseX + " " + pmouseY );
         var MAX = ZOOMPIXELS + 20;     // can draw up to this pixels in corner 
         if ( (mouseX < MAX) && (mouseY < MAX) && (pmouseX < MAX) && (pmouseY < MAX) )
         {
            mousedrag =!0;       // start a mouse drag 
            doodle_exists =!0;
            doodle.stroke('white');
            doodle.strokeWeight( DOODLE_THICK );
            doodle.line(mouseX, mouseY, pmouseX, pmouseY);      
         }
      }
      else 
      {
          // are we exiting a drawing
          if ( mousedrag )
          {
                mousedrag =!1;
                // console.log ("Exiting draw. Now blurring.");
                doodle.filter (BLUR, DOODLE_BLUR);    // just blur once 
                //   console.log (doodle);
          }
      }
    }
}




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


function makeDemo()
{
    demo_exists =!0;
    var  t = AB.randomIntAtoB ( 0, NOTEST - 1 );  
    demo        = mnist.test_images[t];     
    var label   = mnist.test_labels[t];
    
  thehtml="Test image no: "+t+"<br>Classification: "+ letters[e-1]+"<br>";

   AB.msg ( thehtml, 8 );
   
   // type "demo" in console to see raw data 
}


function drawDemo()
{
    var t = getImage( demo );
     //  console.log (e);
    image ( t,   0,                canvasheight - ZOOMPIXELS,    ZOOMPIXELS,     ZOOMPIXELS  );      // magnified 
    image ( t,   ZOOMPIXELS+50,    canvasheight - ZOOMPIXELS,    PIXELS,         PIXELS      );      // original
}


function guessDemo()
{
   let t= getInputs( demo ); 
   
  demo_inputs = t;  // can inspect in console 
  
  let e = getcnnInputs(t);       // array of outputs 
  let n = findMax(cnnModel.forward(e).w);      // the top output 

   thehtml =   " We classify it as: " + greenspan + letters[n-1] + "</span>" ;
   AB.msg ( thehtml, 9 );
}




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

function drawDoodle()
{
    // doodle is createGraphics not createImage
    let t = doodle.get();
    // console.log (e);
    
    image ( t,   0,                0,    ZOOMPIXELS,     ZOOMPIXELS  );      // original 
    image ( t,   ZOOMPIXELS+50,    0,    PIXELS,         PIXELS      );      // shrunk
}
      
      
function guessDoodle() 
{
   // doodle is createGraphics not createImage
   let t = doodle.get();
  
  t.resize ( PIXELS, PIXELS );     
  t.loadPixels();

  // set upe  
  let e= [];
  for (let n = 0; n < PIXELSSQUARED ; n++) 
  {
     e[n] = t.pixels[n * 4] / 255;
  }
  
  doodle_inputs = e;     // can inspect in console 

  // feed forward to make prediction 
  let n    = getcnnInputs(e);      // array of outputs 
  let o = find12(cnnModel.forward(n).w);       // get no.1 and no.2 guesses  

  thehtml =   " We classify it as: " + greenspan +letters[o[0]-1]+ "</span> <br>" +
            " No.2 guess is: " + greenspan +letters[o[1]-1]+ "</span>";
  AB.msg ( thehtml, 2 );
}


function wipeDoodle()    
{
    doodle_exists =!1;
    doodle.background('black');
}




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


function showInputs (t)
// displayerow by row, corresponding to square of pixels 
{
     var e = "";
    for (let n = 0; n < t.length; n++) 
    {
      if ( n % PIXELS === 0 )    e+="\n";                                 // new line for each row of pixels 
        e=e+" "+t[n].toFixed(2);
    }
    console.log (e);
}