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.
MeanShift.h
Go to the documentation of this file.
1 
33 #ifndef GRT_MEAN_SHIFT_HEADER
34 #define GRT_MEAN_SHIFT_HEADER
35 
36 #include "../../CoreModules/MLBase.h"
37 
38 GRT_BEGIN_NAMESPACE
39 
40 class MeanShift : public MLBase{
41 public:
42  MeanShift() : MLBase("MeanShift") {
43  }
44 
45  virtual ~MeanShift(){
46 
47  }
48 
49  bool search( const VectorFloat &meanStart, const Vector< VectorFloat > &points, const Float searchRadius, const Float sigma = 20.0 ){
50 
51  //clear the results from any previous search
52  clear();
53 
54  const unsigned int numDimensions = (unsigned int)meanStart.size();
55  const unsigned int numPoints = (unsigned int)points.size();
56  const Float gamma = 1.0 / (2 * SQR(sigma) );
57  unsigned int iteration = 0;
58  VectorFloat numer(2,0);
59  VectorFloat denom(2,0);
60  VectorFloat kernelDist(2,0);
61  Float pointsWithinSearchRadius = 0;
62 
63  mean = meanStart;
64  VectorFloat lastMean = mean;
65 
66  //Start the search loop
67  while( true ){
68 
69  //Reset the counters
70  pointsWithinSearchRadius = 0;
71  std::fill(numer.begin(),numer.end(),0);
72  std::fill(denom.begin(),denom.end(),0);
73  std::fill(kernelDist.begin(),kernelDist.end(),0);
74 
75  //Update the numerator and denominator for points that are with the search radius
76  for(unsigned int i=0; i<numPoints; i++){
77 
78  //Compute the distance of the current point to the mean
79  Float distToMean = euclideanDist( mean, points[i] );
80 
81  //If the point is within the search radius then update numer and denom
82  if( distToMean < searchRadius ){
83 
84  for(unsigned int j=0; j<numDimensions; j++){
85  kernelDist[j] = gaussKernel( points[i][j], mean[j], gamma );
86  numer[j] += kernelDist[j] * points[i][j];
87  denom[j] += kernelDist[j];
88  }
89 
90  pointsWithinSearchRadius++;
91  }
92  }
93 
94  //Update the mean
95  Float change = 0;
96  for(unsigned int j=0; j<numDimensions; j++){
97 
98  mean[j] = numer[j] / denom[j];
99 
100  change += grt_sqr( mean[j] - lastMean[j] );
101 
102  lastMean[j] = mean[j];
103  }
104  change = grt_sqrt( change );
105 
106  trainingLog << "iteration: " << iteration;
107  trainingLog << " mean: ";
108  for(unsigned int j=0; j<numDimensions; j++){
109  trainingLog << mean[j] << " ";
110  }
111  trainingLog << " change: " << change << std::endl;
112 
113  if( change < minChange ){
114  trainingLog << "min changed limit reached - stopping search" << std::endl;
115  break;
116  }
117 
118  if( ++iteration >= maxNumEpochs ){
119  trainingLog << "max number of iterations reached - stopping search." << std::endl;
120  break;
121  }
122 
123  }
124  numTrainingIterationsToConverge = iteration;
125  trained = true;
126 
127  return true;
128  }
129 
130  VectorFloat getMean() const {
131  return mean;
132  }
133 
134  Float gaussKernel( const Float &x, const Float &mu, const Float gamma ){
135  return exp( gamma * grt_sqr(x-mu) );
136  }
137 
138  Float gaussKernel( const VectorFloat &x, const VectorFloat &mu, const Float gamma ){
139 
140  Float y = 0;
141  const UINT N = x.getSize();
142  for(UINT i=0; i<N; i++){
143  y += grt_sqr(x[i]-mu[i]);
144  }
145  return exp( gamma * y );
146  }
147 
148  Float euclideanDist( const VectorFloat &x, const VectorFloat &y ){
149 
150  Float z = 0;
151  const UINT N = x.getSize();
152  for(UINT i=0; i<N; i++){
153  z += grt_sqr(x[i]-y[i]);
154  }
155  return sqrt( z );
156  }
157 
158 protected:
159 
160  VectorFloat mean;
161 
162 };
163 
164 GRT_END_NAMESPACE
165 
166 #endif //GRT_MEAN_SHIFT_HEADER
MLBase(const std::string &id="", const BaseType type=BASE_TYPE_NOT_SET)
Definition: MLBase.cpp:26
UINT getSize() const
Definition: Vector.h:201
virtual bool clear()
Definition: MLBase.cpp:149
This is the main base class that all GRT machine learning algorithms should inherit from...
Definition: MLBase.h:72