21 #define GRT_DLL_EXPORTS 22 #include "ParticleClassifier.h" 27 const std::string ParticleClassifier::id =
"ParticleClassifier";
35 this->numParticles = numParticles;
36 this->sensorNoise = sensorNoise;
37 this->transitionSigma = transitionSigma;
38 this->phaseSigma = phaseSigma;
39 this->velocitySigma = velocitySigma;
40 useNullRejection =
true;
41 supportsNullRejection =
true;
42 classifierMode = TIMESERIES_CLASSIFIER_MODE;
58 this->numParticles = rhs.numParticles;
59 this->sensorNoise = rhs.sensorNoise;
60 this->transitionSigma = rhs.transitionSigma;
61 this->phaseSigma = rhs.phaseSigma;
62 this->velocitySigma = rhs.velocitySigma;
63 this->particleFilter = rhs.particleFilter;
72 if( classifier == NULL )
return false;
77 this->numParticles = ptr->numParticles;
78 this->sensorNoise = ptr->sensorNoise;
79 this->transitionSigma = ptr->transitionSigma;
80 this->phaseSigma = ptr->phaseSigma;
81 this->velocitySigma = ptr->velocitySigma;
82 this->particleFilter = ptr->particleFilter;
100 trainingData.
scale(0, 1);
104 particleFilter.train( numParticles, trainingData, sensorNoise, transitionSigma, phaseSigma, velocitySigma );
106 classLabels.
resize(numClasses);
107 classLikelihoods.
resize(numClasses,0);
108 classDistances.
resize(numClasses,0);
110 for(
unsigned int i=0; i<numClasses; i++){
123 errorLog << __GRT_LOG__ <<
" The model has not been trained!" << std::endl;
127 if( numInputDimensions != inputVector.
getSize() ){
128 errorLog << __GRT_LOG__ <<
" The number of features in the model " << numInputDimensions <<
" does not match that of the input vector " << inputVector.
getSize() << std::endl;
134 for(
unsigned int j=0; j<numInputDimensions; j++){
135 inputVector[j] =
scale(inputVector[j],ranges[j].minValue,ranges[j].maxValue,0,1);
139 predictedClassLabel = 0;
141 std::fill(classLikelihoods.begin(),classLikelihoods.end(),0);
142 std::fill(classDistances.begin(),classDistances.end(),0);
145 particleFilter.
filter( inputVector );
148 unsigned int gestureTemplate = 0;
149 unsigned int gestureLabel = 0;
150 unsigned int gestureIndex = 0;
151 for(
unsigned int i=0; i<numParticles; i++){
152 gestureTemplate = (
unsigned int)particleFilter[i].x[0];
153 gestureLabel = particleFilter.gestureTemplates[ gestureTemplate ].classLabel;
156 classDistances[ gestureIndex ] += particleFilter[i].w;
159 bool rejectPrediction =
false;
160 if( useNullRejection ){
162 rejectPrediction =
true;
167 for(
unsigned int i=0; i<numClasses; i++){
169 classLikelihoods[ i ] = rejectPrediction ? 0 : classDistances[i];
171 if( classLikelihoods[i] > maxLikelihood ){
172 predictedClassLabel = classLabels[i];
173 maxLikelihood = classLikelihoods[i];
188 particleFilter.
clear();
197 particleFilter.reset();
206 errorLog <<
"save(fstream &file) - The file is not open!" << std::endl;
219 errorLog <<
"load(string filename) - Could not open file to load model" << std::endl;
228 bestDistance = DEFAULT_NULL_DISTANCE_VALUE;
230 classDistances.
resize(numClasses,DEFAULT_NULL_DISTANCE_VALUE);
236 return particleFilter.gestureTemplates;
240 return particleFilter;
243 VectorDouble ParticleClassifier::getStateEstimation()
const{
247 Float ParticleClassifier::getPhase()
const{
254 Float ParticleClassifier::getVelocity()
const{
261 bool ParticleClassifier::setNumParticles(
const unsigned int numParticles){
265 this->numParticles = numParticles;
270 bool ParticleClassifier::setSensorNoise(
const unsigned int sensorNoise){
274 this->sensorNoise = sensorNoise;
279 bool ParticleClassifier::setTransitionSigma(
const unsigned int transitionSigma){
283 this->transitionSigma = transitionSigma;
288 bool ParticleClassifier::setPhaseSigma(
const unsigned int phaseSigma){
292 this->phaseSigma = phaseSigma;
297 bool ParticleClassifier::setVelocitySigma(
const unsigned int velocitySigma){
301 this->velocitySigma = velocitySigma;
Float getWeightSum() const
std::string getId() const
#define DEFAULT_NULL_LIKELIHOOD_VALUE
virtual bool filter(SENSOR_DATA &data)
virtual ~ParticleClassifier(void)
virtual bool resize(const unsigned int size)
virtual bool load(std::fstream &file)
ParticleClassifier & operator=(const ParticleClassifier &rhs)
Vector< MinMax > getRanges() const
Vector< ClassTracker > getClassTracker() const
virtual bool save(std::fstream &file) const
virtual bool predict_(VectorFloat &inputVector)
virtual bool train_(TimeSeriesClassificationData &trainingData)
bool copyBaseVariables(const Classifier *classifier)
UINT getClassLabelIndexValue(const UINT classLabel) const
ParticleClassifier(const unsigned int numParticles=2000, const Float sensorNoise=20.0, const Float transitionSigma=0.005, const Float phaseSigma=0.1, const Float velocitySigma=0.01)
bool scale(const Float minTarget, const Float maxTarget)
UINT getNumDimensions() const
UINT getNumClasses() const
static std::string getId()
virtual bool deepCopyFrom(const Classifier *classifier)
This is the main base class that all GRT Classification algorithms should inherit from...
VectorFloat getStateEstimation() const
Float scale(const Float &x, const Float &minSource, const Float &maxSource, const Float &minTarget, const Float &maxTarget, const bool constrain=false)