Ejemplo n.º 1
0
sp_mat make_fourier_basis(const Points & points,
                        uint K,double max_freq){
  uint N = points.n_rows;
  mat basis = mat(N,K);

  basis.col(0) = ones<vec>(N);
  basis.col(1) = sin(datum::pi*sum(points,1));
  basis.col(2) = sin(datum::pi*(points.col(0) - points.col(1)));
  for(uint i = 3; i < K; i++){
    vec freq = 2.0*datum::pi * randi<vec>(2, distr_param(1,(uint)max_freq));
    vec flip = randn<vec>(1);
    if(flip(0) > 0.5)
      basis.col(i) = sin(points * freq);
    else
      basis.col(i) = cos(points * freq);
  }

  basis = orth(basis); // Explicitly orthonormalize
  return sp_mat(basis);
}
Ejemplo n.º 2
0
inline
cube
randg(const uword n_rows, const uword n_cols, const uword n_slices, const distr_param& param = distr_param())
  {
  arma_extra_debug_sigprint();
  
  return randg<cube>(n_rows, n_cols, n_slices, param);
  }
Ejemplo n.º 3
0
inline
cube_type
randg(const uword n_rows, const uword n_cols, const uword n_slices, const distr_param& param = distr_param(), const typename arma_Cube_only<cube_type>::result* junk = 0)
  {
  arma_extra_debug_sigprint();
  arma_ignore(junk);
  
  #if defined(ARMA_USE_CXX11)
    {
    cube_type out(n_rows, n_cols, n_slices);
  
    double a;
    double b;
    
    if(param.state == 0)
      {
      a = double(1);
      b = double(1);
      }
    else
    if(param.state == 1)
      {
      a = double(param.a_int);
      b = double(param.b_int);
      }
    else
      {
      a = param.a_double;
      b = param.b_double;
      }
    
    arma_debug_check( ((a <= double(0)) || (b <= double(0))), "randg(): a and b must be greater than zero" );
    
    #if defined(ARMA_USE_EXTERN_CXX11_RNG)
      {
      arma_rng_cxx11_instance.randg_fill(out.memptr(), out.n_elem, a, b);
      }
    #else
      {
      arma_rng_cxx11 local_arma_rng_cxx11_instance;
      
      typedef typename arma_rng_cxx11::seed_type seed_type;
      
      local_arma_rng_cxx11_instance.set_seed( seed_type(arma_rng::randi<seed_type>()) );
      
      local_arma_rng_cxx11_instance.randg_fill(out.memptr(), out.n_elem, a, b);
      }
    #endif
    
    return out;
    }
  #else
    {
    arma_ignore(n_rows);
    arma_ignore(n_cols);
    arma_ignore(n_slices);
    arma_ignore(param);
    
    arma_stop("randg(): C++11 compiler required");
    
    return cube_type();
    }
  #endif
  }
