1 /*//////////////////////////////////////////////////////////////////////////////////////
2 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
3
4 // By downloading, copying, installing or using the software you agree to this license.
5 // If you do not agree to this license, do not download, install,
6 // copy or use the software.
7
8 // This is a implementation of the Logistic Regression algorithm in C++ in OpenCV.
9
10 // AUTHOR:
11 // Rahul Kavi rahulkavi[at]live[at]com
12 //
13
14 // contains a subset of data from the popular Iris Dataset (taken from
15 // "http://archive.ics.uci.edu/ml/datasets/Iris")
16
17 // # You are free to use, change, or redistribute the code in any way you wish for
18 // # non-commercial purposes, but please maintain the name of the original author.
19 // # This code comes with no warranty of any kind.
20
21 // #
22 // # You are free to use, change, or redistribute the code in any way you wish for
23 // # non-commercial purposes, but please maintain the name of the original author.
24 // # This code comes with no warranty of any kind.
25
26 // # Logistic Regression ALGORITHM
27
28 // License Agreement
29 // For Open Source Computer Vision Library
30
31 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
32 // Copyright (C) 2008-2011, Willow Garage Inc., all rights reserved.
33 // Third party copyrights are property of their respective owners.
34
35 // Redistribution and use in source and binary forms, with or without modification,
36 // are permitted provided that the following conditions are met:
37
38 // * Redistributions of source code must retain the above copyright notice,
39 // this list of conditions and the following disclaimer.
40
41 // * Redistributions in binary form must reproduce the above copyright notice,
42 // this list of conditions and the following disclaimer in the documentation
43 // and/or other materials provided with the distribution.
44
45 // * The name of the copyright holders may not be used to endorse or promote products
46 // derived from this software without specific prior written permission.
47
48 // This software is provided by the copyright holders and contributors "as is" and
49 // any express or implied warranties, including, but not limited to, the implied
50 // warranties of merchantability and fitness for a particular purpose are disclaimed.
51 // In no event shall the Intel Corporation or contributors be liable for any direct,
52 // indirect, incidental, special, exemplary, or consequential damages
53 // (including, but not limited to, procurement of substitute goods or services;
54 // loss of use, data, or profits; or business interruption) however caused
55 // and on any theory of liability, whether in contract, strict liability,
56 // or tort (including negligence or otherwise) arising in any way out of
57 // the use of this software, even if advised of the possibility of such damage.*/
58
59 #include <iostream>
60
61 #include <opencv2/core.hpp>
62 #include <opencv2/ml.hpp>
63 #include <opencv2/highgui.hpp>
64
65 using namespace std;
66 using namespace cv;
67 using namespace cv::ml;
68
showImage(const Mat & data,int columns,const String & name)69 static void showImage(const Mat &data, int columns, const String &name)
70 {
71 Mat bigImage;
72 for(int i = 0; i < data.rows; ++i)
73 {
74 bigImage.push_back(data.row(i).reshape(0, columns));
75 }
76 imshow(name, bigImage.t());
77 }
78
calculateAccuracyPercent(const Mat & original,const Mat & predicted)79 static float calculateAccuracyPercent(const Mat &original, const Mat &predicted)
80 {
81 return 100 * (float)countNonZero(original == predicted) / predicted.rows;
82 }
83
main()84 int main()
85 {
86 const String filename = "../data/data01.xml";
87 cout << "**********************************************************************" << endl;
88 cout << filename
89 << " contains digits 0 and 1 of 20 samples each, collected on an Android device" << endl;
90 cout << "Each of the collected images are of size 28 x 28 re-arranged to 1 x 784 matrix"
91 << endl;
92 cout << "**********************************************************************" << endl;
93
94 Mat data, labels;
95 {
96 cout << "loading the dataset...";
97 FileStorage f;
98 if(f.open(filename, FileStorage::READ))
99 {
100 f["datamat"] >> data;
101 f["labelsmat"] >> labels;
102 f.release();
103 }
104 else
105 {
106 cerr << "file can not be opened: " << filename << endl;
107 return 1;
108 }
109 data.convertTo(data, CV_32F);
110 labels.convertTo(labels, CV_32F);
111 cout << "read " << data.rows << " rows of data" << endl;
112 }
113
114 Mat data_train, data_test;
115 Mat labels_train, labels_test;
116 for(int i = 0; i < data.rows; i++)
117 {
118 if(i % 2 == 0)
119 {
120 data_train.push_back(data.row(i));
121 labels_train.push_back(labels.row(i));
122 }
123 else
124 {
125 data_test.push_back(data.row(i));
126 labels_test.push_back(labels.row(i));
127 }
128 }
129 cout << "training/testing samples count: " << data_train.rows << "/" << data_test.rows << endl;
130
131 // display sample image
132 showImage(data_train, 28, "train data");
133 showImage(data_test, 28, "test data");
134
135 // simple case with batch gradient
136 cout << "training...";
137 //! [init]
138 Ptr<LogisticRegression> lr1 = LogisticRegression::create();
139 lr1->setLearningRate(0.001);
140 lr1->setIterations(10);
141 lr1->setRegularization(LogisticRegression::REG_L2);
142 lr1->setTrainMethod(LogisticRegression::BATCH);
143 lr1->setMiniBatchSize(1);
144 //! [init]
145 lr1->train(data_train, ROW_SAMPLE, labels_train);
146 cout << "done!" << endl;
147
148 cout << "predicting...";
149 Mat responses;
150 lr1->predict(data_test, responses);
151 cout << "done!" << endl;
152
153 // show prediction report
154 cout << "original vs predicted:" << endl;
155 labels_test.convertTo(labels_test, CV_32S);
156 cout << labels_test.t() << endl;
157 cout << responses.t() << endl;
158 cout << "accuracy: " << calculateAccuracyPercent(labels_test, responses) << "%" << endl;
159
160 // save the classfier
161 const String saveFilename = "NewLR_Trained.xml";
162 cout << "saving the classifier to " << saveFilename << endl;
163 lr1->save(saveFilename);
164
165 // load the classifier onto new object
166 cout << "loading a new classifier from " << saveFilename << endl;
167 Ptr<LogisticRegression> lr2 = StatModel::load<LogisticRegression>(saveFilename);
168
169 // predict using loaded classifier
170 cout << "predicting the dataset using the loaded classfier...";
171 Mat responses2;
172 lr2->predict(data_test, responses2);
173 cout << "done!" << endl;
174
175 // calculate accuracy
176 cout << labels_test.t() << endl;
177 cout << responses2.t() << endl;
178 cout << "accuracy: " << calculateAccuracyPercent(labels_test, responses2) << "%" << endl;
179
180 waitKey(0);
181 return 0;
182 }
183