Ask Your Question

Haar cascade training

asked 2016-05-25 13:51:59 -0500

marcosbontempo gravatar image


I want to use a haar cascade classifier to detect objects in a image.

I have 18 positive images and 24 negative images. Their size is 50 x 30. With them, I used this command to generate 1500 samples:

perl bin/ positives.txt negatives.txt samples 1500 "opencv_createsamples -bgcolor 0 -bgthresh 0 -maxxangle 1.1 -maxyangle 1.1 maxzangle 0.5 -maxidev 40 -w 50 -h 30"

After merging, I started the haar cascade training with this command:

opencv_traincascade -data classifier -vec samples.vec -bg negatives.txt -numStages 20 -minHitRate 0.999 -maxFalseAlarmRate 0.5 -numPos 1000 -numNeg 600 -w 50 -h 30 -mode ALL -precalcValBufSize 2048 -precalcIdxBufSize 2048

But it's converging too soon and it hangs in the third state:

cascadeDirName: classifier
vecFileName: samples.vec
bgFileName: negatives.txt
numPos: 1000
numNeg: 600
numStages: 20
precalcValBufSize[Mb] : 2048
precalcIdxBufSize[Mb] : 2048
stageType: BOOST
featureType: HAAR
sampleWidth: 50
sampleHeight: 30
boostType: GAB
minHitRate: 0.999
maxFalseAlarmRate: 0.5
weightTrimRate: 0.95
maxDepth: 1
maxWeakCount: 100
mode: ALL

===== TRAINING 0-stage =====
POS count : consumed   1000 : 1000
NEG count : acceptanceRatio    600 : 1
Precalculation time: 36
|  N |    HR   |    FA   |
|   1|        1|0.00333333|
Training until now has taken 0 days 0 hours 7 minutes 8 seconds.

===== TRAINING 1-stage =====
POS count : consumed   1000 : 1000
NEG count : acceptanceRatio    600 : 0.0812568
Precalculation time: 31
|  N |    HR   |    FA   |
|   1|        1|        0|
Training until now has taken 0 days 0 hours 13 minutes 37 seconds.

I used this link as a reference:

Does anybody know which parameter can I change to improve the training? Do I need to use more positive or negative images?

Any tip will be very helpful,


edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2016-05-26 06:47:32 -0500

Oh boy, skip the link you are using. It is old and outdated. This forum contains way more better information. A complete guideline on training object models can be found in chapter 5 of OpenCV 3 Blueprints but here are already some pointers

  1. I have 18 positive images and 24 negative images simply forget ever training a decent detector with that. As stated many times before this will force you to use the createsamples tool do generate more samples and basically start with a biased unnatural and artificial dataset. Better collect more real life and application specific training samples.
  2. -numPos 1000 -numNeg 600 this makes actually no sense. Any vision applications and specially detectors have more background information then objects, so you will need at least more negatives then positive windows. Maybe you can start with a 1000:2500 ratio first.
  3. 1|0.00333333| means that your FA drops drastically using just a single feature. In this case I am wonderig what data you are using since this is very weird for cascade classifiers. Can you provide some samples?
  4. BTW it did not converge yet... keep training to increase complexity and robustness of the model ...
edit flag offensive delete link more


Thanks for warning me @StevenPuttemans! Almost everything that I read about haar cascade traing uses this link. I'll read the book's chapter as you suggested.

  1. I increased the number of images to 50 positives and 150 negatives. Do you think it is enough? Is there a magical number recommended?
  2. I changed the ratio and now my results are much better.
  3. I uploaded my samples:!V5txVYaB!4DxsN_Jxw1.... Could you please take a look?

My model is working better now with your advices, but it sometimes does not identify positive images. I'm facing this issue with black cars. Do you have any idea of how can I improve it? I though about adding more black car positive images.

marcosbontempo gravatar imagemarcosbontempo ( 2016-05-26 12:10:40 -0500 )edit

The number of pos and neg images highly depends on what you want to achieve. I have models running on 100 pos and 250 negatives, but on the other hand models that have 10000 pos and 100000 neg files. If it does not identify positives, then you will need to add more positives and make sure there is enough variances!

StevenPuttemans gravatar imageStevenPuttemans ( 2016-05-27 04:22:49 -0500 )edit

Question Tools

1 follower


Asked: 2016-05-25 13:51:59 -0500

Seen: 1,048 times

Last updated: May 26 '16