26 RegisterClassifierModule< BAG > BAG::registerModule(
"BAG");
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!" << endl;
112 numInputDimensions = N;
114 classLabels.resize(K);
120 trainingData.
scale(0, 1);
123 UINT ensembleSize = (UINT)ensemble.size();
125 if( ensembleSize == 0 ){
126 errorLog <<
"train_(ClassificationData &trainingData) - The ensemble size is zero! You need to add some classifiers to the ensemble first." << 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!" << endl;
138 for(UINT i=0; i<ensembleSize; i++){
141 trainingLog <<
"Training ensemble " << i+1 <<
". Ensemble type: " << ensemble[i]->getClassType() << endl;
144 if( !ensemble[i]->
train( boostedDataset ) ){
145 errorLog <<
"train_(ClassificationData &trainingData) - The classifier at ensemble index " << i <<
" failed training!" << endl;
162 errorLog <<
"predict_(VectorDouble &inputVector) - Model Not Trained!" << endl;
166 predictedClassLabel = 0;
167 maxLikelihood = -10000;
169 if( !trained )
return false;
171 if( inputVector.size() != numInputDimensions ){
172 errorLog <<
"predict_(VectorDouble &inputVector) - The size of the input vector (" << inputVector.size() <<
") does not match the num features in the model (" << numInputDimensions << 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.size() != numClasses ) classLikelihoods.resize(numClasses);
183 if( classDistances.size() != numClasses ) classDistances.resize(numClasses);
186 for(UINT k=0; k<numClasses; k++){
187 classLikelihoods[k] = 0;
188 classDistances[k] = 0;
193 UINT ensembleSize = (UINT)ensemble.size();
194 for(UINT i=0; i<ensembleSize; i++){
196 if( !ensemble[i]->
predict(inputVector) ){
197 errorLog <<
"predict_(VectorDouble &inputVector) - The " << i <<
" classifier in the ensemble failed prediction!" << 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] /= double(ensembleSize);
219 predictedClassLabel = classLabels[ maxIndex ];
220 maxLikelihood = classLikelihoods[ maxIndex ];
228 for(UINT i=0; i<ensemble.size(); i++){
229 if( ensemble[i] != NULL ){
230 ensemble[i]->reset();
243 for(UINT i=0; i<ensemble.size(); i++){
244 if( ensemble[i] != NULL ){
245 ensemble[i]->clear();
256 errorLog <<
"saveModelToFile(fstream &file) - The file is not open!" << endl;
263 file <<
"GRT_BAG_MODEL_FILE_V2.0\n";
267 errorLog <<
"saveModelToFile(fstream &file) - Failed to save classifier base settings to file!" << endl;
273 file <<
"EnsembleSize: " << ensembleSize << endl;
281 if( i < ensembleSize-1 ) file <<
"\t";
286 file <<
"ClassifierTypes: ";
288 file << ensemble[i]->getClassifierType() << endl;
292 file <<
"Ensemble: \n";
295 errorLog <<
"saveModelToFile(fstream &file) - Failed to save classifier " << i <<
" to file!" << endl;
311 UINT ensembleSize = 0;
315 errorLog <<
"loadModelFromFile(string filename) - Could not open file to load model" << 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" << endl;
335 errorLog <<
"loadModelFromFile(string filename) - Failed to load base settings from file!" << endl;
343 if(word !=
"EnsembleSize:"){
344 errorLog <<
"loadModelFromFile(string filename) - Could not find the EnsembleSize!" << endl;
347 file >> ensembleSize;
350 weights.resize( ensembleSize );
353 if(word !=
"Weights:"){
354 errorLog <<
"loadModelFromFile(string filename) - Could not find the Weights!" << endl;
357 for(UINT i=0; i<ensembleSize; i++){
362 vector< string > classifierTypes( ensembleSize );
365 if(word !=
"ClassifierTypes:"){
366 errorLog <<
"loadModelFromFile(string filename) - Could not find the ClassifierTypes!" << 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!" << 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] << endl;
390 errorLog <<
"loadModelFromFile(string filename) - Failed to load ensemble classifier: " << i << endl;
400 maxLikelihood = DEFAULT_NULL_LIKELIHOOD_VALUE;
401 bestDistance = DEFAULT_NULL_DISTANCE_VALUE;
402 classLikelihoods.resize(numClasses,DEFAULT_NULL_LIKELIHOOD_VALUE);
403 classDistances.resize(numClasses,DEFAULT_NULL_DISTANCE_VALUE);
410 return (UINT)ensemble.size();
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( fstream &file ){
virtual bool predict_(VectorDouble &inputVector)
static Classifier * createInstanceFromString(string const &classifierType)
virtual bool recomputeNullRejectionThresholds()
vector< UINT > getClassLabels() const
virtual bool deepCopyFrom(const Classifier *classifier)
bool copyBaseVariables(const Classifier *classifier)
UINT getNumDimensions() const
bool loadBaseSettingsFromFile(fstream &file)
const vector< Classifier * > getEnsemble() const
UINT getNumSamples() const
virtual bool train(ClassificationData trainingData)
BAG(bool useScaling=false)
UINT getNumClasses() const
bool saveBaseSettingsToFile(fstream &file) const
double scale(const double &x, const double &minSource, const double &maxSource, const double &minTarget, const double &maxTarget, const bool constrain=false)
bool scale(const double minTarget, const double maxTarget)
BAG & operator=(const BAG &rhs)
virtual bool train_(ClassificationData &trainingData)
UINT getClassLabelIndexValue(UINT classLabel) const
virtual bool predict(VectorDouble inputVector)
bool addClassifierToEnsemble(const Classifier &classifier, double weight=1)
This class implements the bootstrap aggregator classifier. Bootstrap aggregating (bagging) is a machi...
virtual bool saveModelToFile(fstream &file) const
UINT getPredictedClassLabel() const
Classifier * createNewInstance() const
VectorDouble getEnsembleWeights() const
UINT getEnsembleSize() const
vector< MinMax > getRanges() const
string getClassifierType() const
bool setWeights(const VectorDouble &weights)
virtual bool loadModelFromFile(fstream &file)
ClassificationData getBootstrappedDataset(UINT numSamples=0) const
virtual bool deepCopyFrom(const Classifier *classifier)