2013-08-05 21:32:39 +08:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// sample_logistic_regression.cpp
|
|
|
|
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
|
|
|
|
|
|
|
// By downloading, copying, installing or using the software you agree to this license.
|
|
|
|
// If you do not agree to this license, do not download, install,
|
|
|
|
// copy or use the software.
|
|
|
|
|
|
|
|
// This is a sample program demostrating classification of digits 0 and 1 using Logistic Regression
|
|
|
|
|
|
|
|
// AUTHOR:
|
|
|
|
// Rahul Kavi rahulkavi[at]live[at]com
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include <opencv2/core/core.hpp>
|
|
|
|
#include <opencv2/ml/ml.hpp>
|
2013-08-08 08:39:46 +08:00
|
|
|
#include <opencv2/highgui/highgui.hpp>
|
|
|
|
|
2013-08-05 21:32:39 +08:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace cv;
|
|
|
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
Mat data_temp, labels_temp;
|
|
|
|
Mat data, labels;
|
2013-08-08 08:39:46 +08:00
|
|
|
|
|
|
|
Mat data_train, data_test;
|
|
|
|
Mat labels_train, labels_test;
|
|
|
|
|
2013-08-05 21:32:39 +08:00
|
|
|
Mat responses, result;
|
|
|
|
|
|
|
|
FileStorage f;
|
|
|
|
|
|
|
|
cout<<"*****************************************************************************************"<<endl;
|
|
|
|
cout<<"\"data01.xml\" contains digits 0 and 1 of 20 samples each, collected on an Android device"<<endl;
|
|
|
|
cout<<"Each of the collected images are of size 28 x 28 re-arranged to 1 x 784 matrix"<<endl;
|
|
|
|
cout<<"*****************************************************************************************\n\n"<<endl;
|
|
|
|
|
|
|
|
cout<<"loading the dataset\n"<<endl;
|
|
|
|
|
|
|
|
f.open("data01.xml", FileStorage::READ);
|
|
|
|
|
|
|
|
f["datamat"] >> data_temp;
|
|
|
|
f["labelsmat"] >> labels_temp;
|
|
|
|
|
|
|
|
data_temp.convertTo(data, CV_32F);
|
|
|
|
labels_temp.convertTo(labels, CV_32F);
|
|
|
|
|
2013-08-08 08:39:46 +08:00
|
|
|
for(int i =0;i<data.rows;i++)
|
|
|
|
{
|
|
|
|
if(i%2 ==0)
|
|
|
|
{
|
|
|
|
data_train.push_back(data.row(i));
|
|
|
|
labels_train.push_back(labels.row(i));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
data_test.push_back(data.row(i));
|
|
|
|
labels_test.push_back(labels.row(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cout<<"training samples per class: "<<data_train.rows/2<<endl;
|
|
|
|
cout<<"testing samples per class: "<<data_test.rows/2<<endl;
|
|
|
|
|
|
|
|
// display sample image
|
|
|
|
Mat img_disp1 = data_train.row(2).reshape(0,28).t();
|
|
|
|
Mat img_disp2 = data_train.row(18).reshape(0,28).t();
|
|
|
|
|
|
|
|
imshow("digit 0", img_disp1);
|
|
|
|
imshow("digit 1", img_disp2);
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-08-05 21:32:39 +08:00
|
|
|
cout<<"initializing Logisitc Regression Parameters\n"<<endl;
|
|
|
|
|
|
|
|
CvLR_TrainParams params = CvLR_TrainParams();
|
|
|
|
|
|
|
|
params.alpha = 0.001;
|
|
|
|
params.num_iters = 10;
|
|
|
|
params.norm = CvLR::REG_L2;
|
|
|
|
params.regularized = 1;
|
|
|
|
params.train_method = CvLR::BATCH;
|
|
|
|
|
|
|
|
cout<<"training Logisitc Regression classifier\n"<<endl;
|
|
|
|
|
2013-08-08 08:39:46 +08:00
|
|
|
CvLR lr_(data_train, labels_train, params);
|
|
|
|
lr_.predict(data_test, responses);
|
|
|
|
labels_test.convertTo(labels_test, CV_32S);
|
|
|
|
|
2013-08-05 21:32:39 +08:00
|
|
|
cout<<"Original Label :: Predicted Label"<<endl;
|
2013-08-08 08:39:46 +08:00
|
|
|
result = (labels_test == responses)/255;
|
|
|
|
|
|
|
|
for(int i=0;i<labels_test.rows;i++)
|
2013-08-05 21:32:39 +08:00
|
|
|
{
|
2013-08-08 08:39:46 +08:00
|
|
|
cout<<labels_test.at<int>(i,0)<<" :: "<< responses.at<int>(i,0)<<endl;
|
2013-08-05 21:32:39 +08:00
|
|
|
}
|
2013-08-08 08:39:46 +08:00
|
|
|
|
2013-08-05 21:32:39 +08:00
|
|
|
// calculate accuracy
|
|
|
|
cout<<"accuracy: "<<((double)cv::sum(result)[0]/result.rows)*100<<"%\n";
|
2013-08-08 08:39:46 +08:00
|
|
|
cout<<"saving the classifier"<<endl;
|
2013-08-05 21:32:39 +08:00
|
|
|
|
|
|
|
// save the classfier
|
|
|
|
lr_.save("NewLR_Trained.xml");
|
|
|
|
|
|
|
|
// load the classifier onto new object
|
|
|
|
CvLR lr2;
|
|
|
|
cout<<"loading a new classifier"<<endl;
|
|
|
|
|
|
|
|
lr2.load("NewLR_Trained.xml");
|
|
|
|
|
|
|
|
Mat responses2;
|
|
|
|
|
|
|
|
// predict using loaded classifier
|
|
|
|
cout<<"predicting the dataset using the loaded classfier\n"<<endl;
|
|
|
|
|
2013-08-08 08:39:46 +08:00
|
|
|
lr2.predict(data_test, responses2);
|
2013-08-05 21:32:39 +08:00
|
|
|
|
|
|
|
// calculate accuracy
|
2013-08-08 08:39:46 +08:00
|
|
|
result = (labels_test == responses2)/255;
|
2013-08-05 21:32:39 +08:00
|
|
|
cout<<"accuracy using loaded classifier: "<<((double)cv::sum(result)[0]/result.rows)*100<<"%\n";
|
2013-08-08 08:39:46 +08:00
|
|
|
waitKey(0);
|
2013-08-05 21:32:39 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|