Пример #1
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;
  }
Пример #2
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);
 }
Пример #3
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;
}
Пример #4
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");
                       }
                   }
               });
Пример #5
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++;
	}
      }
    }
  }