Code viewer for World: Smart Doodle & Char Recogn...

// Cloned by rohan on 1 Dec 2021 from World "Smart Doodle & Char Recognition Neural Network " by Paul Geoghegan 
// Please leave this clone trail here.
 
const PIXELS=28,PIXELSSQUARED=PIXELS*PIXELS,NOTRAIN=6e4,NOTEST=1e4;let numDoodleDraw=0,doodleCorrectCnt=0,doodlePercentage=0,numDemotestCnt=0,demoCorrectCnt=0,demoPercentage=0;const max_doodles=20;var saved_doodles,saved_d_array=[],doodle_test_cntr=0,test_Saved_Dcount=0,Saved_Doodle_Ctr=-1,draw_saved_Doodles=!1;const noinput=PIXELSSQUARED,nohidden=64,nooutput=10,learningrate=.1;let do_training=!0;const TRAINPERSTEP=30,TESTPERSTEP=5,ZOOMFACTOR=7,ZOOMPIXELS=7*PIXELS,canvaswidth=PIXELS+ZOOMPIXELS+50,canvasheight=4*ZOOMPIXELS+30,DOODLE_THICK=18,DOODLE_BLUR=3;let mnist,nn,doodle,demo,trainrun=1,train_index=0,testrun=1,test_index=0,total_tests=0,total_correct=0,doodle_exists=!1,demo_exists=!1,mousedrag=!1;var train_inputs,test_inputs,demo_inputs,doodle_inputs,thehtml;function randomWeight(){return AB.randomFloatAtoB(-.5,.5)}$("#runheaderbox").css({"max-height":"95vh","max-width":"95vh"}),AB.onDesktop()?AB.runloggedin&&(thehtml="<hr> <h1> <span style= color:purple> 1. Doodle (User Input) </span></h1> Top row: Doodle (left) and shrunk (right).  <span style='font-weight:bold; color:purple'>Draw your doodle (using mouse)</span> in top LHS. <br><br> <button onclick='wipeDoodle();' class='normbutton' > Clear doodle</button>  <button onclick='recordDoodle();' class='normbutton' > Record doodle</button> <button onclick='deleteRecordDoodle();' class='normbutton' > Delete Recorded doodle(s)</button>  <button onclick='saveData();' class='normbutton' >Save Recorded doodles</button> <br> <br> <button onclick='restoreDoodleData();' class='normbutton' >Restore Saved doodles</button> <button onclick='draw_Rec_doodles();' class='normbutton' >Draw Recorded doodles</button> <button onclick='test_Rec_doodles();' class='normbutton' >Test Recorded doodles</button><br> <br> Click if doodle is:  <button onclick='doodleStats(true);' class='normbutton' > Correct</button> <button onclick='doodleStats(false);' class='normbutton' > Incorrect</button> <br>",AB.msg(thehtml,1)):(thehtml="<hr> <h1> <span style= color:purple> 1. Doodle (User Input) </span></h1> Top row: Doodle (left) and shrunk (right).  <span style='font-weight:bold; color:purple'>Draw your doodle (using mouse)</span> in top LHS.<br> <button onclick='wipeDoodle();' class='normbutton' > Clear doodle</button>  <button onclick='recordDoodle();' class='normbutton' > Record doodle</button> <br>  Click if doodle is:  <button onclick='doodleStats(true);' class='normbutton' > Correct</button> <button onclick='doodleStats(false);' class='normbutton' > Incorrect</button> <br>",AB.msg(thehtml,1)),thehtml="<hr> <h1> <span style= color:green>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><span style= color:purple> 3. Demo (User Input) </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>  <span style='font-weight:bold; color:purple'> Click if demo is:</span> <button onclick='demoStats(true);' class='normbutton' > Correct</button> <button onclick='demoStats(false);' class='normbutton' > Incorrect</button> <br>",AB.msg(thehtml,7);const greenspan="<span style='font-weight:bold; font-size:x-large; color:darkgreen'> ";function deleteRecordDoodle(){for(;saved_d_array.length>0;)saved_d_array.shift();console.log(saved_d_array)}function saveData(){console.log("Saving "+saved_d_array.length+" doodles to server"),AB.saveData(saved_d_array)}function restoreDoodleData(){AB.queryDataExists(function(e){AB.restoreData(function(e){saved_d_array=e,console.log("Restoring "+e.length+" doodles from server"),console.log(saved_d_array)})})}function doodleStats(e){numDoodleDraw++,e&&doodleCorrectCnt++,doodlePercentage=100*doodleCorrectCnt/numDoodleDraw}function demoStats(e){numDemotestCnt++,e&&demoCorrectCnt++,demoPercentage=100*demoCorrectCnt/numDemotestCnt}function draw_Rec_doodles(){draw_saved_Doodles=!0,Saved_Doodle_Ctr++}function drawSaveddoodles(){if(Saved_Doodle_Ctr<saved_d_array.length){let e=saved_d_array[Saved_Doodle_Ctr];console.log(e),image(e,0,canvasheight-ZOOMPIXELS,ZOOMPIXELS,ZOOMPIXELS),image(e,ZOOMPIXELS+50,canvasheight-ZOOMPIXELS,PIXELS,PIXELS)}}function recordDoodle(){if(doodle_exists){let e=doodle.get();console.log("record doodle image"+e),saved_d_array.length<max_doodles&&(saved_d_array.push(e),console.log(saved_d_array.length),console.log(saved_d_array))}}function test_Rec_doodles(){let e=saved_d_array[doodle_test_cntr];console.log(e),doodle_test_cntr++,console.log(doodle_test_cntr),doodle_test_cntr>saved_d_array.length-1&&(doodle_test_cntr=0),e.resize(PIXELS,PIXELS),e.loadPixels();let t=[];for(let o=0;o<PIXELSSQUARED;o++)t[o]=e.pixels[4*o]/255;let o=find12(nn.predict(t));thehtml=" We classify it as: "+greenspan+o[0]+"</span> <br> No.2 guess is: "+greenspan+o[1]+"</span> <br>  Correct Guess: "+greenspan+doodlePercentage.toFixed(2)+"% </span>   Num of Doodles: "+greenspan+numDoodleDraw,AB.msg(thehtml,2)}function setup(){createCanvas(canvaswidth,canvasheight),(doodle=createGraphics(ZOOMPIXELS,ZOOMPIXELS)).pixelDensity(1),AB.loadingScreen(),$.getScript("/uploads/codingtrain/matrix.js",function(){$.getScript("/uploads/codingtrain/nn.js",function(){$.getScript("/uploads/codingtrain/mnist.js",function(){console.log("All JS loaded"),(nn=new NeuralNetwork(noinput,nohidden,nooutput)).setLearningRate(learningrate),loadData()})})})}function loadData(){loadMNIST(function(e){mnist=e,console.log("All data loaded into mnist object:"),console.log(mnist),AB.removeLoading()})}function getImage(e){let t=createImage(PIXELS,PIXELS);t.loadPixels();for(let o=0;o<PIXELSSQUARED;o++){let n=e[o],s=4*o;t.pixels[s+0]=n,t.pixels[s+1]=n,t.pixels[s+2]=n,t.pixels[s+3]=255}return t.updatePixels(),t}function getInputs(e){let t=[];for(let o=0;o<PIXELSSQUARED;o++){let n=e[o];t[o]=n/255}return t}function trainit(e){let t=mnist.train_images[train_index],o=mnist.train_labels[train_index];if(e){var n=getImage(t);image(n,0,ZOOMPIXELS+50,ZOOMPIXELS,ZOOMPIXELS),image(n,ZOOMPIXELS+50,ZOOMPIXELS+50,PIXELS,PIXELS)}let s=getInputs(t),r=[0,0,0,0,0,0,0,0,0,0];r[o]=1,train_inputs=s,nn.train(s,r),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 e=mnist.test_images[test_index],t=mnist.test_labels[test_index],o=getInputs(e);test_inputs=o;let n=findMax(nn.predict(o));total_tests++,n==t&&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(e){let t=0,o=0,n=0,s=0;for(let r=0;r<e.length;r++)e[r]>n?(s=n,t=r,n=e[r]):e[r]>s&&(o=r,s=e[r]);return[t,o]}function findMax(e){let t=0,o=0;for(let n=0;n<e.length;n++)e[n]>o&&(t=n,o=e[n]);return t}function draw(){if(void 0!==mnist){if(background("black"),do_training){for(let e=0;e<TRAINPERSTEP;e++)trainit(0==e);for(let e=0;e<TESTPERSTEP;e++)testit()}if(demo_exists&&(drawDemo(),guessDemo()),doodle_exists&&(drawDoodle(),guessDoodle()),draw_saved_Doodles&&drawSaveddoodles(),mouseIsPressed){var e=ZOOMPIXELS+20;mouseX<e&&mouseY<e&&pmouseX<e&&pmouseY<e&&(mousedrag=!0,doodle_exists=!0,doodle.stroke("white"),doodle.strokeWeight(DOODLE_THICK),doodle.line(mouseX,mouseY,pmouseX,pmouseY))}else mousedrag&&(mousedrag=!1,doodle.filter(BLUR,DOODLE_BLUR))}}function makeDemo(){demo_exists=!0;var e=AB.randomIntAtoB(0,NOTEST-1);demo=mnist.test_images[e];var t=mnist.test_labels[e];thehtml="Test image no: "+e+"<br>Classification: "+t+"<br>",AB.msg(thehtml,8)}function drawDemo(){var e=getImage(demo);console.log("draw demo image"+e),image(e,0,canvasheight-ZOOMPIXELS,ZOOMPIXELS,ZOOMPIXELS),image(e,ZOOMPIXELS+50,canvasheight-ZOOMPIXELS,PIXELS,PIXELS)}function guessDemo(){let e=getInputs(demo);demo_inputs=e;let t=findMax(nn.predict(e));thehtml=" We classify it as: "+greenspan+t+"</span>  Correct Guess: "+greenspan+demoPercentage.toFixed(2)+"% </span>   Num of Demo tests: "+greenspan+numDemotestCnt,AB.msg(thehtml,10)}function drawDoodle(){let e=doodle.get();console.log(" the doodle image "+e),image(e,0,0,ZOOMPIXELS,ZOOMPIXELS),image(e,ZOOMPIXELS+50,0,PIXELS,PIXELS)}function guessDoodle(){let e=doodle.get();e.resize(PIXELS,PIXELS),e.loadPixels();let t=[];for(let o=0;o<PIXELSSQUARED;o++)t[o]=e.pixels[4*o]/255;doodle_inputs=t;let o=find12(nn.predict(t));thehtml=" We classify it as: "+greenspan+o[0]+"</span> <br> No.2 guess is: "+greenspan+o[1]+"</span> <br>  Correct Guess: "+greenspan+doodlePercentage.toFixed(2)+"% </span>   Num of Doodles: "+greenspan+numDoodleDraw,AB.msg(thehtml,2)}function wipeDoodle(){doodle_exists=!1,doodle.background("black")}function showInputs(e){var t="";for(let o=0;o<e.length;o++){o%PIXELS==0&&(t+="\n"),t=t+" "+e[o].toFixed(2)}console.log(t)}