inline
void
BoW::create_vocabulary(int N_cent, const string path_run_folders)
{ 
  
  cout << "Calculating Vocabulary " << endl;
  cout << "# clusters: " << N_cent << endl;
  
  mat uni_features;
  
  for (uword pe=0; pe<peo_train.n_rows; ++pe)    {
    
    mat mat_features_tmp;
    mat mat_features;
    
    for (uword act = 0 ; act < actions.n_rows;  ++act) {
      
      for (uword sc = 1 ; sc <= 4;  ++sc) {
	
	mat mat_features_video_i;
	std::stringstream ssName_feat_video;
	//ssName_feat_video << "./run"<< run <<"/features/train/feat_vec" << peo_train(pe) << "_" << actions(act) << "_d" << sc;
	ssName_feat_video << path_run_folders <<"/features_all_nor/feat_vec_" << peo_train(pe) << "_" << actions(act) << "_d" << sc;
	mat_features_video_i.load( ssName_feat_video.str() );
	
	if ( mat_features_video_i.n_cols>0 )
	{
	  mat_features_tmp	 = join_rows( mat_features_tmp, mat_features_video_i );
	  
	}
	else 
	{
	  cout << "# vectors = 0 in " << ssName_feat_video.str() << endl;
	  
	}
	
      }
      
    }
    
    cout << "mat_features_tmp.n_cols "<< mat_features_tmp.n_cols << endl;   
    const uword N_max = 100000;  // maximum number of vectors per action to create universal GMM
    //const uword N_max = 100000; //???
    if (mat_features_tmp.n_cols > N_max)
    {
      ivec tmp1 = randi( N_max, distr_param(0,mat_features_tmp.n_cols-1) );
      ivec tmp2 = unique(tmp1);
      uvec my_indices = conv_to<uvec>::from(tmp2);
      mat_features = mat_features_tmp.cols(my_indices);  // extract a subset of the columns
      
    }
    else
    {
      mat_features = mat_features_tmp;
      
    }
    
    cout << "mat_features.n_cols "<< mat_features.n_cols << endl;
    
    
    if ( mat_features.n_cols>0 )
    {
      uni_features	 = join_rows( uni_features, mat_features );
      
    }
    else 
    {
      cout << "# vectors = 0 in uni_features" << endl;
      
    }
    
    //uni_features =  join_rows( uni_features, mat_features );
    mat_features_tmp.reset();
    mat_features.reset();
    
    
  }
  
  
  cout << "r&c "<<  uni_features.n_rows << " & " << uni_features.n_cols << endl;
  bool is_finite = uni_features.is_finite();
  
  if (!is_finite )
  {
    cout << "is_finite?? " << is_finite << endl;
    cout << uni_features.n_rows << " " << uni_features.n_cols << endl;
    getchar();
    
  }
  
  

  
  fmat f_uni_features = conv_to< fmat >::from(uni_features);
  //uni_features.reset();
  
  
  cv::Mat featuresUnclustered(f_uni_features.n_cols, dim, CV_32FC1, f_uni_features.memptr() );
  
  //cv::Mat featuresUnclustered( featuresUnclusteredTMP.t() );
  
  int rows = featuresUnclustered.rows;
  int cols = featuresUnclustered.cols;
  
  cout << "OpenCV rows & cols " << rows << " & " << cols << endl;
  //cout << "Press a Key" << endl;
  //getchar();
  
  //cout << f_uni_features.col(1000) << endl;
  //cout << uni_features.col(1000) << endl;
  //cout << featuresUnclustered.row(1000) << endl;
  
  //cout << "Press a Key" << endl;
  //getchar();
  
  
  
  
  
  //Construct BOWKMeansTrainer
  //the number of bags
  int dictionarySize = N_cent;
  
  //define Term Criteria
  cv::TermCriteria tc(CV_TERMCRIT_ITER,100,0.001);
  //retries number
  int retries=1;
  //necessary flags
  int flags=cv::KMEANS_PP_CENTERS;
  //Create the BoW (or BoF) trainer
  cv::BOWKMeansTrainer bowTrainer(dictionarySize,tc,retries,flags);
  //cluster the feature vectors
  cv::Mat dictionary = bowTrainer.cluster(featuresUnclustered);  
  
  //Displaying # of Rows&Cols
  int rows_dic = dictionary.rows;
  int cols_dic = dictionary.cols;
  cout << "OpenCV Dict rows & cols " << rows_dic << " & " << cols_dic << endl;
  
  
  
  
  //store the vocabulary
  std::stringstream name_vocabulary;
  name_vocabulary << "./run"<< run <<"/visual_vocabulary/means_Ng" << N_cent << "_dim" <<dim << "_all_sc" << ".yml"; 
  
  cv::FileStorage fs(name_vocabulary.str(), cv::FileStorage::WRITE);
  fs << "vocabulary" << dictionary;
  fs.release();
  cout << "DONE"<< endl;
  
}
Ejemplo n.º 5
0
arma_warn_unused
inline
obj_type
randg(const uword n_rows, const uword n_cols, const distr_param& param = distr_param(), const typename arma_Mat_Col_Row_only<obj_type>::result* junk = 0)
  {
  arma_extra_debug_sigprint();
  arma_ignore(junk);
  
  #if defined(ARMA_USE_CXX11)
    {
    if(is_Col<obj_type>::value == true)
      {
      arma_debug_check( (n_cols != 1), "randg(): incompatible size" );
      }
    else
    if(is_Row<obj_type>::value == true)
      {
      arma_debug_check( (n_rows != 1), "randg(): incompatible size" );
      }
    
    obj_type out(n_rows, n_cols);
    
    double a;
    double b;
    
    if(param.state == 0)
      {
      a = double(1);
      b = double(1);
      }
    else
    if(param.state == 1)
      {
      a = double(param.a_int);
      b = double(param.b_int);
      }
    else
      {
      a = param.a_double;
      b = param.b_double;
      }
    
    arma_debug_check( ((a <= double(0)) || (b <= double(0))), "randg(): a and b must be greater than zero" );
    
    #if defined(ARMA_USE_EXTERN_CXX11_RNG)
      {
      arma_rng_cxx11_instance.randg_fill(out.memptr(), out.n_elem, a, b);
      }
    #else
      {
      arma_rng_cxx11 local_arma_rng_cxx11_instance;
      
      typedef typename arma_rng_cxx11::seed_type seed_type;
      
      local_arma_rng_cxx11_instance.set_seed( seed_type(arma_rng::randi<seed_type>()) );
      
      local_arma_rng_cxx11_instance.randg_fill(out.memptr(), out.n_elem, a, b);
      }
    #endif
    
    return out;
    }
  #else
    {
    arma_ignore(n_rows);
    arma_ignore(n_cols);
    arma_ignore(param);
    
    arma_stop_logic_error("randg(): C++11 compiler required");
    
    return obj_type();
    }
  #endif
  }
Ejemplo n.º 6
0
 // Random integer in [0,n-1]
 size_t randint(const size_t & n){
   return randi<vec>(1,distr_param(0,static_cast<int>(n-1)))(0);
 }
Ejemplo n.º 7
0
 //! Random integer in [a,b]
 int randint(int a, int b){
   return randi<vec>(1,distr_param(a,b))(0);
 }
