GestureRecognitionToolkit  Version: 0.2.5
The Gesture Recognition Toolkit (GRT) is a cross-platform, open-source, c++ machine learning library for real-time gesture recognition.
GestureRecognitionPipeline.h
Go to the documentation of this file.
1 
27 #ifndef GRT_GESTURE_RECOGNITION_PIPELINE_HEADER
28 #define GRT_GESTURE_RECOGNITION_PIPELINE_HEADER
29 
30 #include "../Util/GRTCommon.h"
31 #include "PreProcessing.h"
32 #include "FeatureExtraction.h"
33 #include "Classifier.h"
34 #include "Regressifier.h"
35 #include "Clusterer.h"
36 #include "PostProcessing.h"
37 #include "Context.h"
38 #include "../DataStructures/ClassificationDataStream.h"
39 #include "../Util/ClassificationResult.h"
40 #include "../Util/TestResult.h"
41 
42 GRT_BEGIN_NAMESPACE
43 
44 #define INSERT_AT_END_INDEX 99999
45 
53 class GRT_API GestureRecognitionPipeline : public MLBase
54 {
55 public:
56  enum ContextLevels{START_OF_PIPELINE=0,AFTER_PREPROCESSING,AFTER_FEATURE_EXTRACTION,AFTER_CLASSIFIER,END_OF_PIPELINE,NUM_CONTEXT_LEVELS};
57 
62 
67 
71  virtual ~GestureRecognitionPipeline(void);
72 
77 
84  GestureRecognitionPipeline& operator << (const PreProcessing &module);
85 
92  GestureRecognitionPipeline& operator << (const FeatureExtraction &module);
93 
100  GestureRecognitionPipeline& operator << (const Classifier &module);
101 
108  GestureRecognitionPipeline& operator << (const Regressifier &module);
109 
116  GestureRecognitionPipeline& operator << (const Clusterer &module);
117 
124  GestureRecognitionPipeline& operator << (const PostProcessing &module);
125 
135  virtual bool train_(ClassificationData &trainingData) override;
136 
146  virtual bool train(const ClassificationData &trainingData,const UINT kFoldValue,const bool useStratifiedSampling = false );
147 
159  virtual bool train_(ClassificationData &trainingData,const UINT kFoldValue,const bool useStratifiedSampling = false);
160 
170  virtual bool train_(TimeSeriesClassificationData &trainingData) override;
171 
181  virtual bool train(const TimeSeriesClassificationData &trainingData,const UINT kFoldValue,const bool useStratifiedSampling = false);
182 
193  virtual bool train_(TimeSeriesClassificationData &trainingData,const UINT kFoldValue,const bool useStratifiedSampling = false);
194 
204  virtual bool train_(ClassificationDataStream &trainingData) override;
205 
215  virtual bool train_(RegressionData &trainingData) override;
216 
227  virtual bool train_(RegressionData &trainingData,RegressionData &validationData) override;
228 
237  virtual bool train(const RegressionData &trainingData,const UINT kFoldValue);
238 
249  virtual bool train_(RegressionData &trainingData,const UINT kFoldValue);
250 
260  virtual bool train_(UnlabelledData &trainingData) override;
261 
271  virtual bool test(const ClassificationData &testData);
272 
282  virtual bool test(const TimeSeriesClassificationData &testData);
283 
293  virtual bool test(const ClassificationDataStream &testData);
294 
304  virtual bool test(const RegressionData &testData);
305 
313  virtual bool predict_(VectorFloat &inputVector) override;
314 
322  virtual bool predict_(MatrixFloat &inputMatrix) override;
323 
334  GRT_DEPRECATED_MSG( "map_(VectorFloat &inputVector) is deprecated, use predict_(VectorFloat &inputVector) instead", virtual bool map_(VectorFloat &inputVector) override );
335 
342  virtual bool reset() override;
343 
350  virtual bool clear() override;
351 
358  virtual bool clearModel();
359 
366  virtual bool save(const std::string &filename) const override;
367 
373  GRT_DEPRECATED_MSG( "savePipelineToFile(std::string filename) is deprecated, use save(std::string &filename) instead", bool savePipelineToFile(const std::string &filename) const );
374 
382  virtual bool load(const std::string &filename) override;
383 
389  GRT_DEPRECATED_MSG( "loadPipelineFromFile(std::string filename) is deprecated, use load(std::string &filename) instead", bool loadPipelineFromFile(const std::string &filename) );
390 
405  bool preProcessData(VectorFloat inputVector,bool computeFeatures = true);
406 
412  virtual bool getIsInitialized() const;
413 
419  bool getIsPreProcessingSet() const;
420 
426  bool getIsFeatureExtractionSet() const;
427 
433  bool getIsClassifierSet() const;
434 
440  bool getIsRegressifierSet() const;
441 
447  bool getIsClustererSet() const;
448 
454  bool getIsPostProcessingSet() const;
455 
461  bool getIsContextSet() const;
462 
468  bool getIsPipelineModeSet() const;
469 
475  bool getIsPipelineInClassificationMode() const;
476 
482  bool getIsPipelineInRegressionMode() const;
483 
489  UINT getInputVectorDimensionsSize() const;
490 
497  UINT getOutputVectorDimensionsSize() const;
498 
504  UINT getNumClassesInModel() const;
505 
512  UINT getNumClasses() const;
513 
519  UINT getNumPreProcessingModules() const;
520 
526  UINT getNumFeatureExtractionModules() const;
527 
533  UINT getNumPostProcessingModules() const;
534 
543  UINT getPredictionModuleIndexPosition() const;
544 
550  UINT getPredictedClassLabel() const;
551 
558  UINT getUnProcessedPredictedClassLabel() const;
559 
565  UINT getNumTrainingSamples() const;
566 
572  UINT getNumTestSamples() const;
573 
579  Float getMaximumLikelihood() const;
580 
586  Float getPhase() const;
587 
593  Float getTrainingSetAccuracy() const;
594 
602  Float getCrossValidationAccuracy() const;
603 
609  Float getTestAccuracy() const;
610 
616  Float getTestRMSError() const;
617 
623  Float getTestSSError() const;
624 
632  Float getTestFMeasure(const UINT classLabel) const;
633 
641  Float getTestPrecision(const UINT classLabel) const;
642 
650  Float getTestRecall(const UINT classLabel) const;
651 
658  Float getTestRejectionPrecision() const;
659 
666  Float getTestRejectionRecall() const;
667 
673  Float getTestTime() const;
674 
680  Float getTrainingTime() const;
681 
687  Float getTrainingRMSError() const;
688 
694  Float getTrainingSSError() const;
695 
702  MatrixFloat getTestConfusionMatrix() const;
703 
712 
720  TestResult getTestResults() const;
721 
728  VectorFloat getTestPrecision() const;
729 
736  VectorFloat getTestRecall() const;
737 
744  VectorFloat getTestFMeasure() const;
745 
752  VectorFloat getClassLikelihoods() const;
753 
760  VectorFloat getClassDistances() const;
761 
767  VectorFloat getNullRejectionThresholds() const;
768 
774  VectorFloat getRegressionData() const;
775 
781  VectorFloat getUnProcessedRegressionData() const;
782 
788  VectorFloat getPreProcessedData() const;
789 
796  VectorFloat getPreProcessedData(UINT moduleIndex) const;
797 
803  VectorFloat getFeatureExtractionData() const;
804 
811  VectorFloat getFeatureExtractionData(const UINT moduleIndex) const;
812 
819  Vector< UINT > getClassLabels() const;
820 
826  Vector< TestInstanceResult > getTestInstanceResults() const;
827 
833  Vector< TestResult > getCrossValidationResults() const;
834 
841  PreProcessing* getPreProcessingModule(const UINT moduleIndex) const;
842 
849  FeatureExtraction* getFeatureExtractionModule(const UINT moduleIndex) const;
850 
856  Classifier* getClassifier() const;
857 
863  Regressifier* getRegressifier() const;
864 
870  Clusterer* getClusterer() const;
871 
878  PostProcessing* getPostProcessingModule(UINT moduleIndex) const;
879 
887  Context* getContextModule(const UINT contextLevel,const UINT moduleIndex) const;
888 
895  template <class T> const T* getPreProcessingModule(const UINT moduleIndex) const{
896  if( moduleIndex < preProcessingModules.getSize() ){
897 
898  if( preProcessingModules[ moduleIndex ] == NULL ) return NULL;
899 
900  if( T::getId() == preProcessingModules[ moduleIndex ]->getId() ){
901  return dynamic_cast<const T*>( preProcessingModules[ moduleIndex ] );
902  }
903  }
904 
905  return NULL;
906  }
907 
914  template <class T> T* getPreProcessingModule(const UINT moduleIndex) {
915  if( moduleIndex < preProcessingModules.getSize() ){
916 
917  if( preProcessingModules[ moduleIndex ] == NULL ) return NULL;
918 
919  if( T::getId() == preProcessingModules[ moduleIndex ]->getId() ){
920  return dynamic_cast<T*>( preProcessingModules[ moduleIndex ] );
921  }
922  }
923 
924  return NULL;
925  }
926 
933  template <class T> const T* getFeatureExtractionModule(const UINT moduleIndex) const{
934  if( moduleIndex < featureExtractionModules.getSize() ){
935 
936  if( featureExtractionModules[ moduleIndex ] == NULL ) return NULL;
937 
938  if( T::getId() == featureExtractionModules[ moduleIndex ]->getId() ){
939  return dynamic_cast<const T*>( featureExtractionModules[ moduleIndex ] );
940  }
941  }
942 
943  return NULL;
944  }
945 
952  template <class T> T* getFeatureExtractionModule(const UINT moduleIndex){
953  if( moduleIndex < featureExtractionModules.getSize() ){
954 
955  if( featureExtractionModules[ moduleIndex ] == NULL ) return NULL;
956 
957  if( T::getId() == featureExtractionModules[ moduleIndex ]->getId() ){
958  return dynamic_cast<T*>( featureExtractionModules[ moduleIndex ] );
959  }
960  }
961 
962  return NULL;
963  }
964 
971  template <class T> const T* getClassifier() const{
972 
973  if( classifier == NULL ) return NULL;
974 
975  if( T::getId() == classifier->getId() ){
976  return dynamic_cast<const T*>( classifier );
977  }
978 
979  return NULL;
980  }
981 
988  template <class T> T* getClassifier(){
989 
990  if( classifier == NULL ) return NULL;
991 
992  if( T::getId() == classifier->getId() ){
993  return dynamic_cast<T*>( classifier );
994  }
995 
996  return NULL;
997  }
998 
1005  template <class T> const T* getRegressifier() const{
1006 
1007  if( regressifier == NULL ) return NULL;
1008 
1009  if( T::getId() == regressifier->getId() ){
1010  return dynamic_cast<const T*>( regressifier );
1011  }
1012 
1013  return NULL;
1014  }
1015 
1022  template <class T> T* getRegressifier(){
1023 
1024  if( regressifier == NULL ) return NULL;
1025 
1026  if( T::getId() == regressifier->getId() ){
1027  return dynamic_cast<T*>( regressifier );
1028  }
1029 
1030  return NULL;
1031  }
1032 
1039  template <class T> const T* getCluster() const{
1040 
1041  if( clusterer == NULL ) return NULL;
1042 
1043  if( T::getId() == clusterer->getId() ){
1044  return dynamic_cast<const T*>( clusterer );
1045  }
1046 
1047  return NULL;
1048  }
1049 
1056  template <class T> T* getCluster(){
1057 
1058  if( clusterer == NULL ) return NULL;
1059 
1060  if( T::getId() == clusterer->getId() ){
1061  return dynamic_cast<T*>( clusterer );
1062  }
1063 
1064  return NULL;
1065  }
1066 
1073  template <class T> const T* getPostProcessingModule(const UINT moduleIndex) const{
1074  if( moduleIndex < postProcessingModules.getSize() ){
1075  if( postProcessingModules[ moduleIndex ] == NULL ) return NULL;
1076 
1077  if( T::getId() == postProcessingModules[ moduleIndex ]->getId() ){
1078  return dynamic_cast<const T*>( postProcessingModules[ moduleIndex ] );
1079  }
1080  }
1081  return NULL;
1082  }
1083 
1090  template <class T> T* getPostProcessingModule(const UINT moduleIndex){
1091  if( moduleIndex < postProcessingModules.getSize() ){
1092  if( postProcessingModules[ moduleIndex ] == NULL ) return NULL;
1093 
1094  if( T::getId() == postProcessingModules[ moduleIndex ]->getId() ){
1095  return dynamic_cast<T*>( postProcessingModules[ moduleIndex ] );
1096  }
1097  }
1098  return NULL;
1099  }
1100 
1108  template <class T> T* const getContextModule(const UINT contextLevel,const UINT moduleIndex) const{
1109  if( contextLevel < contextModules.getSize() ){
1110  if( moduleIndex >= contextModules[ contextLevel ].getSize() ){
1111  return NULL;
1112  }
1113 
1114  if( contextModules[ contextLevel ][ moduleIndex ] == NULL ) return NULL;
1115 
1116  if( T::getId() == contextModules[ contextLevel ][ moduleIndex ]->getId() ){
1117  return dynamic_cast<const T*>(contextModules[ contextLevel ][ moduleIndex ]);
1118  }
1119  }
1120  return NULL;
1121  }
1122 
1130  template <class T> T* getContextModule(const UINT contextLevel,const UINT moduleIndex){
1131  if( contextLevel < contextModules.getSize() ){
1132  if( moduleIndex >= contextModules[ contextLevel ].getSize() ){
1133  return NULL;
1134  }
1135 
1136  if( contextModules[ contextLevel ][ moduleIndex ] == NULL ) return NULL;
1137 
1138  if( T::getId() == contextModules[ contextLevel ][ moduleIndex ]->getId() ){
1139  return dynamic_cast<T*>(contextModules[ contextLevel ][ moduleIndex ]);
1140  }
1141  }
1142  return NULL;
1143  }
1144 
1150  virtual std::string getModelAsString() const override;
1151 
1157  std::string getPipelineModeAsString() const;
1158 
1159  /*
1160  Gets the pipeline info text as a string.
1161 
1162  @return returns the pipeline info as a string
1163  */
1164  std::string getInfo() const;
1165 
1172  UINT getPipelineModeFromString(std::string pipelineMode) const;
1173 
1182  bool addPreProcessingModule(const PreProcessing &preProcessingModule,UINT insertIndex = INSERT_AT_END_INDEX);
1183 
1190  bool setPreProcessingModule(const PreProcessing &preProcessingModule);
1191 
1200  bool addFeatureExtractionModule(const FeatureExtraction &featureExtractionModule,UINT insertIndex = INSERT_AT_END_INDEX);
1201 
1208  bool setFeatureExtractionModule(const FeatureExtraction &featureExtractionModule);
1209 
1216  bool setClassifier(const Classifier &classifier);
1217 
1224  bool setRegressifier(const Regressifier &regressifier);
1225 
1232  bool setClusterer(const Clusterer &clusterer);
1233 
1242  bool addPostProcessingModule(const PostProcessing &postProcessingModule,UINT insertIndex = INSERT_AT_END_INDEX);
1243 
1250  bool setPostProcessingModule(const PostProcessing &postProcessingModule);
1251 
1261  bool addContextModule(const Context &contextModule,UINT contextLevel,UINT insertIndex = INSERT_AT_END_INDEX);
1262 
1272  bool updateContextModule(bool value,UINT contextLevel = 0,UINT moduleIndex = 0);
1273 
1279  bool removeAllPreProcessingModules();
1280 
1287  bool removePreProcessingModule(UINT moduleIndex);
1288 
1294  bool removeAllFeatureExtractionModules();
1295 
1302  bool removeFeatureExtractionModule(UINT moduleIndex);
1303 
1309  bool removeClassifier(){ deleteClassifier(); return true; }
1310 
1316  bool removeRegressifier(){ deleteRegressifier(); return true; }
1317 
1323  bool removeClusterer(){ deleteClusterer(); return true; }
1324 
1330  bool removeAllPostProcessingModules();
1331 
1338  bool removePostProcessingModule(const UINT moduleIndex);
1339 
1345  bool removeAllContextModules();
1346 
1354  bool removeContextModule(const UINT contextLevel,const UINT moduleIndex);
1355 
1362  GRT_DEPRECATED_MSG( "use clear() instead.", bool clearAll() );
1363 
1369  bool clearTestResults();
1370 
1375  bool setInfo(const std::string &info);
1376 
1377  using MLBase::train;
1378  using MLBase::train_;
1379  using MLBase::predict;
1380 
1381 protected:
1382  bool init();
1383  bool predict_classifier(const VectorFloat &inputVector);
1384  bool predict_timeseries( const MatrixFloat &input );
1385  bool predict_frame( const MatrixFloat &input );
1386  bool predict_regressifier(const VectorFloat &inputVector);
1387  bool predict_clusterer(const VectorFloat &inputVector);
1388  void deleteAllPreProcessingModules();
1389  void deleteAllFeatureExtractionModules();
1390  void deleteClassifier();
1391  void deleteRegressifier();
1392  void deleteClusterer();
1393  void deleteAllPostProcessingModules();
1394  void deleteAllContextModules();
1395  bool updateTestMetrics(const UINT classLabel,const UINT predictedClassLabel,VectorFloat &precisionCounter,VectorFloat &recallCounter,Float &rejectionPrecisionCounter,Float &rejectionRecallCounter,VectorFloat &confusionMatrixCounter);
1396  bool computeTestMetrics(VectorFloat &precisionCounter,VectorFloat &recallCounter,Float &rejectionPrecisionCounter,Float &rejectionRecallCounter,VectorFloat &confusionMatrixCounter,const UINT numTestSamples);
1397 
1398  bool initialized;
1399  std::string info;
1400  UINT inputVectorDimensions;
1401  UINT outputVectorDimensions;
1402  UINT predictedClassLabel;
1403  UINT predictedClusterLabel;
1404  UINT pipelineMode;
1405  UINT predictionModuleIndex;
1406  UINT numTrainingSamples;
1407  UINT numTestSamples;
1408  Float testAccuracy;
1409  Float testRMSError;
1410  Float testSquaredError;
1411  Float testTime;
1412  Float trainingTime;
1413  VectorFloat testFMeasure;
1414  VectorFloat testPrecision;
1415  VectorFloat testRecall;
1416  VectorFloat regressionData;
1417  Float testRejectionPrecision;
1418  Float testRejectionRecall;
1419  MatrixFloat testConfusionMatrix;
1420  Vector< TestResult > crossValidationResults;
1421  Vector< TestInstanceResult > testResults;
1422 
1423  Vector< PreProcessing* > preProcessingModules;
1424  Vector< FeatureExtraction* > featureExtractionModules;
1425  Classifier *classifier;
1426  Regressifier *regressifier;
1427  Clusterer *clusterer;
1428  Vector< PostProcessing* > postProcessingModules;
1429  Vector< Vector< Context* > > contextModules;
1430 
1431  enum PipelineModes{PIPELINE_MODE_NOT_SET=0,CLASSIFICATION_MODE,REGRESSION_MODE,CLUSTER_MODE};
1432 };
1433 
1434 GRT_END_NAMESPACE
1435 
1436 #endif //GRT_GESTURE_RECOGNITION_PIPELINE_HEADER
1437 
1438 
This is the main base class that all GRT Feature Extraction algorithms should inherit from...
virtual bool predict(VectorFloat inputVector)
Definition: MLBase.cpp:135
This is the main base class that all GRT Clustering algorithms should inherit from.
virtual bool predict_(VectorFloat &inputVector)
Definition: MLBase.cpp:137
virtual bool reset()
Definition: MLBase.cpp:147
virtual bool train(ClassificationData trainingData)
Definition: MLBase.cpp:107
virtual bool save(const std::string &filename) const
Definition: MLBase.cpp:167
T * getPostProcessingModule(const UINT moduleIndex)
T * getContextModule(const UINT contextLevel, const UINT moduleIndex)
This is the main base class that all GRT PostProcessing algorithms should inherit from...
T * getPreProcessingModule(const UINT moduleIndex)
virtual std::string getModelAsString() const
Definition: MLBase.cpp:215
T * getFeatureExtractionModule(const UINT moduleIndex)
const T * getPreProcessingModule(const UINT moduleIndex) const
virtual bool clear()
Definition: MLBase.cpp:149
virtual bool train_(ClassificationData &trainingData)
Definition: MLBase.cpp:109
GRT_DEPRECATED_MSG("saveModelToFile(std::string filename) is deprecated, use save(const std::string &filename) instead", virtual bool saveModelToFile(const std::string &filename) const )
T *const getContextModule(const UINT contextLevel, const UINT moduleIndex) const
#define INSERT_AT_END_INDEX
This is the main base class that all GRT PreProcessing algorithms should inherit from.
virtual bool map_(VectorFloat &inputVector)
Definition: MLBase.cpp:145
virtual bool load(const std::string &filename)
Definition: MLBase.cpp:190
Vector< TrainingResult > getTrainingResults() const
Definition: MLBase.cpp:431
const T * getFeatureExtractionModule(const UINT moduleIndex) const
const T * getPostProcessingModule(const UINT moduleIndex) const
This is the main base class that all GRT machine learning algorithms should inherit from...
Definition: MLBase.h:72
This is the main base class that all GRT Classification algorithms should inherit from...
Definition: Classifier.h:41
This is the main base class that all GRT Feature Extraction algorithms should inherit from...
This is the main base class that all GRT Regression algorithms should inherit from.