BitmapToolkit Scol plugin
NeuralNetwork.h
Go to the documentation of this file.
1/*
2-----------------------------------------------------------------------------
3This source file is part of OpenSpace3D
4For the latest info, see http://www.openspace3d.com
5
6Copyright (c) 2012 I-maginer
7
8This program is free software; you can redistribute it and/or modify it under
9the terms of the GNU Lesser General Public License as published by the Free Software
10Foundation; either version 2 of the License, or (at your option) any later
11version.
12
13This program is distributed in the hope that it will be useful, but WITHOUT
14ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
16
17You should have received a copy of the GNU Lesser General Public License along with
18this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19Place - Suite 330, Boston, MA 02111-1307, USA, or go to
20http://www.gnu.org/copyleft/lesser.txt
21
22-----------------------------------------------------------------------------
23*/
24
25#ifndef _NEURAl_NETWORK_H_
26
27#define _NEURAl_NETWORK_H_
28
29#include <string>
30#include <opencv2/opencv.hpp>
31#include <opencv2/core/core.hpp>
32#include <opencv2/ml/ml.hpp>
33
34#include <string>
35#include <list>
36#include <vector>
37#include <map>
38
39//keep for now, most of used shared_mutex and some other are C++17 only
40#include <boost/thread/thread.hpp>
41
42#define SCOL_PI 3.14159265358979323846f
43
44class MlFeature : public cv::Point3d
45{
46public:
48 {
49 x = 0.0f;
50 y = 0.0f;
51 z = 0.0f;
52 t = 0;
53 }
54
55 MlFeature(const cv::Point3d &p, int tl)
56 {
57 x = p.x;
58 y = p.y;
59 z = p.z;
60 t = tl;
61 }
62
63 inline MlFeature &operator+(const MlFeature &rhs)
64 {
65 x += rhs.x;
66 y += rhs.y;
67 z += rhs.z;
68 t += rhs.t;
69 return *this;
70 }
71
72 inline MlFeature &operator-(const MlFeature &rhs)
73 {
74 x -= rhs.x;
75 y -= rhs.y;
76 z -= rhs.z;
77 t -= rhs.t;
78 return *this;
79 }
80
81 inline MlFeature &operator*(const MlFeature &rhs)
82 {
83 x *= rhs.x;
84 y *= rhs.y;
85 z *= rhs.z;
86 t *= rhs.t;
87 return *this;
88 }
89
90 inline MlFeature &operator+=(const MlFeature &rhs)
91 {
92 x += rhs.x;
93 y += rhs.y;
94 z += rhs.z;
95 t += rhs.t;
96 return *this;
97 }
98
99 inline MlFeature &operator-=(const MlFeature &rhs)
100 {
101 x -= rhs.x;
102 y -= rhs.y;
103 z -= rhs.z;
104 t -= rhs.t;
105 return *this;
106 }
107
108 inline MlFeature &operator/=(const MlFeature &rhs)
109 {
110 x /= rhs.x;
111 y /= rhs.y;
112 z /= rhs.z;
113 t /= rhs.t;
114 return *this;
115 }
116
117 int t;
118};
119
121{
122public:
129
142
144 {
147 };
148
149 typedef std::map <unsigned int, std::vector<std::vector<MlFeature> > > SamplesMap;
150protected:
151private:
152 MlType m_type;
153 MlMode m_mode;
154 cv::Ptr<cv::ml::KNearest> m_knn;
155 int m_knn_p;
156 cv::Ptr<cv::ml::NormalBayesClassifier> m_bayes;
157 cv::Ptr<cv::ml::SVM> m_svm;
158 cv::Ptr<cv::ml::DTrees> m_dt;
159 cv::Ptr<cv::ml::RTrees> m_rt;
160 cv::Ptr<cv::ml::Boost> m_boost;
161 cv::Ptr<cv::ml::ANN_MLP> m_ann;
162
163 std::vector<std::string> m_categories;
164 std::vector<std::vector<float>> m_lastDetected;
165 SamplesMap m_samples;
166
167 TrainingState m_trainingState;
168 boost::thread* m_trainingThread;
169 boost::mutex m_trainingCriticalSection;
170
171 boost::thread* m_detectionThread;
172 boost::mutex m_detectionCriticalSection;
173
174 unsigned int m_featurePerSample;
175 int m_nbSamples;
176 int m_nbMaxSamples;
177 int m_nbMinSamples;
178 unsigned int m_nbAnchorPoints;
179 float m_sensibility;
180 int m_lastInputDate;
181 int m_averageTime;
182 std::vector<std::vector<MlFeature>> m_dataHistory;
183 bool m_newHistoryData;
184
185public:
186 /* \brief constructor
187 * \param sampleSize : the number of values per sample
188 * \param type : the machine learning type
189 */
190 NeuralNetwork(int featurePerSample, float sensibility, MlMode mode = ML_POSE, MlType type = KNN);
191
192 /* \brief destructor
193 *
194 */
196
197 /* \brief set ML type
198 *
199 */
200 inline void SetType(MlType type)
201 {
202 m_type = type;
203 }
204
205 /* \brief get ML type
206 *
207 */
209 {
210 return m_type;
211 }
212
213 /* \brief set the categories
214 *
215 */
216 inline void SetCategories(const std::vector<std::string>& names)
217 {
218 m_categories = names;
219 }
220
221 /* \brief get the category name list
222 *
223 */
224 std::vector<std::string> GetCategories();
225
226 /* \brief get a category
227 *
228 */
229 std::string GetCategory(unsigned int pos);
230
231 /* \brief fetch the index of a label
232 */
233 int GetCategoryPos(std::string name);
234
235 /* \brief Launch the training of the neural network
236 */
237 void Train();
238
239 /* \brief Load a saved neural network training file
240 */
241 void Load(std::string filename);
242
243 /* \brief Save the training of a neural network
244 */
245 void Save(std::string saveFile);
246
247 /* \brief Add a single labelled input for future training
248 * \param input : input vector
249 * \param label : label associated to the input
250 */
251 void AddTrainingData(std::vector<cv::Point3d> input, std::string label);
252
253 /* \brief Set threshold activation value for a given label
254 */
255 //void SetThreshold(std::string label, float threshold);
256
257 /* \brief Get threshold activation value for a given label
258 */
259 //float GetThreshold(std::string label);
260
261 /* \brief Calculate the label which should be associated to the input vector
262 */
263 void Reconize(cv::Mat reconizeIn, cv::Mat reconizeOut);
264
265 /* \brief Add data to the detection history
266 * \param input : input vector
267 */
268 void AddDetectionData(std::vector<cv::Point3d>);
269
270 /* \brief current state of the neural network (safe threaded)
271 */
273
274protected:
275private:
276 // ! THREADED FUNCTIONS !
277 // launch the training in a thread
278 void TrainingThread();
279 void DetectionThread();
280
281 // ! MUTEX INSIDE !
282 void SetTrainingState(TrainingState state);
283
284 int Evaluate(cv::Mat& predicted, float &accuracy);
285
286 std::vector<std::vector<MlFeature>> ComputeData(std::vector<std::vector<MlFeature>> ldata);
287 void ValidateDetectedData();
288};
289
290#endif
MlFeature & operator+(const MlFeature &rhs)
MlFeature & operator-(const MlFeature &rhs)
MlFeature & operator/=(const MlFeature &rhs)
MlFeature & operator+=(const MlFeature &rhs)
MlFeature & operator*(const MlFeature &rhs)
MlFeature & operator-=(const MlFeature &rhs)
MlFeature(const cv::Point3d &p, int tl)
void Save(std::string saveFile)
std::map< unsigned int, std::vector< std::vector< MlFeature > > > SamplesMap
void SetType(MlType type)
std::string GetCategory(unsigned int pos)
std::vector< std::string > GetCategories()
void Reconize(cv::Mat reconizeIn, cv::Mat reconizeOut)
int GetCategoryPos(std::string name)
TrainingState GetTrainingState()
void Load(std::string filename)
void AddTrainingData(std::vector< cv::Point3d > input, std::string label)
void SetCategories(const std::vector< std::string > &names)
void AddDetectionData(std::vector< cv::Point3d >)