BitmapToolkit Scol plugin
MlToolkit.cpp
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#include "Prerequisites.h"
26
27#include "NeuralNetwork.h"
28#include <algorithm>
29
30// Scol object
32
33// For CB
36
43/************************* OBJECT CREATION AND DESTRUCTION******************************/
44
45// scol CB destructor
46int destroyMlObj(mmachine m, SCOL_PTR_TYPE handsys, int obj)
47{
48 NeuralNetwork* nnObj = MMgetPointer<NeuralNetwork*>(m, MTOP(obj));
49 SAFE_DELETE(nnObj);
50 MMsetPointer<NeuralNetwork*>(m, MTOP(obj), 0);
51 return 0;
52}
53
63int _CRml(mmachine m)
64{
65 //fetch params
66 int sensibility = MMpull(m);
67 int nbval = MMpull(m);
68 int mode = MMpull(m);
69 int channel = MMget(m, 0);
70
71 if (channel == NIL || nbval == NIL)
72 {
73 MMset(m, 0, NIL);
74 MMechostr(MSKDEBUG, "_CRml : failed -> NIL parameter used during contruction\n");
75 return 0;
76 }
77
78 if (sensibility == NIL)
79 sensibility = FTOM(0.8f);
80
82 mode = MTOI(mode);
83 if (mode >= 1)
85
86 NeuralNetwork* neuralnetwork = 0;
87 try
88 {
89 neuralnetwork = new NeuralNetwork(MTOI(nbval), MTOF(sensibility), mlMode, NeuralNetwork::KNN);
90 }
91 catch (const cv::Exception& e)
92 {
93 MMechostr(MSKDEBUG, const_cast<char*>(e.what()));
94 MMset(m, 0, NIL);
95 return 0;
96 }
97
98 if ((MMpushPointer(m, neuralnetwork) != 0))
99 {
100 MMset(m, 0, NIL);
101 SAFE_DELETE(neuralnetwork);
102 MMechostr(MSKDEBUG, "_CRML : Memory allocation failed\n");
103 return 0;
104 }
105
106 // Create a new ML object
107 MMechostr(MSKDEBUG, "_CRML ...object creation successful\n");
108 return OBJcreate(m, OBJMLSCOL, SCOL_PTR neuralnetwork, NIL, 0);
109}
110
117int _DSml(mmachine m)
118{
119#ifdef _SCOL_DEBUG_
120MMechostr(MSKDEBUG, "_DSml\n");
121#endif
122
123 int mlTab = MMget(m, 0);
124 if (mlTab == NIL)
125 {
126 MMechostr(MSKDEBUG, "_DSML : object NIL");
127 MMset(m, 0, NIL);
128 return 0;
129 }
130
131 OBJdelTM(m, OBJMLSCOL, mlTab);
132
133 MMset(m, 0, ITOM(0));
134
135#ifdef _SCOL_DEBUG_
136 MMechostr(MSKDEBUG, "ok\n");
137#endif
138 return 0;
139}
140
148int _MlAddDetectionData(mmachine m)
149{
150 int list = MMpull(m);
151 int mlTab = MMget(m, 0);
152 if (mlTab == NIL || list == NIL)
153 {
154 MMechostr(MSKDEBUG, "_MlPredict : failed to fetch neural network");
155 MMset(m, 0, NIL);
156 return 0;
157 }
158
159 NeuralNetwork* nn = MMgetPointer<NeuralNetwork*>(m, MTOP(mlTab));
160 if(!nn)
161 {
162 MMechostr(MSKDEBUG, "_MlPredict : failed");
163 MMset(m, 0, NIL);
164 return 0;
165 }
166
167 list = MTOP(list);
168 std::vector<cv::Point3d> input;
169
170 while(list != NIL)
171 {
172 // Get point coordinate
173 int tuple = MTOP(MMfetch(m, list, 0));
174 cv::Point3d vec;
175 vec.x = MTOF(MMfetch(m, tuple, 0));
176 vec.y = MTOF(MMfetch(m, tuple, 1));
177 vec.z = MTOF(MMfetch(m, tuple, 2));
178
179 input.push_back(vec);
180 list = MTOP(MMfetch(m, list, 1));
181 }
182 nn->AddDetectionData(input);
183
184 MMset(m, 0, ITOM(0));
185 return 0;
186}
187
188
197int _MlAddTrainingData(mmachine m)
198{
199 int name = MMpull(m);
200 int list = MMpull(m);
201 int mlTab = MMget(m, 0);
202
203 if (mlTab == NIL || list == NIL || name == NIL)
204 {
205 MMechostr(MSKDEBUG, "_MlAddTrainingData : received nil parameter");
206 MMset(m, 0, NIL);
207 return 0;
208 }
209
210 std::string label = MMstartstr(m, MTOP(name));
211 NeuralNetwork* nn = MMgetPointer<NeuralNetwork*>(m, MTOP(mlTab));
212 if(!nn)
213 {
214 MMechostr(MSKDEBUG, "_MlAddTrainingData : failed");
215 MMset(m, 0, NIL);
216 return 0;
217 }
218
219 list = MTOP(list);
220 std::vector<cv::Point3d> input;
221
222 while(list != NIL)
223 {
224 // Get point coordinate
225 int tuple = MTOP(MMfetch(m, list, 0));
226 cv::Point3d vec;
227 vec.x = MTOF(MMfetch(m, tuple, 0));
228 vec.y = MTOF(MMfetch(m, tuple, 1));
229 vec.z = MTOF(MMfetch(m, tuple, 2));
230
231 input.push_back(vec);
232 list = MTOP(MMfetch(m, list, 1));
233 }
234
235 nn->AddTrainingData(input, label);
236
237 MMset(m, 0, ITOM(0));
238 return 0;
239}
240
247int _MlTrain(mmachine m)
248{
249 int mlTab = MMget(m, 0);
250 if (mlTab == NIL)
251 {
252 MMset(m, 0, NIL);
253 return 0;
254 }
255
256 NeuralNetwork* nn = MMgetPointer<NeuralNetwork*>(m, MTOP(mlTab));
257 if(!nn)
258 {
259 MMechostr(MSKDEBUG, "_MlTrain : failed");
260 MMset(m, 0, NIL);
261 return 0;
262 }
263
264 nn->Train();
265
266 MMset(m, 0, ITOM(0));
267 return 0;
268}
269
270
278int _MlSaveData(mmachine m)
279{
280 int ifile = MMpull(m);
281 int mlTab = MMget(m, 0);
282 if (mlTab == NIL || ifile == NIL)
283 {
284 MMechostr(MSKDEBUG, "_MlSaveData : failed");
285 MMset(m, 0, NIL);
286 return 0;
287 }
288
289 NeuralNetwork* nn = MMgetPointer<NeuralNetwork*>(m, MTOP(mlTab));
290 if(!nn)
291 {
292 MMechostr(MSKDEBUG, "_MlSaveData : failed");
293 MMset(m, 0, NIL);
294 return 0;
295 }
296
297 std::string nameFile = MMstartstr(m, MTOP(ifile));
298 try
299 {
300 nn->Save(nameFile);
301 }
302 catch(const cv::Exception&)
303 {
304 MMechostr(MSKDEBUG, "_MlSaveData : failed");
305 MMset(m, 0, NIL);
306 return 0;
307 }
308
309 MMset(m, 0, ITOM(0));
310 return 0;
311}
312
320int _MlLoadData(mmachine m)
321{
322 int ifile = MMpull(m);
323 int mlTab = MMget(m, 0);
324 if (mlTab == NIL || ifile == NIL)
325 {
326 MMechostr(MSKDEBUG, "_MlLoadData : failed");
327 MMset(m, 0, NIL);
328 return 0;
329 }
330
331 NeuralNetwork* nn = MMgetPointer<NeuralNetwork*>(m, MTOP(mlTab));
332 if(!nn)
333 {
334 MMechostr(MSKDEBUG, "_MlLoadData : failed");
335 MMset(m, 0, NIL);
336 return 0;
337 }
338
339 std::string nameFile = MMstartstr(m, MTOP(ifile));
340 nn->Load(nameFile);
341
342 MMset(m, 0, ITOM(0));
343 return 0;
344}
345
346
353int _MlGetCategories(mmachine m)
354{
355 int mlTab = MMget(m, 0);
356 if (mlTab == NIL)
357 {
358 MMechostr(MSKDEBUG, "_MlGetCategories : failed");
359 MMset(m, 0, NIL);
360 return 0;
361 }
362
363 NeuralNetwork* nn = MMgetPointer<NeuralNetwork*>(m, MTOP(mlTab));
364 if(!nn)
365 {
366 MMechostr(MSKDEBUG, "_MlGetCategories : failed");
367 MMset(m, 0, NIL);
368 return 0;
369 }
370
371 // remove last param
372 MMpull(m);
373
374 int k = 0;
375
376 // push the category list
377 std::vector<std::string> names = nn->GetCategories();
378 for(unsigned int i = 0; i < names.size(); i++)
379 {
380 if ((k = Mpushstrbloc(m, (char*)names[i].c_str())))
381 return k;
382 }
383
384 if (MMpush(m, NIL))
385 return MERRMEM;
386
387 for(unsigned int j = 0; j < names.size(); j++)
388 {
389 if (MMpush(m, 2*2))
390 return MERRMEM;
391 if ((k = MBdeftab(m)))
392 return k;
393 }
394
395 return 0;
396}
397
398
405{
406 MMechostr(MSKDEBUG, "_CBMlTrainingFinished training finished\n");
407 return OBJaddreflex(m, OBJMLSCOL, SCOL_ML_TRAINING_FINISHED_CB);
408}
409
410int getMlTrainingFinishedCb(mmachine m, SCOL_PTR_TYPE id, SCOL_PTR_TYPE msg_ptr)
411{
412 if (OBJbeginreflex(m, OBJMLSCOL, SCOL_PTR id, SCOL_ML_TRAINING_FINISHED_CB))
413 {
414 MMechostr(MSKDEBUG, "getMlTrainingFinished ...MlTraining error\n");
415 return 0;
416 }
417 return OBJcallreflex(m, 0);
418}
419
425int _CBMlDetect(mmachine m)
426{
427 MMechostr(MSKDEBUG, "_CBMlDetect training finished\n");
428 return OBJaddreflex(m, OBJMLSCOL, SCOL_ML_DETECTION_CB);
429}
430
431int getMlDetectionCb(mmachine m, SCOL_PTR_TYPE id, SCOL_PTR_TYPE msg_ptr)
432{
433 std::string* category = (std::string*)msg_ptr;
434 if (OBJbeginreflex(m, OBJMLSCOL, SCOL_PTR id, SCOL_ML_DETECTION_CB))
435 {
436 MMechostr(MSKDEBUG, "getMlDetectionCb ...MlDetection error\n");
437 delete category;
438 return 0;
439 }
440
441 // push the category name
442 Mpushstrbloc(m, (char*)category->c_str());
443 int p = OBJcallreflex(m, 1);
444
445 delete category;
446 return p;
447}
448
450
454#define NbMltkPKG 11
455
457{
458 "ObjMl",
459 "_CRml",
460 "_DSml",
461 "_MlAddTrainingData",
462 "_MlAddDetectionData",
463 "_CBMlTrainingFinished",
464 "_MlLoadData",
465 "_MlSaveData",
466 "_MlGetCategories",
467 "_MlTrain",
468 "_CBMlDetect"
469};
470
471int (*MltkFunc[NbMltkPKG]) (mmachine m) =
472{
473 NULL, //ObjMl
474 _CRml,
475 _DSml,
482 _MlTrain,
484};
485
487{
488 TYPTYPE, //ObjMl
489 4, //_CRml
490 1, //_DSml
491 3, //_MlAddTrainingData
492 2, //_MlAddDetectionData
493 3, //_CBMlTrainingFinished
494 2, //_MlLoadData
495 2, //_MlSaveData
496 1, //_MlGetCategories
497 1, //_MlTrain
498 3 //_CBMlDetect
499};
500
502{
503 NULL, //ObjMl
504 "fun [Chn I I F] ObjMl", //_CRml
505 "fun [ObjMl] I", //_DSml
506 "fun [ObjMl [[F F F] r1] S] I", //_MlAddTrainingData
507 "fun [ObjMl [[F F F] r1]] I", //_MlAddDetectionData
508 "fun [ObjMl fun [ObjMl u0] u1 u0] ObjMl", //_CBMlTrainingFinished
509 "fun [ObjMl P] I", //_MlLoadData
510 "fun [ObjMl W] I", //_MlSaveData
511 "fun [ObjMl] [S r1]", //_MlGetCategories
512 "fun [ObjMl] I", //_MlTrain
513 "fun [ObjMl fun [ObjMl u0 S] u1 u0] ObjMl" //_CBMlDetect
514};
515
516
517
518// Everything inside _cond and _endcond is ignored by doxygen
520
531int LoadMlToolkit(mmachine m)
532{
533 int k;
534 MMechostr(MSKDEBUG," > Loading MlToolkit\n");
535
536 OBJMLSCOL = OBJregister(2 /*nb of callback*/, /* deleted from parent */ 1, destroyMlObj, "OBJ_ML_SCOL");
537
538 // Callbacks
539 WM_ML_TRAINING_FINISHED = OBJgetUserEvent();
541
542 WM_ML_DETECTION = OBJgetUserEvent();
543 OBJdefEvent(WM_ML_DETECTION, getMlDetectionCb);
544
545 k = PKhardpak(m, "MltkEngine", NbMltkPKG, MltkName, MltkFunc, MltkNArg, MltkType);
546 MMechostr(MSKDEBUG," > Successfully Loaded\n");
547 return k;
548}
549
550
char * MltkType[NbMltkPKG]
#define NbMltkPKG
Nb of Scol functions or types.
int getMlTrainingFinishedCb(mmachine m, SCOL_PTR_TYPE id, SCOL_PTR_TYPE msg_ptr)
int OBJMLSCOL
Definition MlToolkit.cpp:31
char * MltkName[NbMltkPKG]
int destroyMlObj(mmachine m, SCOL_PTR_TYPE handsys, int obj)
Definition MlToolkit.cpp:46
int(* MltkFunc[NbMltkPKG])(mmachine m)
int MltkNArg[NbMltkPKG]
int getMlDetectionCb(mmachine m, SCOL_PTR_TYPE id, SCOL_PTR_TYPE msg_ptr)
int WM_ML_TRAINING_FINISHED
Definition MlToolkit.cpp:34
int WM_ML_DETECTION
Definition MlToolkit.cpp:35
#define SCOL_ML_DETECTION_CB
#define SCOL_ML_TRAINING_FINISHED_CB
int LoadMlToolkit(mmachine m)
void Save(std::string saveFile)
std::vector< std::string > GetCategories()
void Load(std::string filename)
void AddTrainingData(std::vector< cv::Point3d > input, std::string label)
void AddDetectionData(std::vector< cv::Point3d >)
int _MlGetCategories(mmachine m)
Get the ML loaded categories Prototype: fun [ObjMl] [S r1].
int _MlTrain(mmachine m)
Launch the training of the nn Prototype: fun [ObjMl] I.
int _DSml(mmachine m)
destroy a neural network Prototype: fun [ObjMl] I
int _MlAddTrainingData(mmachine m)
Add data to learn to the format <input, label> Prototype: fun [ObjMl [[F F F] r1] S] I.
int _MlSaveData(mmachine m)
Save the settings of a trained neural network Prototype: fun [ObjMl W] I.
int _CRml(mmachine m)
Create a new neural network Prototype: fun [Chn I F] ObjMl.
Definition MlToolkit.cpp:63
int _CBMlDetect(mmachine m)
Set a CB function linked to detection signal Prototype: fun [ObjMl fun [ObjMl u0 S] u1 u0] ObjMl.
int _MlLoadData(mmachine m)
Load an already trained file neural network AND the used parameters (high level) Prototype: fun [ObjM...
int _MlAddDetectionData(mmachine m)
Get a label for the input Prototype: fun [ObjMl [[F F F] r1]] I.
int _CBMlTrainingFinished(mmachine m)
Set a CB function linked to the finished training signal Prototype: fun [ObjMl fun [ObjMl u0] u1 u0] ...