Example #1
0
END_TEST

START_TEST(test_clogger_expanded_macro)
{
    new_output();
    ck_assert_int_eq(output_size(), 0);
    LOG_INFO("This is a log line");
    fflush(output_fd);
    ck_assert_int_gt(output_size(), 0);
}
Example #2
0
END_TEST

START_TEST(test_clogger_log_levels)
{
    new_output();
    clogger_set_level(Warn);
    ck_assert_int_eq(output_size(), 0);
    LOG_INFO("This is a log line");
    fflush(output_fd);
    ck_assert_int_eq(output_size(), 0);
    LOG_WARN("This is a log line");
    fflush(output_fd);
    ck_assert_int_gt(output_size(), 0);
}
Example #3
0
void generate_video_bw(const cv::Mat1b & query,
                       std::string implementation_name,
                       bool crop_img_before,
                       bool show_contour_brighter = false) {
  // init iterator
  VoronoiIterator it;
  it.init(query, implementation_name, crop_img_before);

  // video writer
  // for a weird reason, odd width and height result in an empty video
  cv::Size output_size(it.cols() * 2, it.rows() * 2);
  std::ostringstream out_filename;
  out_filename << implementation_name << "_bw.avi";
  cv::VideoWriter writer(out_filename.str(), codec, 20,
                         output_size, false); // BW
  assert(writer.isOpened());
  cv::Mat1b curr_resized;
  cv::resize((show_contour_brighter ? it.contour_brighter(it.first_img()) : it.first_img()),
             curr_resized, output_size, CV_INTER_NN);
  writer << curr_resized;
  cv::imshow("curr_resized", curr_resized); cv::waitKey(10);

  // loop
  while (!it.has_converged()) {
    it.iter();
    cv::resize((show_contour_brighter ? it.contour_brighter(it.current_skel()) : it.current_skel()),
               curr_resized, output_size, CV_INTER_NN);
    writer << curr_resized;
    cv::imshow("curr_resized", curr_resized); cv::waitKey(10);
  }
  printf("generated '%s' (%i frames, %ix%i)\n",
         out_filename.str().c_str(), it._nframes, output_size.width, output_size.height);
}
Example #4
0
 virtual void _apply(const AzPmat *m_inp, AzPmat *m_out) const {
   m_out->reform(m_inp->rowNum(), m_inp->colNum()/input_size()*output_size()); 
   if      (p.ptyp == AzpPoolingDflt_Avg) upward_avg(m_inp, m_out); 
   else if (p.ptyp == AzpPoolingDflt_Max) apply_max(m_inp, m_out); 
   else if (p.ptyp == AzpPoolingDflt_L2)  apply_l2(m_inp, m_out); 
   else if (p.ptyp == AzpPoolingDflt_None) upward_asis(m_inp, m_out); 
 }
Example #5
0
 virtual void _upward(const AzPmat *m_inp, AzPmat *m_out) {  
   int r_num = m_inp->rowNum(); 
   int c_num = m_inp->colNum()/input_size()*output_size(); 
   m_out->reform(r_num, c_num); 
   if      (p.ptyp == AzpPoolingDflt_Avg) upward_avg(m_inp, m_out); 
   else if (p.ptyp == AzpPoolingDflt_Max) upward_max(m_inp, m_out); 
   else if (p.ptyp == AzpPoolingDflt_L2)  upward_l2(m_inp, m_out); 
   else if (p.ptyp == AzpPoolingDflt_None) upward_asis(m_inp, m_out); 
 }
Example #6
0
 virtual void downward(const AzPmat *m_lossd_after, AzPmat *m_lossd_before) const {
   int r_num = m_lossd_after->rowNum(); 
   int c_num = m_lossd_after->colNum()/output_size()*input_size(); 
   m_lossd_before->reform(r_num, c_num); 
   if      (p.ptyp == AzpPoolingDflt_Avg) downward_avg(m_lossd_after, m_lossd_before); 
   else if (p.ptyp == AzpPoolingDflt_Max) downward_max(m_lossd_after, m_lossd_before); 
   else if (p.ptyp == AzpPoolingDflt_L2)  downward_l2(m_lossd_after, m_lossd_before); 
   else if (p.ptyp == AzpPoolingDflt_None) downward_asis(m_lossd_after, m_lossd_before); 
 }
