最近在了解人脸检测部分。作为一名新手,对所谓的机器学习一窍不通,尽管OpenCV里提供了这部分的代码,还是看得一头雾水。所以先从如何使用OpenCV里的程序和入手来入门。 发到blog里来作为备份,欢迎网友指出我理解的不对的地方。
前半部分讲的是如何使用OpenCV里现有的程序,从而实现在有了测试DB的情况下可以输出训练的结果。主要是复制黏贴的[1].
后半部讲的是我对运行程序的时候出的Debug信息的理解。(不知道其它像我一样的新手是否也有这样的需求)
第一步 采集样本
1、 将正负样本分别放在两个不同的文件夹下面
分别取名pos和neg,其中pos用来存放正样本图像,neg用来存放负样本
注意事项:1、正样本要统一切成24*24像素(或者其他,以下假设为长24,宽24的情况)的格式,建议保存成灰度图,节省空间
2、正样本的数目越多,训练的时间也将越长,训练出来的效果也就越好
3、负样本的数量想对于正样本一定要足够的多,很多朋友在训练的时候,往往出现了CPU占用率达到了100%,但是训练只是停留在一个分类器长达几小时没有相应,问题出现在取负样本的那个函数icvGetHaarTrainingDataFromBG中; 当剩下所有的negtive样本在临时的cascade Classifier中,evaluate的结果都是0(也就是拒绝了),随机取样本的数目到几百万都是找不到误检测的neg样本了,因而没法跳出循环
2、建立正负样本的说明文件
在cmd下面进入pos目录,输入 dir /b > pos.txt
这个时候会在pos文件加下面生成一个pos.txt文件,打开pos.txt
我们对它进行如下编辑:
(1)、将BMP 替换成为 BMP 1 0 0 24 24
注意:1代表此图片出现的目标个数 后面的 0 0 24 24代表目标矩形框(0,0)到(24,24),用户可以根据自身需要调整数值
(2)、删除文本中最后一行的“pos.txt”
2、对负样本进行编辑
在CMD下输入 dir /b > neg.txt
同理,打开neg目录下的neg.txt文件,只需要删除最后一行的neg.txt这一句
注意:1、负样本说明文件不能含有目标物体
2、负样本图像尺寸不受到限制,但是尺寸越大,训练所用的时间越长,
3、负样本图像可以是灰度图,也可以不是,笔者建议使用灰度图,这样处理起来可能更有效率
4、负样本图像一定不要重复,增大负样本图像的差异性,可以增加分类器的使用范围,笔者建议可以使用网上的素材库,将1000多张不含目标的图片灰度处理后用来训练,效果更佳
二、使用opencv_createsamples.exe创立样本VEC文件
1、首先我们将要用的的2个程序opencv_createsamples.exe和opencv_haartraining.exe拷到根目录下
在CMD下输入如下命令:
opencv_createsamples.exe -vec pos.vec -info pos\pos.txt -bg neg\neg.txt -w 20-h 20 -num 142
文档里说生成的 vec文件里包含正样本数目,宽高,以及所有样本图像数据。
以上参数的含义如下:
-vec
训练好的正样本的输出文件名。
-info
标记特征的图片集合的描述文件。
-img
源目标图片(例如:一个公司图标)
-bg
背景描述文件。
-num
要产生的正样本的数量,和正样本图片数目相同。
-bgcolor
背景色(假定当前图片为灰度图)。背景色制定了透明色。对于压缩图片,颜色方差量由bgthresh
参数来指定。则在bgcolor-bgthresh 和bgcolor+bgthresh 中间的像素被认为是透明的。
-bgthresh
-inv
如果指定,颜色会反色
-randinv
如果指定,颜色会任意反色
-maxidev
背景色最大的偏离度。
-maxangel
-maxangle
-maxzangle
最大旋转角度,以弧度为单位。
-show
如果指定,每个样本会被显示出来,按下"esc"会关闭这一开关,即不显示样本图片,而创建过程
继续。这是个有用的debug 选项。
-w
输出样本的宽度(以像素为单位)
-h《sample_height》
输出样本的高度,以像素为单位。
按下回车后得到我们的正样本pos.vec文件。
三、使用opencv_haartraing.exe进行训练
参数说明如下:
Haartraining 的命令行参数如下:
-data
-vec
-bg
-npos
-nneg
-nPos = 7000;nNeg = 3000-nstages
-nsplits
如果1,则一个简单的stump classifier 被使用。如果是2 或者更多,则带有number_of_splits 个内部节点的CART 分类器被使用。
-mem
-minhitrate《min_hit_rate》每个阶段分类器需要的最小的命中率。总的命中率为min_hit_rate 的number_of_stages 次方。
-maxfalsealarm
-weighttrimming
-mode
-bt
在CMD下输入opencv_haartraining.exe -data xml -vec pos.vec -bg neg\neg.txt –nstages10 –npos 2000 –nneg 1000 -w 24 -h 24 -mem 1024 –mode BASIC 开始训练
训练结束后,根目录下就会生成xml.xml文件,文件夹xml里面会生成每个stage里的输出结果。训练过程的debug信息如下:(个人采用的是CBCL的training人脸作为pos,Tutorial的1000个bg图片作为neg)。个人对debug信息的理解在旁边注释。
Data dirname: xml
Vec file name:pos.vec
BG file name: neg\neg.txt, is a vecfile: no
Num pos: 2000//每个stage在训练当前强分类器的所需要的正样本数(必须是通过了之前的stage(强分类器)之后的2000个,也就是说从我们的pos文件夹里要尝试大于等于2000个才能得到2000个)
Num neg: 1000//每个stage在训练当前强分类器的所需要的负样本数(必须是通过了之前的stage(强分类器)之后的1000个,由于之前的分类器都可以达到FA小于50%,那么假如之前分类器只有一级的话,为了当前的强分类器的这1000个负样本理论上怎么也要从neg文件夹里尝试大于2000次才能搞定。)
Num stages: 10//这个可以自己设置,但是如果还没训练到10个stage的时候FA就低于pow(0.5,10)了程序就结束了。
Num splits: 1(stump as weak classifier)//这里指每个stage(强分类器)里面的每个弱分类器里需要的特征数
Mem: 1024 MB
Symmetric: TRUE//要不要左右对称
Min hit rate:0.995000//每个stage在训练当前强分类器的目标Hit Rate(把1分类成了0),必须高于此值
Max false alarmrate: 0.500000//每个stage在训练当前强分类器的目标false alarm(把0分类成了1),必须低于此值
Weight trimming:0.950000//boost里面的一个参数。具体作用需回头调查
Equal weights:FALSE
Mode: BASIC//仅仅使用垂直特征
Width: 19//正样本的宽
Height: 19//正样本的高
Applied boostingalgorithm: GAB//Gentle Adaboost
Error (valid onlyfor Discrete and Real AdaBoost): misclass
Max number ofsplits in tree cascade: 0
Min number ofpositive samples per cluster: 500
Required leaf falsealarm rate: 0.000976563// = pow(0.5,10)
Tree Classifier
Stage
+---+
| 0|
+---+
Number of featuresused : 33000//这个数字是根据高,宽,mode,symmetric来计算出来的。icvCreateIntHaarFeatures
Parent node: NULL
*** 1 cluster ***
POS: 2000 20001.000000//为了当前stage取出的正样本数目,尝试过的正样本数目两者之比
NEG: 1000 1//为了当前stage取出的负样本数目,在取的过程中的False Alarm。(个人理解两者相除即为尝试的次数)
PS。由于这是第一个stage,所以百分比都是100%。
BACKGROUNDPROCESSING TIME: 0.01
Precalculationtime: 0.42
+----+----+-+---------+---------+---------+---------+
| N |%SMP|F| ST.THR | HR | FA | EXP. ERR|
+----+----+-+---------+---------+---------+---------+
| 1|100%|-|-0.876358| 1.000000| 1.000000|0.072000|
+----+----+-+---------+---------+---------+---------+
| 2|100%|+|-1.728757| 1.000000| 1.000000|0.073000|
+----+----+-+---------+---------+---------+---------+
| 3|100%|-|-2.331521| 1.000000| 1.000000|0.028000|
+----+----+-+---------+---------+---------+---------+
| 4|100%|+|-1.608770| 0.999500| 0.343000|0.030000|
+----+----+-+---------+---------+---------+---------+
以上过程应该就是AdaBoost的过程,上面这个表的意思是这个stage需要4个Haar-like特征来组合到一起来实现这个stage的强分类器。
上面的表的解释如下:(来自【2】) (SMP和EXP.ERR还不太理解)
N |
The iteration number of feature selection training. |
%SMP |
The percentage of original samples left. |
F |
+ indicates the feature is flipped. Related to -sym (default) option. |
ST.THR |
Stage threshold |
HR |
Hit rate |
FA |
False alarm rate. FYI: (HR, FA) = (1.0, 1.0) means the detector simply alarms every time for everything. |
EXP.ERR |
Expected (misclassification) error. |
Stage trainingtime: 57.14
Number of usedfeatures: 4
Parent node: NULL
Chosen number ofsplits: 0
Total number ofsplits: 0
Tree Classifier
Stage
+---+
| 0|
+---+
到这第一个stage就训练完成啦。因为FA达到34.3%啦,比50%要小了。这时候在xml文件夹下会有一个0文件夹,里面有个AdaBoostCARTHaarClassifier.txt,里面记下了这一层的训练结果,回头n个stage都训练完后这些训练结果会被放到一个xml文件里的。再次以我实验的为例,这个txt文件内容如下,我对其内容的理解也注释在旁。
4 //这个强分类器一共4个弱分类器,以下分别是4个弱分类器的具体信息。
1 //从这开始是第一个弱分类器,这个1表示的其实是参数中的-nsplits,表示一个弱分类器里就一个haar-like 特征
2 //当前特征需要几个rect来计算,下面有提示这个是haar_y2所以只需要两个
0 3 19 6 0 -1 //第一个rect的x, y, width, height, band(不懂), weight(文章里黑和白决定)
0 6 19 3 0 2 //第二个rect的x, y, width, height, band(不懂), weight(文章里黑和白决定)
haar_y2 //类型
9.305671e-002 0 -1 //threshold for this single weak classifierleft right (?)
-8.763583e-0018.130261e-001 //个人理解是如果计算出来的haar-like特征值小于上面的threshold就输出左面的数,反之输出右面的数。(response)
PS.以下三组格式都类似。最后有个Threshold(-1.608770e+000),估计是把这4个弱分类器的response之和跟Threshold比较,大的话就输出1,反之-1
1
2
5 0 9 10 0 -1
8 0 3 10 0 3
haar_x3
8.252650e-002 0 -1
-8.523988e-0017.785095e-001
1
2
4 2 2 15 0 -1
4 7 2 5 0 3
haar_y3
5.264061e-002 0 -1
-6.027635e-0018.538383e-001
1
2
8 3 3 6 0 -1
9 3 1 6 0 3
haar_x3
1.202950e-002 0 -1
-6.566088e-0017.227510e-001
-1.608770e+000 //Threshold
-1 //parent node,表示parent强分类器的节点
-1 //next node,一般来说都是-1
0
Parent node: 0
*** 1 cluster ***
POS: 2000 20010.999500
NEG: 1000 0.354736 //这里的0.3547 约等于上个表最后的0.343 乘以 1
BACKGROUNDPROCESSING TIME: 0.01
Precalculationtime: 0.44
+----+----+-+---------+---------+---------+---------+
| N |%SMP|F| ST.THR | HR | FA | EXP. ERR|
+----+----+-+---------+---------+---------+---------+
| 1|100%|-|-0.836412| 1.000000| 1.000000|0.122667|
+----+----+-+---------+---------+---------+---------+
| 2|100%|+|-1.571471| 1.000000| 1.000000| 0.126333|
+----+----+-+---------+---------+---------+---------+
| 3|100%|-|-1.033975| 0.998000| 0.596000|0.067333|
+----+----+-+---------+---------+---------+---------+
| 4|100%|+|-1.670214| 0.995500| 0.499000|0.069000|
+----+----+-+---------+---------+---------+---------+
Stage trainingtime: 56.91
Number of usedfeatures: 4
Parent node: 0
Chosen number ofsplits: 0
Total number ofsplits: 0
Tree Classifier
Stage
+---+---+
| 0| 1|
+---+---+
0---1
Parent node: 1
*** 1 cluster ***
POS: 2000 20100.995025
NEG: 1000 0.140017 //这里的0.140017 约等于上个表最后的0.4999乘以上上个表最后的0.343 乘以再上面的1,但是有起伏,以下类推
BACKGROUNDPROCESSING TIME: 0.04
Precalculationtime: 0.44
+----+----+-+---------+---------+---------+---------+
| N |%SMP|F| ST.THR | HR | FA | EXP. ERR|
+----+----+-+---------+---------+---------+---------+
| 1|100%|-|-0.798650| 1.000000| 1.000000|0.125333|
+----+----+-+---------+---------+---------+---------+
| 2|100%|+|-1.565732| 1.000000| 1.000000| 0.112000|
+----+----+-+---------+---------+---------+---------+
| 3|100%|-|-1.059954| 0.997000| 0.753000|0.068667|
+----+----+-+---------+---------+---------+---------+
| 4|100%|+|-1.708828| 0.997000| 0.753000|0.071667|
+----+----+-+---------+---------+---------+---------+
| 5| 95%|-|-1.329489| 0.997000| 0.550000|0.059667|
+----+----+-+---------+---------+---------+---------+
| 6| 95%|+|-1.442027| 0.995500| 0.333000|0.042667|
+----+----+-+---------+---------+---------+---------+
Stage training time:83.61
Number of usedfeatures: 6
Parent node: 1
Chosen number ofsplits: 0
Total number ofsplits: 0
Tree Classifier
Stage
+---+---+---+
| 0| 1| 2|
+---+---+---+
0---1---2
Parent node: 2
*** 1 cluster ***
POS: 2000 20190.990589
NEG: 1000 0.0989022
BACKGROUNDPROCESSING TIME: 0.06
Precalculationtime: 0.40
+----+----+-+---------+---------+---------+---------+
| N |%SMP|F| ST.THR | HR | FA | EXP. ERR|
+----+----+-+---------+---------+---------+---------+
| 1|100%|-|-0.857136| 1.000000| 1.000000|0.225333|
+----+----+-+---------+---------+---------+---------+
| 2|100%|+|-1.735158| 1.000000| 1.000000|0.144667|
+----+----+-+---------+---------+---------+---------+
| 3|100%|-|-1.308313| 1.000000| 0.871000|0.209000|
+----+----+-+---------+---------+---------+---------+
| 4| 95%|+|-0.915000| 0.998500| 0.700000|0.190667|
+----+----+-+---------+---------+---------+---------+
| 5| 97%|-|-1.510146| 0.995500| 0.595000|0.070667|
+----+----+-+---------+---------+---------+---------+
| 6| 59%|+|-1.320606| 0.999000| 0.588000|0.177333|
+----+----+-+---------+---------+---------+---------+
| 7| 53%|-|-0.918971| 0.997000| 0.550000|0.157333|
+----+----+-+---------+---------+---------+---------+
| 8| 42%|+|-1.798309| 0.996500| 0.564000|0.165000|
+----+----+-+---------+---------+---------+---------+
| 9| 30%|-|-1.263797| 0.995500| 0.485000|0.033000|
+----+----+-+---------+---------+---------+---------+
Stage trainingtime: 79.99
Number of usedfeatures: 9
Parent node: 2
Chosen number ofsplits: 0
Total number ofsplits: 0
Tree Classifier
Stage
+---+---+---+---+
| 0| 1| 2| 3|
+---+---+---+---+
0---1---2---3
Parent node: 3
*** 1 cluster ***
POS: 2000 20280.986193
NEG: 1000 0.0338983
BACKGROUNDPROCESSING TIME: 0.17
Precalculationtime: 0.36
+----+----+-+---------+---------+---------+---------+
| N |%SMP|F| ST.THR | HR | FA | EXP. ERR|
+----+----+-+---------+---------+---------+---------+
| 1|100%|-|-0.916164| 1.000000| 1.000000|0.298667|
+----+----+-+---------+---------+---------+---------+
| 2|100%|+|-1.849250| 1.000000| 1.000000|0.095333|
+----+----+-+---------+---------+---------+---------+
| 3|100%|-|-1.412137| 1.000000| 0.959000|0.289333|
+----+----+-+---------+---------+---------+---------+
| 4| 98%|+|-0.993077| 0.999000| 0.908000|0.041000|
+----+----+-+---------+---------+---------+---------+
| 5| 99%|-|-0.581992| 0.996000| 0.819000|0.042000|
+----+----+-+---------+---------+---------+---------+
| 6| 99%|+|-1.306047| 0.996000| 0.841000|0.264333|
+----+----+-+---------+---------+---------+---------+
| 7| 48%|-|-1.090664| 0.995500| 0.827000|0.027333|
+----+----+-+---------+---------+---------+---------+
| 8| 52%|+|-0.961630| 0.995500| 0.791000|0.031667|
+----+----+-+---------+---------+---------+---------+
| 9| 50%|-|-1.486213| 0.996000| 0.114000|0.022000|
+----+----+-+---------+---------+---------+---------+
Stage trainingtime: 80.34
Number of usedfeatures: 9
Parent node: 3
Chosen number ofsplits: 0
Total number of splits:0
Tree Classifier
Stage
+---+---+---+---+---+
| 0| 1| 2| 3| 4|
+---+---+---+---+---+
0---1---2---3---4
Parent node: 4
*** 1 cluster ***
POS: 2000 20360.982318
NEG: 10000.00528647
BACKGROUNDPROCESSING TIME: 1.06
Precalculationtime: 0.45
+----+----+-+---------+---------+---------+---------+
| N |%SMP|F| ST.THR | HR | FA | EXP. ERR|
+----+----+-+---------+---------+---------+---------+
| 1|100%|-|-0.547415| 1.000000| 1.000000|0.230000|
+----+----+-+---------+---------+---------+---------+
| 2|100%|+|-1.186195| 1.000000| 1.000000|0.219000|
+----+----+-+---------+---------+---------+---------+
| 3|100%|-|-1.682588| 1.000000| 1.000000|0.143333|
+----+----+-+---------+---------+---------+---------+
| 4|100%|+|-1.412621| 0.999500| 0.896000|0.131667|
+----+----+-+---------+---------+---------+---------+
| 5|100%|-|-1.937362| 0.999000| 0.842000|0.110000|
+----+----+-+---------+---------+---------+---------+
| 6| 97%|+|-2.273295| 0.999500| 0.889000| 0.114667|
+----+----+-+---------+---------+---------+---------+
| 7| 68%|-|-2.008629| 0.999500| 0.730000|0.093000|
+----+----+-+---------+---------+---------+---------+
| 8| 68%|+|-1.715718| 0.998500| 0.597000|0.087000|
+----+----+-+---------+---------+---------+---------+
| 9| 65%|-|-1.541976| 0.995500| 0.440000|0.079000|
+----+----+-+---------+---------+---------+---------+
Stage trainingtime: 112.91
Number of usedfeatures: 9
Parent node: 4
Chosen number ofsplits: 0
Total number ofsplits: 0
Tree Classifier
Stage
+---+---+---+---+---+---+
| 0| 1| 2| 3| 4| 5|
+---+---+---+---+---+---+
0---1---2---3---4---5
Parent node: 5
*** 1 cluster ***
POS: 2000 20450.977995
NEG: 10000.00219693
BACKGROUNDPROCESSING TIME: 2.53
Precalculationtime: 0.46
+----+----+-+---------+---------+---------+---------+
| N |%SMP|F| ST.THR | HR | FA | EXP. ERR|
+----+----+-+---------+---------+---------+---------+
| 1|100%|-|-0.567053| 1.000000| 1.000000|0.235333|
+----+----+-+---------+---------+---------+---------+
| 2|100%|+|-1.088740| 1.000000| 1.000000|0.282333|
+----+----+-+---------+---------+---------+---------+
| 3|100%|-|-1.759195| 1.000000| 1.000000|0.157667|
+----+----+-+---------+---------+---------+---------+
| 4|100%|+|-1.478334| 0.997500| 0.905000|0.144000|
+----+----+-+---------+---------+---------+---------+
| 5| 96%|-|-1.212285| 0.996000| 0.740000|0.148333|
+----+----+-+---------+---------+---------+---------+
| 6| 98%|+|-1.876123| 0.995500| 0.763000|0.116667|
+----+----+-+---------+---------+---------+---------+
| 7| 96%|-|-1.641806| 0.996000| 0.632000|0.097333|
+----+----+-+---------+---------+---------+---------+
| 8| 68%|+|-2.028667| 0.996500| 0.619000|0.105667|
+----+----+-+---------+---------+---------+---------+
| 9| 74%|-|-1.755526| 0.995500| 0.515000|0.079667|
+----+----+-+---------+---------+---------+---------+
| 10| 65%|+|-1.573657| 0.995500| 0.476000|0.072000|
+----+----+-+---------+---------+---------+---------+
Stage trainingtime: 126.46
Number of usedfeatures: 10
Parent node: 5
Chosen number ofsplits: 0
Total number ofsplits: 0
Tree Classifier
Stage
+---+---+---+---+---+---+---+
| 0| 1| 2| 3| 4| 5| 6|
+---+---+---+---+---+---+---+
0---1---2---3---4---5---6
Parent node: 6
*** 1 cluster ***
POS: 2000 20540.973710
NEG: 10000.00104579
BACKGROUNDPROCESSING TIME: 5.32
Precalculationtime: 0.43
+----+----+-+---------+---------+---------+---------+
| N |%SMP|F| ST.THR | HR | FA | EXP. ERR|
+----+----+-+---------+---------+---------+---------+
| 1|100%|-|-0.441408| 1.000000| 1.000000|0.274333|
+----+----+-+---------+---------+---------+---------+
| 2|100%|+|-1.019107| 1.000000| 1.000000| 0.268333|
+----+----+-+---------+---------+---------+---------+
| 3|100%|-|-1.743181| 1.000000| 1.000000|0.179000|
+----+----+-+---------+---------+---------+---------+
| 4|100%|+|-1.474703| 0.996000| 0.946000|0.159000|
+----+----+-+---------+---------+---------+---------+
| 5| 98%|-|-1.867563| 0.999000| 0.902000|0.150333|
+----+----+-+---------+---------+---------+---------+
| 6| 99%|+|-1.480276| 0.996000| 0.702000|0.113333|
+----+----+-+---------+---------+---------+---------+
| 7| 76%|-|-1.437744| 0.995500| 0.654000|0.108667|
+----+----+-+---------+---------+---------+---------+
| 8| 74%|+|-1.752111| 0.995500| 0.619000|0.099667|
+----+----+-+---------+---------+---------+---------+
| 9| 71%|-|-1.549213| 0.995500| 0.510000|0.080000|
+----+----+-+---------+---------+---------+---------+
| 10| 71%|+|-1.576178| 0.995500| 0.482000|0.071000|
+----+----+-+---------+---------+---------+---------+
Stage trainingtime: 124.25
Number of usedfeatures: 10
Parent node: 6
Chosen number ofsplits: 0
Total number ofsplits: 0
Tree Classifier
Stage
+---+---+---+---+---+---+---+---+
| 0| 1| 2| 3| 4| 5| 6| 7|
+---+---+---+---+---+---+---+---+
0---1---2---3---4---5---6---7
Parent node: 7
*** 1 cluster ***
POS: 2000 20650.968523
NEG: 10000.000523652
BACKGROUNDPROCESSING TIME: 10.53
Required leaf falsealarm rate achieved. Branch training terminated.//这里发现比目标的FA要低了,所以就可以停了。
Total number ofsplits: 0
Tree Classifier
Stage
+---+---+---+---+---+---+---+---+
| 0| 1| 2| 3| 4| 5| 6| 7|
+---+---+---+---+---+---+---+---+
0---1---2---3---4---5---6---7
Cascade performance
POS: 2000 20650.968523
NEG: 10000.000531382
BACKGROUNDPROCESSING TIME: 10.36
Reference
【1】http://hi.baidu.com/ccb163163/item/22ba182edcc6fac00e37f9dd
【2】 https://docs.google.com/document/pub?id=14r34Pd51lKZNlfJIQVRS_3kbow1OcJBKV7wTRRAW5Vg