void Reconstruction::ReconstructPlanarUnconstrIter(const mat& matchesInit, LaplacianMesh& resMesh, uvec& inlierMatchIdxs)
{
  // Input check
  if (matchesInit.n_rows == 0) 
  {
    inlierMatchIdxs.resize(0);
    return;
  }

  Timer timer;

  double wr     = this->wrInit;      // Currently used regularization weight 
  double radius = this->radiusInit;  // Currently used radius of the estimator
  vec    reprojErrors;               // Reprojection errors

  // First, we need to build the correspondent matrix with all given matches to avoid re-computation
  this->buildCorrespondenceMatrix(matchesInit);

  // Then compute MPinit. Function reconstructPlanarUnconstr() will use part of MPinit w.r.t currently used matches
  this->MPinit = this->Minit * this->refMesh.GetBigParamMat();

  uvec matchesInitIdxs = linspace<uvec>(0, matchesInit.n_rows-1, matchesInit.n_rows);

  // Currently used matches represented by their indices. Initially, use all matches: [0,1,2..n-1]
  inlierMatchIdxs = matchesInitIdxs;
  
  for (int i = 0; i < nUncstrIters; i++)
  {
    this->reconstructPlanarUnconstr(inlierMatchIdxs, wr, resMesh);
    
    // If it is the final iteration, break and don't update "inlierMatchIdxs" or "weights", "radius"
    if (i == nUncstrIters - 1) 
    {
      //cout << "Current radius: " << radius << endl;
      //cout << "Current wr: " << wr << endl;
      //Reconstruction::computeCurrentMatrices(currentMatchIdxs, 325);  // For Ferns
      break;
    }

    // Otherwise, remove outliers
    int iterTO = nUncstrIters - 2;
    if (i >= iterTO)
      reprojErrors = this->computeReprojectionErrors(resMesh, matchesInit, matchesInitIdxs);
    else
      reprojErrors = this->computeReprojectionErrors(resMesh, matchesInit, inlierMatchIdxs);

    uvec idxs = find( reprojErrors < radius );
    if (idxs.n_elem == 0)
      break;

    if (i >= iterTO)
      inlierMatchIdxs = matchesInitIdxs.elem(idxs);
    else
      inlierMatchIdxs = inlierMatchIdxs.elem(idxs);

    // Update parameters
    wr     = wr     / Reconstruction::ROBUST_SCALE;
    radius = radius / Reconstruction::ROBUST_SCALE;
  }
}
예제 #2
0
파일: PGS.cpp 프로젝트: YinanZheng/PGS
//-----------------------------------------------------
// Cross-validation across an array of lambda and pick up the best.
List CV_lam_grid_cpp(vec y_vect, mat x_mat, vec id_vect, mat hat_R_full, vec beta_ini, int fold, int n, vec m, int obs_n, int p, uvec start, uvec end, vec lam_vect, double eps_tozero, double eps_stop, int iter_try){
  
  int lam_length = lam_vect.n_elem;
  double lam_temp, cv_sum, flag_stop_sum, iter_n_sum, cv_min = math::inf(), lam_min = -1;
  uvec cvgrps_seq = linspace<uvec>(0, (n-1), n); 
  uvec cvgrps_subsets = shuffle(cvgrps_seq);
  uvec cvgrps_which = cvgrps_seq - floor( cvgrps_seq / fold) * fold;
  uvec index_cv_train, index_cv_test; 
  vec cv_vect(lam_length), flag_stop_vect(lam_length), iter_n_vect(lam_length);
  uvec idx_train, idx_test;
  vec y_train, y_test, id_train, m_train, beta_train;
  mat x_train, x_test;
  List indGen_res, beta_shrink_res;
  
  for(int lam_iter = 0; lam_iter < lam_length; lam_iter++)
  {
    lam_temp = lam_vect(lam_iter);
    cv_sum = 0; 
    flag_stop_sum = 0; 
    iter_n_sum = 0;
    
    for(int k = 0; k < fold; k++)
    {
      index_cv_train = cvgrps_subsets.elem(find(cvgrps_which != k));
      index_cv_test = cvgrps_subsets.elem(find(cvgrps_which == k));
      
      idx_train = unique(seqJoin_vec(start.elem(index_cv_train), end.elem(index_cv_train), m.elem(index_cv_train)));
      idx_test = unique(seqJoin_vec(start.elem(index_cv_test), end.elem(index_cv_test), m.elem(index_cv_test)));
      
      y_train = y_vect.elem(idx_train);
      x_train = x_mat.rows(idx_train);
      id_train = id_vect.elem(idx_train);
      indGen_res = indGen_cpp(id_train);
      
      y_test = y_vect.elem(idx_test);
      x_test = x_mat.rows(idx_test);
      
      beta_shrink_res = beta_shrink_normal_cpp(y_train, x_train, id_train, hat_R_full, beta_ini, as<int>(indGen_res[0]), as<vec>(indGen_res[1]), as<int>(indGen_res[2]), p, as<uvec>(indGen_res[3]), as<uvec>(indGen_res[4]), as<vec>(indGen_res[5]), as<uvec>(indGen_res[6]), lam_temp, eps_tozero, eps_stop, iter_try);
      cv_sum += sqrt(mean(pow((y_test - x_test * as<vec>(beta_shrink_res[0])),2)));                        
      flag_stop_sum += as<double>(beta_shrink_res[2]);
      iter_n_sum += as<double>(beta_shrink_res[3]);
    }
    
    // Calculate average across k-fold validation
    cv_vect(lam_iter) = cv_sum / fold;
    flag_stop_vect(lam_iter) = flag_stop_sum / fold;
    iter_n_vect(lam_iter) = iter_n_sum / fold;
    
    if(cv_sum < cv_min)
    {
      lam_min = lam_temp;
      cv_min = cv_sum;
    }
  }
  
  return List::create(Named("lam.vect") = lam_vect,
                      Named("cv.vect") = cv_vect,
                      Named("flag_stop_vect") = flag_stop_vect,
                      Named("iter_n_vect") = iter_n_vect,
                      Named("lam.min") = lam_min,
                      Named("cv.min") = cv_min
  );
}