Exemple #1
0
 void jitter::set(const idx<t_jitter> &j) {
   s = j.get(0);
   h = (int) j.get(1);
   w = (int) j.get(2);
   r = j.get(3);
   idx_copy(j, jitts);
 }
Exemple #2
0
  unsigned int draw_layer(idx<float> &img, int dimn, int layern, const char *s,
			  unsigned int h, unsigned int w) {
#ifdef __GUI__
    idx<float> layer = img.select(dimn, layern);
    draw_matrix(layer, s, h, w, 1.0, 1.0, (float)-1.0, (float)1.0);
#endif
    return img.dim(1) + 3;
  }
Exemple #3
0
  void matlab::read_cast_matrix(mxArray *var, idx<T> &m) {
#ifdef __MATLAB__
    // allocate a temporary matrix with same type as original matrix type
    idx<Tmatlab> tmp(m.get_idxdim());
    // load data
    void *data = mxGetData(var);
    // copy to idx 
    memcpy(m.idx_ptr(), (Tmatlab*) data, m.nelements() * sizeof (Tmatlab));
    // copy-cast
    idx_copy(tmp, m);
#endif
  }
Exemple #4
0
 void padder<T>::pad(idx<T> &in, idx<T> &out) {
   // allocate padded buffer
   idxdim d = in;
   d.setdim(1, d.dim(1) + nrow + nrow2);
   d.setdim(2, d.dim(2) + ncol + ncol2);
   if (out.get_idxdim() != d)
     out.resize(d);
   idx_clear(out);
   // copy in to padded buffer
   idx<T> tmp = out.narrow(1, in.dim(1), nrow);
   tmp = tmp.narrow(2, in.dim(2), ncol);
   idx_copy(in, tmp);
   if (bmirror)
     mirror(in, out);
 }
Exemple #5
0
 idx<T> padder<T>::pad(idx<T> &in) {
   // allocate padded buffer
   idxdim d = in;
   d.setdim(1, d.dim(1) + nrow + nrow2);
   d.setdim(2, d.dim(2) + ncol + ncol2);
   idx<T> out(d);
   idx_clear(out);
   // copy in to padded buffer
   idx<T> tmp = out.narrow(1, in.dim(1), nrow);
   tmp = tmp.narrow(2, in.dim(2), ncol);
   idx_copy(in, tmp);
   if (bmirror)
     mirror(in, out);
   // return result
   return out;
 }
Exemple #6
0
unsigned int draw_layer(idx<float> &layer, const char *s,
			unsigned int h, unsigned int w) {
#ifdef __GUI__
  draw_matrix(layer, s, h, w, 1.0, 1.0, (float)-1.0, (float)1.0);
#endif
  return layer.dim(1) + 3;
}
Exemple #7
0
  // display original and new images.
  void display_images(idx<ubyte> &classes, int label, idx<float> &original,
		      idx<float> &new_images, const int channels_mode,
		      idx<ubyte> &ds_names, int current_ds) {
#ifdef __GUI__
    unsigned int h = 0, w = 0;
    static unsigned int cnt = 0;
    idx<float> layer;

    // only display every 15 images
    if (cnt++ % 15 != 0) return ;
    
    // reset window and do a batch display
    disable_window_updates();
    clear_window();
    
    // original image (RGB)
    static string s;
    s = "RGB"; s += " - "; s += (char *) classes[label].idx_ptr();
    draw_matrix(original, s.c_str(), h, w);
    w = 0; h += new_images.dim(1) + 5;
    gui << at(h, w) << black_on_white();
    gui << ds_names[current_ds].idx_ptr() << " dataset:";
    h += 15;
    
    // new images
    idx_bloop1(image, new_images, float) {
      switch (channels_mode) {
      case 1: // YpUV
	w += draw_layer(image, 2, 0, "Yp", h, w);
	w += draw_layer(image, 2, 1, "U", h, w);
	w += draw_layer(image, 2, 2, "V", h, w);
	break ;
      case 3: // Yp
	w += draw_layer(image, 2, 0, "Yp", h, w);
	break ;
      case 4: { // YpH3
	w += draw_layer(image, 2, 0, "Yp", h, w);
	w += draw_layer(image, 2, 1, "H3", h, w);
	idx_addc(layer, (float)1.0, layer);
	idx_dotc(layer, (float)210.0, layer);
	w += layer.dim(1) + 5;
	static idx<float> rgb(layer.dim(0), layer.dim(1), 3);
	h3_to_rgb(layer, rgb);
	draw_matrix(rgb, "H3 (colored)", h, w);
	break ;
      }
      case 5: // VpH2SV
	w += draw_layer(image, 2, 0, "Vp", h, w);
	w += draw_layer(image, 2, 1, "H1", h, w);
	w += draw_layer(image, 2, 2, "H2", h, w);
	w += draw_layer(image, 2, 3, "S", h, w);
	w += draw_layer(image, 2, 4, "V", h, w);
	w = 0; h += image.dim(1) + 5;
	break ;
      }
    }
    enable_window_updates();
#endif
  }
