Ejemplo n.º 1
0
double fMat44::operator () (int i, int j) const
{
#ifndef NDEBUG
	if(i < 0 || i >= 4 || j < 0 || j >= 4)
	{
		cerr << "matrix size error at operator ()" << endl;
		return temp;
	}
#endif
	if(i<3 && j<3) return m_mat(i,j);
	else if(i<3 && j==3) return m_vec(i);
	else if(i==3 && j==3) return m_scalar;
	else return temp;
}
Ejemplo n.º 2
0
    void image::resize(int32_t width, int32_t height, bool cover_crop /* = true */)
    {
        if (m_mat.data == nullptr)
            return;

        cv::Size size(width, height);
        if (cover_crop == true)
        {
            cv::Size mat_size(m_mat.cols, m_mat.rows);
            // This code assumes the cover looks like the following
            //  -------------------------
            // |   front   | |   back    |
            // |           | |           |
            // |           | |           |
            // |           | |           |
            // |           | |           |
            // |           | |           |
            // |           | |           |
            //  -------------------------
            //  We want the front

            if (m_mat.cols > m_mat.rows)
            {
                // Using cv::Rect on the stack always results in bogus values, for whatever reason :/
                std::unique_ptr<cv::Rect> region_front(new cv::Rect(0, 0, get_width() / 2, get_height())); 
                cv::Mat cropped_mat;
                m_mat(*region_front).copyTo(cropped_mat);
                

                cv::Size new_size(region_front->width, region_front->height);
                m_mat = cropped_mat;
                resize(width, height, false);
                /*cv::resize(cropped_mat, m_mat, new_size, 0.0, 0.0, cv::INTER_AREA);*/
                
                return;
            }
        }

        if (m_mat.data != nullptr)
            cv::resize(m_mat, m_mat, size, 0.0, 0.0, cv::INTER_AREA);

    }
