OpenCV Cascade Training Part 3

It is now time to train the cascade!  Negative images have been prepared, positive images created, and the vector file generated in preparation.  To train the cascade, the opencv_traincascade command is used.  There should be a data folder created in the workspace to store the training data.  In this directory, each stage will place a temporary xml file, and at the end of the training, combine all the stage information into a single cascade.xml file.  It is important to keep the stage.xml files.  If something happens while the training is running, the stage files provide a “save point” in the process.  So if 5 stages were complete and the computer froze, you would be able to pick up at stage 5 (and this can save you a lot of hours of training).

The command with arguments I used:

 opencv_traincascade -data data -vec all.vec -bg bg.txt -numPos 20000 -numNeg 10000 -numStages 21 -w 50 -h 50

This will take a LONG time with this many samples and stages.  On my computer (12 GIG RAM, 5 dedicated CPUs) this took over 700 hours of processing time!  I suggest you start with a small number of positive samples.  You may desire to reduce the size to 20×20 instead of 50×50.  If your computer doesn’t have a lot of memory, you will not have a choice but to use smaller image sizes.

Once you have your training complete, in the data directory will be files like shown below.

Note:  I have several cascadeX.xml files instead of one cascade.xml file.  This is because I stopped the process several times and “checked” the accuracy of the run.  I checked it so I could monitor the success/failure of the training and make any adjustments if necessary.  What adjustments?  Items like better training images, max angles to create samples etc…  After 5 stages (about 70 hours per stage), the test recognition looked like the images below.  (We will discuss how to program and run these tests in a different post).

The same cascade file used in a live video feed of the test is seen below:

As can be seen, the training is going well at only 5 stages in.  It should be noted all images in this set were used for training.  Thus far we have not tested the training against an untrained image or set.

The amount of time to train a cascade file seems extreme.  It was at this point (stage 5 in HAAR training) that I decided to try training with the same set of positive images with another OpenCV option.  It is the LBP option of the training call.  Use the same training call, but with the featureType argument.

opencv_traincascade -data data -vec all.vec -bg bg.txt -featureType LBP -numPos 20000 -numNeg 10000 -numStages 21 -w 50 -h 50

This method is not necessarily as accurate as HAAR but is MUCH faster to train.  In fact, I ran 17 stages of training with LBP in under 72 hours!  The HAAR training took 72 hours for EACH stage.  A tremendous difference.  Below are the same images as above, but with the LBP option.

and the LBP video capture after 5 stages.

(My apologies for the low-quality image).  You will note it is doing as well or better than the HAAR.  The busy gray LBP photo is because the scan size is large enough to pick up the same positive area multiple times.  Thus there are two boxes around some positive hits.  But overwhelmingly the LBP is working better for the clovers than the HAAR.  Below are 9 test with the HAAR results on the left and LBP results on the right.


Test 1
Test 2
Test 3
Test 4
Test 5
Test 6
Test 7
Test 8
Test 9

In test 2, HAAR picked up the 4 leaf clover in the lower left corner and LBP did not.  But in test 2, it is difficult to say for sure if HAAR picked up the 4 leaf since it is not in the center of the box, but none-the-less it is marked.

In test 4, the LBP is much cleaner.

In test 9, LBP picked up the clover in the center and HAAR did not.

All HAAR tests were run with the same settings.  All LBP tests were run with the same sittings.  HAAR and LBP were optimized to best meet a general scan.  The images in the test were untrained images.

Overall, my opinion is that LBP is a better option for the clovers.  It is a fast trainer with equal or better performance.  For the cascade xml file, the LBP will be used.  I suggest you run a test on your images and do the same type of comparison.  HAAR and LBP take a different approach (for detailed information, search for their published papers).  LBP brings a huge advantage in the speed of training.