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.
ParticleClassifierParticleFilter.h
1 /*
2 GRT MIT License
3 Copyright (c) <2012> <Nicholas Gillian, Media Lab, MIT>
4 
5 Permission is hereby granted, free of charge, to any person obtaining a copy of this software
6 and associated documentation files (the "Software"), to deal in the Software without restriction,
7 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
9 subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in all copies or substantial
12 portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
15 LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
18 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 */
20 
21 #ifndef GRT_PARTICLE_CLASSIFIER_PARTICLE_FILTER_HEADER
22 #define GRT_PARTICLE_CLASSIFIER_PARTICLE_FILTER_HEADER
23 
24 #include "../../CoreAlgorithms/ParticleFilter/ParticleFilter.h"
25 
26 GRT_BEGIN_NAMESPACE
27 
29  public:
31  classLabel = 0;
32  }
33 
35 
36  }
37 
38  unsigned int getLength() const {
39  return timeseries.getNumRows();
40  }
41 
42  unsigned int classLabel;
43  MatrixFloat timeseries;
44 };
45 
46 class ParticleClassifierParticleFilter : public ParticleFilter< Particle,VectorFloat > {
47 
48 public:
50  setEstimationMode( WEIGHTED_MEAN );
51  clear();
52  }
53 
55 
56  }
57 
58  virtual bool preFilterUpdate( VectorFloat &data ){
59 
60  //Randomly reset a small number of particles to ensure the classifier does not get stuck on one gesture
61  unsigned int numRandomFlipParticles = 0;//(unsigned int)floor( processNoise[0] * Float(numParticles) );
62 
63  for(unsigned int i=0; i<numRandomFlipParticles; i++){
64  //unsigned int randomParticleIndex = rand.getRandomNumberInt(0, numParticles);
65  unsigned int randomParticleIndex = i;
66 
67  particles[ randomParticleIndex ].x[0] = rand.getRandomNumberInt(0, numTemplates); //Randomly pick a template
68  particles[ randomParticleIndex ].x[1] = rand.getRandomNumberUniform(0,1); //Randomly pick a phase
69  particles[ randomParticleIndex ].x[2] = 0; //rand.getRandomNumberUniform(-processNoise[2],processNoise[2]); //Randomly pick a speed
70  }
71 
72  return true;
73  }
74 
75  virtual bool postFilterUpdate( VectorFloat &data ){
76 
77  return true;
78  }
79 
80  virtual bool predict( Particle &p ){
81 
82  //Given the prior set of particles, randomly generate new state estimations using the process model
83  const Float phase = p.x[1];
84  const Float velocity = p.x[2];
85 
86  //Update the phase
87  p.x[1] = Util::limit( phase + rand.getRandomNumberGauss(0.0,processNoise[1]) , 0, 1);
88 
89  //Update the velocity
90  p.x[2] += phase-p.x[1];
91 
92  //Limit the velocity
93  p.x[2] = Util::limit( p.x[2], -processNoise[2], processNoise[2] );
94 
95  return true;
96  }
97 
98  virtual bool update( Particle &p, VectorFloat &data ){
99 
100  //Generate the weights for the current particle
101  p.w = 1;
102 
103  //Get the gesture template
104  const unsigned int templateIndex = (unsigned int)p.x[0];
105 
106  if( templateIndex >= numTemplates ){
107  errorLog << "update( Particle &p, VectorFloat &data ) - Template index out of bounds! templateIndex: " << templateIndex << std::endl;
108  return false;
109  }
110 
111  //Get the current position in the template
112  const unsigned int templateLength = gestureTemplates[templateIndex].timeseries.getNumRows();
113  const unsigned int templatePos = (unsigned int)(p.x[1] * Float(templateLength-1));
114 
115  if( templatePos >= templateLength ){
116  errorLog << "update( Particle &p, VectorFloat &data ) - Template position out of bounds! templatePos: " << templatePos << " templateLength: " << templateLength << std::endl;
117  return false;
118  }
119 
120  for(unsigned int j=0; j<numInputDimensions; j++){
121  p.w *= gauss( data[j], gestureTemplates[templateIndex].timeseries[templatePos][j], measurementNoise[j] );
122  }
123 
124  return true;
125  }
126 
127  virtual bool clear(){
128 
129  //Clear the base class
131 
132  numInputDimensions = 0;
133  numTemplates = 0;
134  numClasses = 0;
135  resampleCounter = 0;
136  gestureTemplates.clear();
137 
138  return true;
139  }
140 
141  bool train( const unsigned int numParticles, const TimeSeriesClassificationData &trainingData, Float sensorNoise, Float transitionSigma, Float phaseSigma, Float velocitySigma){
142 
143  //Clear any previous model
144  clear();
145 
146  this->numParticles = numParticles;
147  numInputDimensions = trainingData.getNumDimensions();
148  numTemplates = trainingData.getNumSamples();
149  numClasses = trainingData.getNumClasses();
150 
151  gestureTemplates.resize( numTemplates );
152  for(unsigned int i=0; i<numTemplates; i++){
153  gestureTemplates[i].classLabel = trainingData[i].getClassLabel();
154  gestureTemplates[i].timeseries = trainingData[i].getData();
155  }
156 
157  //Init the particle filter
158  //State vector is:
159  //[0] gesture template index
160  //[1] phase (position within the template, normalized [0 1])
161  //[2] velocity (value between [-0.2 0.2])
162  stateVectorSize = 3;
165  measurementNoise.resize( numInputDimensions );
166 
167  //Min state range
168  initModel[0][0] = 0;
169  initModel[1][0] = 0;
170  initModel[2][0] = -0.2;
171 
172  //Max state range
173  initModel[0][1] = numTemplates;
174  initModel[1][1] = 1;
175  initModel[2][1] = 0.2;
176 
177  //Set the measurement noise - this sets the sigma difference between the estimated position in the template and sensor input
178  for(unsigned int i=0; i<numInputDimensions; i++){
179  measurementNoise[i] = sensorNoise;
180  }
181 
182  //Set the process noise used for the
183  processNoise[0] = transitionSigma; //Controls the random template selection
184  processNoise[1] = phaseSigma; //Controls the phase update
185  processNoise[2] = velocitySigma; //Controls the maximum velocity update
186 
188  initialized = true;
189 
190  if( !initParticles( numParticles ) ){
191  errorLog << "ERROR: Failed to init particles!" << std::endl;
192  clear();
193  return false;
194  }
195 
196  return true;
197  }
198 
199  /*
200  virtual bool checkForResample(){
201  if( ++resampleCounter >= 100 ){
202  resampleCounter = 0;
203  return true;
204  }
205  return false;
206  }
207  */
208 
209  unsigned int numInputDimensions;
210  unsigned int numTemplates;
211  unsigned int numClasses;
212  unsigned int resampleCounter;
214 
215 };
216 
217 GRT_END_NAMESPACE
218 
219 #endif //GRT_PARTICLE_CLASSIFIER_PARTICLE_FILTER_HEADER
220 
virtual bool initParticles(const UINT numParticles)
bool setEstimationMode(const unsigned int estimationMode)
bool initialized
A flag that indicates if the filter has been initialized.
VectorFloat x
The state estimation.
virtual bool resize(const unsigned int size)
Definition: Vector.h:133
virtual bool preFilterUpdate(VectorFloat &data)
Float getRandomNumberGauss(Float mu=0.0, Float sigma=1.0)
Definition: Random.h:209
virtual bool update(Particle &p, VectorFloat &data)
Vector< VectorFloat > initModel
The noise model for the initial starting guess.
VectorFloat processNoise
The noise covariance in the system.
unsigned int getNumRows() const
Definition: Matrix.h:542
VectorFloat measurementNoise
The noise covariance in the measurement.
virtual bool clear()
unsigned int numParticles
The number of particles in the filter.
Random rand
A random number generator.
Float gauss(Float x, Float mu, Float sigma)
Float getRandomNumberUniform(Float minRange=0.0, Float maxRange=1.0)
Definition: Random.h:198
static Float limit(const Float value, const Float minValue, const Float maxValue)
Definition: Util.cpp:165
Vector< Particle > & particles
A reference to the current active particle Vector.
int getRandomNumberInt(int minRange, int maxRange)
Definition: Random.h:88
unsigned int stateVectorSize
The size of the state Vector (x)
virtual bool postFilterUpdate(VectorFloat &data)