void VstoneCameraUndistortionParam::copyParams(VstoneCameraUndistortionParam &dst) const
{
	dst.input_size(input_size());
	dst.output_size(output_size());
	dst.center(center());
	dst.r1(r1());
	dst.r2(r2());
	dst.offset_th(offset_th());
}
Example #8
0
std::vector<int64_t> conv_output_size(
    IntList input_size, IntList weight_size,
    IntList padding, IntList stride, IntList dilation)
{
  auto dim = input_size.size();
  std::vector<int64_t> output_size(dim);
  output_size[0] = input_size[input_batch_size_dim];
  output_size[1] = weight_size[weight_output_channels_dim];
  for (size_t d = 2; d < dim; ++d) {
    auto kernel = dilation[d - 2] * (weight_size[d] - 1) + 1;
    output_size[d] = (input_size[d] + (2 * padding[d - 2])
                        - kernel) / stride[d - 2] + 1;
  }
  return output_size;
}
Example #9
0
void generate_video_comparer(const cv::Mat1b & query,
                             bool crop_img_before,
                             int gallerycols,
                             const std::string out_prefix = "comparer_") {
  // init iterators
  static const unsigned int nimpls = 3;
  std::string impls[] = {IMPL_MORPH, IMPL_ZHANG_SUEN_FAST, IMPL_GUO_HALL_FAST};
  cv::Mat3b curr, curr_resized;
  VoronoiIterator its[nimpls];
  for (unsigned int impl_idx = 0; impl_idx < nimpls; ++impl_idx)
    its[impl_idx].init(query, impls[impl_idx], crop_img_before);
  cv::Mat3b first_img_color;
  // first_img_color = its[0].contour_color(its[0].first_img());
  cv::cvtColor(its[0].first_img(), first_img_color, CV_GRAY2BGR);
  int /*cols1 = first_img_color.cols, */ rows1 = first_img_color.rows;
  //cv::Point text_pos(cols1 / 2, rows1 - 10);
  // image_utils::draw_text_centered
  cv::Point text_pos(10, rows1 - 10);
  cv::putText(first_img_color, "query",
              text_pos, CV_FONT_HERSHEY_PLAIN, 1, CV_RGB(0, 255, 0));

  // video writer
  // for a weird reason, odd width and height result in an empty video
  cv::Size output_size
      (gallerycols * 2 * its[0].cols(), ceil(4 / gallerycols) * 2 * its[0].rows());
  unsigned int fps = 5;
  std::ostringstream out_filename;
  out_filename << out_prefix << ".avi";
  cv::VideoWriter writer(out_filename.str(), codec, fps, output_size, true); // color
  assert(writer.isOpened());
  unsigned int n_terminated = 0, iters = 0;
  while(n_terminated < nimpls) {
    ++iters;
    // iterate all implementations
    n_terminated = 0;
    std::vector<cv::Mat3b> curr_imgs;
    curr_imgs.push_back(first_img_color);
    for (unsigned int impl_idx = 0; impl_idx < nimpls; ++impl_idx) {
      if (its[impl_idx].has_converged())
        ++n_terminated;
      else
        its[impl_idx].iter();
      cv::Mat3b curr_img_color = its[impl_idx].contour_color(its[impl_idx].current_skel());
      cv::dilate(curr_img_color, curr_img_color, cv::Mat());
      //image_utils::draw_text_centered
      cv::putText
          (curr_img_color, impls[impl_idx],
           text_pos, CV_FONT_HERSHEY_PLAIN, 1, CV_RGB(0, 255, 0));
      curr_imgs.push_back(curr_img_color);
    } // end for impl_idx
    // now make collage
    paste_images_gallery(curr_imgs, curr, gallerycols, cv::Vec3b(0, 0, 0), true, CV_RGB(255, 255, 255));
    cv::resize(curr, curr_resized, output_size, CV_INTER_NN);
    writer << curr_resized;
    cv::imshow("curr", curr); cv::waitKey(10);
    // http://denislantsman.com/?p=111 - save as png too
    // convert: ffmpeg -i out_%d.png -vcodec png out.avi
    // http://stackoverflow.com/questions/4839303/convert-image-sequence-to-lossless-movie
    std::ostringstream name; name << "comparer_" << std::setprecision(3) << std::setfill('0') << iters << ".png";
    cv::imwrite(name.str(), curr);
  } // end while1
  // add the last image for twp second
  for (unsigned int i = 0; i < 2 * fps; ++i) {
    writer << curr_resized;
    std::ostringstream name; name << "comparer_" << std::setprecision(3) << std::setfill('0') << ++iters << ".png";
    cv::imwrite(name.str(), curr);
  } // end loop i

  std::ostringstream out_filename_lossless;
  out_filename_lossless << out_prefix << "_lossless.avi";
  std::ostringstream command;
  command << "ffmpeg -r " << fps << " -y -i comparer_%d.png "
          << " -vcodec png "
             //<< " -vcodec huffyuv "
             // << " -vcodec mjpeg "
          << out_filename_lossless.str() << "; rm comparer_*.png";
  system(command.str().c_str());

  printf("generated '%s' and '%s' (%i frames, %ix%i)\n",
         out_filename.str().c_str(), out_filename_lossless.str().c_str(),
         iters, output_size.width, output_size.height);
} // end generate_video_comparer();
Example #10
0
 inline void downward_l2(const AzPmat *m_out, AzPmat *m_inp) const {
   app.unpooling_l2(m_inp, m_out, output_size(), &pia2_inp2out, &m_inp_save, &m_out_save); 
 }
