《FAQ:OpenCV Haartraining》——使用OpenCV训练Haar like+Adaboost分类器的常见问题

最近使用OpenCV训练Haar like+Adaboost分类器,查阅了一些资料,这些资料对训练过程陈述的很详细,但是缺少一些细节,偶然看到了一篇英文资料,觉得很好,简单翻译了自己觉得有用的部分。

原文链接:FAQ:OpenCV Haartraining

关于正样本图片

1、 I have  positive images, how create vec file of positive samples?
     有了正样本图片,如果生成对应的vec文件?
 OpenCV中有一个工具可以生成vec文件,在安装目录C:\Program Files\OpenCV\apps\HaarTraining\src createsamples.cpp。
使用方法:
createsamples -info positive_description.txt -vec samples.vec -w 20 -h 20
2、What’s positive description file?
    正样本描述文件是什么?
以人脸为例,在正样本的图片中,可能有数个人脸,每个人脸的位置可以用一个矩形框来表示:矩形左上角是x, y,宽、高是width,height。所以我们可以写一个文件来表示每张图片中的每个人脸如下:
positive_image_name  num_of_objects x y width height x y width height …
像这样,指定了正样本中需要检测目标位置的文件就叫正样本描述文件。在生成vec文件时,添加到vec文件中的是需要检测的目标,而不是整张图片。本质上来讲,vec文件用来加速机器学习。
3、Do I always need description file, even if I have only one object on a image?
    描述文件是不是必须的?如果正样本图片中只包含一个需要检测的目标时呢?
    使用createsamples工具时,描述文件是必须的。如果正样本图片中只有一个被检测物,矩形框就是整幅图片。你也可以自己编写工具来生成vec文件。

4、Should lightning conditions and background be various on positive images?
    正样本图片中的关照和背景的变化是否需要很大?
是的,而且这很重要。在正样本图片中,除了被检测目标,剩下的就是背景。应该尝试用随机噪声来填充背景,避免没有变化的背景。
5、How much background should be on positive image?
    在正样本图片中,应该有多少背景?
如果在正样本图片中,背景相对被检测目标来说,所占像素很多的话,会产生不好的效果,因为haartraining(haar训练 程序)会把背景当作被检测物的特征记下来。如果在正样本图片中根本没有背景像素,也会造成训练的结果不好。在正样本图片中有很少背景是比较理想的。
6、What’ s  -w and -h should I put in createsamples? Should it be always square?
    在使用createsamples工具时,-w -h参数应该设定为多少?是不是一定要设定成正方向?
-w -h参数的值应该根据希望检测目标的比例来设定。但是,使用生成的分类器来进行检测时,比-w -h所确定的形状小的目标不会被检测到。在检测脸时,通常使用的值是24*24,20*20。你也可以使用24*20,20*24等等类似的值。

7、Errors during vec file generation: Incorrect size of input array, 0 kb vec file,
    生成vec文件时报错:输入矩阵大小错误,vec文件0kb
-首先,检测描述文件:例如正样本图片的路径是否正确
-描述文件末尾不能有空行
-正样本图片的分辨率不能小于设置的-w -h参数
-检查正样本图片是否可用,是否被占用
-正样本图片格式是否正确。支持的格式有jpeg、bmp、ppm。

关于负样本图片

1、What negative images should I take?
    应该使用什么样的负样本图片?
可是使用任何OpenCV支持的图片格式,而且其中不能包含被检测目标。负样本图片应该变化很大,多种多样。

2、Should negative images have the same size?
    负样本图片是否需要缩放到同样大小?
不需要,但是负样本图片不能小于使用createsamples工具生成vec文件时设定的-w -h参数。

3、What’s description file for negative image?
    负样本的描述文件是什么样的?
负样本的描述文件是一个简单的文本文件,通常命名为negative.dat。它指定了负样本图片,并且不能有空行(包括末尾)。例如:
     image_name1.jpg
    image_name2.jpg
4、How many negative/positive image should I take?
    通常需要使用多少正/负样本图片?
这往往根据你的需求来确定。例如,对cascades来说,需要有1000个正样本和2000个负样本。
比较好的比例关系是 positive : negative = 1:2,但这并不是硬性规定。我推荐先使用少量的样本来尝试产生 cascades,然后再扩大样本。

关于haartraining.exe的使用

1、Example of launching
    使用haartraining的示例
