30 this->useScaling = useScaling;
31 useNullRejection =
false;
33 classifierType = classType;
34 classifierMode = STANDARD_CLASSIFIER_MODE;
35 debugLog.setProceedingText(
"[DEBUG BAG]");
36 errorLog.setProceedingText(
"[ERROR BAG]");
37 trainingLog.setProceedingText(
"[TRAINING BAG]");
38 warningLog.setProceedingText(
"[WARNING BAG]");
43 classifierType = classType;
44 classifierMode = STANDARD_CLASSIFIER_MODE;
45 debugLog.setProceedingText(
"[DEBUG BAG]");
46 errorLog.setProceedingText(
"[ERROR BAG]");
47 trainingLog.setProceedingText(
"[TRAINING BAG]");
48 warningLog.setProceedingText(
"[WARNING BAG]");
63 this->weights = rhs.weights;
77 if( classifier == NULL )
return false;
80 BAG *ptr = (
BAG*)classifier;
86 this->weights = ptr->weights;
108 errorLog <<
"train_(ClassificationData &trainingData) - Training data has zero samples!" << std::endl;
112 numInputDimensions = N;
120 trainingData.
scale(0, 1);
123 UINT ensembleSize = ensemble.
getSize();
125 if( ensembleSize == 0 ){
126 errorLog <<
"train_(ClassificationData &trainingData) - The ensemble size is zero! You need to add some classifiers to the ensemble first." << std::endl;
130 for(UINT i=0; i<ensembleSize; i++){
131 if( ensemble[i] == NULL ){
132 errorLog <<
"train_(ClassificationData &trainingData) - The classifier at ensemble index " << i <<
" has not been set!" << std::endl;
138 for(UINT i=0; i<ensembleSize; i++){
141 trainingLog <<
"Training ensemble " << i+1 <<
". Ensemble type: " << ensemble[i]->getClassType() << std::endl;
144 if( !ensemble[i]->
train( boostedDataset ) ){
145 errorLog <<
"train_(ClassificationData &trainingData) - The classifier at ensemble index " << i <<
" failed training!" << std::endl;
162 errorLog <<
"predict_(VectorFloat &inputVector) - Model Not Trained!" << std::endl;
166 predictedClassLabel = 0;
167 maxLikelihood = -10000;
169 if( !trained )
return false;
171 if( inputVector.
getSize() != numInputDimensions ){
172 errorLog <<
"predict_(VectorFloat &inputVector) - The size of the input Vector (" << inputVector.
getSize() <<
") does not match the num features in the model (" << numInputDimensions << std::endl;
177 for(UINT n=0; n<numInputDimensions; n++){
178 inputVector[n] =
scale(inputVector[n], ranges[n].minValue, ranges[n].maxValue, 0, 1);
182 if( classLikelihoods.
getSize() != numClasses ) classLikelihoods.
resize(numClasses);
183 if( classDistances.
getSize() != numClasses ) classDistances.
resize(numClasses);
186 for(UINT k=0; k<numClasses; k++){
187 classLikelihoods[k] = 0;
188 classDistances[k] = 0;
193 UINT ensembleSize = ensemble.
getSize();
194 for(UINT i=0; i<ensembleSize; i++){
196 if( !ensemble[i]->
predict(inputVector) ){
197 errorLog <<
"predict_(VectorFloat &inputVector) - The " << i <<
" classifier in the ensemble failed prediction!" << std::endl;
210 for(UINT i=0; i<numClasses; i++){
211 if( classLikelihoods[i] > maxCount ){
213 maxCount = classLikelihoods[i];
215 classLikelihoods[i] /= sum;
216 classDistances[i] /= Float(ensembleSize);
219 predictedClassLabel = classLabels[ maxIndex ];
220 maxLikelihood = classLikelihoods[ maxIndex ];
228 for(UINT i=0; i<ensemble.
getSize(); i++){
229 if( ensemble[i] != NULL ){
230 ensemble[i]->reset();
243 for(UINT i=0; i<ensemble.
getSize(); i++){
244 if( ensemble[i] != NULL ){
245 ensemble[i]->clear();
256 errorLog <<
"saveModelToFile(fstream &file) - The file is not open!" << std::endl;
263 file <<
"GRT_BAG_MODEL_FILE_V2.0\n";
267 errorLog <<
"saveModelToFile(fstream &file) - Failed to save classifier base settings to file!" << std::endl;
273 file <<
"EnsembleSize: " << ensembleSize << std::endl;
281 if( i < ensembleSize-1 ) file <<
"\t";
286 file <<
"ClassifierTypes: ";
288 file << ensemble[i]->getClassifierType() << std::endl;
292 file <<
"Ensemble: \n";
295 errorLog <<
"saveModelToFile(fstream &file) - Failed to save classifier " << i <<
" to file!" << std::endl;
311 UINT ensembleSize = 0;
315 errorLog <<
"loadModelFromFile(string filename) - Could not open file to load model" << std::endl;
323 if( word ==
"GRT_BAG_MODEL_FILE_V1.0" ){
324 return loadLegacyModelFromFile( file );
328 if(word !=
"GRT_BAG_MODEL_FILE_V2.0"){
329 errorLog <<
"loadModelFromFile(string filename) - Could not find Model File Header" << std::endl;
335 errorLog <<
"loadModelFromFile(string filename) - Failed to load base settings from file!" << std::endl;
343 if(word !=
"EnsembleSize:"){
344 errorLog <<
"loadModelFromFile(string filename) - Could not find the EnsembleSize!" << std::endl;
347 file >> ensembleSize;
350 weights.
resize( ensembleSize );
353 if(word !=
"Weights:"){
354 errorLog <<
"loadModelFromFile(string filename) - Could not find the Weights!" << std::endl;
357 for(UINT i=0; i<ensembleSize; i++){
365 if(word !=
"ClassifierTypes:"){
366 errorLog <<
"loadModelFromFile(string filename) - Could not find the ClassifierTypes!" << std::endl;
369 for(UINT i=0; i<ensembleSize; i++){
370 file >> classifierTypes[i];
375 if(word !=
"Ensemble:"){
376 errorLog <<
"loadModelFromFile(string filename) - Could not find the Ensemble!" << std::endl;
379 ensemble.
resize(ensembleSize,NULL);
380 for(UINT i=0; i<ensembleSize; i++){
383 if( ensemble[i] == NULL ){
384 errorLog <<
"loadModelFromFile(string filename) - Could not create a new classifier instance from the classifierType: " << classifierTypes[i] << std::endl;
390 errorLog <<
"loadModelFromFile(string filename) - Failed to load ensemble classifier: " << i << std::endl;
401 bestDistance = DEFAULT_NULL_DISTANCE_VALUE;
403 classDistances.
resize(numClasses,DEFAULT_NULL_DISTANCE_VALUE);
427 if( newClassifier == NULL ){
435 weights.push_back( weight );
436 ensemble.push_back( newClassifier );
444 for(UINT i=0; i<ensemble.size(); i++){
445 if( ensemble[i] != NULL ){
458 if( this->weights.size() != weights.size() ){
461 this->weights = weights;
465 bool BAG::loadLegacyModelFromFile( std::fstream &file ){
bool saveBaseSettingsToFile(std::fstream &file) const
virtual bool predict(VectorFloat inputVector)
const Vector< Classifier * > getEnsemble() const
virtual bool recomputeNullRejectionThresholds()
#define DEFAULT_NULL_LIKELIHOOD_VALUE
VectorFloat getEnsembleWeights() const
Float scale(const Float &x, const Float &minSource, const Float &maxSource, const Float &minTarget, const Float &maxTarget, const bool constrain=false)
UINT getEnsembleSize() const
std::string getClassifierType() const
virtual bool resize(const unsigned int size)
virtual bool train(ClassificationData trainingData)
BAG(bool useScaling=false)
UINT getClassLabelIndexValue(UINT classLabel) const
Vector< UINT > getClassLabels() const
unsigned int getSize() const
virtual bool deepCopyFrom(const Classifier *classifier)
UINT getNumSamples() const
virtual bool deepCopyFrom(const Classifier *classifier)
virtual bool saveModelToFile(std::fstream &file) const
UINT getPredictedClassLabel() const
static Classifier * createInstanceFromString(std::string const &classifierType)
bool copyBaseVariables(const Classifier *classifier)
bool loadBaseSettingsFromFile(std::fstream &file)
bool addClassifierToEnsemble(const Classifier &classifier, Float weight=1)
This class implements the bootstrap aggregator classifier. Bootstrap aggregating (bagging) is a machi...
UINT getNumDimensions() const
UINT getNumClasses() const
virtual bool train_(ClassificationData &trainingData)
Vector< MinMax > getRanges() const
BAG & operator=(const BAG &rhs)
ClassificationData getBootstrappedDataset(UINT numSamples=0, bool balanceDataset=false) const
bool scale(const Float minTarget, const Float maxTarget)
virtual bool predict_(VectorFloat &inputVector)
Classifier * createNewInstance() const
virtual bool loadModelFromFile(std::fstream &file)
bool setWeights(const VectorFloat &weights)