Thursday, 27 August 2015

Deep learning 00--opencv, eigen and softmax regression, part 1--use softmax to clasisify data

    Recently I am studying deep learning algorithms and try to implement some of them, there are many deep learning framework of c++ out there(ex : caffe), why do I try to build them by myself?the reasons push me to implement they are

  1. This is a good way to study the algorithm
  2. Not all of the deep learning library developed by c++ community are easy to build, more precisely, it is a pain to build them on some major platform(ex : windows), c and c++ community do not have a standard build system really is a big problem.

    The first algorithm I am trying to build is softmax regression based on the tutorial of UFLDL and softmax regression with opencv(I implement it with Eigen and opencv rather than matlab build in function).In this post I want to record how do I implement the softmax regression and show some results. I pick Eigen to help me implement the algorithms because it is

  1. Portable and very easy to compile
  2. Api are clean, easy to use and expressive
  3. Performance is quite good
  4. Well maintain, nice document 
  5. Expression template rock 
    Softmax regression is a more general logistic regression, that is, logistic regression can classify two lables only, but sofmax regression can classify more than two labels. If this is the first time you try to implement an algorithm which involve a lot of matrix manipulation, you may overwhelm with the math equations, wondering how to write fast, clean vectorization codes. Following are some steps help me to develop the algorithms I want to share with you, take it as some reference materials but not golden rules.

  1. Find a good matrix library, like Eigen or Armadillo
  2. Study the algorithm carefully, make sure you understand every steps of it
  3. Write down the matrix operations on a white paper, check the dimensions, the operations results of each step is reasonable or not, if you can not persuade yourself this result is meaningful, go back to step 2, do not bet on luck
  4. Implement the algorithms
  5. Run the gradient checking algorithms to check the result, if error, go back to step 2 or step 3
  6.  Run on clean examples like MNIST, those examples already do preprocess for you
  7. If the result are poor, try to tune the parameters or go back to step 2
    Ok, enough of talk, just show you some codes.

using namespace ocv::ml;


using EMat = ocv::ml::softmax<>::EigenMat;

EMat read_data(std::string const &file)
    std::ifstream in(file);
    std::cout<<"read file\n";
        std::cout<<"is open\n";
        EMat output(30, 284);
        for(size_t col = 0; col != output.cols(); ++col){
            for(size_t row = 0; row != output.rows(); ++row){
                in>>output(row, col);

        return output;
        std::cout<<"cannot open file : "<<file<<"\n";

    return softmax<>::EigenMat();

std::vector<int> read_label(std::string const &file)
    std::ifstream in(file);
    std::vector<double> output;
    //not the most efficient way, but easier to write

    return std::vector<int>(output.begin(),


void softmax_test()
    ocv::ml::softmax<> sm;
    auto const TestData =
    auto const TestLabel =
    double correct = 0;
    for(size_t i = 0; i != TestLabel.size(); ++i){
        auto const Result =
                sm.predict(TestData.block(0, i, TestData.rows(),
        if(Result == TestLabel[i]){

    std::cout<<"true positive pro : "<<

   This class use mini-batch to train the data, the results should within 89%~94%.The example use the ocv_libs of v1.1, the test data is located at here(I download from eric yuan).  The test example(softmax_test) is located at here(v1.0).

    Next post of deep learning will talk about the implementation details of this softmax class.

No comments:

Post a Comment