29 #ifndef GRT_ADABOOST_CLASS_MODEL_HEADER 30 #define GRT_ADABOOST_CLASS_MODEL_HEADER 40 errorLog.setKey(
"[ERROR AdaBoostClassModel]");
53 this->classLabel = rhs.classLabel;
56 for(UINT i=0; i<rhs.getNumWeakClassifiers(); i++){
57 if( !addClassifierToCommitee(rhs.weakClassifiers[i], rhs.weights[i]) ){
59 errorLog <<
"operator= Failed to deep copy weak classifiers!" << std::endl;
70 for(UINT i=0; i<weakClassifiers.size(); i++){
71 if( weakClassifiers[i] != NULL ){
72 delete weakClassifiers[i];
73 weakClassifiers[i] = NULL;
76 weakClassifiers.clear();
80 bool setClassLabel(
const UINT classLabel){
81 this->classLabel = classLabel;
85 bool addClassifierToCommitee(
const WeakClassifier *weakClassifier,Float weight){
86 if( weakClassifier == NULL )
return false;
90 if( !weakClassifierPtr->
deepCopyFrom( weakClassifier ) ){
91 delete weakClassifierPtr;
92 weakClassifierPtr = NULL;
93 errorLog <<
"addClassifierToCommitee(...) Failed to add weak classifier to commitee!" << std::endl;
96 weights.push_back( weight );
97 weakClassifiers.push_back( weakClassifierPtr );
104 UINT N = (UINT)weakClassifiers.getSize();
105 for(UINT i=0; i<N; i++){
106 v += weakClassifiers[i]->predict( inputVector ) * weights[i];
113 std::cout <<
"ClassLabel: " << classLabel << std::endl;
114 for(UINT i=0; i<weakClassifiers.getSize(); i++){
115 std::cout <<
"Weight: " << weights[i] << std::endl;
116 weakClassifiers[i]->print();
120 UINT getClassLabel()
const{
124 UINT getNumWeakClassifiers()
const{
125 return weakClassifiers.getSize();
132 for(UINT i=0; i<getNumWeakClassifiers(); i++){
136 if( !weakClassifierPtr->
deepCopyFrom( weakClassifiers[i] ) ){
139 classifiers.push_back( weakClassifierPtr );
145 template<
class T > T* getWeakClassifier(
const UINT &index){
147 if( index >= weakClassifiers.getSize() )
return NULL;
151 if( temp.getWeakClassifierType() == weakClassifiers[index]->getWeakClassifierType() ){
152 return (T*)weakClassifiers[index];
161 bool save( std::fstream &file )
const{
168 UINT N = getNumWeakClassifiers();
171 file <<
"ClassLabel: " << classLabel << std::endl;
172 file <<
"NumWeakClassifiers: "<< getNumWeakClassifiers() << std::endl;
173 file <<
"WeakClassifierTypes: " << std::endl;
174 for(UINT i=0; i<N; i++){
175 if( weakClassifiers[i] == NULL )
return false;
176 file << weakClassifiers[i]->getWeakClassifierType() << std::endl;
180 for(UINT i=0; i<N; i++){
181 file << weights[i] <<
" ";
185 file <<
"WeakClassifiers: " << std::endl;
186 for(UINT i=0; i<N; i++){
187 if( weakClassifiers[i] == NULL )
return false;
188 if( !weakClassifiers[i]->saveModelToFile( file ) )
return false;
194 bool load( std::fstream &file ){
201 errorLog <<
"load(fstream &file) - The file is not open!" << std::endl;
206 UINT numWeakClassifiers = 0;
209 if( word !=
"ClassLabel:" ){
210 errorLog <<
"load(fstream &file) - Failed to read ClassLabel header!" << std::endl;
216 if( word !=
"NumWeakClassifiers:" ){
217 errorLog <<
"load(fstream &file) - Failed to read NumWeakClassifiers header!" << std::endl;
220 file >> numWeakClassifiers;
223 if( word !=
"WeakClassifierTypes:" ){
224 errorLog <<
"load(fstream &file) - Failed to read WeakClassifierTypes header!" << std::endl;
229 if( numWeakClassifiers > 0 ){
230 weights.resize( numWeakClassifiers, 0 );
231 weakClassifiers.resize( numWeakClassifiers, NULL );
233 for(UINT i=0; i<numWeakClassifiers; i++){
236 if( weakClassifiers[i] == NULL ){
237 errorLog <<
"load(fstream &file) - WeakClassifier " << i <<
" is NULL!" << std::endl;
245 if( word !=
"Weights:" ){
246 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read Weights header!" << std::endl;
249 for(UINT i=0; i<numWeakClassifiers; i++){
255 if( word !=
"WeakClassifiers:" ){
256 errorLog <<
"loadModelFromFile(fstream &file) - Failed to read WeakClassifiers header!" << std::endl;
257 errorLog << word << std::endl;
260 for(UINT i=0; i<numWeakClassifiers; i++){
261 if( !weakClassifiers[i]->loadModelFromFile( file ) ){
262 errorLog <<
"loadModelFromFile(fstream &file) - Failed to load weakClassifer: " << i << std::endl;
271 bool normalizeWeights(){
272 if( weights.size() == 0 )
return false;
274 UINT N = (UINT)weights.size();
275 for(UINT i=0; i<N; i++){
278 for(UINT i=0; i<N; i++){
294 #endif// GRT_ADABOOST_CLASS_MODEL_HEADER
virtual bool deepCopyFrom(const WeakClassifier *weakClassifer)
This is the main base class for all GRT WeakClassifiers.
WeakClassifier * createNewInstance() const
static WeakClassifier * createInstanceFromString(std::string const &weakClassifierType)