Example #11
0
 inline void downward_avg(const AzPmat *m_out, AzPmat *m_inp) const {
   app.unpooling_avg(m_inp, m_out, output_size(), &pia2_inp2out, &pia_out2num); 
 }
int main(int argc, char* argv[]) {

  if(argc != 2) {
    std::cerr << "Usage : " << argv[0] << "<0,1>" <<std::endl;
    std::cerr << "with : " << std::endl;
    std::cerr << "0 : quadratic loss" << std::endl;
    std::cerr << "1 : cross entropy loss" << std::endl;
    return -1;
  }

  bool quadratic_loss = (atoi(argv[1]) == 0);

  srand(time(NULL));

  // We compare our computation of the gradient to 
  // a finite difference approximation
  // The loss is also involved
  std::cout << "---------------------------------" << std::endl;
  std::cout << "Comparing the analytical gradient and numerical approximation " << std::endl;
  auto input = gaml::mlp::input<X>(INPUT_DIM, fillInput);
  auto l1 = gaml::mlp::layer(input, HIDDEN_LAYER_SIZE, gaml::mlp::mlp_sigmoid(), gaml::mlp::mlp_dsigmoid());
  auto l2 = gaml::mlp::layer(l1, HIDDEN_LAYER_SIZE, gaml::mlp::mlp_identity(), gaml::mlp::mlp_didentity());
  auto l3 = gaml::mlp::layer(l2, HIDDEN_LAYER_SIZE, gaml::mlp::mlp_tanh(), gaml::mlp::mlp_dtanh());
  auto l4 = gaml::mlp::layer(l3, OUTPUT_DIM, gaml::mlp::mlp_sigmoid(), gaml::mlp::mlp_dsigmoid());
  auto mlp = gaml::mlp::perceptron(l4, output_of);

  std::cout << "We use the following architecture : " << std::endl;
  std::cout << mlp << std::endl;
  std::cout << "which has a total of " << mlp.psize() << " parameters"<< std::endl;

  gaml::mlp::parameters_type params(mlp.psize());
  gaml::mlp::parameters_type paramsph(mlp.psize());
  gaml::mlp::values_type derivatives(mlp.psize());
  gaml::mlp::values_type forward_sweep(mlp.size());
  X x;

  auto loss_ce = gaml::mlp::loss::CrossEntropy();
  auto loss_quadratic = gaml::mlp::loss::Quadratic();

  auto f = [&mlp, &params] (const typename decltype(mlp)::input_type& x) -> gaml::mlp::values_type {
    auto output = mlp(x, params);
    gaml::mlp::values_type voutput(mlp.output_size());
    fillOutput(voutput.begin(), output);
    return voutput;
  };
  auto df = [&mlp, &forward_sweep, &params] (const typename decltype(mlp)::input_type& x, unsigned int parameter_dim) -> gaml::mlp::values_type {
    return mlp.deriv(x, params, forward_sweep, parameter_dim);
  };

  unsigned int nbtrials = 100;
  unsigned int nbfails = 0;
  std::cout << "I will compare " << nbtrials << " times a numerical approximation and the analytical gradient we compute" << std::endl;


  for(unsigned int t = 0 ; t < nbtrials ; ++t) {
  
    randomize_data(params, -1.0, 1.0);
    randomize_data(x, -1.0, 1.0);

    // Compute the output at params
    auto output = mlp(x, params);
    gaml::mlp::values_type raw_output(OUTPUT_DIM);
    fillOutput(raw_output.begin(), output);
    gaml::mlp::values_type raw_outputph(OUTPUT_DIM);

    // For computing the loss, we need a target
    gaml::mlp::values_type raw_target(OUTPUT_DIM);
    randomize_data(raw_target);

    double norm_dh = 0.0;

    for(unsigned int i = 0 ; i < mlp.psize() ; ++i) {
      // Let us compute params + h*[0 0 0 0 0 0 1 0 0 0 0 0], the 1 at the ith position
      std::copy(params.begin(), params.end(), paramsph.begin());
      double dh = (sqrt(DBL_EPSILON) * paramsph[i]);
      paramsph[i] += dh;
      norm_dh += dh*dh;
      // Compute the output at params + h
      auto outputph = mlp(x, paramsph);
      fillOutput(raw_outputph.begin(), outputph);
      
      // We now compute the approximation of the derivative
      if(quadratic_loss)
	derivatives[i] = (loss_quadratic(raw_target, raw_outputph) - loss_quadratic(raw_target, raw_output))/dh;
      else
	derivatives[i] = (loss_ce(raw_target, raw_outputph) - loss_ce(raw_target, raw_output))/dh;
	
    }
  
    // We now compute the analytical derivatives
    mlp(x, params);
    std::copy(mlp.begin(), mlp.end(), forward_sweep.begin());

    gaml::mlp::values_type our_derivatives(mlp.psize());
    for(unsigned int i = 0 ; i < mlp.psize() ; ++i) {
      if(quadratic_loss)
	our_derivatives[i] = loss_quadratic.deriv(x, raw_target, forward_sweep, f, df, i);
      else
	our_derivatives[i] = loss_ce.deriv(x, raw_target, forward_sweep, f, df, i);
	
    }
  
    // We finally compute the norm of the difference
    double error = 0.0;
    auto diter = derivatives.begin();
    for(auto& ourdi : our_derivatives) {
      error = (ourdi - *diter) * (ourdi - *diter);
      diter++;
    }
    error = sqrt(error);
    std::cout << "Error between the analytical and numerical gradients " << error << " with a step size of " << sqrt(norm_dh) << " in norm" << std::endl;
    if(error > 1e-7) 
      ++nbfails;

    /*
    std::cout << "numerical " << std::endl;
    for(auto & di : derivatives)
      std::cout << di << " ";
    std::cout << std::endl;
    std::cout << "our :" << std::endl;
    for(auto& di : our_derivatives)
      std::cout << di << " ";
    std::cout << std::endl;
    */

  }

  std::cout << nbfails << " / " << nbtrials << " with an error higher than 1e-7" << std::endl;
}
Example #13
0
	ostream& tree_t<T>::write(const T& data, ostream& os) const
	{
		os << setw(output_size())<< data;
		return os;
	}
