I am trying to use OpenCV's opencv_traincascade to generate a Haar Cascade. So far I have 87 distinctive positive samples and 39 negative samples for testing purposes. I generated the .vec file with opencv_createsamples, which worked fine. When I'm running opencv_traincascade it always gets stuck after a few stages, no matter how I change the parameters. My call looks like this:
opencv_traincascade -data /opencvimgs/haarcascades/data/ -vec /opencvimgs/haarcascades/out.vec -bg /opencvimgs/haarcascades/neg.txt -numPos 87 -numNeg 39
I tried increasing and decreasing minHitRate and maxFalseAlarmRate as well as numPos and numNeg without any success. It might run for a few more stages but then it seems to hang in an infine loop again. How can I resolve this?
The output below is what the programm writes to the console:
opencv_traincascade -data /opencvimgs/haarcascades/data/ -vec
/opencvimgs/haarcascades/out.vec -bg /opencvimgs/haarcascades/neg.txt -numPos 87 -numNeg 39
PARAMETERS:
cascadeDirName: /opencvimgs/haarcascades/data/
vecFileName: /opencvimgs/haarcascades/out.vec
bgFileName: /opencvimgs/haarcascades/neg.txt
numPos: 87
numNeg: 39
numStages: 20
precalcValBufSize[Mb] : 256
precalcIdxBufSize[Mb] : 256
stageType: BOOST
featureType: HAAR
sampleWidth: 24
sampleHeight: 24
boostType: GAB
minHitRate: 0.995
maxFalseAlarmRate: 0.5
weightTrimRate: 0.95
maxDepth: 1
maxWeakCount: 100
mode: BASIC
===== TRAINING 0-stage =====
<BEGIN
POS count : consumed 87 : 87
NEG count : acceptanceRatio 39 : 1
Precalculation time: 1
+----+---------+---------+
| N | HR | FA |
+----+---------+---------+
| 1| 1| 0|
+----+---------+---------+
END>
===== TRAINING 1-stage =====
<BEGIN
POS count : consumed 87 : 87
NEG count : acceptanceRatio 39 : 0.0697674
Precalculation time: 1
+----+---------+---------+
| N | HR | FA |
+----+---------+---------+
| 1| 1| 0|
+----+---------+---------+
END>
===== TRAINING 2-stage =====
<BEGIN
POS count : consumed 87 : 87
NEG count : acceptanceRatio 39 : 0.00945455
Precalculation time: 1
+----+---------+---------+
| N | HR | FA |
+----+---------+---------+
| 1| 1| 0|
+----+---------+---------+
END>
===== TRAINING 3-stage =====
<BEGIN
POS count : consumed 87 : 87
NEG count : acceptanceRatio 39 : 0.000326907
Precalculation time: 1
+----+---------+---------+
| N | HR | FA |
+----+---------+---------+
| 1| 1| 0|
+----+---------+---------+
END>
===== TRAINING 4-stage =====
<BEGIN
POS count : consumed 87 : 87
A possible answer is that you're using too few negative samples.
Read the instruction from OpenCV documents and reference paper from Viola and Jones.
They are using cascaded classifier to achieve high accuracy and low false alarms by eliminate part of the negative samples each time. If you are using too few negative samples, it defeat the purpose of the cascaded classifier in the first place.
Notice that, for practical use, the system have much much more images without faces than with faces.
Related
My Haar training gets hung in the 7th stage. Could someone please help to understand why and how I can get rid of such hung?
I use only 10 stages and have around 500 images.This is my training command:
opencv_traincascade -data data_50x50 -vec samples_50x50.vec -bg neg_70x70.dat -numPos 495 -numNeg 570 -numStages 10 -w 20 -h 20 -featureType HAAR -mode ALL -precalcValBufSize 1024 -precalcIdxBufSize 1024 -minHitRate 0.999 -maxFalseAlarmRate 0.1 -maxWeakCount 1000
Till 6th stage it runs fast (<5 hours), then in 7th stage, I am waiting days:
I can see that FA is decreasing till 6th stage.
+----+---------+---------+
END>
Training until now has taken 0 days 0 hours 27 minutes 11 seconds.
===== TRAINING 6-stage =====
<BEGIN
POS count : consumed 495 : 495
NEG count : acceptanceRatio 570 : 1.59311e-06
Precalculation time: 4
+----+---------+---------+
| N | HR | FA |
+----+---------+---------+
| 1| 1| 1|
+----+---------+---------+
| 2| 1| 1|
+----+---------+---------+
| 3| 1| 0.166667|
+----+---------+---------+
| 4| 1| 0|
+----+---------+---------+
END>
Training until now has taken 0 days 4 hours 2 minutes 32 seconds.
===== TRAINING 7-stage =====
<BEGIN
POS count : consumed 495 : 495
Remains in such stage for ages (>2 days)
Thanks
While searching for this error I only found cases where it happened right at the beginning. In my case, it occurred at the beginning of the 3-stage training.
I'm using OpenCV 2.4.10, with OpenMP enabled. Bellow is the command line I used and the output. Does anyone knows how to solve this problem?
root#6b0f88eaadb9:/opt/ocr-samples3/train-detector# opencv_traincascade -data ./out// -vec ./positive/vecfile.vec -bg ./negative/negative.txt -w 247 -h 80 -numPos 78 -numNeg 1325 -featureType LBP -numStages 8
libdc1394 error: Failed to initialize libdc1394
Training parameters are loaded from the parameter file in data folder!
Please empty the data folder if you want to use your own set of parameters.
PARAMETERS:
cascadeDirName: ./out//
vecFileName: ./positive/vecfile.vec
bgFileName: ./negative/negative.txt
numPos: 78
numNeg: 1325
numStages: 8
precalcValBufSize[Mb] : 256
precalcIdxBufSize[Mb] : 256
stageType: BOOST
featureType: LBP
sampleWidth: 247
sampleHeight: 80
boostType: GAB
minHitRate: 0.995
maxFalseAlarmRate: 0.5
weightTrimRate: 0.95
maxDepth: 1
maxWeakCount: 100
===== TRAINING 0-stage =====
<BEGIN
POS count : consumed 78 : 78
NEG count : acceptanceRatio 1325 : 1
Precalculation time: 6
+----+---------+---------+
| N | HR | FA |
+----+---------+---------+
| 1| 1| 1|
+----+---------+---------+
| 2| 1|0.0211321|
+----+---------+---------+
END>
Training until now has taken 0 days 1 hours 22 minutes 40 seconds.
===== TRAINING 1-stage =====
<BEGIN
POS count : consumed 78 : 78
NEG count : acceptanceRatio 1325 : 0.0928456
Precalculation time: 10
+----+---------+---------+
| N | HR | FA |
+----+---------+---------+
| 1| 1| 1|
+----+---------+---------+
| 2| 1| 1|
+----+---------+---------+
| 3| 1|0.0324528|
+----+---------+---------+
END>
Training until now has taken 0 days 3 hours 19 minutes 57 seconds.
===== TRAINING 2-stage =====
<BEGIN
POS count : consumed 78 : 78
NEG count : acceptanceRatio 1325 : 0.00679104
Precalculation time: 7
+----+---------+---------+
| N | HR | FA |
+----+---------+---------+
| 1| 1| 1|
+----+---------+---------+
| 2| 1| 0.08|
+----+---------+---------+
END>
Training until now has taken 0 days 4 hours 38 minutes 25 seconds.
===== TRAINING 3-stage =====
<BEGIN
POS count : consumed 78 : 78
Train dataset for temp stage can not be filled. Branch training terminated.
How big are you negative images. I had the same problem. The error say that he hasn't enough negative images. Your negative images doe not have to be the same size as your positive. So what I did take the original negative images but a black rectangle on the object. And started again.
The function negative search in the image for new negative images, when he used one negative image he will not use it again. It randomly chosen and has the same size as your positive image.
I can't train the classifier properly.. When I use the generated cascade.xml the object is not recognized. Instead some parts of the object are.
The command line for training is this:
opencv_traincascade -data data -vec samples.vec -bg negatives.txt -numPos 200 -numNeg 100 -numStages 13 -w 80 -h 80 -featureType LBP -minHitRate 0.999 -maxFalseAlarmRate 0.5
While training the output looks like this:
===== TRAINING 10-stage =====
<BEGIN
POS count : consumed 200 : 200
NEG count : acceptanceRatio 100 : 0.00586029
Precalculation time: 2
+----+---------+---------+
| N | HR | FA |
+----+---------+---------+
| 1| 1| 1|
+----+---------+---------+
| 2| 1| 0.09|
+----+---------+---------+
END>
Training until now has taken 0 days 0 hours 6 minutes 9 seconds.
===== TRAINING 11-stage =====
<BEGIN
POS count : consumed 200 : 200
NEG count : acceptanceRatio 100 : 0.00724585
Precalculation time: 3
+----+---------+---------+
| N | HR | FA |
+----+---------+---------+
| 1| 1| 1|
+----+---------+---------+
| 2| 1| 0.09|
+----+---------+---------+
END>
Training until now has taken 0 days 0 hours 6 minutes 49 seconds.
===== TRAINING 12-stage =====
<BEGIN
And when I use it to detect my object (in my case the object is a steering wheel) the result is:
You are using too few images as a samples for the image which has a 80x80 dimension.
I'd minimize the dimension of the images, meanwhile adding some more samples.
Another thing is the set of the negative images. What are you using as a negative samples? Try to enlarge your negative image set by adding this false detected chunks.
I am in Linux with OpenCV 3.0 Alpha
I search alot on the web on training own object detector
But when i follow some instruction, it doesnt work on me. Here's my situtation:
First I download 550 positive samples in 100px width and 40px height
and i also got 550 negative samples in 100px width and 40px height
then create the positives.info and negatives.txt
I am sure the info file and txt file are in correct content and the images are nice enough
then I create sample vec file:
opencv_createsamples -info positives.info -num 550 -w 48 -h 24 -vec cars.vec
and it comes out:
Info file name: positives.info
Img file name: (NULL)
Vec file name: cars.vec
BG file name: (NULL)
Num: 550
BG color: 0
BG threshold: 80
Invert: FALSE
Max intensity deviation: 40
Max x angle: 1.1
Max y angle: 1.1
Max z angle: 0.5
Show samples: FALSE
Width: 48
Height: 24
Create training samples from images collection...
Done. Created 550 samples
It seems ok with the 550 samples,
then i train the cascade:
opencv_traincascade -data data -vec cars.vec -bg negatives.txt -numPos 500 -numNeg 500 -numStages 2 -w 48 -h 24 -featureType LBP
and it comes :
PARAMETERS:
cascadeDirName: data
vecFileName: cars.vec
bgFileName: negatives.txt
numPos: 500
numNeg: 500
numStages: 2
precalcValBufSize[Mb] : 256
precalcIdxBufSize[Mb] : 256
stageType: BOOST
featureType: LBP
sampleWidth: 48
sampleHeight: 24
boostType: GAB
minHitRate: 0.995
maxFalseAlarmRate: 0.5
weightTrimRate: 0.95
maxDepth: 1
maxWeakCount: 100
===== TRAINING 0-stage =====
<BEGIN
POS count : consumed 500 : 500
NEG count : acceptanceRatio 500 : 1
Precalculation time: 0
+----+---------+---------+
| N | HR | FA |
+----+---------+---------+
| 1| 1| 1|
+----+---------+---------+
| 2| 1| 1|
+----+---------+---------+
| 3| 1| 0.414|
+----+---------+---------+
END>
Training until now has taken 0 days 0 hours 0 minutes 3 seconds.
===== TRAINING 1-stage =====
<BEGIN
POS count : consumed 500 : 500
NEG count : acceptanceRatio 500 : 0.578035
Precalculation time: 0
+----+---------+---------+
| N | HR | FA |
+----+---------+---------+
| 1| 1| 1|
+----+---------+---------+
| 2| 1| 1|
+----+---------+---------+
| 3| 1| 0.46|
+----+---------+---------+
END>
Training until now has taken 0 days 0 hours 0 minutes 6 seconds.
And i found the cascade.xml
so I test it, I just use the face detect provided by the opencv, but i use my own cascade file.
but i comes out the blue rectangle box which should mark up the car but just
draw right in the middle of the image. I test positive image and negative image, it just
draw a rectangle in the middle.
which step i gone wrong? and how do i fix it ?
You should change the training parameters. NumberofStages should increase , also you can use HAAR as a featureType , and you can set minhitrate and maxfalselarm rate.
I trained a classifier to detect a MacBeth colorchart which is an artificial pattern and should be easy to detect. Training was done detecting haarfeatures from 1500 negatives and 4000 positives using bg photos from google (http://tutorial-haartraining.googlecode.com/svn/trunk/data/negatives/) that were similar to the environment where the pattern is most probably to be found.
The statistics of the training seemed to be very reasonable:
===== TRAINING 9-stage =====
POS count : consumed 4000 : 4170
NEG count : acceptanceRatio 1500 : 0.00365317
Precalculation time: 3
| N | HR | FA |
| 1| 1| 1|
| 2| 1| 1|
| 3| 1| 1|
| 4| 0.996| 0.865333|
| 5| 0.996| 0.865333|
| 6| 0.99625| 0.688|
| 7| 0.99525| 0.633333|
| 8| 0.99575| 0.517333|
| 9| 0.99525| 0.489333|
It went through all 10-stages and produced a 33kb XML cascade description file.
However when I try to find the pattern, it detects all sorts of things as the pattern.
Does anyone have an idea how to improve the settings or properly find that pattern?
Here are my training parameters:
PARAMETERS:
cascadeDirName: /training3
vecFileName: pos_samples_vec_004.vec
bgFileName: bg5.txt
numPos: 4000
numNeg: 1500
numStages: 10
precalcValBufSize[Mb] : 256
precalcIdxBufSize[Mb] : 256
stageType: BOOST
featureType: HAAR
sampleWidth: 32
sampleHeight: 32
boostType: GAB
minHitRate: 0.995
maxFalseAlarmRate: 0.5
weightTrimRate: 0.95
maxDepth: 1
maxWeakCount: 100
mode: ALL
Increase the number of stages, a minimum of 20 stages are usually needed in order to achieve any amount of specificity in detection. Also keep track of the acceptance ratio in the negatives, the lower it is the better.