Ejemplo n.º 8
0
inline
void
BoW::create_universal_gmm(int N_cent, const string path_run_folders)
{ 
  
  cout << "Calculating Universal GMM " << endl;
  cout << "# clusters: " << N_cent << endl;
  
  mat uni_features;
  
   for (uword pe=0; pe<peo_train.n_rows; ++pe)    {
     
     mat mat_features_tmp;
     mat mat_features;
     
     for (uword act = 0 ; act < actions.n_rows;  ++act) {
       
       for (uword sc = 1 ; sc <= 4;  ++sc) {
	 
       mat mat_features_video_i;
       std::stringstream ssName_feat_video;
       //ssName_feat_video << "./run"<< run <<"/features/train/feat_vec" << peo_train(pe) << "_" << actions(act) << "_d" << sc;
       ssName_feat_video << path_run_folders <<"/features_all_nor/feat_vec_" << peo_train(pe) << "_" << actions(act) << "_d" << sc;
       mat_features_video_i.load( ssName_feat_video.str() );
       
       if ( mat_features_video_i.n_cols>0 )
       {
	 mat_features_tmp	 = join_rows( mat_features_tmp, mat_features_video_i );
	 
      }
      else 
      {
	cout << "# vectors = 0 in " << ssName_feat_video.str() << endl;
	
      }
	 
      }
       
    }
    
    cout << "mat_features_tmp.n_cols "<< mat_features_tmp.n_cols << endl;   
    const uword N_max = 100000*4;  // 4 sc. maximum number of vectors per action to create universal GMM
    //const uword N_max = 100000; //???
    if (mat_features_tmp.n_cols > N_max)
    {
      ivec tmp1 = randi( N_max, distr_param(0,mat_features_tmp.n_cols-1) );
      ivec tmp2 = unique(tmp1);
      uvec my_indices = conv_to<uvec>::from(tmp2);
      mat_features = mat_features_tmp.cols(my_indices);  // extract a subset of the columns
      
    }
    else
    {
      mat_features = mat_features_tmp;
      
    }
    
    cout << "mat_features.n_cols "<< mat_features.n_cols << endl;
    
    
      if ( mat_features.n_cols>0 )
      {
	uni_features	 = join_rows( uni_features, mat_features );
	
      }
      else 
      {
	cout << "# vectors = 0 in uni_features" << endl;
	
      }
    
    //uni_features =  join_rows( uni_features, mat_features );
    mat_features_tmp.reset();
    mat_features.reset();
    
    
  }
  
  
  cout << "r&c "<<  uni_features.n_rows << " & " << uni_features.n_cols << endl;
  bool is_finite = uni_features.is_finite();
 
    if (!is_finite )
    {
      cout << "is_finite?? " << is_finite << endl;
      cout << uni_features.n_rows << " " << uni_features.n_cols << endl;
      getchar();
    
    }
  
  
  cout << "universal GMM" << endl;
  gmm_diag gmm_model;
  gmm_diag bg_model;
  
  
  
  bool status_em = false;
  int rep_em=0;
  
  
  int km_iter = 10;
  double var_floor = 1e-10;
  bool print_mode = true;
  
  
  
  
 bool status_kmeans = false;

 int rep_km = 0;
 
 while (!status_kmeans)
 {
   arma_rng::set_seed_random();
   status_kmeans = gmm_model.learn(uni_features, N_cent, eucl_dist, random_subset, km_iter, 0, var_floor, print_mode);   //Only Kmeans
   bg_model = gmm_model;
   
   rep_km++;
    }
    
 
  cout <<"K-means was repeated " << rep_km << endl;
   
  means = gmm_model.means;
 
  
  //Saving statistics

  std::stringstream ss_means;
  ss_means << "./run"<< run <<"/visual_vocabulary/means_Ng" << N_cent << "_dim" <<dim << "_all_sc" << ".dat"; 
  means.save( ss_means.str(), raw_ascii );

}
Ejemplo n.º 9
0
arma_warn_unused
inline
icube
randi(const uword n_rows, const uword n_cols, const uword n_slices, const distr_param& param = distr_param())
  {
  arma_extra_debug_sigprint();
  
  return randi<icube>(n_rows, n_cols, n_slices, param);
  }
Ejemplo n.º 10
0
arma_warn_unused
inline
cube_type
randi(const uword n_rows, const uword n_cols, const uword n_slices, const distr_param& param = distr_param(), const typename arma_Cube_only<cube_type>::result* junk = 0)
  {
  arma_extra_debug_sigprint();
  arma_ignore(junk);
  
  typedef typename cube_type::elem_type eT;
  
  cube_type out(n_rows, n_cols, n_slices);
  
  int a;
  int b;
  
  if(param.state == 0)
    {
    a = 0;
    b = arma_rng::randi<eT>::max_val();
    }
  else
  if(param.state == 1)
    {
    a = param.a_int;
    b = param.b_int;
    }
  else
    {
    a = int(param.a_double);
    b = int(param.b_double);
    }
  
  arma_debug_check( (a > b), "randi(): incorrect distribution parameters: a must be less than b" );
  
  arma_rng::randi<eT>::fill(out.memptr(), out.n_elem, a, b);
  
  return out;
  }