vec文件为samples.vec,负样本描述文件为negative.dat。使用haartraining.exe的方法如下:
 haartraining -data haarcascade -vec samples.vec -bg negatives.dat -nstages 20  -minhitrate 0.999 -maxfalsealarm 0.5 -npos 1000 -nneg 2000 -w 20 -h 20 -nonsym -mem 1024

-data haarcascade  
 
       生成的xml文件目录为haarcascade 

-vec   samples.vec         
    
vec文件为samples.vec
-bg   negatives.dat          
  
   负样本描述文件为negatives.dat
-nstages 20            
  
           分类器的级联层数
 -minhitrate 0.999               每一层的最低正确检测率99.9%
-maxfalsealarm 0.5             最大 错误接受 率50%
-npos 1000 -nneg 2000     正、负样本数
-w  20 -h 20            
  
             -w -h参数与生成vec文件时设置的数值相同

2、What’ s falsealarm and hitrate of stage?
  stage的 ”错误接受率“ 和 ”正确检测率“ 是什么?
要了解这两个参数,需要查阅adaboost算法中关于强分类器的理论。stage就是强分类器。简单来说:

如果有1000个正样本,你希望检测系统能检测出其中的900个,期望的”正确检测率“就是900/1000 = 0.9。通常将 minhitrate设置为0.999;
如果有1000个负样本,如果检测系统错误的将其中490当作了检测目标,”错误接受率“就是490/1000 = 0.49。通常false alram设置为0.5。
3、Are falsealarm and hitrate depend on each other?
    错误接受率和正确检测率之间有关系吗?
它们之间是有关系的,不能这样设置:min hitrate = 1.0,   max falsealarm  
= 0.0。
首先,系统根据期望的hitrate创建分类器,然后计算分类器的falsealarm。如果falsealarm比设置的maxfalsealarm高,系统会拒绝这个分类器,开始创建新的分类器。所以在训练过程中,你可以看到类似下面的信息:

N |%SMP|F| ST.THR | HR | FA | EXP. ERR|
+—-+—-+-+———+———+———+———+
| 0 |25%|-|-1423.312590| 1.000000| 1.000000| 0.876272|
HR – hitrate
FA – falsealarm
4、What’s falsealarm and hitrate of whole cascade?
    整个级联分类器的falsealarm和hitrate是多少?
False alarm of cascade = false alarm of  stage 1* false alarm of  stage 2* …
Hit rate = hitrate of  stage 1 * hitrate of stage 2* …
5、How many stages should be used?
    级联分类器应该被设定为多少层?
通常,14-25层就足够了。
如果层数过多,分类器的false alarm就更小,但是产生级联分类器的时间更长。
如果层数过多,分类器的hitrate就更小。(原因见4)
如果层数过多,检测速度更慢。
如果正、负样本较少,层数没必要设置很多。
6、What’s weighttrimming, eqw, bt, nonsym options?
     weighttrimming、eqw、bt、 nonsym  选项表示什么?
nonsym                
如果检测目标不是x或者y轴对称的,使用-nonsym选项。系统默认使用-sym选项
eqw             
  
      — 如果正负样本数目不相等,不要使用此选项
weighttrimming — 计算最优化选项。能减一点计算事件,但是质量也会降低
bt                       
  
— 使用哪种adaboost算法:Real AB,Gentle AB等
7、What’s  minpos, nsplits, maxtreesplits options?
    minpos、nsplits、nsplits、maxtreesplits选项是什么?
nsplits                  ——  
  树节点数的最小值
maxtreesplits     ——     树节点数的最大值
minpos                 ——  
 训练过程中,节点可使用的正样本数目。正样本根据节点被分类,通常来说,minpos不小于npos / nsplits

8、   Errors and stranges during haartraining!
    训练过程中的错误!
①Error (valid only for Discrete and Real AdaBoost): misclass
这是警告而不是错误。D and R Adaboost算法有一些特别的选项。
②控制台上都是类似下面的提示 : | 1000 |25%|-|-1423.312590| 1.000000| 1.000000| 0.876272| 
训练进入了循环,重新启动训练。正常情况下,第一列应小于100

③cvAlloc fails. Out of memory
负样本太多或者vec文件太大,所有的图片都加载到内存,导致内存不足。
④注意-w -h参数的值与生成vec文件时的值相同
⑤注意正、负样本数目与-npos 、-nneg参数设定值相同
⑥防止dat文件(描述文件)中的空行
⑦Required leaf false alarm rate achieved. Branch training terminated
负样本图片中可能包含了被检测目标。maxfalsealarm值应该设定到0.4 - 0.5之间。

