General Tips to Improve Haar Vision Training Data Results [openCV]

computer-vision, opencv

Solution

From my experience based on training hand(palm) detector based on haar features, first thing you have to ask yourself is whether the objects I would like to detect is susceptible to haar features detection. Before I explain this let me remind what haar features actually are - they are just (or at least) able to detect gradient change features - by those I mean e.g. any kind of vertical and horizontal line combinations (single line, lines between two others etc.) And yes, that's basically it. Gradient change. If you think about it, some objects have such feature that there exists some kind of substructure in them that makes them easy to learn which haar features "exists" in the objects. For example, human's face is the best known one. It has several keypoints that might and are considered as an existence of haar featurtes. And that's the description of what I called susceptible to haar features. You have to answer this question before you start to train classifier. If the answer is negative, just don't try to find another solution.

For example, I considered detection open palm gesture. I thought what kind of gesture can be detected by haar method. I thought one solution will be to restrict gesture to palm with open fingers, straight,crowded. This simple trick leads to better haar detection and learning because of existence of additional "edges" between fingers that touch each other. So apart from these edges. And that edges are visible even though the background is similar to the skin color. And I trained classifier, based mainly of those edges between fingers (because you cannot rely on edges around the hand, because it depends of the background - in some cases those edges can even disappear). I used this command:

opencv_traincascade -data HAAR -vec samples.vec -bg negative.dat -numStages 20 -minHitRate 0.999 -numPos 700 -numNeg 2500 -w 20 -h 11 -mode ALL -featureType HAAR

As you can see, I used 700 positive and 2500 negative samples. Please notice -w and -h parameters. It is a size of detector windows. All cropped regions from positive samples are scaled to this size, so remember that the regions you crop must have the same aspect ratio. Finally, I successfully created a haar classifier able to detect specific hand gesture - palm with fingers open, straight, and packed next to each other.

EDIT: You can check log from training here.

Problem

