轉自 http://blog.csdn.net/wuxiaoyao12/article/details/39227189
好久沒有來寫blog了,學生生涯終結,就不好好總結了,今天把opencv里關于adaboost訓練和檢測的過程記錄下來,方便別人也方便自己~~~啊哈哈~~~~
(2015.8.28更改,見綠色)
一、基礎知識準備
首先,opencv目前僅支持三種特征的訓練檢測, HAAR、LBP、HOG,選擇哪個特征就去補充哪個吧。opencv的這個訓練算法是基于adaboost而來的,所以需要先對adaboost進行基礎知識補充啊,網上一大堆資料,同志們速度去查閱。我的資源里也有,大家去下載吧,這些我想都不是大家能直接拿來用的,我下面將直接手把手告訴大家訓練怎么操作,以及要注意哪些細節。
二、關于正樣本的準備
1、采集正樣本圖片
因為正樣本最后需要大小歸一化,所以我在采集樣本的時候就直接把它從原圖里摳出來了,方便后面縮放嘛,而不是只保存它的框個數和框位置信息(框個數、框位置信息看下一步解釋),在裁剪的過程中盡量保持樣本的長寬比例一致。比如我最后要歸一化成20 X 20,在裁剪樣本的時候,我都是20X20或者21X21、22X22等等,最大我也沒有超過30X30(不超過跟我的自身用途有關,對于人臉檢測這種要保證縮放不變性的樣本,肯定就可以超過啦),我資源里也給出可以直接用的裁剪樣本程序。
(這里我說錯了,根據createsamples.cpp ,我們不需要提前進行縮放操作,它在第3步變成vec時就包含了縮放工作.如果我們是用objectMaker標記樣本,程序同時生成的關于每一幅圖的samplesInfo信息,直接給第三步用即可。當然,你提前縮放了也沒關系,按照第2步操作即可)
2、獲取正樣本路徑列表
在你的圖片文件夾里,編寫一個bat程序(get route.bat,bat是避免每次都需要去dos框輸入,那里又不能復制又不能粘貼?。?,如下所示:
運行bat文件,就會生成如下dat文件:
把這個dat文件中的所有非圖片的路徑都刪掉,比如上圖的頭兩行,再將bmp 替換成 bmp 1 0 0 20 20,如下:
(1代表個數,后四個分別對應 left top width height,如果我們之前不是把樣本裁剪下來的,那么你的這個dat可能就長成這樣1. bmp 3 1 3 24 24 26 28 25 25 60 80 26 26,1.bmp是完全的原圖啊,你之前的樣本就是從這張圖上扣下來的)
3、獲取供訓練的vec文件
這里,我們得利用opencv里的一個程序叫opencv_createsamples.exe,可以把它拷貝出來。針對它的命令輸入也是寫成bat文件啦,因為cascade訓練的時候用的是vec。如下:
運行bat,就在我們得pos文件夾里生成了如下vec文件:
就此有關正樣本的東西準備結束。
(vec中其實就是保存的每一個sample圖,并且已經統一w、h大小了,如果你想看所有的sample,也可以通過調用opencv_createsamples.exe,使用操作,見附)
三、關于負樣本的準備
這個特別簡單,直接拿原始圖,不需要裁剪摳圖(不裁剪還能保證樣本的多樣性),也不需要保存框(網上說只要保證比正樣本大小大哈,大家就保證吧),只要把路徑保存下來。同正樣本類似,步驟圖如下:
至此有關負樣本的也準備完成。
四、開始訓練吧
這里我們用opencv_traincascade.exe(opencv_haartraining.exe的用法跟這個很相似,具體需要輸入哪些參數去看opencv的源碼吧,網上資料也有很多,主要是opencv_traincascade.exe比opencv_haartraining.exe包含更多的特征,功能齊全些啊),直接上圖:
命令輸入也直接用bat文件,請務必保證好大小寫一致,不然不予識別參數。小白兔,跑起來~~~
這是程序識別到的參數,沒有錯把,如果你哪個字母打錯了,你就會發現這些參數會跟你預設的不一樣啊,所以大家一定要看清楚了~~~~
跑啊跑啊跑啊跑,如下:
這一級的強訓練器達到你預設的比例以后就跑去訓練下一級了,同志們那個HR比例不要設置太高,不然會需要好多樣本,然后stagenum不要設置太小啊,不然到時候拿去檢測速度會很慢。
等這個bat跑結束,我的xml文件也生成了。如下:
其實這個訓練可以中途停止的,因為下次開啟時它會讀取這些xml文件,接著進行上次未完成的訓練。哈哈~~~~好人性化??!
訓練結束,我要到了我的cascade.xml文件,現在我要拿它去做檢測了??!呼呼~~~~
五、開始檢測吧
opencv有個opencv_performance.exe程序用于檢測,但是它只能用在用opencv_haartraining.exe來用的,所以我這里是針對一些列圖片進行檢測的,檢測代碼如下:
[cpp] view plain copy以下1)~4)是按順序判斷,且有且僅有一個
1)提供imagename%20和vecname時,調用以下操作[cpp] view%20plain copy函數內容:讀取當前圖中所有標記的sample(x,y,w,h),并將其縮放到winwidth、winheight大小,故在這之前的人為縮放操作不需要
(可以看到,僅需要num、w、h參數)4)僅vecname時,可以將vec里面的所有縮放后的samples都顯示出來[cpp] view%20plain copy2、opencv_haartraining.exe的參數
(haartraining.cpp )
[cpp] view%20plain copy3、opencv_performance.exe參數
(performance.cpp )
[cpp] view%20plain copy4、opencv_traincascade.exe參數說明
——traincascade.cpp
[cpp] view%20plain copy其中cascadeParams.printDefaults();——cascadeclassifier.cpp 如下
[cpp] view%20plain copy通用參數:
-data<cascade_dir_name>
目錄名,如不存在訓練程序會創建它,用于存放訓練好的分類器
-vec<vec_file_name>
包含正樣本的vec文件名(由 opencv_createsamples 程序生成)
-bg<background_file_name>
背景描述文件,也就是包含負樣本文件名的那個描述文件
-numPos<number_of_positive_samples>
每級分類器訓練時所用的正樣本數目
-numNeg<number_of_negative_samples>
每級分類器訓練時所用的負樣本數目,可以大于 -bg 指定的圖片數目
-numStages<number_of_stages>
訓練的分類器的級數。
-precalcValBufSize<precalculated_vals_buffer_size_in_Mb>
緩存大小,用于存儲預先計算的特征值(feature values),單位為MB
-precalcIdxBufSize<precalculated_idxs_buffer_size_in_Mb>
緩存大小,用于存儲預先計算的特征索引(feature indices),單位為MB。內存越大,訓練時間越短
-baseFormatSave
這個參數僅在使用Haar特征時有效。如果指定這個參數,那么級聯分類器將以老的格式存儲
級聯參數:
-stageType<BOOST(default)>
級別(stage)參數。目前只支持將BOOST分類器作為級別的類型
-featureType<{HAAR(default),LBP}>
特征的類型: HAAR - 類Haar特征;LBP - 局部紋理模式特征
-w<sampleWidth>
-h<sampleHeight>
訓練樣本的尺寸(單位為像素)。必須跟訓練樣本創建(使用 opencv_createsamples 程序創建)時的尺寸保持一致
Boosted分類器參數:
-bt<{DAB,RAB,LB,GAB(default)}>
Boosted分類器的類型: DAB - Discrete AdaBoost,RAB - Real AdaBoost,LB - LogitBoost, GAB - Gentle AdaBoost
-minHitRate<min_hit_rate>
分類器的每一級希望得到的最小檢測率(正樣本被判成正樣本的比例)??偟臋z測率大約為 min_hit_rate^number_of_stages。可以設很高,如0.999
-maxFalseAlarmRate<max_false_alarm_rate>
分類器的每一級希望得到的最大誤檢率(負樣本被判成正樣本的比例)??偟恼`檢率大約為 max_false_alarm_rate^number_of_stages??梢栽O較低,如0.5
-weightTrimRate<weight_trim_rate>
Specifies whether trimming should be used and its weight. 一個還不錯的數值是0.95
-maxDepth<max_depth_of_weak_tree>
弱分類器樹最大的深度。一個還不錯的數值是1,是二叉樹(stumps)
-maxWeakCount<max_weak_tree_count>
每一級中的弱分類器的最大數目。The boosted classifier (stage) will have so many weak trees (<=maxWeakCount), as needed to achieve the given-maxFalseAlarmRate
類Haar特征參數:
-mode<BASIC(default)| CORE|ALL>
選擇訓練過程中使用的Haar特征的類型。 BASIC 只使用右上特征, ALL 使用所有右上特征和45度旋轉特征
5、detectMultiScale函數參數說明
該函數會在輸入圖像的不同尺度中檢測目標:
image %20-輸入的灰度圖像,
objects -被檢測到的目標矩形框向量組,
scaleFactor -為每一個圖像尺度中的尺度參數,默認值為1.1
minNeighbors -為每一個級聯矩形應該保留的鄰近個數,默認為3,表示至少有3次檢測到目標,才認為是目標
flags -CV_HAAR_DO_CANNY_PRUNING,利用Canny邊緣檢測器來排除一些邊緣很少或者很多的圖像區域;
%20 %20 %20 %20 CV_HAAR_SCALE_IMAGE,按比例正常檢測;
%20 %20 %20 CV_HAAR_FIND_BIGGEST_OBJECT,只檢測最大的物體;
%20 %20 %20 %20 CV_HAAR_DO_ROUGH_SEARCH,只做粗略檢測。默認值是0
minSize和maxSize -用來限制得到的目標區域的范圍(先找maxsize,再用1.1參數縮小,直到小于minSize終止檢測)
6、opencv關于Haar介紹
(haarfeatures.cpp%20——opencv3.0)
The%20object%20detector%20described%20below%20has%20been%20initially%20proposed%20by%20Paul%20Viola [pdf] and%20improved%20by%20Rainer%20Lienhart [pdf] .
First,%20a%20classifier%20(namely%20a cascade%20of%20boosted%20classifiers%20working%20with%20haar-like%20features)%20is%20trained%20with%20a%20few%20hundred%20sample%20views%20of%20a%20particular%20object%20(i.e.,%20a%20face%20or%20a%20car),%20called%20positive%20examples,%20that%20are%20scaled%20to%20the%20same%20size%20(say,%2020x20),%20and%20negative%20examples%20-%20arbitrary%20images%20of%20the%20same%20size.
After%20a%20classifier%20is%20trained,%20it%20can%20be%20applied%20to%20a%20region%20of%20interest%20(of%20the%20same%20size%20as%20used%20during%20the%20training)%20in%20an%20input%20image.%20The%20classifier%20outputs%20a%20"1"%20if%20the%20region%20is%20likely%20to%20show%20the%20object%20(i.e.,%20face/car),%20and%20"0"%20otherwise.%20To%20search%20for%20the%20object%20in%20the%20whole%20image%20one%20can%20move%20the%20search%20window%20across%20the%20image%20and%20check%20every%20location%20using%20the%20classifier.%20The%20classifier%20is%20designed%20so%20that%20it%20can%20be%20easily%20"resized"%20in%20order%20to%20be%20able%20to%20find%20the%20objects%20of%20interest%20at%20different%20sizes,%20which%20is%20more%20efficient%20than%20resizing%20the%20image%20itself.%20So,%20to%20find%20an%20object%20of%20an%20unknown%20size%20in%20the%20image%20the%20scan%20procedure%20should%20be%20done%20several%20times%20at%20different%20scales.
The%20Word%20"cascade"%20in%20the%20classifier%20name%20means%20that%20the%20resultant%20classifier%20consists%20of%20several%20simpler%20classifiers%20(stages)%20that%20are%20applied%20subsequently%20to%20a%20region%20of%20interest%20until%20at%20some%20stage%20the%20candidate%20is%20rejected%20or%20all%20the%20stages%20are%20passed.%20The%20word%20"boosted"%20means%20that%20the%20classifiers%20at%20every%20stage%20of%20the%20cascade%20are%20complex%20themselves%20and%20they%20are%20built%20out%20of%20basic%20classifiers%20using%20one%20of%20four%20different%20boosting%20techniques%20(weighted%20voting).%20Currently%20Discrete%20Adaboost,%20Real%20Adaboost,%20Gentle%20Adaboost%20and%20Logitboost%20are%20supported.%20The%20basic%20classifiers%20are%20decision-tree%20classifiers%20with%20at%20least%202%20leaves.%20Haar-like%20features%20are%20the%20input%20to%20the%20basic%20classifiers,%20and%20are%20calculated%20as%20described%20below.%20The%20current%20algorithm%20uses%20the%20following%20Haar-like%20features:
The feature used in a particular classifier is specified by its shape (1a, 2b etc.), position within the region of interest and the scale (this scale is not the same as the scale used at the detection stage, though these two scales are multiplied). For example, in the case of the third line feature (2c) the response is calculated as the difference between the sum of image pixels under the rectangle covering the whole feature (including the two white stripes and the black stripe in the middle) and the sum of the image pixels under the black stripe multiplied by 3 in order to compensate for the differences in the size of areas. The sums of pixel values over a rectangular regions are calculated rapidly using integral images (see below and the integral description).
To see the object detector at work, have a look at the facedetect demo: https://github.com/Itseez/opencv/tree/master/samples/cpp/dbt_face_detection.cpp
The following reference is for the detection part only. There is a separate application called opencv_traincascade that can train a cascade of boosted classifiers from a set of samples.
NoteIn the new C++ interface it is also possible to use LBP (local binary pattern) features in addition to Haar-like features. .. [Viola01] Paul Viola and Michael J. Jones. Rapid Object Detection using a Boosted Cascade of Simple Features. IEEE CVPR, 2001. The paper is available online at https://www.cs.cmu.edu/~efros/courses/LBMV07/Papers/viola-cvpr-01.pdf(上述有提到)
7、opencv關于boost
(boost.cpp——opencv3.0)
A common machine learning task is supervised learning. In supervised learning, the goal is to learn the functional relationship
Boosting is a powerful learning concept that provides a solution to the supervised classification learning task. It combines the performance of many "weak" classifiers to produce a powerful committee [125] . A weak classifier is only required to be better than chance, and thus can be very simple and computationally inexpensive. However, many of them smartly combine results to a strong classifier that often outperforms most "monolithic" strong classifiers such as SVMs and Neural Networks.
Decision trees are the most popular weak classifiers used in boosting schemes. Often the simplest decision trees with only a single split node per tree (called stumps ) are sufficient.
The boosted model is based on
Different variants of boosting are known as Discrete Adaboost, Real AdaBoost, LogitBoost, and Gentle AdaBoost [49] . All of them are very similar in their overall structure. Therefore, this chapter focuses only on the standard two-class Discrete AdaBoost algorithm, outlined below. Initially the same weight is assigned to each sample (step 2). Then, a weak classifier
Two-class Discrete AdaBoost Algorithm
SetTo reduce computation time for boosted models without substantially losing accuracy, the influence trimming technique can be employed. As the training algorithm proceeds and the number of trees in the ensemble is increased, a larger number of the training samples are classified correctly and with increasing confidence, thereby those samples receive smaller weights on the subsequent iterations. Examples with a very low relative weight have a small impact on the weak classifier training. Thus, such examples may be excluded during the weak classifier training without having much effect on the induced classifier. This process is controlled with the weight_trim_rate parameter. Only examples with the summary fraction weight_trim_rate of the total weight mass are used in the weak classifier training. Note that the weights for all training examples are recomputed at each training iteration. Examples deleted at a particular iteration may be used again for learning some of the weak classifiers further [49]
See alsocv::ml::BoostStatModel::predict(samples, results, flags) should be used. Pass flags=StatModel::RAW_OUTPUT to get the raw sum from Boost classifier.
8、關于訓練過程打印信息的解釋
1)POS count : consumed n1 : n2
每次都調用updateTrainingSet( requiredLeafFARate, tempLeafFARate );函數
[cpp] view plain copy新聞熱點
疑難解答