分类器xml文件

1、 During haartraining, there are txt file in haarcascade folder, how can we
get XML  from them?
    训练过程中会产生txt文件,如果根据这些文件生成xml文件?
使用convert_cascade工具:
 convert_cascade –size=”20×20″ haarcascade haarcascade.xml
其中,haarcascade 是txt文件所在目录
2、How can I test generated XML cascade?
       如何测试生成的分类器?
使用performance工具来测试,其中正样本是训练过程中未使用的图片:

performance -data haarcascade -w 20 -h 20 -info positive_description.txt -ni
performance -data haarcascade.xml -info positive_description.txt -ni

使用OpenCV和训练得到的分类器检测物体

1、Is it possible to detect rotated faces?
    能不能检测旋转过的脸?
训练一个分类器来检测旋转不同角度的人脸是不可能的。但是,可是训练出分类器来检测旋转到对应角度的人脸。可以使用选择到这一角度的人脸图片来训练出分类器。在训练分类器时,加上-mode all选项表示使用所有的特征,但是早期的OpenCV版本实现的比较差。你也可以增加自己的特征,这并不困难。
另一种方法是计算出头部的选择角度,然后根据选择角度旋转检测图片来得到正脸,这样就可以使用OpenCV自带的人脸检测期来进行检测了。
2、Is it possible to recognize gender, attention, race with  Haar features?

    使用Haar特征能否分辨出性别、注意力、种族?
我们有过尝试,但是使用OpenCV的haartraining没有成功。我们使用性别、注意力分类器。当然你可以使用Adaboost算法,但我们没有得到很好的结果。
3、Is it possible to detect faces in real time?
    能否做到实时检测人脸?

在电脑上,OpenCV自带的人脸检测器检测一幅分辨率为640*480的图片需要200ms,也就是5fps,非实时。我们可以改进检测器,做到15fps,进行实时检测。

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

英文原文:

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Hi All, before posting your question, please look at this FAQ carefully! Also you can readOpenCV haartraining article.  If you are sure, there is no answer to your question, feel free to post comment.  Also please, put comments about improvement of this post.  This post will be updated, if needed.

Positive images


Why positive images are named so?

Because a positive image contains the target object which you want machine to detect. Unlike them, a negative image doesn’t contain such target objects.

What’s vec file in OpenCV haartraining?

During haartraining positive samples should have the same width and height as you define in command “-w -h size”.  So original positive images are resized  and packed as thumbs to vec file. Vec file has header: number of positive samples, width, height and contain positive thumbs in body.

Is it possible to merge vec files?

Yes, use Google, there are free tools, written by OpenCV’s community.

I have  positive images, how create vec file of positive samples?

There is tool in C:\Program Files\OpenCV\apps\HaarTraining\src createsamples.cpp.  Usage:

createsamples -info positive_description.txt -vec samples.vec -w 20 -h 20

What’s positive description file?

The matter is that, on each positive image, there can be several objects. They have bounding rectangles: x,y, width, height.  So you can write such description info of image:

positive_image_name  num_of_objects x y width height x y width height …

Text file, which contains such info about positive images is called description file. So during vec file generation,  really objects are packed, but not whole image. Essentially vec file is needed to speed up machine learning.

Do I always need description file, even if I have only one object on a image?

Yes, with createsamples you need description file.  If you have only one object, it’s bounding rectangle may be bounding rectangle of whole image. If you want, write your own tool for vec file generation =)

Should lightning conditions and background be various on positive images?

Yes, it’s very important. On each positive image, beside object, there is background. Try to fill this background with random noise, avoid constant background.

How much background should be on positive image?

If you have much background pixels on your positive images in comparison with object’s pixels – it’s bad since the haartraining could remember the background as feature of positive image.

If you don’t have background pixels at all – it’s also bad. There should be small background frame on positive image

Should all original positive images have the same size?

No, original images can have any size.  But it’s important that width, height of this rectangle have the same aspect ratio as -w -h.

What’ s  -w and -h should I put in createsamples? Should it be always square?

You can put any value to -w and -h depend on aspect ratio of the target object which you want to detect.  But objects of smaller size will not be detected! For faces, commonly used values are 24×24, 20×20. But you may use 24×20, 20×24, etc.

Errors during vec file generation: Incorrect size of input array, 0 kb vec file,

