<commit_before>package BIDMach.caffe
import BIDMat.{Mat,SBMat,CMat,CSMat,DMat,FMat,FND,GMat,GIMat,GSMat,HMat,Image,IMat,ND,SMat,SDMat}
import BIDMat.MatFunctions._
import BIDMat.SciFunctions._
import BIDMach.datasources._
import edu.berkeley.bvlc.SGDSOLVER
import edu.berkeley.bvlc.NET
import edu.berkeley.bvlc.CAFFE

// Caffe Images are W < H < D (< N), Java images are D < W < H, Matlab means file is W < H < D

class Net () {
  
  val _net = new NET
  
  def init(modelfile:String, paramfile:String) = {
  	_net.init(modelfile, paramfile)
   	input_data = FND(inwidth, inheight, inchannels, num)
   	output_data = FND(outwidth, outheight, outchannels, num)
   	
  }
  
  def init(modelfile:String) = {
  	_net.init(modelfile)
   	input_data = FND(inwidth, inheight, inchannels, num)
   	output_data = FND(outwidth, outheight, outchannels, num)
  }

  def inchannels = _net.input_blob(0).channels
  
  def inwidth = _net.input_blob(0).width
  
  def inheight = _net.input_blob(0).height
  
  def outchannels = _net.output_blob(0).channels
  
  def outwidth = _net.output_blob(0).width
  
  def outheight = _net.output_blob(0).height
  
  def num = _net.input_blob(0).num
  
  def set_mean(mfile:String, varname:String = "image_mean") = {
    var meanf:FND = load(mfile, varname)                                   // Matlab means file is W < H < D, BGR
    if (meanf.dims(0) != inwidth || meanf.dims(1) != inheight) {
    	meanf = meanf.transpose(2, 0, 1)                                     // First go to resizing order D < W < H
    	meanf = Image(meanf).resize(inwidth, inheight).toFND                 // Resize if needed
    	meanf = meanf.transpose(1, 2, 0)                                     // Now back to W < H < D
    }
    _mean = meanf
  }
  
  def set_input_scale(v:Float) = {_scale = v};
  
  def set_channel_swap(v:IMat) = {_channel_swap = v};
  
  def set_image_dims(dd:Array[Int]) = {_image_dims = dd};  
  
  def forward() = {
    push_inputs
    _net.forward
    pull_outputs
  }
  
  def backward() = _net.backward
  
  def preprocess(im:Image):FND = {                                          // Preprocess a D < W < H image
    var cafimg = im.toFND;
    if (cafimg.dims(1) != _image_dims(0) || cafimg.dims(2) != _image_dims(1)) {
      cafimg = Image(cafimg).resize(_image_dims(0), _image_dims(1)).toFND;
    }<commit_msg>fixes to Net<commit_after>package BIDMach.caffe
import BIDMat.{Mat,SBMat,CMat,CSMat,DMat,FMat,FND,GMat,GIMat,GSMat,HMat,Image,IMat,ND,SMat,SDMat}
import BIDMat.MatFunctions._
import BIDMat.SciFunctions._
import BIDMach.datasources._
import edu.berkeley.bvlc.SGDSOLVER
import edu.berkeley.bvlc.NET
import edu.berkeley.bvlc.CAFFE

// Caffe Images are W < H < D (< N), Java images are D < W < H, Matlab means file is W < H < D

class Net () {
  
  val _net = new NET
  
  def init(modelfile:String, paramfile:String) = {
  	_net.init(modelfile, paramfile)
   	input_data = FND(inwidth, inheight, inchannels, num)
   	output_data = FND(outwidth, outheight, outchannels, num)
   	
  }
  
  def init(modelfile:String) = {
  	_net.init(modelfile)
   	input_data = FND(inwidth, inheight, inchannels, num)
   	output_data = FND(outwidth, outheight, outchannels, num)
  }

  def inchannels = _net.input_blob(0).channels
  
  def inwidth = _net.input_blob(0).width
  
  def inheight = _net.input_blob(0).height
  
  def outchannels = _net.output_blob(0).channels
  
  def outwidth = _net.output_blob(0).width
  
  def outheight = _net.output_blob(0).height
  
  def num = _net.input_blob(0).num
  
  def set_mean(mfile:String, varname:String = "image_mean") = {
    var meanf:FND = load(mfile, varname)                                   // Matlab means file is W < H < D, BGR
    println("mean file %d %d %d" format(meanf.dims(0), meanf.dims(1), meanf.dims(2)))
    if (meanf.dims(0) != inwidth || meanf.dims(1) != inheight) {
    	meanf = meanf.transpose(2, 0, 1)                                     // First go to resizing order D < W < H
    	meanf = Image(meanf).resize(inwidth, inheight).toFND                 // Resize if needed
    	meanf = meanf.transpose(1, 2, 0)                                     // Now back to W < H < D
    }
    _mean = meanf
  }
  
  def set_input_scale(v:Float) = {_scale = v};
  
  def set_channel_swap(v:IMat) = {_channel_swap = v};
  
  def set_image_dims(dd:Array[Int]) = {_image_dims = dd};  
  
  def forward() = {
    push_inputs
    _net.forward
    pull_outputs
  }
  
  def backward() = _net.backward
  
  def preprocess(im:Image):FND = {                                          // Preprocess a D < W < H image
    var cafimg = im.toFND;
    if (cafimg.dims(1) != _image_dims(0) || cafimg.dims(2) != _image_dims(1)) {
      cafimg = Image(cafimg).resize(_image_dims(0), _image_dims(1)).toFND;
    }
{'fraction_non_alphanumeric': 0.12303829252981795, 'fraction_numerical': 0.009416195856873822, 'mean_word_length': 2.583208395802099, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1772738', 'n_tokens_mistral': 1825, 'n_tokens_neox': 1823, 'n_words': 372}