OpenCV Haar Cascade training parameters and time - opencv

I have been fiddling around with OpenCV's cascade trainer in an attempt to train my own classifier. The problem is that it has been training for 25+hours now and it is yet to even pass stage 1.
Initially, I ran it with the following command
nohup opencv_traincascade -data data -vec board.vec -bg bg.txt -numPos 580 -numNeg 1160 -numStages 2 -w 115 -h 153 -featureType LBP &
After about 24 hours, it wasn't able to get through even stage 1. A look into the nohup.out file, I realized that the default precalcValBufSize was set to 1024Mb. I figured maybe increasing this to 4096Mb will help with the processing so I went ahead and re-started the training with the following command
nohup opencv_traincascade -data data -vec board.vec -bg bg.txt -numPos 580 -numNeg 1160 -numStages 2 -w 115 -h 153 -featureType LBP -precalcIdxBufSize 4096 -precalcValBufSize 4096 &
The training has been running for almost 25 hours now and it also hasn't even produced the XML file for stage 0.
A look into the process itself states that its using 8284M of virtual memory but 930M of physical memory and this shows all the files currently in use by the process. Its doing a great job burning through my cores but none at producing any results or even letting me know how far its got.
My question(s) is/are, is there any way of making it use more of my actual physical memory in attempt to speed it up? If not, are there any adjustments I need to make on my training dataset?
Side Note: I know the general standard for dataset size is 24x24 but I already tried that out and it was really horrible even after 10 stages.
At that size, my object's outline no longer attains its features correctly. At 24x24 or even 48x48 it looks like a giant horizontally distorted blob of black pixels without even some of its unique features being visible.

I bet that the problem is in samples size.
The bigger size requires muuuuch bigger memory buffers and much more time. Detecting features is quite a difficult operation.
You have to minimize your samples (don't forget to rerum cv_createsamples(...)). Samples shouldn't be square it may be 25*15 (make sure proportions are saved and the biggest side is about 30px).
You are using featureType LBP which itself is faster than Haar.

Related

OpenCV Haar Classifier: training stops prematurely

I have been trying to train image databases to detect faces using Haar cascades. I have made 2 attempts:
1) I have used the following database for positive images:
http://robotics.csie.ncku.edu.tw/Databases/FaceDetect_PoseEstimate.htm#Our_Database_ (6660 images)
For negative images I have used this database:
https://github.com/sonots/tutorial-haartraining/tree/master/data/negatives (3300 images)
I have used this command to train the samples:
opencv_createsamples -info info.dat -vec samples2.vec -w 32 -h 24 -num 6660
I have used this command to train the data:
opencv_traincascade -data ./classifier3 -vec samples2.vec -bg bg.txt -numPos 6000 -numNeg 12000 -numStages 30 -precalcValBufSize 5120 -precalcIdxBufSize 5120 -numThreads 12 -acceptanceRatioBreakValue 10e-5 -w 32 -h 24 -minHitRate 0.99 -maxFalseAlarmRate 0.5 -mode ALL
The training goes on up to stage 9. Then the acceptanceRatio break value is crossed.(The required acceptanceRatio for the model has been reached to avoid over-fitting of training data. Branch training terminated.)
I don't understand the issue here. I have only used the recommended values for the parameters. I had tried changing the minHitRate to 0.95, yet the result is the same. I can think of some potential reasons:
i) I had used the positive images directly without cropping. But I don't
think that should be an issue, as the background is completely plain.
ii) The image database contains faces in different poses. That could lead
to complications while training. Is it a good idea to train faces
under different poses using the same cascade classifier? Or should I
use different classifiers for each pose?
iii) My negative images might be too different compared to the positive
images. Is that the case? If yes, what kind of negative images should
I be looking for?
I tried testing the cascade.xml file on a few sample images, but nothing is detected at all.
2) Keeping in mind the potential reason i), I used this database already cropped, for positive images: http://conradsanderson.id.au/lfwcrop/ (around 13000 images)
But still the problem persists. This time it trains upto stage 11. In this case I used -numPos as 8000 -numNeg as 20000( increased the ratio to give the training more negative samples), -w as 24 and -h as 24.
Can anyone please guide me here?

Hand detection and tracking methods

