--- layout: post title: "Deep Learning Haar Cascade Explained" subtitle: "" description: "翻译" excerpt: "这是我认为最通俗易懂的 Haar 级联分类器的解释说明,文末视频非常形象。" date: 2022-09-06 09:07:00 author: "Rick Chan" tags: ["AI", "Classifier", "Haar"] categories: ["Algorithm"] published: true --- - [1. Haar cascade](#1-haar-cascade) - [2. Cascade Classifier](#2-cascade-classifier) - [3. Haar Cascade - Facial Detection IN ACTION](#3-haar-cascade---facial-detection-in-action) - [4. Next Steps](#4-next-steps) - [5. References](#5-references) - [6. 原文标注](#6-原文标注) - [7. 译注](#7-译注) - [8. 外部参考资料](#8-外部参考资料) [原文连接](http://www.willberger.org/cascade-haar-explained/) “Alright! This is where we start having some fun! The concept behind the Haar Cascade and how it is used in the real world is nothing short of amazing. So what is it?” Haar 级联分类器的背后思想以及其现实应用实在令人倍感惊奇。 ## 1. Haar cascade “Haar Cascade is a machine learning object detection algorithm used to identify objects in an image or video and based on the concept of features proposed by Paul Viola and Michael Jones in their paper "Rapid Object Detection using a Boosted Cascade of Simple Features" in 2001.“ Haar 级联分类器是一种机器学习算法,最初由 Paul Viola 和 Michael Jones 在 2001 年的论文《Rapid Object Detection using a Boosted Cascade of Simple Features》中提出。该算法能够通过特征来识别图像或视频中的物体。 “It is a machine learning based approach where a cascade function is trained from a lot of positive[a] and negative[a] images. It is then used to detect objects in other images.” 通过阳性 images(包含检测目标的样本)[a] 和阴性 images(不包含检测目标的样本)[a]样本集进行训练的级联分类器,能够检测其他图像中的目标物体。 “The algorithm has four stages: 1. Haar[1] Feature Selection 2. Creating Integral Images[2] 3. Adaboost[3] Training 4. Cascading Classifiers“ Haar 级联分类器主要有四个步骤: 1. Haar[1] 特征选择 2. 创建积分图[2] 3. Adaboost[3] 训练 4. 级联各个分类器 “It is well known for being able to detect faces and body parts in an image, but can be trained to identify almost any object.” Haar 级联分类器最著名的应用是检测图像中的人脸或身体,但其实也可以检测其他物体。 “Lets take face detection as an example. Initially, the algorithm needs a lot of positive images of faces and negative images without faces to train the classifier. Then we need to extract features from it.” 本文以人脸检测为例。首先需要大量包含人脸的阳性 image 和不包含人脸的阴性 image 来训练分类器。然后我们从其中提取特征。 “First step is to collect the Haar Features. A Haar feature considers adjacent rectangular regions at a specific location in a detection window, sums up the pixel intensities in each region and calculates the difference between these sums.” 第一步是收集 Haar 特征。可以将 Haar 特征看作检测窗口中相邻的举行区域,将各自区域的像素强度加合,然后计算它们的差值。 ![Integral Images](./img/Deep_Learning_Haar_Cascade_Explained/001.jpg) “Integral Images are used to make this super fast.” 可以使用积分图加快这一运算过程。 “But among all these features we calculated, most of them are irrelevant. For example, consider the image below. Top row shows two good features. The first feature selected seems to focus on the property that the region of the eyes is often darker than the region of the nose and cheeks. The second feature selected relies on the property that the eyes are darker than the bridge of the nose. But the same windows applying on cheeks or any other place is irrelevant.” 但是,这些特征中的很大一部分是与任务目标不相关的(没有意义的特征)。以下图为例,应用第一行的选择窗口可以得到两个好的特征:选取第一个特征,主要是由于眼部区域通常比鼻子和脸颊更黑;选取第二个特征则依赖于眼部比鼻梁更黑这一现象。但是,将同样的检测窗口应用于脸颊或其他部位则得不到有意义的特征。 ![Integral Images](./img/Deep_Learning_Haar_Cascade_Explained/002.png) “So how do we select the best features out of 160000+ features? This is accomplished using a concept called Adaboost which both selects the best features and trains the classifiers that use them. This algorithm constructs a “strong” classifier as a linear combination of weighted simple “weak” classifiers. The process is as follows.” 在人脸检测这样的任务中,特征总数足有 160000 多个,如何从中选取最有意义的特征?这由 Adaboost 算法完成,该算法会选取最佳特征并训练分类器来使用这些特征。Adaboost 算法引入了强分类器和弱分类器的概念,强分类器由弱分类器线性组合而成。其算法过程如下: “During the detection phase, a window of the target size is moved over the input image, and for each subsection of the image and Haar features are calculated. You can see this in action in the video below. This difference is then compared to a learned threshold that separates non-objects from objects. Because each Haar feature is only a "weak classifier" (its detection quality is slightly better than random guessing) a large number of Haar features are necessary to describe an object with sufficient accuracy and are therefore organized into cascade classifiers to form a strong classifier.” 在检测阶段,一个特定的窗口在输入图像上移动,并在每个位置上计算 Haar 特征。下面的视频演示了这一过程。计算结果将与阈值进行比较以区分是否为检测目标,这个阈值是通过学习得到的。由于每个 Haar 特征只是一个“弱分类器”(弱分类器的检测质量仅比随即猜测好一点儿),我们需要大量的 Haar 特征才能够对目标进行精确的检测,因此这些弱分类器被级联成了强分类器。 ## 2. Cascade Classifier ![Integral Images](./img/Deep_Learning_Haar_Cascade_Explained/003.png) “The cascade classifier[b] consists of a collection of stages, where each stage is an ensemble of weak learners. The weak learners are simple classifiers called decision stumps. Each stage is trained using a technique called boosting. Boosting provides the ability to train a highly accurate classifier by taking a weighted average of the decisions made by the weak learners.” 级联分类器[b]由一系列小的阶段组成,每个阶段就是一个弱学习过程。弱学习过程是一个被称作单层分类器的简单分类器。每个阶段由 boosting 算法进行训练。通过对每个阶段的决策进行加权,Boosting 提供了训练高准确度分类器的方法。 “Each stage of the classifier labels the region defined by the current location of the sliding window as either positive or negative. Positive indicates that an object was found and negative indicates no objects were found. If the label is negative, the classification of this region is complete, and the detector slides the window to the next location. If the label is positive, the classifier passes the region to the next stage. The detector reports an object found at the current window location when the final stage classifies the region as positive.” 每个阶段的分类器对滑动窗口的当前区域打标签(阳性 或 阴性)。阳性意味着检测到了目标,阴性意味着没有检测到目标。如果没有检测到目标则完成当前区域的检测,滑动窗口移动到下一个区域。如果检测结果为阳性,则分类器将当前区域传递给下一个阶段继续进行检测。当所有检测阶段都返回阳性,那么检测器报告在当前位置发现了目标物。 “The stages are designed to reject negative samples as fast as possible. The assumption is that the vast majority of windows do not contain the object of interest. Conversely, true positives are rare and worth taking the time to verify. - A true positive occurs when a positive sample is correctly classified. - A false positive occurs when a negative sample is mistakenly classified as positive. - A false negative occurs when a positive sample is mistakenly classified as negative.” 所有的阶段都被设计为能够快速的滤除阴性采样。这是因为我们假设大部分的滑动窗口不包含待检测目标。相反,真阳性比较稀少,值得多花费一些时间去处理。 - 真阳性:是指被正确分类的阳性采样。 - 假阳性:是指被错误分类的阴性采样。 - 假阴性:是指被错误分类的阳性采样。 “To work well, each stage in the cascade must have a low false negative rate. If a stage incorrectly labels an object as negative, the classification stops, and you cannot correct the mistake. However, each stage can have a high false positive rate. Even if the detector incorrectly labels a nonobject as positive, you can correct the mistake in subsequent stages. Adding more stages reduces the overall false positive rate, but it also reduces the overall true positive rate.” 为了保证好的检测效果,级联分类器每个阶段的假阴性率必须非常低。如果某个目标(阳性采样)被错误的识别为阴性,那么检测就会停止,我们再也没有机会去纠正这个错误了。然而,每个阶段的假阳性率可以比较高,甚至可以将不包含目标的标记为阳性,因为可以在下一个阶段中纠正这个错误。增加处理阶段将降低假阳性率,同时也会降低真阳性率。 “Cascade classifier training requires a set of positive samples and a set of negative images. You must provide a set of positive images with regions of interest specified to be used as positive samples. You can use the Image Labeler to label objects of interest with bounding boxes. The Image Labeler outputs a table to use for positive samples. You also must provide a set of negative images from which the function generates negative samples automatically. To achieve acceptable detector accuracy, set the number of stages, feature type, and other function parameters.“ 训练级联分类器需要一组阳性采样和一组阴性采样图像。可以提供一组包含感兴趣区的阳性图像作为阳性采样。使用 Image Labeler 标记由包围盒框选的目标。Image Labeler 输出阳性采样标签。同时,还需要一组阴性图像,算法函数将自动生成阴性采样。为了达到可接受的精度,需要设置级联深度,特征类型和其他参数。 ![Integral Images](./img/Deep_Learning_Haar_Cascade_Explained/004.png) “The video below shows this in action.“ 下面的视频展示了以上所述的检测过程。 ## 3. Haar Cascade - Facial Detection IN ACTION “If a picture is worth a thousand words this would be a million words. This is where it all comes together. The Ahh-hah moment.” “This simple video helped crystalize for me how this algorithm works. Here are some observations: - Notice how the algorithm moves the window systematically over the image, applying the Haar features as it is trying to detect the face. This is depicted by the green rectangles. - Notice underneath the red boundary square, we see the classifier executing stages quickly discarding window frames that are clearly not a match (stages 1-25) - To the right of the stage we see the how well it performed in identifying the face. - Notice as it gets closer and closer to identifying the face, the number of stages increases into the 20s. (around the 1 minute mark). This demonstrates the cascading effect where the early stages are discarding the input as it has identified them as irrelevant. As it gets closer to finding a face it pays closer attention.” 本文视频能够帮助大家理解算法的工作过程。我们可以观察到下面一些情况。 - 注意观察算法是如何有组织滑动窗口来覆盖图像,应用 Haar 特征并试图检测出人脸。这一过程由绿色矩形来描述。 - 注意,在红色矩形边界下,我们观察到,分类器迅速的丢掉了没匹配成功的窗口。 - 在其余的阶段中,我们能观察到分类器很好的检测出了人脸。 - 注意,随着越来越接近人脸检测结果,检测过程所消耗的时间也越来越长,这表示越接近目标分类器的工作越精细,在检测的初始阶段,分类器只要进行粗略的分类即可。 ![Integral Images](./img/Deep_Learning_Haar_Cascade_Explained/005.gif) [点击查看原始视频](https://www.youtube.com/watch?v=hPCTwxF0qf4&feature=youtu.be) “Let me know if you have any questions or have any comments below. I want to make sure I got this post right. It will be critical that you understand this before we go into the next section where we will implement a full Custom Object Haar Cascade detector.” 如果对本文有任何疑问请告知,我需要确保本文的正确性。理解本文的内容是至关重要的,这有助于进入下一个阶段的学习——定制 Haar 级联分类器。 ## 4. Next Steps “I don't know about you, but I find the best way to understand something is by doing it. Conceptually we now have an idea for how the machine learning Haar Cascade object detection works. Now lets build a real world custom Object Detector, train it, and see it in action. I have a really cool example for us! Click on the button below.” 按钮的连接错了,所以我实在不知道作者所说的 cool example 是哪一个,不过作者网站确实有一些很酷的例子,感兴趣的朋友可以点击连接进入[作者网站](http://www.willberger.org/category/ai/),也可以自行练习一些例子,毕竟理解事物的最好方法就是实践。 ## 5. References 1. Wikipedia, Wikipedia. “AdaBoost.” Wikipedia, Wikimedia Foundation, 13 Jan. 2018, en.wikipedia.org/wiki/AdaBoost. 2. Docs, OpenCV. “Face Detection Using Haar Cascades.” OpenCV: Face Detection Using Haar Cascades, 4 Aug. 2017, docs.opencv.org/3.3.0/d7/d8b/tutorial_py_face_detection.html. 3. Wikipedia, Wikipedia. “Viola–Jones object detection framework.” Wikipedia, Wikimedia Foundation, 4 November. 2017, en.wikipedia.org/wiki/Viola–Jones_object_detection_framework. 4. Wikipedia, Wikipedia. “Cascading Classifiers.” Wikipedia, Wikimedia Foundation, 15 October. 2013, en.wikipedia.org/wiki/Cascading_classifiers. 5. Mathworks, Mathworks. “Train a Cascade Object Detector” Mathworks, 2017, www.mathworks.com/help/vision/ug/train-a-cascade-object-detector.html. ## 6. 原文标注 [1] Haar A Haar-like feature considers adjacent rectangular regions at a specific location in a detection window, sums up the pixel intensities in each region and calculates the difference between these sums. This difference is then used to categorize subsections of an image. For example, let us say we have an image database with human faces. It is a common observation that among all faces the region of the eyes is darker than the region of the cheeks. Therefore a common Haar feature for face detection is a set of two adjacent rectangles that lie above the eye and the cheek region. The position of these rectangles is defined relative to a detection window that acts like a bounding box to the target object (the face in this case). [2] Integral Images An integral image is summed-area table is a data structure and algorithm for quickly and efficiently generating the sum of values in a rectangular subset of a grid. To understand this look at image 1 and image 2. Image 1 is the source table, Image 2 is the summation table. Notice in Image 2 row 1, col 2 value 33 is sum of Image 1 row 1 (col 1 + col 2). ![Integral Images](./img/Deep_Learning_Haar_Cascade_Explained/006.png) [3] Adaboost Problems in machine learning often suffer from the curse of dimensionality — each sample may consist of a huge number of potential features (for instance, there can be 162,336 Haar features, as used by the Viola–Jones object detection framework, in a 24×24 pixel image window), and evaluating every feature can reduce not only the speed of classifier training and execution, but in fact reduce predictive power, per the Hughes Effect. Unlike neural networks and SVMs, the AdaBoost training process selects only those features known to improve the predictive power of the model, reducing dimensionality and potentially improving execution time as irrelevant features need not be computed. ## 7. 译注 [a] positive and negative 指阳性(positive)或阴性(negative),包含待检测目标的为阳性,不包含待检测目标的为阴性。检测结果可能出现假阳性或假阴性,是指检测出现了错误,相反则为真阳性或真阴性。 [b] Cascade Classifier 级联分类器是将一些小的分类过程组合成完整分类器的方法。以下图为例,需要将图中的“+”和“-”数据分开,仅靠一个分类过程很难实现。 ![Integral Images](./img/Deep_Learning_Haar_Cascade_Explained/007.png) 但将下图中的三种分类方法组合到一起就可以得到一个非常好的分类器。其中每个分类方法都是级联分类器中的一个弱分类器,它们组合成了一个强分类器。注意,每个弱分类器都只对部分数据进行了分类。 ![Integral Images](./img/Deep_Learning_Haar_Cascade_Explained/008.png) ## 8. 外部参考资料 以下是译者本人提供的一些额外参考资料: 1. [人脸检测之Haar分类器](https://www.cnblogs.com/zyly/p/9410563.html) 2. [What’s the Difference Between Haar-Feature Classifiers and Convolutional Neural Networks?](https://towardsdatascience.com/whats-the-difference-between-haar-feature-classifiers-and-convolutional-neural-networks-ce6828343aeb) 3. [Computer Vision — Detecting objects using Haar Cascade Classifier](https://towardsdatascience.com/computer-vision-detecting-objects-using-haar-cascade-classifier-4585472829a9) 4. [Deep Learning Haar Cascade Explained](http://www.willberger.org/cascade-haar-explained/) 5. [浅析人脸检测之Haar分类器方法:Haar特征、积分图、AdaBoost、级联](https://blog.csdn.net/zouxy09/article/details/7922923) 6. [AdaBoost & AdaRank](https://my.oschina.net/supersonic/blog/379438) 7. [Cascade Classifier Training](https://docs.opencv.org/2.4/doc/user_guide/ug_traincascade.html) 8. [使用Haar分类器进行面部检测](https://www.cnblogs.com/21207-iHome/articles/6253796.html)