-First check you description file: positive_image_name should be absolute path namewithout spaces like “C:\content\image.jpg” not “C:\con tent\image.jpg” or relative path name.

-Avoid empty lines in description file

-Resolution of original positive image file should be not less, then -w -h parameters you put.

-Check that positive images are available in your file systems and not corrupted.

-There can be unsupported formats. Jpeg, Bmp, PPM are supported!

Example of vec file generation!

Let’s working directory be C:\haartraining. In it there is createsamples.exe. There is folder

C:\haartraining\positives. So create description file positive_desc.txt.

positives\image1.jpg 1 10 10 20 20

positives\image2.jpg 2 30 30 50 50 60 60 70 70

or

C:\haartraining\positives\image1.jpg 1 10 10 20 20

C:\haartraining\positives\image2.jpg 2 30 30 50 50 60 60 70 70

You should avoid empty lines and empty space in image’s path

createsamples -info positive_desc.txt -vec samples.vec -w 20 -h 20


Negative images


What negative images should I take?

You can use any image of OpenCV supported formats, which does not contain target objects (which are present on positive images). But they should be various – it’s important! Good enough database is here

Should negative images have the same size?

No. But the size should not be less than -w -h, which were put during vec file generation.

What’s description file for negative image?

It’s just text file, often called negative.dat, which contains full path to negative images like:

image_name1.jpg

image_name2.jpg

Avoid empty lines in it.

How many negative/positive image should I take?

It depends on your task.  For real cascades there should be about 1000 positive images and 2000 negative images e.g.

Good enough proportion is  positive:negative = 1:2, but it’s not hard rule! I would recommend first to use small number of samples, generate cascade, test it, then enlarge number of samples.

Launch haartraining.exe (OpenCV\apps\HaarTraining\src)


Example of launching

Working directory is C:\haartraining with haartraining.exe tool and samples.vec file.

Let’s negative images are in C:\haartraining\negative, in this case negative.dat should be like this:

negative\neg1.jpg

negative\neg2.jpg

So in C:\haartraining launch this:  haartraining -data haarcascade -vec samples.vec -bg negatives.dat -nstages 20  -minhitrate 0.999 -maxfalsealarm 0.5 -npos 1000 -nneg 2000 -w 20 -h 20 -nonsym -mem 1024

  • w  h  is the same, you put during vec file generation
  • npos nneg  – number of positive samples and negative samples
  • mem – RAM memory, that program may use
  • maxfalsealarm – maximum false alarm, that stage may have. If big false alarm – it could be bad detection system
  • minhitrate – minimal hit rate, that should stage have at least
  • nstage – number of stages in cascade

What’ s falsealarm and hitrate of stage?

You should read theory of adaboost about strong classifier. Stage is strong classifier. In short:

  • For example you have 1000 positive samples. You want your system to detect 900 of them. So desired hitrate = 900/1000 = 0.9. Commonly, put minhitrate = 0.999
  • For example you have 1000 negative samples. Because it’s negative, you don’t want your system to detect them. But your system, because it has error, will detect some of them. Let error be about 490 samples, so false alarm = 490/1000 = 0.49. Commonly,put false alarm  = 0.5

Are falsealarm and hitrate depend on each other?

Yes, there is dependency. You could not put minhitrate = 1.0 and maxfalsealarm = 0.0. .

Firstly, the system builds classifier with desired hitrate, then it will calculate it’s falsealarm, if the false alarm is higher than maxfalsealarm, the system will reject such classifier and will build the next one. During haartraining you may see such:

N |%SMP|F| ST.THR | HR | FA | EXP. ERR|
+—-+—-+-+———+———+———+———+
| 0 |25%|-|-1423.312590| 1.000000| 1.000000| 0.876272|

HR – hitrate

FA – falsealarm

What’s falsealarm and hitrate of whole cascade?

Cascade is linked list (or three) of stages. That’s why:

  • False alarm of cascade = false alarm of  stage 1* false alarm of  stage 2* …
  • Hit rate = hitrate of  stage 1 * hitrate of stage 2* …

How many stages should be used?

  • If you set big number of stages, then you will achieve better false alarm, but it will take more time for generating cascade.
  • If you set big number of stages, then the detection time could be slower
  • If you set big number of stages, then the worse hitrate will be (0.99*0.99*… etc). Commonly 14-25 stages are enough
  • It’s useless to set many stage, if you have small number of positive, negative samples

What’s weighttrimming, eqw, bt, nonsym options?

