Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Logistic Regression on MNIST dataset

In this post you can find a very good tutorial on how to apply SVM classifier to MNIST dataset. I was wondering if I could use logistic regression instead of SVM classifier. So I searhed for Logistic regression in openCV, And I found that the syntax for both classifiers are almost identical. So I guessed that I could just comment out these parts:

    cv::Ptr<cv::ml::SVM> svm = cv::ml::SVM::create();
    svm->setType(cv::ml::SVM::C_SVC);
    svm->setKernel(cv::ml::SVM::POLY);//LINEAR, RBF, SIGMOID, POLY 
    svm->setTermCriteria(cv::TermCriteria(cv::TermCriteria::MAX_ITER, 100, 1e-6));
    svm->setGamma(3); 
    svm->setDegree(3);
    svm->train( trainingMat , cv::ml::ROW_SAMPLE , labelsMat );

and replace it with:

    cv::Ptr<cv::ml::LogisticRegression> lr1 = cv::ml::LogisticRegression::create();
    lr1->setLearningRate(0.001);
    lr1->setIterations(10);
    lr1->setRegularization(cv::ml::LogisticRegression::REG_L2);
    lr1->setTrainMethod(cv::ml::LogisticRegression::BATCH);
    lr1->setMiniBatchSize(1);
    lr1->train( trainingMat, cv::ml::ROW_SAMPLE, labelsMat);

But first I got this error: OpenCV Error: Bad argument(data and labels must be a floating point matrix)

Then I changed

cv::Mat labelsMat(labels.size(), 1, CV_32S, labelsArray);

to:

cv::Mat labelsMat(labels.size(), 1, CV_32F, labelsArray);

And now I get this error: OpenCV Error: bad argument(data should have atleast two classes)

I have 10 classes (0,1,...,9) but I don't know why I get this error. My codes are almost identical with the ones in the mentioned tutorial.