Main Page · Modules · All Classes · Class Hierarchy
MAModel.hpp
1 /*
2  * This file is part of the AiBO+ project
3  *
4  * Copyright (C) 2005-2016 Csaba Kertész (csaba.kertesz@gmail.com)
5  *
6  * AiBO+ is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * AiBO+ is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
19  *
20  */
21 
22 #pragma once
23 
24 #include "core/MACoreTypes.hpp"
25 #include "MAClassifier.hpp"
26 
27 #include <boost/scoped_ptr.hpp>
28 
29 #include <string>
30 
31 class CvEmWrapper;
32 class CvStatModel;
33 class DlibWrapper;
34 class MAClassifier;
35 class MAModel;
36 
41 namespace MA
42 {
44 typedef boost::shared_ptr<MAModel> ModelSPtr;
45 }
46 
50 class MAModel
51 {
52  friend class MAClassifier;
53 
54  /*
55  * @brief Class constructor
56  *
57  * @param model_str XML string (OpenCV) or serialized (Dlib) representation of the classifier
58  *
59  */
60 
61  explicit MAModel(const std::string& model_str);
62 
63 public:
64 
66  ~MAModel();
67 
68 private:
69 
70  /*
71  * @brief Decode an OpenCV classifier from XML format
72  *
73  * @param model_str XML string representation of the OpenCV classifier
74  *
75  * @return True if the operation is successful otherwise false.
76  *
77  */
78 
79  bool LoadFromOpenCVXmlFormat(const std::string& model_str);
80 
81  /*
82  * @brief Decode a Dlib classifier from serialized format
83  *
84  * @param model_str Serialized representation of a Dlib classifier
85  *
86  * @return True if the operation is successful otherwise false.
87  *
88  */
89 
90  bool LoadFromDlibFormat(const std::string& model_str);
91 
92 public:
93 
94  /*
95  * @brief Get the size of the serialized string size
96  *
97  * @return The size of the serialized string size
98  *
99  */
100 
101  unsigned int GetSerializedStringSize() const;
102 
103  /*
104  * @brief Get the feature count
105  *
106  * @return The feature count in a feature vector
107  *
108  */
109 
110  unsigned int GetFeatureCount() const;
111 
112  /*
113  * @brief Get list of labels in the model
114  *
115  * @return Reference to the label list in the model
116  *
117  */
118 
119  MC::FloatList& GetModelLabels();
120 
121  /*
122  * @brief Predict some samples with labels
123  *
124  * @param input_vectors Input samples for prediction
125  * @param[out] confidences Confidence values for the predictions
126  *
127  * @return Predicted labels
128  *
129  */
130 
131  MC::FloatList Predict(const MC::FloatTable& input_vectors, MC::FloatList& confidences);
132 
133  /*
134  * @brief Predict one sample with a label
135  *
136  * @param input_vector One input sample for prediction
137  * @param[out] confidence Confidence value for the prediction
138  *
139  * @return Predicted label
140  *
141  */
142 
143  float Predict(const MC::FloatList& input_vector, MC::FloatList& confidence);
144 
145  /*
146  * @brief Get the variable importances
147  *
148  * @return Variable importances for decision trees and random forest
149  *
150  */
151 
152  MC::FloatList GetVariableImportances();
153 
154  /*
155  * @brief Encode the model into binary data
156  *
157  * @return Binary data
158  *
159  */
160 
161  MCBinaryData* Encode() const;
162 
163  /*
164  * @brief Export the model into XML format
165  *
166  * @return Binary data
167  *
168  */
169 
170  MCBinaryData* ExportInRawFormat() const;
171 
172  /*
173  * @brief Decode the model from binary data
174  *
175  * @param data Binary data
176  *
177  * @return Decoded model otherwise NULL.
178  *
179  */
180 
181  static MAModel* Decode(const MCBinaryData& data);
182 
183 private:
185  unsigned int SerializedStringSize;
187  MC::FloatList Labels;
189  boost::scoped_ptr<CvStatModel> Model;
191  boost::scoped_ptr<CvEmWrapper> EmModel;
193  boost::scoped_ptr<DlibWrapper> DlibFunctions;
197  MA::FeaturePreprocessingType PreprocessingMode;
201  MA::CRMethodType Classifier;
202 };
203 
boost::scoped_ptr< DlibWrapper > DlibFunctions
Wrapper for Dlib classifier/regression functions.
Definition: MAModel.hpp:193
unsigned int SerializedStringSize
Serialized string size.
Definition: MAModel.hpp:185
MA::CRMethodType Classifier
Classifier type.
Definition: MAModel.hpp:201
Binary data class.
int FeatureCount
Feature count.
Definition: MAModel.hpp:195
MCBinaryData * Encode() const
Encode the classifier into binary data.
Classifier model based on OpenCV classifiers.
Definition: MAModel.hpp:50
MA::FeaturePreprocessingType PreprocessingMode
Preprocessing mode.
Definition: MAModel.hpp:197
static MAClassifier * Decode(const MCBinaryData &data)
Decode the classifier from binary data.
boost::scoped_ptr< CvStatModel > Model
OpenCV classifier model.
Definition: MAModel.hpp:189
MC::FloatList Predict(const MC::FloatTable &input_vectors, MC::FloatList &confidences)
Predict some samples with labels.
MC::FloatTable PreprocessingCoefficients
Preprocessing coefficients.
Definition: MAModel.hpp:199
MC::FloatList GetModelLabels() const
Get the list of unique labels.
MC::FloatList Labels
List of labels in the model.
Definition: MAModel.hpp:187
Common inferface for multiple classifiers and regression algorithms.
boost::scoped_ptr< CvEmWrapper > EmModel
OpenCV classifier model (Expectation Maximization)
Definition: MAModel.hpp:191