So, guys, please help me with detecting/tracking hand for user who are sitting at the computer in front of computer(laptop) frontal camera.
I've tried these methods:
Colour based detection(I've detected the human face by opencv haar cascade face detection and extracted the skin HSV ranges. In the next I've found the objects with the skin colour. For example, the face I can remove by knowing face detection by haar cascade, but what about other human body parts and background objects with skin colour if I need only hand? How to make this algorithm be more stable for illumination?)
Train own haar cascade classifier(I've trained my own cascade to detect hand using 3.5k positive and 4k negative photos. It took 3 days to train. The dataset is pretty rich(various hand configurations and orientations, light conditions, different backgrounds). It works not so bad but it's very slow because of I set scaleFactor=1.3 and minNeighbors=70. If I decrease minNeighbors false alarms will grow tremendously and small reactangles will cover the whole video frame.
Training params:
opencv_traincascade -data data -vec samples.vec -bg neg.txt -numStages 16 -minhitrate 0.999 -maxFalseAlarmRate 0.5 -numPos 3200 -numNeg 3900 -w 24 -h 24 -mode ALL -precalcValBufSize 1024`` -precalcIdxBufSize 1024
Train LBP cascade classifier (The training was faster than haar cascade and detection works closer to real time but this detection method has a lot mishits) Training params: opencv_traincascade -data lbp -vec samples.vec -bg neg.txt -numStages 25 -minHitRate 0.999 -maxFalseAlarmRate 0.5 -numPos 3200 -numNeg 3900 -w 24 -h 24 -mode ALL -precalcValBufSize 4096 -precalcIdxBufSize 4096 -featureType LBP I tried the different values of numStages from 16 to 25.
Camshift algorithm to track hand The source code is here http://pastebin.com/q5zK8cZt. How it works? Just need to mark 4 poins around detected object and this algorithm must track it and draw rectangle around. The problem is if I started to move my hand this rectangle starts to grow and cover the whole video frame. It looks like this algorithm works only for small objects (or the objects are locating long distance from camera)
Maybe I need to mix these methods or you will suggest another? Maybe I need to train neural network for example YOLO? I don't have wish to do it cause of it takes too long time and have to rent GPU-based servers.
GPU servers? No, you don't: there is a web based backend for object recognition. If you want to use Yolo you will need to mark a huge image train set (Around 2000 per class). I can advise fetch images from here using script like that
(function(global) {
const next = () => Array.from(document.querySelectorAll('.search-pagination__button-text'))[1].click();
const uuid = () => Math.random().toString(36).substring(7);
const sleep = (timeout = 5000) => new Promise((res) => setTimeout(() => res(), timeout));
global.urls = [];
global.next = () => next();
global.start = async () => {
for (let i = 0; i !== 81; i++) {
window.scrollTo(0,document.body.scrollHeight);
await sleep(5000);
document.querySelectorAll('.search-content__gallery-results figure > img[src]').forEach(({src}) => global.urls.push(src));
next();
await sleep(5000);
}
};
})(window);
After that, you need to mark bounded boxes of objects in images. There is a online tool, which work right in your web browser
For training neural network follow this instruction, binaries also can be taken from here.

haar classifier: XML file got from haar cascade

I used typical haar-cascade of OpenCV.
And setup stages as 5 in training process,but in xml & cascade folder only 3 stages were found.
Why I got fewer stages than expected?
Any solutions?
Take this example training command:
opencv_traincascade -data classifier -vec samples.vec -bg negatives.txt\
-numStages 20 -minHitRate 0.999 -maxFalseAlarmRate 0.5 -numPos 1000\
-numNeg 600 -w 80 -h 40 -mode ALL -precalcValBufSize 1024\
-precalcIdxBufSize 1024
This has a maxFalseAlarmRate of 0.5, when the classifier reaches this value it will finish.
For your problem, I imagine you have set the numStages to 5 but after 3 stages it has reached the maxFalseAlarmRate and completed the training.
In order to confirm/dispell this you would need to provide:
Your training command (as above)
The output from your last training stage.
You most likely have not provided traincascade enough information to learn from. This is most likely because we humans are incredibly lazy and hate to work. It would have kept going if it thought it could learn more from the data you specified.
Take more positives. Remember that you can take multiple images of your object a slightly tilted angles (10º-20º or so). And be sure to provide at least hundreds of your objects, especially if there is quite a bit of variation between your objects, like there are with faces.
If you're still stuck, see this tutorial I wrote that can hopefully help you and others: http://johnallen.github.io/opencv-object-detection-tutorial/

OpenCV - LBP Traincascade can’t pass Precalculation in Stage 0, but HAAR works fine?

I run opencv_traincascade, and I never reach the precalculation time for stage 0.
Edit: My problem seems to be specific to LBP training. I changed featureType to HAAR and the classifier below finished training in a matter of minutes
Edit: Precalculation time is in seconds, so at stage 0 you should see it reach precalculation within 10 seconds, but I've seen it as high as 40 seconds. Precalculation time may increase steadily from stage to stage, and then suddenly increase dramatically to half an hour in the later stages (and perhaps even longer than that if you are working with thousands of samples, but I haven't got that far yet)
(I will continue to update this post filling in things I've come to understand that prevented me from understanding precalculation, and perhaps find out why HAAR training would work while LBP doesn't. It would probably just require looking at the source code for initiating the stage cycle)
Problem
I run opencv_traincascade, and I never reach the precalculation time for stage 0.
I’m using an MacBook Air purchased in 2014.
Processor 1.3 GHz Intel Core i5
Memory 4 GB 1600 MHz DDR3
Graphics Intel HD Graphics 5000
Software OS X 10.9.4 (13E28)
My terminal looks something like this, and runs some 390% (really?) of my CPU and 4 threads in my Activity Monitor.
===== TRAINING 0-stage =====
<BEGIN
POS count : consumed x : x
NEG count : acceptanceRatio y : 1
Although I’ve tried creating many classifiers, for the sake of sharing something we can all relate to, I’m going to reference a tutorial for car detection since I get the same result not matter if it’s a training of my own or not. This trainer has been published, and I’ve seen other people reference it, so I assume it works.
https://www.youtube.com/watch?v=WEzm7L5zoZE
On my desktop I have:
A pos folder with the relative file paths and information (1 0 0 100 40) in a cars.info folder;
A bg.txt file with relative paths to a neg folder containing the negative samples;
An empty data folder named “data”;
A cars.vec file of positive samples. I can view the vector file and the images are there.
There are 550 positive samples at 100x40 originally, and 48x24 after using createsamples to create the vec file
There are 500 negative samples at 100x40
Here was the createsamples command, for reference:
opencv_createsamples -info cars.info -num 550 -w 48 -h 24 -vec cars.vec
Then I run the following command:
opencv_traincascade -data data -vec cars.vec -bg bg.txt -numPos 500 -numNeg 500 -numStages 2 -w 48 -h 24 -featureType LBP
PARAMETERS:
cascadeDirName: data
vecFileName: cars.vec
bgFileName: bg.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
This is my output that I’ve never seen reach precalculation for LBP, no matter what I’m trying to train; how I’ve tried to change the sample size, resolution of images, minHitRate, or lowering numPos.
===== TRAINING 0-stage =====
<BEGIN
POS count : consumed 500 : 500
NEG count : acceptanceRatio 500 : 1
Please ask for any information I failed to supply. I apologize.
I’ve read about trainers being stuck in infinite loops, and modification of the source code needed. However, I hope that can be avoided since this appears to work for others (at least the author).
Thank you all for all the past questions and answers that have helped me with various coding projects.
I have met such problem before. The way I solve it is to set the values of precalcValBufSize and precalcIdxBufSize to 0 and 0. It seems that precalculation can not work for LBP. However, it works well after they were set to 0. The training process for LBP is much faster than Haar.

