logistic_regression.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Logistic Regression sample
  2. // AUTHOR: Rahul Kavi rahulkavi[at]live[at]com
  3. #include <iostream>
  4. #include <opencv2/core.hpp>
  5. #include <opencv2/ml.hpp>
  6. #include <opencv2/highgui.hpp>
  7. using namespace std;
  8. using namespace cv;
  9. using namespace cv::ml;
  10. static void showImage(const Mat &data, int columns, const String &name)
  11. {
  12. Mat bigImage;
  13. for(int i = 0; i < data.rows; ++i)
  14. {
  15. bigImage.push_back(data.row(i).reshape(0, columns));
  16. }
  17. imshow(name, bigImage.t());
  18. }
  19. static float calculateAccuracyPercent(const Mat &original, const Mat &predicted)
  20. {
  21. return 100 * (float)countNonZero(original == predicted) / predicted.rows;
  22. }
  23. int main()
  24. {
  25. const String filename = samples::findFile("data01.xml");
  26. cout << "**********************************************************************" << endl;
  27. cout << filename
  28. << " contains digits 0 and 1 of 20 samples each, collected on an Android device" << endl;
  29. cout << "Each of the collected images are of size 28 x 28 re-arranged to 1 x 784 matrix"
  30. << endl;
  31. cout << "**********************************************************************" << endl;
  32. Mat data, labels;
  33. {
  34. cout << "loading the dataset...";
  35. FileStorage f;
  36. if(f.open(filename, FileStorage::READ))
  37. {
  38. f["datamat"] >> data;
  39. f["labelsmat"] >> labels;
  40. f.release();
  41. }
  42. else
  43. {
  44. cerr << "file can not be opened: " << filename << endl;
  45. return 1;
  46. }
  47. data.convertTo(data, CV_32F);
  48. labels.convertTo(labels, CV_32F);
  49. cout << "read " << data.rows << " rows of data" << endl;
  50. }
  51. Mat data_train, data_test;
  52. Mat labels_train, labels_test;
  53. for(int i = 0; i < data.rows; i++)
  54. {
  55. if(i % 2 == 0)
  56. {
  57. data_train.push_back(data.row(i));
  58. labels_train.push_back(labels.row(i));
  59. }
  60. else
  61. {
  62. data_test.push_back(data.row(i));
  63. labels_test.push_back(labels.row(i));
  64. }
  65. }
  66. cout << "training/testing samples count: " << data_train.rows << "/" << data_test.rows << endl;
  67. // display sample image
  68. showImage(data_train, 28, "train data");
  69. showImage(data_test, 28, "test data");
  70. // simple case with batch gradient
  71. cout << "training...";
  72. //! [init]
  73. Ptr<LogisticRegression> lr1 = LogisticRegression::create();
  74. lr1->setLearningRate(0.001);
  75. lr1->setIterations(10);
  76. lr1->setRegularization(LogisticRegression::REG_L2);
  77. lr1->setTrainMethod(LogisticRegression::BATCH);
  78. lr1->setMiniBatchSize(1);
  79. //! [init]
  80. lr1->train(data_train, ROW_SAMPLE, labels_train);
  81. cout << "done!" << endl;
  82. cout << "predicting...";
  83. Mat responses;
  84. lr1->predict(data_test, responses);
  85. cout << "done!" << endl;
  86. // show prediction report
  87. cout << "original vs predicted:" << endl;
  88. labels_test.convertTo(labels_test, CV_32S);
  89. cout << labels_test.t() << endl;
  90. cout << responses.t() << endl;
  91. cout << "accuracy: " << calculateAccuracyPercent(labels_test, responses) << "%" << endl;
  92. // save the classifier
  93. const String saveFilename = "NewLR_Trained.xml";
  94. cout << "saving the classifier to " << saveFilename << endl;
  95. lr1->save(saveFilename);
  96. // load the classifier onto new object
  97. cout << "loading a new classifier from " << saveFilename << endl;
  98. Ptr<LogisticRegression> lr2 = StatModel::load<LogisticRegression>(saveFilename);
  99. // predict using loaded classifier
  100. cout << "predicting the dataset using the loaded classifier...";
  101. Mat responses2;
  102. lr2->predict(data_test, responses2);
  103. cout << "done!" << endl;
  104. // calculate accuracy
  105. cout << labels_test.t() << endl;
  106. cout << responses2.t() << endl;
  107. cout << "accuracy: " << calculateAccuracyPercent(labels_test, responses2) << "%" << endl;
  108. waitKey(0);
  109. return 0;
  110. }