Really all these parameters are related to Adaboost, read theory. In short:

  • nonsym – If you positive samples are not X or Y symmetric, put -nonsym, -sym is default!
  • eqw – if you have different number of pos and neg images, it’s better to put no eqw
  • weighttrimming – for calculation optimization. It can reduce calculation time a little, but quality may be worse
  • bt – what Adaboost algorithm to use: Real AB, Gentle AB, etc.

What’s  minpos, nsplits, maxtreesplits options?

These parameters are related to clustering. In Adaboost different week classifier may be used: stump-based or tree-based.  If you choose nsplits > 0, tree-based will be used and you should set up minpos and maxtreesplits.

  • nsplits – minimun number of nodes in tree
  • maxtreesplits – maximum number of nodes in tree. If maxtreesplits < nsplits,  tree will not be built
  • minpos – number of positive images, that can be used by one node during training.  All positive images are splitted between nodes. Generally minpos  should be not less than  npos/nsplits.

Errors and stranges during haartraining!

  • Error (valid only for Discrete and Real AdaBoost): misclass – it’s warning, but no error.  Some options are specific to D and R Adaboost.  So your haartraining is ok.
  • Screen is filled with such | 1000 |25%|-|-1423.312590| 1.000000| 1.000000| 0.876272| – your training is cycled, restart it. First column should have value < 100
  • cvAlloc fails. Our of memory – you give too much negative images or sample.vec is too big. All these pictures are loaded to RAM.
  • Pay attention you put the same -w and -h, as during vec file generation
  • Pay attention, that number of positive samples and negative samples, you put in -npos -nneg are really available
  • Avoid empty line in negative.dat file
  • Required leaf false alarm rate achieved. Branch training terminated – it’s impossible to build classifier with good false alarm on this negative images. Check your negative images are really negative =),  maxfalsealarm should be in [0.4-0.5]

OpenCV XML haarcascade


During haartraining, there are txt file in haarcascade folder, how can we get XML  from them?

There is OpenCV/samples/c/convert_cascade.c. Use like:

convert_cascade –size=”20×20″ haarcascade haarcascade.xml

How can I test generated XML cascade?

 There is OpenCv/apps/HaarTraining/src /perfomance.cpp. You need have positive images(not used during training) and positive description file.  Use like:

performance -data haarcascade -w 20 -h 20 -info positive_description.txt -ni

performance -data haarcascade.xml -info positive_description.txt -ni

Time and Speed of haar cascade generation


Average time to generate cascade on PC?

It depends on task and your machine.  I generated cascade for face detection, for this used such parameters: -nstages 20 -minhitrate 0.999 -maxfalsealarm 0.5 -npos 4000 -nneg 5000 -w 20 -h 20 -nonsym -mem 1024.  It took 6 days on Pentium 2.7GHZ 2GB RAM.

What is OpenMP?

The OpenMP (Open Multi-Processing) is an application programming interface (API) that supports multi-platform shared memory multiprocessing programming in CC++and Fortran on many architectures, including Unix and Microsoft Windows platforms“. If you have MT processor, you can use it.  In code you should add OpenMP defines and put compile options.  For example in VisualStudio2005: Properties->C/C++->Language->OpenMP support

Is it possible to improve speed of haartraining?

Yes, one of possible ways is to use parallel programming. We have realized OpenCV haartraining using MPI for linux cluster. You can read it here

Object detection with OpenCV XML cascades


Is it possible to detect rotated faces?

Yes. It is impossible to generate cascade, which can detect face in all orientations. But you can generate cascade for each orientation separately. For this you need positive content of rotated faces. You can try to generate cascade with OpenCV , add -mode ALL, with it tilted haar feature will be used. But it’s badly implemented, at least in OpenCV 1.1. If you want you can add your own feature to opencv haartraining – it’s not too hard.

Another approach is to write head pose estimator. Then rotate your pictures, so that you have frontal face and detect it with OpenCV default face cascade

Is it possible to recognize gender, attention, race with  Haar features?

We tried, but could not do it with OpenCV haartraining. That’s why for such classification, we used our own gender and attention classificators. Of course you can use Adaboost for this task, which is implemented in haartraining, but we did not get good results.

Is it possible to detect faces in real time?

Yes.  On PC default OpenCV facedetector takes about 200 ms for 640×480 picture, about 5fps – it’s not real time. We have changed facedetector and get about 15 fps – which is real time. You can see results  here and here


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值