GestureRecognitionToolkit  Version: 0.2.0
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 
35 #ifndef GRT_GESTURE_RECOGNITION_PIPELINE_HEADER
36 #define GRT_GESTURE_RECOGNITION_PIPELINE_HEADER
37 
38 #include "../Util/GRTCommon.h"
39 #include "PreProcessing.h"
40 #include "FeatureExtraction.h"
41 #include "Classifier.h"
42 #include "Regressifier.h"
43 #include "Clusterer.h"
44 #include "PostProcessing.h"
45 #include "Context.h"
46 #include "../DataStructures/ClassificationDataStream.h"
47 #include "../Util/ClassificationResult.h"
48 #include "../Util/TestResult.h"
49 
50 GRT_BEGIN_NAMESPACE
51 
52 #define INSERT_AT_END_INDEX 99999
53 
54 class GRT_API GestureRecognitionPipeline : public GRTBase
55 {
56 public:
57  enum ContextLevels{START_OF_PIPELINE=0,AFTER_PREPROCESSING,AFTER_FEATURE_EXTRACTION,AFTER_CLASSIFIER,END_OF_PIPELINE,NUM_CONTEXT_LEVELS};
58 
63 
68 
72  virtual ~GestureRecognitionPipeline(void);
73 
78 
85  GestureRecognitionPipeline& operator << (const PreProcessing &module);
86 
93  GestureRecognitionPipeline& operator << (const FeatureExtraction &module);
94 
101  GestureRecognitionPipeline& operator << (const Classifier &module);
102 
109  GestureRecognitionPipeline& operator << (const Regressifier &module);
110 
117  GestureRecognitionPipeline& operator << (const Clusterer &module);
118 
125  GestureRecognitionPipeline& operator << (const PostProcessing &module);
126 
136  bool train(const ClassificationData &trainingData);
137 
149  bool train(const ClassificationData &trainingData,const UINT kFoldValue,const bool useStratifiedSampling = false );
150 
160  bool train(const TimeSeriesClassificationData &trainingData);
161 
172  bool train(const TimeSeriesClassificationData &trainingData,const UINT kFoldValue,const bool useStratifiedSampling = false);
173 
183  bool train(const ClassificationDataStream &trainingData);
184 
194  bool train(const RegressionData &trainingData);
195 
206  bool train(const RegressionData &trainingData,const UINT kFoldValue);
207 
217  bool train(const UnlabelledData &trainingData);
218 
228  bool test(const ClassificationData &testData);
229 
239  bool test(const TimeSeriesClassificationData &testData);
240 
250  bool test(const ClassificationDataStream &testData);
251 
261  bool test(const RegressionData &testData);
262 
270  bool predict(const VectorFloat &inputVector);
271 
279  bool predict(const MatrixFloat &inputMatrix);
280 
290  bool map(const VectorFloat &inputVector);
291 
298  bool reset();
299 
306  bool clear();
307 
314  bool clearModel();
315 
323  bool save(const std::string &filename) const;
324 
331  bool savePipelineToFile(const std::string &filename) const;
332 
340  bool load(const std::string &filename);
341 
348  bool loadPipelineFromFile(const std::string &filename);
349 
364  bool preProcessData(VectorFloat inputVector,bool computeFeatures = true);
365 
371  bool getIsInitialized() const;
372 
378  bool getTrained() const;
379 
385  bool getIsPreProcessingSet() const;
386 
392  bool getIsFeatureExtractionSet() const;
393 
399  bool getIsClassifierSet() const;
400 
406  bool getIsRegressifierSet() const;
407 
413  bool getIsClustererSet() const;
414 
420  bool getIsPostProcessingSet() const;
421 
427  bool getIsContextSet() const;
428 
434  bool getIsPipelineModeSet() const;
435 
441  bool getIsPipelineInClassificationMode() const;
442 
448  bool getIsPipelineInRegressionMode() const;
449 
455  UINT getInputVectorDimensionsSize() const;
456 
463  UINT getOutputVectorDimensionsSize() const;
464 
470  UINT getNumClassesInModel() const;
471 
478  UINT getNumClasses() const;
479 
485  UINT getNumPreProcessingModules() const;
486 
492  UINT getNumFeatureExtractionModules() const;
493 
499  UINT getNumPostProcessingModules() const;
500 
509  UINT getPredictionModuleIndexPosition() const;
510 
516  UINT getPredictedClassLabel() const;
517 
524  UINT getUnProcessedPredictedClassLabel() const;
525 
531  UINT getNumTrainingSamples() const;
532 
538  UINT getNumTestSamples() const;
539 
545  Float getMaximumLikelihood() const;
546 
552  Float getPhase() const;
553 
561  Float getCrossValidationAccuracy() const;
562 
568  Float getTestAccuracy() const;
569 
575  Float getTestRMSError() const;
576 
582  Float getTestSSError() const;
583 
591  Float getTestFMeasure(const UINT classLabel) const;
592 
600  Float getTestPrecision(const UINT classLabel) const;
601 
609  Float getTestRecall(const UINT classLabel) const;
610 
617  Float getTestRejectionPrecision() const;
618 
625  Float getTestRejectionRecall() const;
626 
632  Float getTestTime() const;
633 
639  Float getTrainingTime() const;
640 
646  Float getTrainingRMSError() const;
647 
653  Float getTrainingSSError() const;
654 
661  MatrixFloat getTestConfusionMatrix() const;
662 
670  TestResult getTestResults() const;
671 
678  VectorFloat getTestPrecision() const;
679 
686  VectorFloat getTestRecall() const;
687 
694  VectorFloat getTestFMeasure() const;
695 
702  VectorFloat getClassLikelihoods() const;
703 
710  VectorFloat getClassDistances() const;
711 
717  VectorFloat getNullRejectionThresholds() const;
718 
724  VectorFloat getRegressionData() const;
725 
731  VectorFloat getUnProcessedRegressionData() const;
732 
738  VectorFloat getPreProcessedData() const;
739 
746  VectorFloat getPreProcessedData(UINT moduleIndex) const;
747 
753  VectorFloat getFeatureExtractionData() const;
754 
761  VectorFloat getFeatureExtractionData(const UINT moduleIndex) const;
762 
769  Vector< UINT > getClassLabels() const;
770 
776  Vector< TestInstanceResult > getTestInstanceResults() const;
777 
783  Vector< TestResult > getCrossValidationResults() const;
784 
791  PreProcessing* getPreProcessingModule(const UINT moduleIndex) const;
792 
799  FeatureExtraction* getFeatureExtractionModule(const UINT moduleIndex) const;
800 
806  Classifier* getClassifier() const;
807 
813  Regressifier* getRegressifier() const;
814 
820  Clusterer* getClusterer() const;
821 
828  PostProcessing* getPostProcessingModule(UINT moduleIndex) const;
829 
837  Context* getContextModule(const UINT contextLevel,const UINT moduleIndex) const;
838 
845  template <class T> T* getPreProcessingModule(const UINT moduleIndex) const{
846  if( moduleIndex < preProcessingModules.size() ){
847  return (T*)preProcessingModules[ moduleIndex ];
848  }
849  return NULL;
850  }
851 
858  template <class T> T* getFeatureExtractionModule(const UINT moduleIndex) const{
859  if( moduleIndex < featureExtractionModules.size() ){
860  return (T*)featureExtractionModules[ moduleIndex ];
861  }
862  return NULL;
863  }
864 
871  template <class T> T* getClassifier() const{
872 
873  if( classifier == NULL ) return NULL;
874 
875  T temp;
876 
877  if( temp.getClassifierType() == classifier->getClassifierType() ){
878  return dynamic_cast<T*>(classifier);
879  }
880 
881  return NULL;
882  }
883 
890  template <class T> T* getRegressifier() const{
891 
892  if( regressifier == NULL ) return NULL;
893 
894  T temp;
895 
896  if( temp.getRegressifierType() == regressifier->getRegressifierType() ){
897  return (T*)regressifier;
898  }
899 
900  return NULL;
901  }
902 
909  template <class T> T* getCluster() const{
910 
911  if( clusterer == NULL ) return NULL;
912 
913  T temp;
914 
915  if( temp.getClassifierType() == clusterer->getClustererType() ){
916  return (T*)clusterer;
917  }
918 
919  return NULL;
920  }
921 
928  template <class T> T* getPostProcessingModule(const UINT moduleIndex) const{
929  if( moduleIndex < postProcessingModules.getSize() ){
930  return (T*)postProcessingModules[ moduleIndex ];
931  }
932  return NULL;
933  }
934 
942  template <class T> T* getContextModule(const UINT contextLevel,const UINT moduleIndex) const{
943  if( contextLevel < contextModules.getSize() ){
944  if( moduleIndex < contextModules[ contextLevel ].getSize() ){
945  return (T*)contextModules[ contextLevel ][ moduleIndex ];
946  }
947  }
948  return NULL;
949  }
950 
956  std::string getModelAsString() const;
957 
963  std::string getPipelineModeAsString() const;
964 
965  /*
966  Gets the pipeline info text as a string.
967 
968  @return returns the pipeline info as a string
969  */
970  std::string getInfo() const;
971 
978  UINT getPipelineModeFromString(std::string pipelineMode) const;
979 
988  bool addPreProcessingModule(const PreProcessing &preProcessingModule,UINT insertIndex = INSERT_AT_END_INDEX);
989 
996  bool setPreProcessingModule(const PreProcessing &preProcessingModule);
997 
1006  bool addFeatureExtractionModule(const FeatureExtraction &featureExtractionModule,UINT insertIndex = INSERT_AT_END_INDEX);
1007 
1014  bool setFeatureExtractionModule(const FeatureExtraction &featureExtractionModule);
1015 
1022  bool setClassifier(const Classifier &classifier);
1023 
1030  bool setRegressifier(const Regressifier &regressifier);
1031 
1038  bool setClusterer(const Clusterer &clusterer);
1039 
1048  bool addPostProcessingModule(const PostProcessing &postProcessingModule,UINT insertIndex = INSERT_AT_END_INDEX);
1049 
1056  bool setPostProcessingModule(const PostProcessing &postProcessingModule);
1057 
1067  bool addContextModule(const Context &contextModule,UINT contextLevel,UINT insertIndex = INSERT_AT_END_INDEX);
1068 
1078  bool updateContextModule(bool value,UINT contextLevel = 0,UINT moduleIndex = 0);
1079 
1085  bool removeAllPreProcessingModules();
1086 
1093  bool removePreProcessingModule(UINT moduleIndex);
1094 
1100  bool removeAllFeatureExtractionModules();
1101 
1108  bool removeFeatureExtractionModule(UINT moduleIndex);
1109 
1115  bool removeClassifier(){ deleteClassifier(); return true; }
1116 
1122  bool removeRegressifier(){ deleteRegressifier(); return true; }
1123 
1129  bool removeClusterer(){ deleteClusterer(); return true; }
1130 
1136  bool removeAllPostProcessingModules();
1137 
1144  bool removePostProcessingModule(const UINT moduleIndex);
1145 
1151  bool removeAllContextModules();
1152 
1160  bool removeContextModule(const UINT contextLevel,const UINT moduleIndex);
1161 
1168  GRT_DEPRECATED_MSG( "use clear() instead.", bool clearAll() );
1169 
1175  bool clearTestResults();
1176 
1181  bool setInfo(const std::string &info);
1182 
1183 protected:
1184  bool predict_classifier(const VectorFloat &inputVector);
1185  bool predict_timeseries( const MatrixFloat &input );
1186  bool predict_frame( const MatrixFloat &input );
1187  bool predict_regressifier(const VectorFloat &inputVector);
1188  bool predict_clusterer(const VectorFloat &inputVector);
1189  bool init();
1190  void deleteAllPreProcessingModules();
1191  void deleteAllFeatureExtractionModules();
1192  void deleteClassifier();
1193  void deleteRegressifier();
1194  void deleteClusterer();
1195  void deleteAllPostProcessingModules();
1196  void deleteAllContextModules();
1197  bool updateTestMetrics(const UINT classLabel,const UINT predictedClassLabel,VectorFloat &precisionCounter,VectorFloat &recallCounter,Float &rejectionPrecisionCounter,Float &rejectionRecallCounter,VectorFloat &confusionMatrixCounter);
1198  bool computeTestMetrics(VectorFloat &precisionCounter,VectorFloat &recallCounter,Float &rejectionPrecisionCounter,Float &rejectionRecallCounter,VectorFloat &confusionMatrixCounter,const UINT numTestSamples);
1199 
1200  bool initialized;
1201  bool trained;
1202  std::string info;
1203  UINT inputVectorDimensions;
1204  UINT outputVectorDimensions;
1205  UINT predictedClassLabel;
1206  UINT predictedClusterLabel;
1207  UINT pipelineMode;
1208  UINT predictionModuleIndex;
1209  UINT numTrainingSamples;
1210  UINT numTestSamples;
1211  Float testAccuracy;
1212  Float testRMSError;
1213  Float testSquaredError;
1214  Float testTime;
1215  Float trainingTime;
1216  VectorFloat testFMeasure;
1217  VectorFloat testPrecision;
1218  VectorFloat testRecall;
1219  VectorFloat regressionData;
1220  Float testRejectionPrecision;
1221  Float testRejectionRecall;
1222  MatrixFloat testConfusionMatrix;
1223  Vector< TestResult > crossValidationResults;
1224  Vector< TestInstanceResult > testResults;
1225 
1226  Vector< PreProcessing* > preProcessingModules;
1227  Vector< FeatureExtraction* > featureExtractionModules;
1228  Classifier *classifier;
1229  Regressifier *regressifier;
1230  Clusterer *clusterer;
1231  Vector< PostProcessing* > postProcessingModules;
1232  Vector< Vector< Context* > > contextModules;
1233 
1234  enum PipelineModes{PIPELINE_MODE_NOT_SET=0,CLASSIFICATION_MODE,REGRESSION_MODE,CLUSTER_MODE};
1235 };
1236 
1237 GRT_END_NAMESPACE
1238 
1239 #endif //GRT_GESTURE_RECOGNITION_PIPELINE_HEADER
1240 
1241 
This is the main base class that all GRT Feature Extraction algorithms should inherit from...
This is the main base class that all GRT Clustering algorithms should inherit from.
T * getPreProcessingModule(const UINT moduleIndex) const
T * getFeatureExtractionModule(const UINT moduleIndex) const
This is the main base class that all GRT Classification algorithms should inherit from...
This is the main base class that all GRT PostProcessing algorithms should inherit from...
#define INSERT_AT_END_INDEX
This is the main base class that all GRT PreProcessing algorithms should inherit from.
T * getContextModule(const UINT contextLevel, const UINT moduleIndex) const
T * getPostProcessingModule(const UINT moduleIndex) const
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.