29 #ifndef GRT_ADABOOST_CLASS_MODEL_HEADER
30 #define GRT_ADABOOST_CLASS_MODEL_HEADER
40 debugLog.setProceedingText(
"[DEBUG AdaBoostClassModel]");
41 errorLog.setProceedingText(
"[ERROR AdaBoostClassModel]");
42 warningLog.setProceedingText(
"[WARNING AdaBoostClassModel]");
55 this->classLabel = rhs.classLabel;
58 for(UINT i=0; i<rhs.getNumWeakClassifiers(); i++){
59 if( !addClassifierToCommitee(rhs.weakClassifiers[i], rhs.weights[i]) ){
61 errorLog <<
"operator= Failed to deep copy weak classifiers!" << std::endl;
72 for(UINT i=0; i<weakClassifiers.size(); i++){
73 if( weakClassifiers[i] != NULL ){
74 delete weakClassifiers[i];
75 weakClassifiers[i] = NULL;
78 weakClassifiers.clear();
82 bool setClassLabel(
const UINT classLabel){
83 this->classLabel = classLabel;
87 bool addClassifierToCommitee(
const WeakClassifier *weakClassifier,Float weight){
88 if( weakClassifier == NULL )
return false;
92 if( !weakClassifierPtr->
deepCopyFrom( weakClassifier ) ){
93 delete weakClassifierPtr;
94 weakClassifierPtr = NULL;
95 warningLog <<
"addClassifierToCommitee(...) Failed to add weak classifier to commitee!" << std::endl;
98 weights.push_back( weight );
99 weakClassifiers.push_back( weakClassifierPtr );
106 UINT N = (UINT)weakClassifiers.
getSize();
107 for(UINT i=0; i<N; i++){
108 v += weakClassifiers[i]->predict( inputVector ) * weights[i];
115 std::cout <<
"ClassLabel: " << classLabel << std::endl;
116 for(UINT i=0; i<weakClassifiers.
getSize(); i++){
117 std::cout <<
"Weight: " << weights[i] << std::endl;
118 weakClassifiers[i]->print();
122 UINT getClassLabel()
const{
126 UINT getNumWeakClassifiers()
const{
127 return weakClassifiers.
getSize();
134 for(UINT i=0; i<getNumWeakClassifiers(); i++){
137 WeakClassifier *weakClassifierPtr = weakClassifiers[i]->createNewInstance();
138 if( !weakClassifierPtr->
deepCopyFrom( weakClassifiers[i] ) ){
141 classifiers.push_back( weakClassifierPtr );
147 template<
class T > T* getWeakClassifier(
const UINT &index){
149 if( index >= weakClassifiers.
getSize() )
return NULL;
153 if( temp.getWeakClassifierType() == weakClassifiers[index]->getWeakClassifierType() ){
154 return (T*)weakClassifiers[index];
163 bool saveModelToFile( std::fstream &file )
const{
170 UINT N = getNumWeakClassifiers();
173 file <<
"ClassLabel: " << classLabel << std::endl;
174 file <<
"NumWeakClassifiers: "<< getNumWeakClassifiers() << std::endl;
175 file <<
"WeakClassifierTypes: " << std::endl;
176 for(UINT i=0; i<N; i++){
177 if( weakClassifiers[i] == NULL )
return false;
178 file << weakClassifiers[i]->getWeakClassifierType() << std::endl;
182 for(UINT i=0; i<N; i++){
183 file << weights[i] <<
" ";
187 file <<
"WeakClassifiers: " << std::endl;
188 for(UINT i=0; i<N; i++){
189 if( weakClassifiers[i] == NULL )
return false;
190 if( !weakClassifiers[i]->saveModelToFile( file ) )
return false;
196 bool loadModelFromFile( std::fstream &file ){
203 errorLog <<
"loadModelFromFile(fstream &file) - The file is not open!" << std::endl;
208 UINT numWeakClassifiers = 0;
211 if( word !=
"ClassLabel:" ){
212 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read ClassLabel header!" << std::endl;
218 if( word !=
"NumWeakClassifiers:" ){
219 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read NumWeakClassifiers header!" << std::endl;
222 file >> numWeakClassifiers;
225 if( word !=
"WeakClassifierTypes:" ){
226 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read WeakClassifierTypes header!" << std::endl;
231 if( numWeakClassifiers > 0 ){
232 weights.
resize( numWeakClassifiers, 0 );
233 weakClassifiers.
resize( numWeakClassifiers, NULL );
235 for(UINT i=0; i<numWeakClassifiers; i++){
238 if( weakClassifiers[i] == NULL ){
239 errorLog <<
"loadModelFromFile(fstream &file) - WeakClassifier " << i <<
" is NULL!" << std::endl;
247 if( word !=
"Weights:" ){
248 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read Weights header!" << std::endl;
251 for(UINT i=0; i<numWeakClassifiers; i++){
257 if( word !=
"WeakClassifiers:" ){
258 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read WeakClassifiers header!" << std::endl;
259 errorLog << word << std::endl;
262 for(UINT i=0; i<numWeakClassifiers; i++){
263 if( !weakClassifiers[i]->loadModelFromFile( file ) ){
264 errorLog <<
"loadModelFromFile(fstream &file) - Failed to load weakClassifer: " << i << std::endl;
273 bool normalizeWeights(){
274 if( weights.size() == 0 )
return false;
276 UINT N = (UINT)weights.size();
277 for(UINT i=0; i<N; i++){
280 for(UINT i=0; i<N; i++){
299 #endif// GRT_ADABOOST_CLASS_MODEL_HEADER
virtual bool deepCopyFrom(const WeakClassifier *weakClassifer)
virtual bool resize(const unsigned int size)
unsigned int getSize() const
This is the main base class for all GRT WeakClassifiers.
WeakClassifier * createNewInstance() const
static WeakClassifier * createInstanceFromString(std::string const &weakClassifierType)