Ejemplo n.º 3
0
int main(int argc, char **argv){


    Predictor my_predictor;
    std::vector<signed char> categs;

    cv::Mat features;

    assert(argc == 3 || argc == 4 );
    /*training*/
    if(argv[1][0] == 't'){
        std::cout<<"\n~~~~~~~~~~~~~~TRAINING PREDICTOR~~~~~~~~~~~~~\n "<<std::endl;

        DataMaker dm(TRAINING_SET_IMG,TRAINING_SET_IMG_PS);
        if(argc == 3)
            dm.makeData(features,categs);
        else
            dm.makeDataPS(features,categs);
        my_predictor.train(features,categs);
        my_predictor.save(argv[2]);
        std::cout<<"\n~~~~~~~~~~~~~~PREDICTOR TRAINED~~~~~~~~~~~~~\n "<<std::endl;

    }
    else{
        DataMaker dm(TRAINING_SET_IMG,TRAINING_SET_IMG_PS);
        if(argc == 3)
            dm.makeData(features,categs);
        else
            dm.makeDataPS(features,categs);

        my_predictor.loadTrainData(argv[2]);

        std::vector<signed char> pred;
        my_predictor.predict(features,pred);

//'
//'
        int n_false(0);
        cv::Mat m_mat(3,3,CV_32F,cv::Scalar(0));
        std::map<char,int> lut;
        lut['N'] = 0;
        lut['S'] = 1;
        lut['M'] = 2;
        char revLut[3] =  {'N','S','M'};
        std::vector<int> counts(3,0);

        for(unsigned int i = 0; i<pred.size(); i++){
            char p =pred[i];
            char r =categs[i];
            int real = lut[r];
            int pred = lut[p];
            counts[real] +=1;

            if(real != pred){
                n_false++;
            }
            m_mat.at<float>(pred,real) = m_mat.at<float>(pred,real) + 1;
        }
        cv::Mat sum_cols;
        cv::reduce(m_mat, sum_cols, 0, CV_REDUCE_SUM);
        cv::repeat(sum_cols, 3, 1, sum_cols);
        m_mat = 100*m_mat/sum_cols;
        float accur = 1 - ((float) n_false/(float) pred.size());
        std::cout<<"\n~~~~~~~~~~~~~~ASSESSING ACCURACY OF THE PREDICTOR~~~~~~~~~~~~~\n "<<std::endl;
        std::cout<<"ACCURACY = "<<accur<<std::endl;
        std::cout<<"\nTRAINNING CLASS BALANCE:"<<std::endl;

        std::cout.setf(std::ios::fixed);
//        std::cout.precision(1);
        for(unsigned int i = 0; i < counts.size();i++)
            std::cout<<"N(class =="<<revLut[i]<<") "<<counts[i]<<std::endl;



        std::cout<<"\nConfusion Matrix = \n REAL(top) -> PRED(left) \n"<<cv::format(m_mat,"CSV")<<"\n"<<std::endl;

//        std::cout<<cv::format(features,"CSV")<<std::endl;

    }

    return 0;

}
Ejemplo n.º 4
0
  void operator()(const Uint node_idx)
  {
    const Uint nb_moments = 2*m_nb_phases;
    m_rhs.setZero();
    for(Uint alpha = 0; alpha != m_nb_phases; ++alpha) // For all phases
    {
      const Real n_alpha = (*m_concentration_fields[alpha])[node_idx][0];
      const Real vol_alpha = (*m_weighted_volume_fields[alpha])[node_idx][0] / n_alpha;
      for(int ki = 0; ki != nb_moments; ++ki) // For all moments
      {
        const Real k = static_cast<Real>(ki);
        m_mat(ki, alpha) = (1.-k) * pow_int(vol_alpha, ki);
        m_mat(ki, alpha+m_nb_phases) = k * pow_int(vol_alpha, ki-1);
      }
      for(Uint gamma = 0; gamma != m_nb_phases; ++gamma)
      {
        const Real n_gamma = (*m_concentration_fields[gamma])[node_idx][0];
        const Real vol_gamma = (*m_weighted_volume_fields[gamma])[node_idx][0] / n_gamma;
        const Real beta = m_beta->apply(node_idx, alpha, gamma);
//         std::cout << "computing for vol_alpha " << vol_alpha << ", vol_gamma: " << vol_gamma << ", n_alpha " << n_alpha << ", n_gamma " << n_gamma << ", beta " << beta << std::endl;
//         std::cout << "collision value: " << (beta*n_alpha*n_gamma) << std::endl;
        for(int k = 0; k != nb_moments; ++k ) // For all moments
        {
          m_rhs[k] += (0.5*pow_int(vol_alpha + vol_gamma, k ) - pow_int(vol_alpha, k )) * (beta*n_alpha*n_gamma);
        }
        if(is_not_null(m_collision_rate_field))
        {
          const Uint var_idx = alpha*m_nb_phases + gamma;
          cf3_assert(var_idx < m_collision_rate_field->row_size());
          (*m_collision_rate_field)[node_idx][var_idx] = n_alpha*n_gamma*beta;
        }
      }
    }

    Eigen::Map<RealVector> x(&(*m_source_field)[node_idx][0], nb_moments);
    
    // Check for NaN
    for(Uint k = 0; k != nb_moments; ++k)
    {
      if(!std::isfinite(m_rhs[k]))
      {
        x.setZero();
        return;
      }
    }
    
    Eigen::FullPivLU<RealMatrix> lu(m_mat);
    if(!lu.isInvertible())
    {
      Eigen::JacobiSVD<RealMatrix> svd(m_mat, Eigen::ComputeThinU | Eigen::ComputeThinV);
      x = svd.solve(m_rhs);
    }
    else
    {
      x = lu.solve(m_rhs);
    }

//    std::cout << "matrix:\n" << m_mat << std::endl;
//    std::cout << "rhs: " << m_rhs.transpose() << std::endl;
//    std::cout << "x: " << x.transpose() << std::endl;
  }