Before I get into some technical details, here's a youtube video where you can see the OpenCL implementation of my detector in action:
Pretty neat, right? :) So what you just saw was an implementation of a detector based on the WaldBoost algorithm (a variant of AdaBoost) that had as its input a classifier trained for detecting frontal faces (and an awesome video of course) running on a GPU.
If you know anything about boosting algorithms, you'll know that one strong classifier is usually composed of lots of weak classifiers (which are usually very simple and computationally inexpensive functions) - in my case there are 1000 weak classifiers where each uses Local Binary Patterns to extract a feature from the input texture. Unfortunately such strong classifier is resolution dependent, and to be able to detect objects of various sizes in the input image, we need a pre-processing step.
During pre-processing we create a pyramid of images by gradually down-scaling the input (oh and we don't need colors, so we also convert it to greyscale). This way the detector can still detect only faces with resolution of 24x24, but using a mapping function we will know when it actually detected something in any of the downscaled versions of the image and there we have resolution independent detector. Interesting tidbit: it turned out that creating the pyramid texture by putting the downscaled images horizontally instead of vertically (which you can see on the image below) slightly improved performance of the detector - simply because the texture cache unit had higher hit ratio in such setup, but since the pyramid texture is approximately 3.6 times larger than the width of the original image, the detector wouldn't be able to process HD (1280x720) nor Full-HD (1920x1080) videos, because maximum texture size for OpenCL image is 4096 pixels (when using vertical layout though 1080 x 3.6 ~= 3900, so even Full-HD videos can be processed).
![]() |
| Left - original image, right - pyramid of downscaled images (real pyramid texture also has the original on top) |
Once we have our pyramid image, it's divided into small blocks, which are processed by the GPU cores and each work item (or thread if you wish) in this block is evaluating the strong classifier at a particular window position of the pyramid image. Overall we'll evaluate every window position - think of every pixel. (in reality it's more complicated than that - the detector is using multiple kernels and each is evaluating only a part of the strong classifier - that's because WaldBoost can preliminary reject a window without evaluating all weak classifiers, so when a kernel finishes it just reduces the number of candidate windows and next kernel continues to evaluate only windows that survived the previous steps - this also ensures that we keep most of the work items in the work groups busy).
Once the detector finishes, we have a couple of window positions in the pyramid image and response value of the strong classifier in these windows, and these are sent back to the host. The CPU can then finish the detection (by simply thresholding the response values) and map the coordinates back to the input image. If you watched the video carefully you'd have noticed that there are multiple positive responses around a face, so this would be also a good place to do some post-processing and merge these. Plus there's a false detection from time to time, so again good place to get rid of them.
You're surely asking how does this compare to a pure CPU implementation and as you can imagine having to evaluate every window position in the pyramid image is very costly and even optimized SSE implementations can't get close to performance of a GPU (even though you need to copy a lot of data between the host and the GPU). So a simple graph to answer that (note the logarithmic scale):
![]() |
| Processed video frames per second (CPU: Core2 Duo E8200 @ 2.66GHz; GPU: GeForce GTX 285 - driver ver 270) |
Grab the code branch from Launchpad (bzr branch lp:~mhr3/+junk/ocl-detector), or get the tarball (the only dependencies are glib, opencv plus libOpenCL.so somewhere where the linker can find it). Run it with `./oclDetector -s CAM` (and if that doesn't seem to detect anything try `./oclDetector -r -20 -s CAM`).


Nice to see OpenCL and nice work :)
ReplyDeleteAwesome work! Have you tried using this for streaming video e.g from a webcam? I'm wondering if this can be used for face detection and login..... at some point of time in the future.
ReplyDeleteSure, it works fine for stream from a webcam as well, but the algorithm detects objects, you would need another one to actually recognise a face.
DeleteNice work: robust and fast. It's time to replace the old (overused) opencv's adaboost classifiers.
ReplyDeleteHey, compiled everything to Windows 7 with a GeForce GT 335 on my notebook.
ReplyDeleteIt works but the output is a blank and hanged windows ...
Any ideas ?
Best !
Hi Mauricio,
DeleteCan you list the exact steps you took to compile this for Windows?
This comment has been removed by the author.
DeleteUgh, didn't mean to remove that comment.
DeleteI have also compiled for Windows 7, but I get a runtime error. Do I have to use a specific version of OpenCL? I use the one bundled with CUDA 5.0. When I run the program I get this output:
$ ./waldboost -s -v ../waldboost/test.mkv
** (waldboost.exe:8088): DEBUG: Last discarding classifier set to 83
Using device: GeForce GTX 580
Trying to open ../waldboost/test.mkv
Compiler did not align stack variables. Libavcodec has been miscompiled
and may be very slow or crash. This is not a bug in libavcodec,
but in the compiler. You may try recompiling using gcc >= 4.2.
Do not report crashes to FFmpeg developers.
Got a frame [704x416], 3 channels, format: 8
Compiling for NVIDIA device...
Build log:
main.cpp:1150: clEnqueueNDRangeKernel failed! Invalid kernel arguments
I believe this is fixed in the bzr repo.
DeleteHey, thanks for the quick reply! I will try that, had just used the tar-ball previously.
DeleteThat did the trick. The '-s' option (showing results on screen) doesn't work for me. It seems that Windows never gets the time to render the image, but inserting a Sleep(1000) (one sec) does not fix it. However, the '-o' output works nicely.
Deletehi,
ReplyDeleteI am wondering, if you would be able to share the the code?
Hi,
ReplyDeleteI am a graduate student majoring in Computer Engineering. I was determining the topic of my thesis and I am interested in facial identification. Your work has inspired me a lot and I was wondering if you could send me a copy of your paper for reference? Thank you.
Yours sincerely
Xiaoyu
Hey, sorry but it's not in english... Otherwise it'd be linked already. :)
DeleteThis comment has been removed by the author.
DeleteCool! Are you planning to also add face recog?
ReplyDeleteHi,
ReplyDeleteI would like to use some of this for my thesis. Got it up and running with a few changes :) Do you have some documentation on it or on the different methods used? In whatever language. Maybe some references ?
Thanks
Perhaps take a look at the references in my thesis itself - http://www.fit.vutbr.cz/study/DP/DP.php.en?id=10355 (no worries the references are in english :)
DeleteThis comment has been removed by the author.
DeleteHi. I have looked at the references and i think i understand the theory behind the waldboost algorithm. I have your code working and i would like to play a bit around with it. If you have the time i would be very happy if i could help me. Could you explain the algorithm of the program to me in english? How the actual searching in the pyramid image is being done and so on. What does the different kernels do (1,2,3 and 4)?
ReplyDeleteI could find out myself by inspecting the code but i want to spare some time and get a better understanding if you have time to explain these things :) My mail is cped08@student.aau.dk if the contact is easier there.
Hope you have a little spare time to explain some of the key things.
Best regards
sorry for distrubing you, but i just want to share the article about face detection,
ReplyDeletelet's check this link http://repository.gunadarma.ac.id/bitstream/123456789/3365/1/Automed%20Face%20Detection%20and%20Feature%20Extraction%20Using%20Color%20Feret%20Image%20Database.pdf
i wish that article can be usefull
Hi, Would you be able to do contract work on OpenCL for us. We would like to build a people counter to ID and count individuals/people as they walk into a store. Thanks.
ReplyDeleteHi,
ReplyDeletegood job, post it as a library for sale on: www.myseyet.eu
Hi,
ReplyDeleteI'm getting CL_INVALID_KERNEL_ARGS error on main.c:1150 when i'm trying to run.
What could be the problem?
http://mhr3.blogspot.com/2012/03/face-detection-with-opencl.html?showComment=1351176555990#c7203905850195971137
DeleteHow can i generate a XML file file for detecting another object?
ReplyDelete