21 #define GRT_DLL_EXPORTS
31 this->useScaling = useScaling;
32 useNullRejection =
false;
34 classifierType = classType;
35 classifierMode = STANDARD_CLASSIFIER_MODE;
36 debugLog.setProceedingText(
"[DEBUG BAG]");
37 errorLog.setProceedingText(
"[ERROR BAG]");
38 trainingLog.setProceedingText(
"[TRAINING BAG]");
39 warningLog.setProceedingText(
"[WARNING BAG]");
44 classifierType = classType;
45 classifierMode = STANDARD_CLASSIFIER_MODE;
46 debugLog.setProceedingText(
"[DEBUG BAG]");
47 errorLog.setProceedingText(
"[ERROR BAG]");
48 trainingLog.setProceedingText(
"[TRAINING BAG]");
49 warningLog.setProceedingText(
"[WARNING BAG]");
64 this->weights = rhs.weights;
78 if( classifier == NULL )
return false;
81 BAG *ptr = (
BAG*)classifier;
87 this->weights = ptr->weights;
109 errorLog <<
"train_(ClassificationData &trainingData) - Training data has zero samples!" << std::endl;
113 numInputDimensions = N;
121 trainingData.
scale(0, 1);
124 UINT ensembleSize = ensemble.
getSize();
126 if( ensembleSize == 0 ){
127 errorLog <<
"train_(ClassificationData &trainingData) - The ensemble size is zero! You need to add some classifiers to the ensemble first." << std::endl;
131 for(UINT i=0; i<ensembleSize; i++){
132 if( ensemble[i] == NULL ){
133 errorLog <<
"train_(ClassificationData &trainingData) - The classifier at ensemble index " << i <<
" has not been set!" << std::endl;
139 for(UINT i=0; i<ensembleSize; i++){
142 trainingLog <<
"Training ensemble " << i+1 <<
". Ensemble type: " << ensemble[i]->getClassType() << std::endl;
145 if( !ensemble[i]->
train( boostedDataset ) ){
146 errorLog <<
"train_(ClassificationData &trainingData) - The classifier at ensemble index " << i <<
" failed training!" << std::endl;
163 errorLog <<
"predict_(VectorFloat &inputVector) - Model Not Trained!" << std::endl;
167 predictedClassLabel = 0;
168 maxLikelihood = -10000;
170 if( !trained )
return false;
172 if( inputVector.
getSize() != numInputDimensions ){
173 errorLog <<
"predict_(VectorFloat &inputVector) - The size of the input Vector (" << inputVector.
getSize() <<
") does not match the num features in the model (" << numInputDimensions << std::endl;
178 for(UINT n=0; n<numInputDimensions; n++){
179 inputVector[n] =
scale(inputVector[n], ranges[n].minValue, ranges[n].maxValue, 0, 1);
183 if( classLikelihoods.
getSize() != numClasses ) classLikelihoods.
resize(numClasses);
184 if( classDistances.
getSize() != numClasses ) classDistances.
resize(numClasses);
187 for(UINT k=0; k<numClasses; k++){
188 classLikelihoods[k] = 0;
189 classDistances[k] = 0;
194 UINT ensembleSize = ensemble.
getSize();
195 for(UINT i=0; i<ensembleSize; i++){
197 if( !ensemble[i]->
predict(inputVector) ){
198 errorLog <<
"predict_(VectorFloat &inputVector) - The " << i <<
" classifier in the ensemble failed prediction!" << std::endl;
211 for(UINT i=0; i<numClasses; i++){
212 if( classLikelihoods[i] > maxCount ){
214 maxCount = classLikelihoods[i];
216 classLikelihoods[i] /= sum;
217 classDistances[i] /= Float(ensembleSize);
220 predictedClassLabel = classLabels[ maxIndex ];
221 maxLikelihood = classLikelihoods[ maxIndex ];
229 for(UINT i=0; i<ensemble.
getSize(); i++){
230 if( ensemble[i] != NULL ){
231 ensemble[i]->reset();
244 for(UINT i=0; i<ensemble.
getSize(); i++){
245 if( ensemble[i] != NULL ){
246 ensemble[i]->clear();
257 errorLog <<
"save(fstream &file) - The file is not open!" << std::endl;
264 file <<
"GRT_BAG_MODEL_FILE_V2.0\n";
268 errorLog <<
"save(fstream &file) - Failed to save classifier base settings to file!" << std::endl;
274 file <<
"EnsembleSize: " << ensembleSize << std::endl;
282 if( i < ensembleSize-1 ) file <<
"\t";
287 file <<
"ClassifierTypes: ";
289 file << ensemble[i]->getClassifierType() << std::endl;
293 file <<
"Ensemble: \n";
295 if( !ensemble[i]->
save( file ) ){
296 errorLog <<
"save(fstream &file) - Failed to save classifier " << i <<
" to file!" << std::endl;
312 UINT ensembleSize = 0;
316 errorLog <<
"load(string filename) - Could not open file to load model" << std::endl;
324 if( word ==
"GRT_BAG_MODEL_FILE_V1.0" ){
325 return loadLegacyModelFromFile( file );
329 if(word !=
"GRT_BAG_MODEL_FILE_V2.0"){
330 errorLog <<
"load(string filename) - Could not find Model File Header" << std::endl;
336 errorLog <<
"load(string filename) - Failed to load base settings from file!" << std::endl;
344 if(word !=
"EnsembleSize:"){
345 errorLog <<
"load(string filename) - Could not find the EnsembleSize!" << std::endl;
348 file >> ensembleSize;
351 weights.
resize( ensembleSize );
354 if(word !=
"Weights:"){
355 errorLog <<
"load(string filename) - Could not find the Weights!" << std::endl;
358 for(UINT i=0; i<ensembleSize; i++){
366 if(word !=
"ClassifierTypes:"){
367 errorLog <<
"load(string filename) - Could not find the ClassifierTypes!" << std::endl;
370 for(UINT i=0; i<ensembleSize; i++){
371 file >> classifierTypes[i];
376 if(word !=
"Ensemble:"){
377 errorLog <<
"load(string filename) - Could not find the Ensemble!" << std::endl;
380 ensemble.
resize(ensembleSize,NULL);
381 for(UINT i=0; i<ensembleSize; i++){
384 if( ensemble[i] == NULL ){
385 errorLog <<
"load(string filename) - Could not create a new classifier instance from the classifierType: " << classifierTypes[i] << std::endl;
390 if( !ensemble[i]->
load( file ) ){
391 errorLog <<
"load(string filename) - Failed to load ensemble classifier: " << i << std::endl;
402 bestDistance = DEFAULT_NULL_DISTANCE_VALUE;
404 classDistances.
resize(numClasses,DEFAULT_NULL_DISTANCE_VALUE);
428 if( newClassifier == NULL ){
436 weights.push_back( weight );
437 ensemble.push_back( newClassifier );
445 for(UINT i=0; i<ensemble.size(); i++){
446 if( ensemble[i] != NULL ){
459 if( this->weights.size() != weights.size() ){
462 this->weights = weights;
466 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)
virtual bool save(std::fstream &file) const
UINT getClassLabelIndexValue(UINT classLabel) const
Vector< UINT > getClassLabels() const
virtual bool deepCopyFrom(const Classifier *classifier)
UINT getNumSamples() const
virtual bool deepCopyFrom(const Classifier *classifier)
UINT getPredictedClassLabel() const
virtual bool load(std::fstream &file)
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
bool setWeights(const VectorFloat &weights)