Example #14
0
            dll::conv_rbm_desc<40, 14, 12, 40, 7, 3, dll::momentum, dll::batch_size<25>>::layer_t,
            dll::mp_3d_layer_desc<40, 8, 10, 2, 2, 1>::layer_t>>::dbn_t dbn_t;

    auto dataset = mnist::read_dataset_direct<std::vector, etl::fast_dyn_matrix<float, 1, 28, 28>>(200);

    REQUIRE(!dataset.training_images.empty());

    mnist::binarize_dataset(dataset);

    auto dbn = std::make_unique<dbn_t>();

    dbn->pretrain(dataset.training_images, 20);

    auto output = dbn->forward_one(dataset.training_images.front());

    REQUIRE(dbn->output_size() == 800);
    REQUIRE(output.size() == 800);

    auto result = dbn->svm_train(dataset.training_images, dataset.training_labels);

    REQUIRE(result);

    auto test_error = dll::test_set(dbn, dataset.training_images, dataset.training_labels, dll::svm_predictor());
    std::cout << "test_error:" << test_error << std::endl;
    REQUIRE(test_error < 0.1);
}

TEST_CASE("conv_dbn/mnist_10", "max_pooling") {
    typedef dll::dbn_desc<
        dll::dbn_layers<
            dll::conv_rbm_desc<1, 28, 28, 40, 9, 8, dll::momentum, dll::batch_size<25>>::layer_t,
    /// \remarks This method is called after construction, at the start of each
    /// unit test.
    virtual void SetUp()
    {
      nom::SearchPath resources;
      std::string ext = ".json";

      // NOM_LOG_TRACE( NOM );

      // VisualUnitTest environment init
      VisualUnitTest::SetUp();

      // nom::init sets the working directory to this executable's directory
      // path; i.e.: build/tests or build/tests/Debug depending on build
      // environment. Everything is relative from here on out.

      // Determine our resources path based on several possible locations --
      // also dependent upon the build environment
      if( resources.load_file( nom::UnitTest::test_set() + ext, "resources" ) == false )
      {
        FAIL()
        << "Could not determine the resource path for "
        << nom::UnitTest::test_set() + ext;
      }

      // Initialize the core of libRocket; these are the core dependencies that
      // libRocket depends on for successful initialization.
      Rocket::Core::FileInterface* fs =
        new nom::RocketFileInterface( resources.path() );

      Rocket::Core::SystemInterface* sys =
        new RocketSDL2SystemInterface();

      if( nom::init_librocket( fs, sys ) == false )
      {
        FAIL()
        << "Could not initialize libRocket.";
      }

      Rocket::Core::RenderInterface* renderer =
        new nom::RocketSDL2RenderInterface( &this->window_ );

      // Initialize libRocket's debugger as early as possible, so we get visual
      // logging
      this->desktop.enable_debugger();
      if( this->desktop.create_context( "default", this->resolution(), renderer ) == false )
      {
        FAIL()
        << "Could not initialize libRocket context.";
      }

      // Maximum debugger height shall be no more than half the size of the
      // context, add menu height of debugger's button bar
      Size2i debugger_dims( 150, (this->desktop.size().h / 2) + 32 );
      this->desktop.set_debugger_size(debugger_dims);

      Size2i output_size( this->resolution().w / this->window_.scale().x,
                          this->resolution().h / this->window_.scale().y );

      EXPECT_EQ( output_size, this->desktop.size() );

      if( this->desktop.load_font( "Delicious-Bold.otf" ) == false )
      {
        FAIL() << "Could not load font file: Delicious-Bold.otf";
      }

      if( this->desktop.load_font( "OpenSans-Regular.ttf" ) == false )
      {
        FAIL() << "Could not load font file: OpenSans-Regular.ttf";
      }

      if( this->desktop.load_font( "OpenSans-Bold.ttf" ) == false )
      {
        FAIL() << "Could not load font file: OpenSans-Bold.ttf";
      }

      // Load custom decorators for nomlib

      Rocket::Core::DecoratorInstancer* decorator0 = new nom::DecoratorInstancerFinalFantasyFrame();
      Rocket::Core::Factory::RegisterDecoratorInstancer("final-fantasy-theme", decorator0 );
      decorator0->RemoveReference();

      DecoratorInstancerSprite* decorator1 = new nom::DecoratorInstancerSprite();
      Rocket::Core::Factory::RegisterDecoratorInstancer("sprite-sheet", decorator1 );
      decorator1->RemoveReference();

      /// Put our event polling within the main event's loop
      this->append_event_callback( [&] ( const Event ev ) { this->desktop.process_event( ev ); } );

      // Register GUI updates onto our main loop (::on_run).
      this->append_update_callback( [&] ( float delta ) { this->desktop.update(); } );

      // Register GUI rendering onto our main loop (::on_run).
      this->append_render_callback( [&] ( const RenderWindow& win ) { this->desktop.draw(); } );
    }