Exemple #8
0
idx<T> image_region_to_rect(idx<T> &im, const rect<int> &r, uint oheight,
                            uint owidth, rect<int> *cropped,
                            uint dh, uint dw)
{
    // TODO: check that rectangle is within image
    if (im.order() != 2 && im.order() != 3)
        eblerror("expected a 2d or 3d input but got " << im);
    idxdim d(im);
    d.setdim(dh, oheight);
    d.setdim(dw, owidth);
    idx<T> res(d);

    float hcenter = r.h0 + (float)r.height / 2; // input height center
    float wcenter = r.w0 + (float)r.width / 2; // input width center
    // // limit centers to half the width/height away from borders
    // // to handle incorrect regions
    // hcenter = MIN((float)im.dim(dh) - (float)r.height/2,
    //            std::max((float)r.height/2, hcenter));
    // wcenter = MIN((float)im.dim(dw) - (float)r.width/2,
    //            std::max((float)r.width/2, wcenter));
    float h0 = hcenter - (float)oheight / 2; // out height offset in input
    float w0 = wcenter - (float)owidth / 2; // out width offset in input
    float h1 = hcenter + (float)oheight / 2;
    float w1 = wcenter + (float)owidth / 2;
    int gh0 = (int)std::max(0, (int)MIN(im.dim(dh), h0)); // input h offset
    int gw0 = (int)std::max(0, (int)MIN(im.dim(dw), w0)); // input w offset
    int gh1 = (int)std::max(0, (int)MIN(im.dim(dh), h1));
    int gw1 = (int)std::max(0, (int)MIN(im.dim(dw), w1));
    int h = gh1 - gh0 + std::max(0, -r.h0); // out height narrow
    int w = gw1 - gw0 + std::max(0, -r.w0); // out width narrow
    int fh0 = (int)std::max(0, (int)(gh0 - h0)); // out height offset narrow
    int fw0 = (int)std::max(0, (int)(gw0 - w0)); // out width offset narrow

    // narrow original image
    int hmin = std::max(0, std::min((int)res.dim(dh) - fh0,
                                    std::min((int)im.dim(dh) - gh0, h)));
    int wmin = std::max(0, std::min((int)res.dim(dw) - fw0,
                                    std::min((int)im.dim(dw) - gw0, w)));
    // clear result image
    idx_clear(res);
    if (hmin != 0 && wmin != 0)  // only copy if overlap with image
    {
        idx<T> tmpim = im.narrow(dh, hmin, gh0);
        tmpim = tmpim.narrow(dw, wmin, gw0);
        // narrow target image
        idx<T> tmpres = res.narrow(dh, hmin, fh0);
        tmpres = tmpres.narrow(dw, wmin, fw0);
        // copy original to target
        idx_copy(tmpim, tmpres);
    }
    // set cropped rectangle to region in the output image containing input
    if (cropped)
    {
        cropped->h0 = fh0;
        cropped->w0 = fw0;
        cropped->height = hmin;
        cropped->width = wmin;
    }
    return res;
}
Exemple #9
0
    void serialize(Archive & ar, idx<ubyte>& mat, const unsigned int version) {
      intg d1, d2, d3;
      if (Archive::is_saving::value) { // saving to stream
	if (!mat.contiguousp())
	  eblerror("expected contiguous idx for serialization");
	if (mat.order() != 3)
	  eblerror("no support for idx order != 3 for now, got: " << mat);

	d1 = mat.dim(0); 
	d2 = mat.dim(1); 
	d3 = mat.dim(2); 
	
	ar & d1;
	ar & d2;
	ar & d3;
	idx_aloop1(m, mat, ubyte) {
	  ar & *m;
	}
      } else { // loading from stream
Exemple #10
0
idxlooper<T>::idxlooper(idx<T> &m, int ld) : idx<T>((dummyt*)0) {
  if (m.order() == 0) // TODO: allow looping once on 0-order idx
    eblerror("cannot loop on idx with order 0. idx is: " << m);
  i = 0;
  dimd = m.spec.dim[ld];
  modd = m.spec.mod[ld];
  m.spec.select_into(&(this->spec), ld, i);
  this->storage = m.storage;
  this->storage->lock();
}
Exemple #11
0
bool detection_thread<T>::set_data(idx<ubyte> &frame2,
                                   std::string &fullname,
                                   std::string &name, uint id) {
  // lock data (non blocking)
  if (!mutex_in.trylock())
    return false;
  // check frame is correctly allocated, if not, allocate.
  if (frame2.order() != uframe.order())
    uframe = idx<ubyte>(frame2.get_idxdim());
  else if (frame2.get_idxdim() != uframe.get_idxdim())
    uframe.resize(frame2.get_idxdim());
  idx_copy(frame2, uframe);	// copy frame
  frame_loaded = true;        // frame is loaded
  frame_fullname = fullname;
  frame_name = name;		// copy name
  frame_id   = id;		// copy frame_id
  in_updated = true;		// reset updated flag
  bavailable = false;		// declare thread as not available
  bfed       = true;		// data has been fed at least once
  mutex_in.unlock();		// unlock data
  return true;		// confirm that we copied data.
}
Exemple #12
0
 void image_paste_center(idx<T> &in, idx<T> &out) {
   if (in.order() != 3 || out.order() != 3)
     eblerror("expected 3d idx but got " << in << " and " << out);
   int d0 = 1;
   int d1 = d0 + 1;
   intg ci = in.dim(d0) - out.dim(d0);
   intg cj = in.dim(d1) - out.dim(d1);
   intg xci = (int) (ci / 2);
   intg xcj = (int) (cj / 2);
   idx<T> i = in.narrow(d0, out.dim(d0), xci);
   i = i.narrow(d1, out.dim(d1), xcj);
   idx_copy(i, out);
 }
Exemple #13
0
  //! Return an idx of dimensions Nx2 containing all possible N similar pairs.
  idx<int> make_pairs(idx<int> &labels) {
    // allocate maximum number of pairs
    idx<int> pairs((labels.dim(0) * (labels.dim(0) - 1)) / 2, 2);
    int n = 0, r = 0;
    // for each label, loop over all following labels to find pairs
    for (int i = 0; i < labels.dim(0) - 1; ++i) {
      for (int j = i + 1; j < labels.dim(0); ++j) {
	if (labels.get(i) == labels.get(j)) {
	  r = drand(0.0, 1.0); // randomize distribution as 1st or 2nd element
	  pairs.set(i, n, (r > 0.5) ? 0 : 1);
	  pairs.set(j, n, (r > 0.5) ? 1 : 0);
	  n++;
	}
      }
    }
    pairs.resize(n, pairs.dim(1));
    return pairs;
  }
Exemple #14
0
  bool tracking_thread<Tnet>::get_data(vector<bbox*> &bboxes2,
				       idx<ubyte> &frame2, idx<ubyte> &tpl2) {
    // lock data
    pthread_mutex_lock(&mutex_out);
    // only read data if it has been updated
    if (!out_updated) {
      // unlock data
      pthread_mutex_unlock(&mutex_out);
      return false;
    }
    // clear bboxes
    for (ibox = bboxes2.begin(); ibox != bboxes2.end(); ++ibox) {
      if (*ibox)
	delete *ibox;
    }
    bboxes2.clear();
    // copy bboxes pointers (now responsible for deleting them).
    for (ibox = bboxes.begin(); ibox != bboxes.end(); ++ibox) {
      bboxes2.push_back(*ibox);
    }
    // check frame is correctly allocated, if not, allocate.
    if (frame2.order() != frame.order()) 
      frame2 = idx<ubyte>(frame.get_idxdim());
    else if (frame2.get_idxdim() != frame.get_idxdim())
      frame2.resize(frame.get_idxdim());
    // copy frame
    idx_copy(frame, frame2);    
    // check tpl is correctly allocated, if not, allocate.
    if (tpl2.order() != tpl.order()) 
      tpl2 = idx<ubyte>(tpl.get_idxdim());
    else if (tpl2.get_idxdim() != tpl.get_idxdim())
      tpl2.resize(tpl.get_idxdim());
    // copy tpl
    idx_copy(tpl, tpl2);    
    // reset updated flag
    out_updated = false; 
    // unlock data
    pthread_mutex_unlock(&mutex_out);
    // confirm that we copied data.
    return true;
  }
Exemple #15
0
bool detection_thread<T>::get_data(bboxes &bboxes2, idx<ubyte> &frame2,
                                   uint &total_saved_, std::string &frame_name_,
                                   uint *id, svector<midx<T> > *samples,
                                   bboxes *bbsamples, bool *skipped) {
  // lock data
  mutex_out.lock();
  // only read data if it has been updated
  if (!out_updated) {
    // unlock data
    mutex_out.unlock();
    return false;
  }
  // data is updated, but just to tell we skipped the frame
  if (frame_skipped) {
    if (skipped) *skipped = true;
    frame_skipped = false;
    // reset updated flag
    out_updated = false;
    // declare thread as available
    bavailable = true;
    // unlock data
    mutex_out.unlock();
    return false;
  }
  if (skipped) *skipped = false;
  // clear bboxes
  bboxes2.clear();
  bboxes2.push_back_new(bbs);
  bbs.clear(); // no use for local bounding boxes anymore, clear them
  // check frame is correctly allocated, if not, allocate.
  if (frame2.order() != uframe.order())
    frame2 = idx<ubyte>(uframe.get_idxdim());
  else if (frame2.get_idxdim() != uframe.get_idxdim())
    frame2.resize(uframe.get_idxdim());
  // copy frame
  idx_copy(uframe, frame2);
  // set total of boxes saved
  total_saved_ = total_saved;
  // set frame name
  frame_name_ = frame_name;
  // set frame id
  if (id) *id = frame_id;
  // overwrite samples
  if (samples) {
    samples->clear();
    samples->push_back_new(returned_samples);
    returned_samples.clear();
  }
  if (bbsamples) {
    bbsamples->clear();
    bbsamples->push_back_new(returned_samples_bboxes);
    returned_samples_bboxes.clear();
  }
  // reset updated flag
  out_updated = false;
  // declare thread as available
  bavailable = true;
  // unlock data
  mutex_out.unlock();
  // confirm that we copied data.
  return true;
}
Exemple #16
0
 void read_cast_matrix(FILE *fp, idx<T2> &out) {
   idx<T> m(out.get_idxdim());
   read_matrix_body(fp, m);
   idx_copy(m, out);
 }
Exemple #17
0
void padder<T>::mirror(idx<T> &in, idx<T> &padded)
{
    idx<T> tmp, tmp2;
    int i;
    // mirror border left
    for (i = std::max(0, (int)(ncol - in.dim(1) / 2)); i < ncol; ++i)
    {
        tmp2 = in.narrow(1, 1, ncol - i - 1);
        tmp = padded.narrow(1, 1, i);
        tmp = tmp.narrow(2, in.dim(2), ncol);
        idx_copy(tmp2, tmp);
    }

    // mirror border right
    for (i = std::max(0, (int)(ncol - in.dim(1) / 2)); i < ncol; ++i)
    {
        tmp2 = in.narrow(1, 1, in.dim(1) - ncol - 1 + i);
        tmp = padded.narrow(1, 1, padded.dim(1) - 1 - i);
        tmp = tmp.narrow(2, in.dim(2), ncol);
        idx_copy(tmp2, tmp);
    }

    // mirror border top using out as input
    for (i = std::max(0, (int)(nrow - in.dim(2) / 2)); i < nrow; ++i)
    {
        tmp2 = padded.narrow(2, 1, nrow + nrow - i - 1);
        tmp = padded.narrow(2, 1, i);
        idx_copy(tmp2, tmp);
    }

    // mirror border bottom using out as input
    for (i = std::max(0, (int)(nrow - in.dim(2) / 2)); i < nrow; ++i)
    {
        tmp2 = padded.narrow(2, 1, padded.dim(2) - nrow * 2 - 1 + i);
        tmp = padded.narrow(2, 1, padded.dim(2) - 1 - i);
        idx_copy(tmp2, tmp);
    }
}
Exemple #18
0
 cost_module<T1,T2,Tstate1,Tstate2>::cost_module(idx<T1> &targets_)
   : targets(targets_), in2(targets.select(0, 0)), energies(targets_.dim(0)) {
 }
Exemple #19
0
  // convert the input image <src> into the <channels_mode> format.
  void convert_image(idx<float> &src, idx<float> &dst, const int channels_mode,
		     unsigned int fkernel_size) {
    idxdim d = idxdim(src), d2 = idxdim(src);
    d2.setdim(2, dst.dim(2));
    idx<float> in, out, src2(d), tmp(d2), tmp2;
    if (fkernel_size > 0) {
      src2 = src.narrow(0, src.dim(0) - fkernel_size + 1,
			floor(fkernel_size / 2));
      src2 = src2.narrow(1, src.dim(1) - fkernel_size + 1,
			 floor(fkernel_size / 2));
      tmp2 = tmp.narrow(0, tmp.dim(0) - fkernel_size + 1,
			floor(fkernel_size / 2));
      tmp2 = tmp2.narrow(1, tmp.dim(1) - fkernel_size + 1,
			 floor(fkernel_size / 2));
    }
    idx_clear(dst);
    // narrow dst to fit src if smaller
    if (src2.dim(0) < dst.dim(0))
      dst = dst.narrow(0, src2.dim(0), floor((dst.dim(0) - src2.dim(0)) / 2));
    if (src2.dim(1) < dst.dim(1))
      dst = dst.narrow(1, src2.dim(1), floor((dst.dim(1) - src2.dim(1)) / 2));
    // converting
    switch (channels_mode) {
    case 0: // RGB
      idx_copy(src2, dst);
      image_global_normalization(dst);
      break ;
    case 1: // YUV
      rgb_to_yuv(src, tmp);
      in = tmp.select(2, 0);
      d = idxdim(in);
      out = idx<float>(d);      
      image_mexican_filter(in, out, 6, fkernel_size);
      idx_copy(out, in);
      idx_copy(tmp2, dst);
      image_global_normalization(dst);      
      break ;
    case 3: { // Y only 
      rgb_to_y(src, tmp);
      in = tmp.select(2, 0);
      d = idxdim(in);
      out = idx<float>(d);      
      image_global_normalization(in);
      image_local_normalization(in, out, fkernel_size);      
      idx_copy(out, in);
      idx_copy(tmp2, dst);
    }
      break ;
    case 4: // YH3
      rgb_to_yh3(src, tmp);
      in = tmp.select(2, 0);
      d = idxdim(in);
      out = idx<float>(d);      
      image_mexican_filter(in, out, 6, fkernel_size);
      image_global_normalization(out);
      idx_copy(out, in);
      idx_copy(tmp2, dst);
      break ;
    case 5: // VpH2SV
      rgb_to_vph2sv(src, tmp, 6, fkernel_size);
      idx_copy(tmp2, dst);
      break ;
    default:
      cerr << "unknown channel mode: " << channels_mode << endl;
      eblerror("unknown channel mode");
    }
  }
Exemple #20
0
int display(list<string>::iterator &ifname,
	    bool signd, bool load, list<string> *mats) {
  //cout << "displaying " << ifname->c_str() << endl;
  // conf mode
  if (conf)
    return display_net<T>(ifname, signd, load, mats);
  // mat mode
  if (is_matrix(ifname->c_str())) {
    idxdim d = get_matrix_dims(ifname->c_str());
    if (interleaved)
      d.shift_dim(0, 2);
    if (save_individually || print
        || !(d.order() == 2  || (d.order() == 3 &&
                                 (d.dim(2) == 1 || d.dim(2) == 3)))) {
      // this is probably not an image, just display info and print matrix
      string type;
      get_matrix_type(ifname->c_str(), type);
      idx<T> m = load_matrix<T>(ifname->c_str());
      cout << "Matrix " << ifname->c_str() << " is of type " << type
	   << " with dimensions " << d << " (min " << idx_min(m)
	   << ", max " << idx_max(m) << ", mean " << idx_mean(m)
	   << "):" << endl;
      m.print();
      if (has_multiple_matrices(ifname->c_str())) {
	midx<T> ms = load_matrices<T>(ifname->c_str(), true);
	// saving sub-matrices
	if (save_individually) {
	  cout << "Saving each sub-matrix of " << *ifname << " individually..."
	       << endl;
	  save_matrices_individually(ms, *ifname, true);
	}
	// printing sub-matrices
	cout << "This file contains " << m << " matrices: ";
	if (ms.order() == 1) {
	  for (intg i = 0; i < ms.dim(0); ++i) {
	    idx<T> tmp = ms.mget(i);
	    cout << tmp.info() << " ";
	  }
	} else if (ms.order() == 2) {
	  for (intg i = 0; i < ms.dim(0); ++i) {
	    for (intg j = 0; j < ms.dim(1); ++j) {
	      idx<T> tmp = ms.mget(i, j);
	      cout << tmp.info() << " ";
	    }
	    cout << endl;
	  }
	}
	cout << endl;
      } else
	cout << "This is a single-matrix file." << endl;
      return 0;
    }
  }
  // image mode
  int loaded = 0;
  static idx<T> mat;
  uint h = 0, w = 0, rowh = 0, maxh = 0;
  list<string>::iterator fname = ifname;
#ifdef __GUI__
  disable_window_updates();
  clear_window();
  if (show_filename) {
    gui << at(h, w) << black_on_white() << ifname->c_str();
    h += 16;
  }
#endif
  maxh = h;
  for (uint i = 0; i < nh; ++i) {
    rowh = maxh;
    for (uint j = 0; j < nw; ++j) {
      if (fname == mats->end())
	fname = mats->begin();
      try {
	//      if (load)
	mat = load_image<T>(*fname);
	if (print)
	  cout << *fname << ": " << mat << endl << mat.str() << endl;
	// show only some channels
	if (chans >= 0)
	  mat = mat.select(2, chans);
	loaded++;
	maxh = (std::max)(maxh, (uint) (rowh + mat.dim(0)));
	T min = 0, max = 0;
#ifdef __GUI__
	if (autorange || signd) {
	  if (autorange) {
	    min = idx_min(mat);
	    max = idx_max(mat);
	  } else if (signd) {
	    T matmin = idx_min(mat);
	    if ((double)matmin < 0) {
	      min = -1;
	      max = -1;
	    }
	  }
	  draw_matrix(mat, rowh, w, zoom, zoom, min, max);
	} else
	  draw_matrix(mat, rowh, w, zoom, zoom, (T) range[0], (T) range[1]);
#endif
	w += mat.dim(1) + 1;
      } catch(string &err) {
	ERROR_MSG(err.c_str());
      }
      fname++;
      if (fname == ifname)
	break ;
    }
    if (fname == ifname)
      break ;
    maxh++;
    w = 0;
  }
#ifdef __GUI__
  // info
  if (show_info) {
    set_text_colors(0, 0, 0, 255, 255, 255, 255, 200);
    gui << mat;
    gui << at(15, 0) << *fname;
    gui << at(29, 0) << "min: " << idx_min(mat) << " max: " << idx_max(mat);
  }
  // help
  if (show_help) {
    h = 0;
    w = 0;
    uint hstep = 14;
    set_text_colors(0, 0, 255, 255, 255, 255, 255, 200);
    gui << at(h, w) << "Controls:"; h += hstep;
    set_text_colors(0, 0, 0, 255, 255, 255, 255, 200);
    gui << at(h, w) << "Right/Space: next image"; h += hstep;
    gui << at(h, w) << "Left/Backspace: previous image"; h += hstep;
    gui << at(h, w) << "i: image info"; h += hstep;
    gui << at(h, w) << "a: auto-range (use min and max as range)"; h += hstep;
    gui << at(h, w) << "x/z: show more/less images on width axis"; h += hstep;
    gui << at(h, w) << "y/t: show more/less images on height axis"; h += hstep;
    gui << at(h, w) << "0,1,2: show channel 0, 1 or 2 only"; h += hstep;
    gui << at(h, w) << "9: show alls channels"; h += hstep;
    gui << at(h, w) << "h: help";
  }
  // update title
  string title;
  title << "matshow: " << ebl::basename(ifname->c_str());
  set_window_title(title.c_str());
  enable_window_updates();
#endif
  return loaded;
}
Exemple #21
0
  generic_conv_net(parameter &trainableParam, 
		   intg output_size) 
    : layers_n<state_idx>(true) { // owns modules  
    cout << "Initializing ConvNet..." << endl;
    
    //! Define the number of feature maps per layer (C0, C1, C2)
    intg featureMaps0 = 6;
    intg featureMaps1 = 12;
    intg featureMaps2 = 40;

    //! Define tables of connections between layers.
    //! These two are fully connected layer, i.e. each feature map in a layer
    //! is connected to every feature map in the previous layer
    table0 = full_table(1, featureMaps0); //! from input to C0
    table2 = full_table(featureMaps1, featureMaps2); //! from S1 to C2

    //! ... whereas the connections there are sparse (S0 to C1):
    table1 = idx<intg>(44, 2); //! from S0 to C1
    intg tbl[44][2] =
      {{0, 0},  {1, 0},  {2, 0}, //! 0,1,2 in S0 connected to 0 in C1
       {1, 1},  {2, 1},  {3, 1}, //! and so on...
       {2, 2},  {3, 2},  {4, 2},
       {3, 3},  {4, 3},  {5, 3},
       {4, 4},  {5, 4},  {0, 4},
       {5, 5},  {0, 5},  {1, 5},

       {0, 6},  {1, 6},  {2, 6},  {3, 6},
       {1, 7},  {2, 7},  {3, 7},  {4, 7},
       {2, 8},  {3, 8},  {4, 8},  {5, 8},
       {3, 9},  {4, 9},  {5, 9},  {0, 9},
       {4, 10}, {5, 10}, {0, 10}, {1, 10},

       {0, 11}, {1, 11}, {2, 11}, {3, 11}, {4, 11}, {5, 11}};
    memcpy(table1.idx_ptr(), tbl, table1.nelements() * sizeof (intg));


    //! Finally we initialize the architecture of the ConvNet.
    //! In this case we create a CSCSCF network.
    //! It's easy to change the architecture, by simply removing/adding a call
    //! to addModule(...)

    //! C0 Layer
    add_module(new nn_layer_convolution(trainableParam, //! Shared weights
					7, 7, //! Dim of kernel 
					1, 1, //! size of subsampling
					table0), //! Conx btwn input layer and C0 
	       //! state_idx holds the feature maps of C0
	       new state_idx(featureMaps0,1,1));
    //! S0 Layer
    add_module(new nn_layer_subsampling(trainableParam, 
					2, 2,  //! Dim of stride
					2, 2,  //! Dim of subsampling mask
					featureMaps0),
	       new state_idx(featureMaps0,1,1));
    //! C1 Layer
    add_module(new nn_layer_convolution(trainableParam, 
					7, 7,
					1, 1, 
					table1), 
	       new state_idx(featureMaps1,1,1));
    //! S1 Layer
    add_module(new nn_layer_subsampling(trainableParam, 
					2, 2, 
					2, 2, 
					featureMaps1),
	       new state_idx(featureMaps1,1,1));
    //! C2 Layer
    add_module(new nn_layer_convolution(trainableParam, 
					7, 7,
					1, 1, 
					table2), 
	       new state_idx(featureMaps2,1,1));
    //! F Layer
    add_last_module(new nn_layer_full(trainableParam, 
				      featureMaps2, 
				      output_size));

  }
Exemple #22
0
  //! Recursively goes through dir, looking for files matching extension ext.
  void process_dir(const char *dir, const char *ext, const char* leftp, 
		   const char *rightp, unsigned int width,
		   unsigned int fwidth, idx<float> &images,
		   idx<int> &labels, int label, bool silent, bool display,
		   bool *binocular, 
		   const int channels_mode, const int channels_size,
		   idx<ubyte> &classes, unsigned int &counter,
		   idx<unsigned int> &counters_used, idx<int> &ds_assignment,
		   unsigned int fkernel_size, int deformations,
		   idx<int> &deformid, int &ndefid,
		   idx<ubyte> &ds_names) {
    regex eExt(ext);
    string el(".*");
    idx<float> limg(1, 1, 1);
    idx<float> rimg(1, 1, 1);
    idx<float> tmp, left_images;
    idx<int> current_labels;
    idx<int> current_deformid;
    idx<float> tmp2;
    int current_ds;
    unsigned int current_used;
    if (leftp) {
      el += leftp;
      el += ".*";
    }
    regex eLeft(el);
    cmatch what;
    path p(dir);
    if (!exists(p))
      return ;
    directory_iterator end_itr; // default construction yields past-the-end
    for (directory_iterator itr(p); itr != end_itr; ++itr) {
      if (is_directory(itr->status())) {
	process_dir(itr->path().string().c_str(), ext, leftp, rightp, width,
		    fwidth, images, labels, label, silent, display, binocular,
		    channels_mode, channels_size, classes, counter,
		    counters_used, ds_assignment, fkernel_size, deformations,
		    deformid, ndefid, ds_names);
      } else if (regex_match(itr->leaf().c_str(), what, eExt)) {
	if (regex_match(itr->leaf().c_str(), what, eLeft)) {
	  current_ds = ds_assignment.get(counter);
	  if (current_ds != -1) {
	    current_used = counters_used.get(current_ds);
	    // found left image
	    // increase example number
	    labels.set(label, current_ds, counters_used.get(current_ds));
	    // check for right image
	    if (rightp != NULL) {
	      regex reg(leftp);
	      string rp(rightp);
	      string s = regex_replace(itr->leaf(), reg, rp);
	      string sfull = itr->path().branch_path().string();
	      sfull += "/";
	      sfull += s;
	      path r(sfull);
	      if (exists(r)) {
		// found right image
		*binocular = true;
		if (image_read_rgbx(r.string().c_str(), rimg)) {
		  // resize stereo dimension to twice the channels_size
		  if (images.dim(3) == channels_size)
		    images.resize(images.dim(0), images.dim(1), 
				  images.dim(2), images.dim(3) * 2);
		  // take the right most square of the image
		  if (rimg.dim(0) <= rimg.dim(1))
		    rimg = rimg.narrow(1, rimg.dim(0),
				       rimg.dim(1) - rimg.dim(0));
		  else
		    rimg = rimg.narrow(0, rimg.dim(1),
				       rimg.dim(0) - rimg.dim(1));
		  // resize image to target width
		  rimg = image_resize(rimg, width, width, 1);
		  tmp = images[current_ds];
		  tmp = tmp[counters_used.get(current_ds)];
		  tmp = tmp.narrow(2, channels_size, channels_size);
		  // finally copy right images to idx
		  convert_image(rimg, tmp, channels_mode, fkernel_size);
		}
		if (!silent)
		  cout << "Processing (right): " << sfull << endl;
	      }
	    }
	    if (!silent) {
	      cout << counter << "/" << ds_assignment.dim(0) << ": ";
	      cout << itr->path().string().c_str() << endl;
	    }
	    // process left image
	    if (image_read_rgbx(itr->path().string().c_str(), limg)) {
	      // take the left most square of the image
	      int h = limg.dim(0), w = limg.dim(1), newh, neww;
	      if (h > w) {
		newh = width;
		neww = (newh / (float) h) * w;
	      } else {
		neww = width;
		newh = (neww / (float) w) * h;
	      }
	      // resize image to target width
	      limg = image_resize(limg, neww, newh, 1);
	      left_images = images[current_ds];
	      left_images = left_images.narrow(3, channels_size, 0);
	      left_images = left_images.narrow(0, 1 + (deformations<=0?0:
						       deformations),
					       current_used);
	      current_labels = labels[current_ds];
	      current_labels = current_labels.narrow(0, current_labels.dim(0) -
						     current_used,current_used);
	      current_deformid = deformid[current_ds];
	      current_deformid = current_deformid.
		narrow(0, current_deformid.dim(0) - current_used, current_used);
	      tmp = left_images[0];
	      // convert and copy image into images buffer
	      convert_image(limg, tmp, channels_mode, fkernel_size);
	      // if adding to dataset 0, add deformations if deformations > 0
	      if ((current_ds == 0) && (deformations > 0)) {
		add_deformations(left_images, current_labels, label,
				 deformations, current_deformid, ndefid);
		counters_used.set(counters_used.get(current_ds) + deformations,
				  current_ds);
	      } else // no deformations
		left_images = left_images.narrow(0, 1, 0);
	      // display
	      if (display) display_images(classes, label, limg, left_images,
					  channels_mode, ds_names, current_ds);
	    // increment counter for dataset current_ds
	    counters_used.set(counters_used.get(current_ds) + 1, current_ds);
	    }
	  }
	  counter++;
	}
      }
    }
  }
Exemple #23
0
void class_answer<T, Tds1, Tds2>::fprop1(idx<T> &in, idx<T> &out)
{
    // resize out if necessary
    idxdim d(in);

    d.setdim(0, 2); // 2 outputs per pixel: class,confidence
    idx<T> outx = out;
    idx<T> inx = in;
    if (resize_output)
    {
        if (d != out.get_idxdim())
        {
            out.resize(d);
            outx = out;
        }
    }
    else   // if not resizing, narrow to the number of targets
    {
        if (outx.dim(0) != targets.dim(0))
            outx = outx.narrow(0, targets.dim(0), 0);
    }
    // apply tanh if required
    if (apply_tanh)
    {
        mtanh.fprop1(in, tmp);
        inx = tmp;
    }
    // loop on features (dimension 0) to set class and confidence
    int classid;
    T conf, max2 = 0;
    idx_1loop2(ii, inx, T, oo, outx, T, {
                   if (binary_target)
                   {
                       T t0 = targets.gget(0);
                       T t1 = targets.gget(1);
                       T a = ii.gget();
                       if (std::fabs((double)a - t0) < std::fabs((double)a - t1))
                       {
                           oo.set((T)0, 0); // class 0
                           oo.set((T)(2 - std::fabs((double)a - t0)) / 2, 1); // conf
                       }
                       else
                       {
                           oo.set((T)1, 0); // class 1
                           oo.set((T)(2 - std::fabs((double)a - t1)) / 2, 1); // conf
                       }
                   }
                   else if (single_output >= 0)
                   {
                       oo.set((T)single_output, 0); // all answers are the same class
                       oo.set((T)((ii.get(single_output) - target_min) / target_range), 1);
                   }
                   else // 1-of-n target
                   { // set class answer
                       if (force_class >= 0) classid = force_class;
                       else classid = idx_indexmax(ii);
                       oo.set((T)classid, 0);
                       // set confidence
                       intg p;
                       bool ini = false;
                       switch (conf_type)
                       {
                       case confidence_sqrdist: // squared distance to target
                           target = targets.select(0, classid);
                           conf = (T)(1.0 - ((idx_sqrdist(target, ii) - conf_shift)
                                             / conf_ratio));
                           oo.set(conf, 1);
                           break;
                       case confidence_single: // simply return class' out (normalized)
                           conf = (T)((ii.get(classid) - conf_shift) / conf_ratio);
                           oo.set(conf, 1);
                           break;
                       case confidence_max: // distance with 2nd max answer
                           conf = std::max(target_min, std::min(target_max, ii.get(classid)));
                           for (p = 0; p < ii.dim(0); ++p)
                           {
                               if (p != classid)
                               {
                                   if (!ini)
                                   {
                                       max2 = ii.get(p);
                                       ini = true;
                                   }
                                   else
                                   {
                                       if (ii.get(p) > max2)
                                           max2 = ii.get(p);
                                   }
                               }
                           }

                           max2 = std::max(target_min, std::min(target_max, max2));
                           oo.set((T)(((conf - max2) - conf_shift) / conf_ratio), 1);
                           break;
                       default:
                           eblerror("confidence type " << conf_type << " undefined");
                       }
                   }
               });