demo/js/images-demo.js
function() {
  var num_classes = net.layers[net.layers.length-1].out_depth;

  document.getElementById('testset_acc').innerHTML = '';
  var num_total = 0;
  var num_correct = 0;

  // grab a random test image
  for(num=0;num<4;num++) {
    var sample = sample_test_instance();
    var y = sample.label;  // ground truth label

    // forward prop it through the network
    var aavg = new convnetjs.Vol(1,1,num_classes,0.0);
    // ensures we always have a list, regardless if above returns single item or list
    var xs = [].concat(sample.x);
    var n = xs.length;
    for(var i=0;i<n;i++) {
      var a = net.forward(xs[i]);
      aavg.addFrom(a);
    }
    var preds = [];
    for(var k=0;k<aavg.w.length;k++) { preds.push({k:k,p:aavg.w[k]}); }
    preds.sort(function(a,b){return a.p<b.p ? 1:-1;});
    
    var correct = preds[0].k===y;
    if(correct) num_correct++;
    num_total++;

    var div = document.createElement('div');
    div.className = 'testdiv';

    // draw the image into a canvas
    draw_activations_COLOR(div, xs[0], 2); // draw Vol into canv

    // add predictions
    var probsdiv = document.createElement('div');
    
    var t = '';
    for(var k=0;k<3;k++) {
      var col = preds[k].k===y ? 'rgb(85,187,85)' : 'rgb(187,85,85)';
      t += '<div class=\"pp\" style=\"width:' + Math.floor(preds[k].p/n*100) + 'px; background-color:' + col + ';\">' + classes_txt[preds[k].k] + '</div>'
    }
    probsdiv.innerHTML = t;
    probsdiv.className = 'probsdiv';
    div.appendChild(probsdiv);

    // add it into DOM
    $(div).prependTo($("#testset_vis")).hide().fadeIn('slow').slideDown('slow');
    if($(".probsdiv").length>200) {
      $("#testset_vis > .probsdiv").last().remove(); // pop to keep upper bound of shown items
    }
  }
  testAccWindow.add(num_correct/num_total);
  $("#testset_acc").text('test accuracy based on last 200 test images: ' + testAccWindow.get_average());  
}
function() {
  var num_classes = net.layers[net.layers.length-1].out_depth;

  document.getElementById('testset_acc').innerHTML = '';
  var num_total = 0;
  var num_correct = 0;

  // grab a random test image
  for(num=0;num<4;num++) {
    var sample = sample_test_instance();
    var y = sample.label;  // ground truth label

    // forward prop it through the network
    var aavg = new convnetjs.Vol(1,1,num_classes,0.0);
    // ensures we always have a list, regardless if above returns single item or list
    var xs = [].concat(sample.x);
    var n = xs.length;
    for(var i=0;i<n;i++) {
      var a = net.forward(xs[i]);
      aavg.addFrom(a);
    }
    var preds = [];
    for(var k=0;k<aavg.w.length;k++) { preds.push({k:k,p:aavg.w[k]}); }
    preds.sort(function(a,b){return a.p<b.p ? 1:-1;});
    
    var correct = preds[0].k===y;
    if(correct) num_correct++;
    num_total++;

    var div = document.createElement('div');
    div.className = 'testdiv';

    // draw the image into a canvas
    draw_activations_COLOR(div, xs[0], 2); // draw Vol into canv

    // add predictions
    var probsdiv = document.createElement('div');
    
    var t = '';
    for(var k=0;k<3;k++) {
      var col = preds[k].k===y ? 'rgb(85,187,85)' : 'rgb(187,85,85)';
      t += '<div class=\"pp\" style=\"width:' + Math.floor(preds[k].p/n*100) + 'px; background-color:' + col + ';\">' + classes_txt[preds[k].k] + '</div>'
    }
    probsdiv.innerHTML = t;
    probsdiv.className = 'probsdiv';
    div.appendChild(probsdiv);

    // add it into DOM
    $(div).prependTo($("#testset_vis")).hide().fadeIn('slow').slideDown('slow');
    if($(".probsdiv").length>200) {
      $("#testset_vis > .probsdiv").last().remove(); // pop to keep upper bound of shown items
    }
  }
  testAccWindow.add(num_correct/num_total);
  $("#testset_acc").text('test accuracy based on last 200 test images: ' + testAccWindow.get_average());  
}
function ( ) { var num_classes = net . layers [ net . layers . length - 1 ] . out_depth ; document . getElementById ( 'testset_acc' ) . innerHTML = '' ; var num_total = 0 ; var num_correct = 0 ; // grab a random test image for ( num = 0 ; num < 4 ; num ++ ) { var sample = sample_test_instance ( ) ; var y = sample . label ; // ground truth label // forward prop it through the network var aavg = new convnetjs . Vol ( 1 , 1 , num_classes , 0.0 ) ; // ensures we always have a list, regardless if above returns single item or list var xs = [ ] . concat ( sample . x ) ; var n = xs . length ; for ( var i = 0 ; i < n ; i ++ ) { var a = net . forward ( xs [ i ] ) ; aavg . addFrom ( a ) ; } var preds = [ ] ; for ( var k = 0 ; k < aavg . w . length ; k ++ ) { preds . push ( { k : k , p : aavg . w [ k ] } ) ; } preds . sort ( function ( a , b ) { return a . p < b . p ? 1 : - 1 ; } ) ; var correct = preds [ 0 ] . k === y ; if ( correct ) num_correct ++ ; num_total ++ ; var div = document . createElement ( 'div' ) ; div . className = 'testdiv' ; // draw the image into a canvas draw_activations_COLOR ( div , xs [ 0 ] , 2 ) ; // draw Vol into canv // add predictions var probsdiv = document . createElement ( 'div' ) ; var t = '' ; for ( var k = 0 ; k < 3 ; k ++ ) { var col = preds [ k ] . k === y ? 'rgb(85,187,85)' : 'rgb(187,85,85)' ; t += '<div class=\"pp\" style=\"width:' + Math . floor ( preds [ k ] . p / n * 100 ) + 'px; background-color:' + col + ';\">' + classes_txt [ preds [ k ] . k ] + '</div>' } probsdiv . innerHTML = t ; probsdiv . className = 'probsdiv' ; div . appendChild ( probsdiv ) ; // add it into DOM $ ( div ) . prependTo ( $ ( "#testset_vis" ) ) . hide ( ) . fadeIn ( 'slow' ) . slideDown ( 'slow' ) ; if ( $ ( ".probsdiv" ) . length > 200 ) { $ ( "#testset_vis > .probsdiv" ) . last ( ) . remove ( ) ; // pop to keep upper bound of shown items } } testAccWindow . add ( num_correct / num_total ) ; $ ( "#testset_acc" ) . text ( 'test accuracy based on last 200 test images: ' + testAccWindow . get_average ( ) ) ; }
evaluate current network on test set
evaluate current network on test set