TrainCascade stuck on getting new negatives

I'm working with OpenCV 2.4.7 on windows. I'm using TrainCascade to train a new Haar cascade for eyeglasses using the following command:
opencv_traincascade -data trainCascade20 -vec vector3.vec -bg infofile3.txt -numStages 40 -minHitRate 0.999 maxFalseAlarmRate 0.5 -numPos 170 -numNeg 1000 -w 20 -h 20 -mode ALL -precalcValBufSize 1024 -precalcIdxBufSize 1024
It's stuck (or progressing very slow) on stage 24 on the phase of getting new negatives. The negative images file "infofile3.txt" contains about 12K negative image.
Can someone please explain why it's progressing so slowly and what can I do make it progress (a lot) faster?
Thanks in advance,
Gil.
Around 24 hours sounds normal to me. Haar training can actually take up to days depending on size and number of samples. And of course on the computer as well. The longest my training took was approximately a week for hand detection.
If you are really worried, to check whether the haar training is still on-going, you can try to generate an intermediate haar cascade xml file, from the data available. If you are able to generate the xml file, it would show that it's still running(albeit slow) and not stuck.
How to improve the haar training speed, the only solution I know or used before is "paralleling"
A quick search on google about that leads to a few link, here's one of them: http://www.computer-vision-software.com/blog/2009/06/parallel-world-of-opencv/
I have used such methods, and it's pretty efficient in cutting the time taken to train the Haar Cascade. So hope this method suits you well. Do try my method of generating an immediate xml file from the current data available first though. If there is any needs, do comment, I try get back to you soon. Cheers.

Resources