21 #include "ParticleClassifier.h"
30 this->numParticles = numParticles;
31 this->sensorNoise = sensorNoise;
32 this->transitionSigma = transitionSigma;
33 this->phaseSigma = phaseSigma;
34 this->velocitySigma = velocitySigma;
35 useNullRejection =
true;
36 supportsNullRejection =
true;
37 classType =
"ParticleClassifier";
38 classifierType = classType;
39 classifierMode = TIMESERIES_CLASSIFIER_MODE;
40 debugLog.setProceedingText(
"[DEBUG ParticleClassifier]");
41 errorLog.setProceedingText(
"[ERROR ParticleClassifier]");
42 trainingLog.setProceedingText(
"[TRAINING ParticleClassifier]");
43 warningLog.setProceedingText(
"[WARNING ParticleClassifier]");
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++){
122 errorLog <<
"predict_(VectorDouble &inputVector) - The model has not been trained!" << std::endl;
126 if( numInputDimensions != inputVector.size() ){
127 errorLog <<
"predict_(VectorDouble &inputVector) - The number of features in the model " << numInputDimensions <<
" does not match that of the input vector " << inputVector.size() << std::endl;
133 for(
unsigned int j=0; j<numInputDimensions; j++){
134 inputVector[j] =
scale(inputVector[j],ranges[j].minValue,ranges[j].maxValue,0,1);
138 predictedClassLabel = 0;
140 std::fill(classLikelihoods.begin(),classLikelihoods.end(),0);
141 std::fill(classDistances.begin(),classDistances.end(),0);
144 particleFilter.
filter( inputVector );
147 unsigned int gestureTemplate = 0;
148 unsigned int gestureLabel = 0;
149 unsigned int gestureIndex = 0;
150 for(
unsigned int i=0; i<numParticles; i++){
151 gestureTemplate = (
unsigned int)particleFilter[i].x[0];
152 gestureLabel = particleFilter.gestureTemplates[ gestureTemplate ].classLabel;
155 classDistances[ gestureIndex ] += particleFilter[i].w;
158 bool rejectPrediction =
false;
159 if( useNullRejection ){
161 rejectPrediction =
true;
166 for(
unsigned int i=0; i<numClasses; i++){
168 classLikelihoods[ i ] = rejectPrediction ? 0 : classDistances[i];
170 if( classLikelihoods[i] > maxLikelihood ){
171 predictedClassLabel = classLabels[i];
172 maxLikelihood = classLikelihoods[i];
187 particleFilter.
clear();
196 particleFilter.reset();
205 errorLog <<
"saveModelToFile(fstream &file) - The file is not open!" << std::endl;
218 errorLog <<
"loadModelFromFile(string filename) - Could not open file to load model" << std::endl;
227 bestDistance = DEFAULT_NULL_DISTANCE_VALUE;
229 classDistances.
resize(numClasses,DEFAULT_NULL_DISTANCE_VALUE);
235 return particleFilter.gestureTemplates;
239 return particleFilter;
242 VectorDouble ParticleClassifier::getStateEstimation()
const{
246 Float ParticleClassifier::getPhase()
const{
253 Float ParticleClassifier::getVelocity()
const{
260 bool ParticleClassifier::setNumParticles(
const unsigned int numParticles){
264 this->numParticles = numParticles;
269 bool ParticleClassifier::setSensorNoise(
const unsigned int sensorNoise){
273 this->sensorNoise = sensorNoise;
278 bool ParticleClassifier::setTransitionSigma(
const unsigned int transitionSigma){
282 this->transitionSigma = transitionSigma;
287 bool ParticleClassifier::setPhaseSigma(
const unsigned int phaseSigma){
291 this->phaseSigma = phaseSigma;
296 bool ParticleClassifier::setVelocitySigma(
const unsigned int velocitySigma){
300 this->velocitySigma = velocitySigma;
Float getWeightSum() const
#define DEFAULT_NULL_LIKELIHOOD_VALUE
virtual bool filter(SENSOR_DATA &data)
Float scale(const Float &x, const Float &minSource, const Float &maxSource, const Float &minTarget, const Float &maxTarget, const bool constrain=false)
virtual ~ParticleClassifier(void)
std::string getClassifierType() const
virtual bool saveModelToFile(std::fstream &file) const
virtual bool resize(const unsigned int size)
UINT getClassLabelIndexValue(UINT classLabel) const
ParticleClassifier & operator=(const ParticleClassifier &rhs)
Vector< MinMax > getRanges() const
Vector< ClassTracker > getClassTracker() const
virtual bool loadModelFromFile(std::fstream &file)
virtual bool predict_(VectorFloat &inputVector)
virtual bool train_(TimeSeriesClassificationData &trainingData)
bool copyBaseVariables(const Classifier *classifier)
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
virtual bool deepCopyFrom(const Classifier *classifier)
VectorFloat getStateEstimation() const