Ask Your Question
0

Using opencv_traincascade and speed up the processing

asked 2013-08-19 09:09:35 -0600

golbie gravatar image

updated 2013-08-20 05:45:15 -0600

hello there,

I'm using OpenCV to create a "training" file for dog head recognition. I collected positive images (more than 250) and stored them in a directory. The file pointing to the images (positives.dat) looks like follows:

positive_images/314x382xfrench-bulldog.jpg.pagespeed.ic.9df2ynqdWL.jpg 1 0 0 243 243
positive_images/275x480xbeauceron.jpg.pagespeed.ic.VaRD4-_XQX.jpg 1 0 0 200 200
positive_images/shetland-sheepdog12.jpg 1 0 0 185 185
positive_images/7014-doberman-pinscher7.jpg 1 0 0 286 286

...

Every positive image has a good quality and white background (I thought this is better for OpenCV because the image contains the positive content exclusively).

Afterwards I collected negative images too. The file poiting to them looks like this (negatives.dat):

negative_images/neg-4179.jpg
negative_images/neg-0811.jpg
negative_images/neg-3761.jpg
negative_images/neg-4281.jpg

In the next step the vector file has been created by executing this command:

opencv_createsamples -vec samples.vec -bg negatives.dat -info positives.dat -w 80 -h 80 -show

That means that samples.vec contains all the images from positives.dat. Each and every image is scaled to 80x80 pixel.

Finally, I started the training by issuing this command:

    opencv_traincascade -data training -vec samples.vec -bg negatives.dat \
   -numStages 20 -minHitRate 0.999 -maxFalseAlarmRate 0.5 -numPos 200 \
   -numNeg 600 -w 80 -h 80 -mode ALL -precalcValBufSize 1024 -precalcIdxBufSize 1024

By now, opencv_traincascade has been running for approx. 52 hours!! Currently, the stage 19. is processed.

The console output looks like this:

~/OpenCVProjects/DogFaceRecognition$ ./_training.sh 
PARAMETERS:
cascadeDirName: training
vecFileName: samples.vec
bgFileName: negatives.dat
numPos: 200
numNeg: 600
numStages: 20
precalcValBufSize[Mb] : 1024
precalcIdxBufSize[Mb] : 1024
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   200 : 200
NEG count : acceptanceRatio    600 : 1
Precalculation time: 23
+----+---------+---------+
|  N |    HR   |    FA   |
+----+---------+---------+
|   1|        1|        1|
+----+---------+---------+
|   2|        1|        1|
+----+---------+---------+
|   3|        1| 0.298333|
+----+---------+---------+
END>

===== TRAINING 1-stage =====
<BEGIN
POS count : consumed   200 : 200
NEG count : acceptanceRatio    600 : 0.226757
Precalculation time: 23
+----+---------+---------+
|  N |    HR   |    FA   |
+----+---------+---------+
|   1|        1|        1|
+----+---------+---------+
|   2|        1|        1|
+----+---------+---------+
|   3|        1|      0.7|
+----+---------+---------+
|   4|        1| 0.328333|
+----+---------+---------+
END>

===== TRAINING 2-stage =====
<BEGIN
POS count : consumed   200 : 200
NEG count : acceptanceRatio    600 : 0.0837638
Precalculation time: 23
+----+---------+---------+
|  N |    HR   |    FA   |
+----+---------+---------+
|   1|        1|        1|
+----+---------+---------+
|   2|        1|     0.67|
+----+---------+---------+
|   3|        1| 0.308333|
+----+---------+---------+
END>

===== TRAINING 3-stage =====
<BEGIN
POS count : consumed   200 : 200
NEG count : acceptanceRatio    600 : 0.0256663
Precalculation time: 23
+----+---------+---------+
|  N |    HR   |    FA   |
+----+---------+---------+
|   1|        1|        1|
+----+---------+---------+
|   2|        1| 0.556667|
+----+---------+---------+
|   3|        1| 0.311667|
+----+---------+---------+
END>

===== TRAINING 4-stage =====
<BEGIN
POS count : consumed   200 : 200
NEG count : acceptanceRatio    600 : 0.00866226
Precalculation time: 22
+----+---------+---------+
|  N |    HR   |    FA   |
+----+---------+---------+
|   1|        1|        1|
+----+---------+---------+
|   2|        1|        1|
+----+---------+---------+
|   3|        1|    0.535|
+----+---------+---------+
|   4|        1| 0.276667|
+----+---------+---------+
END>

===== TRAINING 5-stage =====
<BEGIN
POS count : consumed   200 : 200
NEG count : acceptanceRatio    600 : 0.00343436
Precalculation time: 23
+----+---------+---------+
|  N |    HR   |    FA   |
+----+---------+---------+
|   1|        1|        1|
+----+---------+---------+
|   2|        1|    0.685|
+----+---------+---------+
|   3|        1| 0.438333|
+----+---------+---------+
END>

===== TRAINING 6-stage =====
<BEGIN
POS count : consumed   200 : 200
NEG count ...
(more)
edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
1

answered 2013-08-20 05:59:44 -0600

Ok let me answer some problems here

1) Long training time is normal. At the last stage, all image windows need to be parsed through the algorithm to check if the final solution is the best one. 52 hours is nothing compared to the fact that a 18 stage HAAR classifier could easily train multiple days. If you want training to be faster, use the LBP feature type, which is an integer type feature and which calculates A LOT faster.

2) Do not supply training samples with white background. You create abnormal edge characteristics, which will lead to features that are good for your set, but never in a real life example. Your image tests show that I am right. If you keep the background, it will be filtered out, because it varies alot over images. The 'general' dog appearance however, will be continuesly and will get good features.

So basically, retraining with the original images (not whitened out) will give you a way better result.

edit flag offensive delete link more

Comments

Thank you Steven.

ad 1) I will use LBP. Do I have to expect some quality issues or is there a HAAR-fallback? ad 2): that was my suspect. I intentionally picked images with white background. My mistake.

Now, I tried to generate some positive images with background by using this script but the white color is not treated as transparent.

<pre>

!/bin/sh

for f in ls ../positive_images; do opencv_createsamples -img ../positive_images/$f -num 10 -bg ../negatives.dat -maxxangle 0.6 -maxyangle 0 -maxzangle 0.3 -maxidev 100 -bgcolor 255 done </pre>

The result is an image composed of a negative image and the skewed positive image with white background. Do you know what I should pass as -bgcolor parameter to achieve the background transparency?

thx golbie

golbie gravatar imagegolbie ( 2013-08-20 09:59:05 -0600 )edit
0

answered 2016-12-12 23:37:22 -0600

Tim Pham gravatar image

Thank Golbie for sharing information, I have found it interesting.

but I am curious about negative images size, I would like to know the negative images size before executing opencv_traincascade, is it Ok for any negative images size regardless to positive images size?

Tim.

edit flag offensive delete link more

Comments

Yes you can add ramdomly sized negative images, because the tool will sequentially grab all negative samples from it, which are then the size of your model, and this on a multi scale approach.

StevenPuttemans gravatar imageStevenPuttemans ( 2016-12-20 06:50:39 -0600 )edit

Thank Steven

Tim Pham gravatar imageTim Pham ( 2016-12-22 00:04:45 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2013-08-19 09:09:35 -0600

Seen: 7,218 times

Last updated: Dec 12 '16