33 #ifndef GRT_MEAN_SHIFT_HEADER
34 #define GRT_MEAN_SHIFT_HEADER
36 #include "../../CoreModules/MLBase.h"
43 classType =
"MeanShift";
44 infoLog.setProceedingText(
"[MeanShift]");
45 debugLog.setProceedingText(
"[DEBUG MeanShift]");
46 errorLog.setProceedingText(
"[ERROR MeanShift]");
47 trainingLog.setProceedingText(
"[TRAINING MeanShift]");
48 warningLog.setProceedingText(
"[WARNING MeanShift]");
60 const unsigned int numDimensions = (
unsigned int)meanStart.size();
61 const unsigned int numPoints = (
unsigned int)points.size();
62 const Float gamma = 1.0 / (2 * SQR(sigma) );
63 unsigned int iteration = 0;
67 Float pointsWithinSearchRadius = 0;
76 pointsWithinSearchRadius = 0;
77 std::fill(numer.begin(),numer.end(),0);
78 std::fill(denom.begin(),denom.end(),0);
79 std::fill(kernelDist.begin(),kernelDist.end(),0);
82 for(
unsigned int i=0; i<numPoints; i++){
85 Float distToMean = euclideanDist( mean, points[i] );
88 if( distToMean < searchRadius ){
90 for(
unsigned int j=0; j<numDimensions; j++){
91 kernelDist[j] = gaussKernel( points[i][j], mean[j], gamma );
92 numer[j] += kernelDist[j] * points[i][j];
93 denom[j] += kernelDist[j];
96 pointsWithinSearchRadius++;
102 for(
unsigned int j=0; j<numDimensions; j++){
104 mean[j] = numer[j] / denom[j];
106 change += grt_sqr( mean[j] - lastMean[j] );
108 lastMean[j] = mean[j];
110 change = grt_sqrt( change );
112 trainingLog <<
"iteration: " << iteration;
113 trainingLog <<
" mean: ";
114 for(
unsigned int j=0; j<numDimensions; j++){
115 trainingLog << mean[j] <<
" ";
117 trainingLog <<
" change: " << change << std::endl;
119 if( change < minChange ){
120 trainingLog <<
"min changed limit reached - stopping search" << std::endl;
124 if( ++iteration >= maxNumEpochs ){
125 trainingLog <<
"max number of iterations reached - stopping search." << std::endl;
130 numTrainingIterationsToConverge = iteration;
140 Float gaussKernel(
const Float &x,
const Float &mu,
const Float gamma ){
141 return exp( gamma * grt_sqr(x-mu) );
148 for(UINT i=0; i<N; i++){
149 y += grt_sqr(x[i]-mu[i]);
151 return exp( gamma * y );
158 for(UINT i=0; i<N; i++){
159 z += grt_sqr(x[i]-y[i]);
173 #endif //GRT_MEAN_SHIFT_HEADER
unsigned int getSize() const