Using trainCascade to train for HAAR-like features. Seeking advice from the community to get better results. In general, what is considered to be a good acceptance ratio? I started with a smaller Training, following this link as a guide: http://coding-robin.de/2013/07/22/train-your-own-opencv-haar-classifier.html Here is my Data: ``` PARAMETERS: cascadeDirName: classifier vecFileName: samples.vec bgFileName: negatives.txt numPos: 68 numNeg: 436 numStages: 20 precalcValBufSize[Mb] : 3072 precalcIdxBufSize[Mb] : 3072 stageType: BOOST featureType: HAAR sampleWidth: 80 sampleHeight: 80 boostType: GAB minHitRate: 0.999 maxFalseAlarmRate: 0.5 weightTrimRate: 0.95 maxDepth: 1 maxWeakCount: 100 mode: ALL ===== TRAINING 0-stage ===== <BEGIN POS count : consumed 68 : 68 NEG count : acceptanceRatio 436 : 1 Precalculation time: 296 +----+---------+---------+ | N | HR | FA | +----+---------+---------+ | 1| 1| 1| +----+---------+---------+ | 2| 1|0.0206422| +----+---------+---------+ END> ===== TRAINING 1-stage ===== <BEGIN POS count : consumed 68 : 68 NEG count : acceptanceRatio 436 : 0.0810108 Precalculation time: 228 +----+---------+---------+ | N | HR | FA | +----+---------+---------+ | 1| 1| 1| +----+---------+---------+ | 2| 1| 1| +----+---------+---------+ | 3| 1| 0.259174| +----+---------+---------+ END> ===== TRAINING 2-stage ===== <BEGIN POS count : consumed 68 : 68 NEG count : acceptanceRatio 436 : 0.0399304 Precalculation time: 279 +----+---------+---------+ | N | HR | FA | +----+---------+---------+ | 1| 1| 1| +----+---------+---------+ | 2| 1| 1| +----+---------+---------+ | 3| 1| 0.155963| +----+---------+---------+ END> ===== TRAINING 3-stage ===== <BEGIN POS count : consumed 68 : 68 NEG count : acceptanceRatio 436 : 0.0106487 Precalculation time: 255 +----+---------+---------+ | N | HR | FA | +----+---------+---------+ | 1| 1| 1| +----+---------+---------+ | 2| 1| 1| +----+---------+---------+ | 3| 1| 0.275229| +----+---------+---------+ END> ===== TRAINING 4-stage ===== <BEGIN POS count : consumed 68 : 68 NEG count : acceptanceRatio 436 : 0.0031086 Precalculation time: 295 +----+---------+---------+ | N | HR | FA | +----+---------+---------+ | 1| 1| 1| +----+---------+---------+ | 2| 1| 1| +----+---------+---------+ | 3| 1| 0.399083| +----+---------+---------+ END> ===== TRAINING 5-stage ===== <BEGIN POS count : consumed 68 : 68 NEG count : acceptanceRatio 436 : 0.00127805 Precalculation time: 282 +----+---------+---------+ | N | HR | FA | +----+---------+---------+ | 1| 1| 1| +----+---------+---------+ | 2| 1| 1| +----+---------+---------+ | 3| 1| 0.389908| +----+---------+---------+ END> ===== TRAINING 6-stage ===== <BEGIN POS count : consumed 68 : 68 NEG count : acceptanceRatio 436 : 0.000522627 Precalculation time: 299 +----+---------+---------+ | N | HR | FA | +----+---------+---------+ | 1| 1| 1| +----+---------+---------+ | 2| 1| 1| +----+---------+---------+ | 3| 1| 0.502294| +----+---------+---------+ | 4| 1| 0.247706| +----+---------+---------+ END> ===== TRAINING 7-stage ===== <BEGIN POS count : consumed 68 : 68 NEG count : acceptanceRatio 436 : 0.000149988 Precalculation time: 283 +----+---------+---------+ | N | HR | FA | +----+---------+---------+ | 1| 1| 1| +----+---------+---------+ | 2| 1| 1| +----+---------+---------+ | 3| 1| 0.511468| +----+---------+---------+ | 4| 1| 0.25| +----+---------+---------+ END> ===== TRAINING 8-stage ===== <BEGIN POS count : consumed 68 : 68 NEG count : acceptanceRatio 436 : 4.31894e-05 Precalculation time: 226 +----+---------+---------+ | N | HR | FA | +----+---------+---------+ | 1| 1| 1| +----+---------+---------+ | 2| 1| 1| +----+---------+---------+ | 3| 1| 0.440367| +----+---------+---------+ END> ===== TRAINING 9-stage ===== <BEGIN POS count : consumed 68 : 68 NEG count : acceptanceRatio 436 : 2.12363e-05 Precalculation time: 208 +----+---------+---------+ | N | HR | FA | +----+---------+---------+ | 1| 1| 1| +----+---------+---------+ | 2| 1| 1| +----+---------+---------+ | 3| 1| 0.541284| +----+---------+---------+ | 4| 1| 0.291284| +----+---------+---------+ END> ===== TRAINING 10-stage ===== <BEGIN POS count : consumed 68 : 68 NEG count : acceptanceRatio 436 : 7.5647e-06 Precalculation time: 294 +----+---------+---------+ | N | HR | FA | +----+---------+---------+ | 1| 1| 1| +----+---------+---------+ | 2| 1| 0.451835| +----+---------+---------+ END> ===== TRAINING 11-stage ===== <BEGIN POS count : consumed 68 : 68 NEG count : acceptanceRatio 436 : 3.79627e-06 Precalculation time: 226 +----+---------+---------+ | N | HR | FA | +----+---------+---------+ | 1| 1| 1| +----+---------+---------+ | 2| 1| 0.463303| +----+---------+---------+ END> ===== TRAINING 12-stage ===== <BEGIN NEG count : acceptanceRatio 436 : 2.03777e-06 Precalculation time: 184 +----+---------+---------+ | N | HR | FA | +----+---------+---------+ | 1| 1| 1| +----+---------+---------+ | 2| 1| 1| +----+---------+---------+ | 3| 1| 0.396789| +----+---------+---------+ END> ===== TRAINING 13-stage ===== <BEGIN POS count : consumed 68 : 68 NEG count : acceptanceRatio 436 : 1.06732e-06 Precalculation time: 262 +----+---------+---------+ | N | HR | FA | +----+---------+---------+ | 1| 1| 1| +----+---------+---------+ | 2| 1| 0.415138| +----+---------+---------+ END> ===== TRAINING 14-stage ===== <BEGIN POS count : consumed 68 : 68 NEG count : acceptanceRatio 436 : 6.80241e-07 Required leaf false alarm rate achieved. Branch training terminated. ``` I found that the classifiers for stage 7 and 8 worked the best, although not perfectly. What have you found that makes good positive samples? Smaller images? Larger images? Rotations? (I did rotate my positive samples(90,180,and 270 degrees). And for negative images? Has anyone experimented with various different grade types of images? Such as those taken from a camera with a good image sensor (like a Canon Rebel) versus those taken with a cell phone camera. Does this make substantial difference in your trainer? I also believe I made a mistake, in training my trainer to find a chrome object. I think the reflections off of it, although minimal, had a large effect on how well it trained.

Original source