Handy Boost code ppt

This page is provided to those who are as lazy as I am to write codes. It contains an implementation of the widely used Ada-boost algorithm. I had been searching for such a simple implementation of ada-boost but could not find one and therefore I had to write my own. The code is published here so as to save your time.

   

This is a C++ implementation of the Ada-boost.M1 algorithm. It is generic so that you can substitute your own Object class to the training process. Usually the object is an image, or a signal or the feature vectors extracted from such objects. It is designed for two-class classifier only, i.e. a training/testing sample is either positive (+1) or negative (-1). Therefore it fits best in the object detection tasks. For a multi-class boost implementation please refer to multiboost.

   

It is prefixed ¡°handy¡± because it is easy to use. If you need something simply works, you are at the right place. Here¡¯s a 4-step how-to with an integer classifier as an example. Those integers in the range of (50,100) should be labeled +1 and otherwise -1. None of the weak classifiers has 100% accuracy but the combined strong classifier is 100% correct.

   

1. Write some weak classifiers that inherits the Classifier class:

//weak classifiers

class WEAK1: public Classifier<int>

{

public:

   string name() { return "weak1";};

   int recognize(int& i)

   {

      if (i>50 && i<90) return 1;

      else

         return -1;

   }

};

class WEAK2: public Classifier<int> ¡­

class WEAK3: public Classifier<int> ¡­

2. Prepare some training data with labels.

   vector<int*> data;

   vector<int> label;

   

   // generate training data

   // 1: 50<i<100 -1: otherwise

   for (unsigned int i=0;i<150;i++)

   {

      data.push_back(new int(i));   

      if (i>50 && i<100)

         label.push_back(1);

      else

         label.push_back(-1);

   }

3. Use ada-boost to get the strong classifier

   vector<Classifier<int>*> clsfrs;

   // 3 weak classifiers, none has 100% recognition rate

   clsfrs.push_back(new WEAK1());

   clsfrs.push_back(new WEAK2());

   clsfrs.push_back(new WEAK3());

   

   

   // use ada-boost to train the strong classifier from the weak classifiers

   vector<float> weights = Classifier<int>::adaboost(clsfrs,data,label,100);

  

   // construct a strong classifier

   MultiClassifier<int> multi(weights,clsfrs);

4. Test the strong classifier

   // recognize using the strong classifier

   for (int i=0;i<150;i++)

   {

      cout<<i<<" : "<<multi.recognize(i)<<endl;

   }

   

Web Counter