opencv訓練分類器

http://docs.opencv.org/2.4/doc/user_guide/ug_traincascade.html?highlight=train

 Cascade Classifier Training

Introduction

The work with a cascade classifier inlcudes two major stages: training and detection. Detection stage is described in a documentation of objdetect module of general OpenCV documentation. Documentation gives some basic information about cascade classifier. Current guide is describing how to train a cascade classifier: preparation of a training data and running the training application.

Important notes

There are two applications in OpenCV to train cascade classifier: opencv_haartraining and opencv_traincascade. opencv_traincascade is a newer version, written in C++ in accordance to OpenCV 2.x API. But the main difference between this two applications is that opencv_traincascade supports both Haar [Viola2001] and LBP [Liao2007] (Local Binary Patterns) features. LBP features are integer in contrast to Haar features, so both training and detection with LBP are several times faster then with Haar features. Regarding the LBP and Haar detection quality, it depends on training: the quality of training dataset first of all and training parameters too. It’s possible to train a LBP-based classifier that will provide almost the same quality as Haar-based one.

opencv_traincascade and opencv_haartraining store the trained classifier in different file formats. Note, the newer cascade detection interface (see CascadeClassifier class in objdetect module) support both formats. opencv_traincascade can save (export) a trained cascade in the older format. But opencv_traincascade and opencv_haartraining can not load (import) a classifier in another format for the futher training after interruption.

Note that opencv_traincascade application can use TBB for multi-threading. To use it in multicore mode OpenCV must be built with TBB.

Also there are some auxilary utilities related to the training.

  • opencv_createsamples is used to prepare a training dataset of positive and test samples. opencv_createsamples produces dataset of positive samples in a format that is supported by both opencv_haartraining and opencv_traincascade applications. The output is a file with *.vec extension, it is a binary format which contains images.

  • opencv_performance may be used to evaluate the quality of classifiers, but for trained by opencv_haartraining only. It takes a collection of marked up images, runs the classifier and reports the performance, i.e. number of found objects, number of missed objects, number of false alarms and other information.

Since opencv_haartraining is an obsolete application, only opencv_traincascade will be described futher. opencv_createsamples utility is  needed to prepare a training data for opencv_traincascade, so it will be described too.

Training data preparation

For training we need a set of samples. There are two types of samples: negative and positive. Negative samples correspond to non-object images. Positive samples correspond to images with detected objects. Set of negative samples must be prepared manually, whereas set of positive samples is created using opencv_createsamples utility.

Negative Samples

Negative samples are taken from arbitrary images. These images must not contain detected objects. Negative samples are enumerated in a special file. It is a text file in which each line contains an image filename (relative to the directory of the description file) of negative sample image. This file must be created manually. Note that negative samples and sample images are also called background samples or background samples images, and are used interchangeably in this document. Described images may be of different sizes. But each image should be (but not nessesarily) larger then a training window size, because these images are used to subsample negative image to the training size.

An example of description file:

Directory structure:

/img
  img1.jpg
  img2.jpg
bg.txt

File bg.txt:

img/img1.jpg
img/img2.jpg

Positive Samples

Positive samples are created by opencv_createsamples utility. They may be created from a single image with object or from a collection of previously marked up images.

Please note that you need a large dataset of positive samples before you give it to the mentioned utility, because it only applies perspective transformation. For example you may need only one positive sample for absolutely rigid object like an OpenCV logo, but you definetely need hundreds and even thousands of positive samples for faces. In the case of faces you should consider all the race and age groups, emotions and perhaps beard styles.

So, a single object image may contain a company logo. Then a large set of positive samples is created from the given object image by random rotating, changing the logo intensity as well as placing the logo on arbitrary background. The amount and range of randomness can be controlled by command line arguments of opencv_createsamples utility.

Command line arguments:

  • -vec <vec_file_name>

    Name of the output file containing the positive samples for training.

  • -img <image_file_name>

    Source object image (e.g., a company logo).

  • -bg <background_file_name>

    Background description file; contains a list of images which are used as a background for randomly distorted versions of the object.

  • -num <number_of_samples>

    Number of positive samples to generate.

  • -bgcolor <background_color>

    Background color (currently grayscale images are assumed); the background color denotes the transparent color. Since there might be compression artifacts, the amount of color tolerance can be specified by -bgthresh. All pixels withing bgcolor-bgthresh and bgcolor+bgthresh range are interpreted as transparent.

  • -bgthresh <background_color_threshold>

  • -inv

    If specified, colors will be inverted.

  • -randinv

    If specified, colors will be inverted randomly.

  • -maxidev <max_intensity_deviation>

    Maximal intensity deviation of pixels in foreground samples.

  • -maxxangle <max_x_rotation_angle>

  • -maxyangle <max_y_rotation_angle>

  • -maxzangle <max_z_rotation_angle>

    Maximum rotation angles must be given in radians.

  • -show

    Useful debugging option. If specified, each sample will be shown. Pressing Esc will continue the samples creation process without.

  • -w <sample_width>

    Width (in pixels) of the output samples.

  • -h <sample_height>

    Height (in pixels) of the output samples.

For following procedure is used to create a sample object instance: The source image is rotated randomly around all three axes. The chosen angle is limited my -max?angle. Then pixels having the intensity from [bg_color-bg_color_threshold; bg_color+bg_color_threshold] range are interpreted as transparent. White noise is added to the intensities of the foreground. If the -inv key is specified then foreground pixel intensities are inverted. If -randinv key is specified then algorithm randomly selects whether inversion should be applied to this sample. Finally, the obtained image is placed onto an arbitrary background from the background description file, resized to the desired size specified by -w and -h and stored to the vec-file, specified by the -vec command line option.

Positive samples also may be obtained from a collection of previously marked up images. This collection is described by a text file similar to background description file. Each line of this file corresponds to an image. The first element of the line is the filename. It is followed by the number of object instances. The following numbers are the coordinates of objects bounding rectangles (x, y, width, height).

An example of description file:

Directory structure:

/img
  img1.jpg
  img2.jpg
info.dat

File info.dat:

img/img1.jpg  1  140 100 45 45
img/img2.jpg  2  100 200 50 50   50 30 25 25

Image img1.jpg contains single object instance with the following coordinates of bounding rectangle: (140, 100, 45, 45). Image img2.jpg contains two object instances.

In order to create positive samples from such collection, -info argument should be specified instead of -img:

  • -info <collection_file_name>

    Description file of marked up images collection.

The scheme of samples creation in this case is as follows. The object instances are taken from images. Then they are resized to target samples size and stored in output vec-file. No distortion is applied, so the only affecting arguments are -w, -h, -show and -num.

opencv_createsamples utility may be used for examining samples stored in positive samples file. In order to do this only -vec, -w and -h parameters should be specified.

Note that for training, it does not matter how vec-files with positive samples are generated. But opencv_createsamples utility is the only one way to collect/create a vector file of positive samples, provided by OpenCV.

Example of vec-file is available here opencv/data/vec_files/trainingfaces_24-24.vec. It can be used to train a face detector with the following window size: -w 24 -h 24.

Cascade Training

The next step is the training of classifier. As mentioned above opencv_traincascade or opencv_haartraining may be used to train a cascade classifier, but only the newer opencv_traincascade will be described futher.

Command line arguments of opencv_traincascade application grouped by purposes:

  1. Common arguments:

  • -data <cascade_dir_name>

    Where the trained classifier should be stored.

  • -vec <vec_file_name>

    vec-file with positive samples (created by opencv_createsamples utility).

  • -bg <background_file_name>

    Background description file.

  • -numPos <number_of_positive_samples>

  • -numNeg <number_of_negative_samples>

    Number of positive/negative samples used in training for every classifier stage.

  • -numStages <number_of_stages>

    Number of cascade stages to be trained.

  • -precalcValBufSize <precalculated_vals_buffer_size_in_Mb>

    Size of buffer for precalculated feature values (in Mb).

  • -precalcIdxBufSize <precalculated_idxs_buffer_size_in_Mb>

    Size of buffer for precalculated feature indices (in Mb). The more memory you have the faster the training process.

  • -baseFormatSave

    This argument is actual in case of Haar-like features. If it is specified, the cascade will be saved in the old format.

Cascade parameters:

  • -stageType <BOOST(default)>

    Type of stages. Only boosted classifier are supported as a stage type at the moment.

  • -featureType<{HAAR(default), LBP}>

    Type of features: HAAR - Haar-like features, LBP - local binary patterns.

  • -w <sampleWidth>

  • -h <sampleHeight>

    Size of training samples (in pixels). Must have exactly the same values as used during training samples creation (opencv_createsamples utility).

Boosted classifer parameters:

  • -bt <{DAB, RAB, LB, GAB(default)}>

    Type of boosted classifiers: DAB - Discrete AdaBoost, RAB - Real AdaBoost, LB - LogitBoost, GAB - Gentle AdaBoost.

  • -minHitRate <min_hit_rate>

    Minimal desired hit rate for each stage of the classifier. Overall hit rate may be estimated as (min_hit_rate^number_of_stages).

  • -maxFalseAlarmRate <max_false_alarm_rate>

    Maximal desired false alarm rate for each stage of the classifier. Overall false alarm rate may be estimated as (max_false_alarm_rate^number_of_stages).

  • -weightTrimRate <weight_trim_rate>

    Specifies whether trimming should be used and its weight. A decent choice is 0.95.

  • -maxDepth <max_depth_of_weak_tree>

    Maximal depth of a weak tree. A decent choice is 1, that is case of stumps.

  • -maxWeakCount <max_weak_tree_count>

    Maximal count of weak trees for every cascade stage. The boosted classifier (stage) will have so many weak trees (<=maxWeakCount), as needed to achieve the given -maxFalseAlarmRate.

Haar-like feature parameters:

  • -mode <BASIC (default) | CORE | ALL>

    Selects the type of Haar features set used in training. BASIC use only upright features, while ALL uses the full set of upright and 45 degree rotated feature set. See [Rainer2002] for more details.

Local Binary Patterns parameters:Local Binary Patterns don’t have parameters.

After the opencv_traincascade application has finished its work, the trained cascade will be saved in cascade.xml file in the folder, which was passed as -data parameter. Other files in this folder are created for the case of interrupted training, so you may delete them after completion of training.

Training is finished and you can test you cascade classifier!

[Viola2001]Paul Viola, Michael Jones. Rapid Object Detection using a Boosted Cascade of Simple Features. Conference on Computer Vision and Pattern Recognition (CVPR), 2001, pp. 511-518.
[Rainer2002]Rainer Lienhart and Jochen Maydt. An Extended Set of Haar-like Features for Rapid Object Detection. Submitted to ICIP2002.
[Liao2007]Shengcai Liao, Xiangxin Zhu, Zhen Lei, Lun Zhang and Stan Z. Li. Learning Multi-scale Block Local Binary Patterns for Face Recognition. International Conference on Biometrics (ICB), 2007, pp. 828-837.

介紹

使用級聯分類器工作包括兩個階段:訓練和檢測。 檢測部分在OpenCVobjdetect 模塊的文檔中有介紹,在那個文檔中給出了一些級聯分類器的基本介紹。當前的指南描述了如何訓練分類器:準備訓練數據和運行訓練程序。

重點注意事項

OpenCV中有兩個程序可以訓練級聯分類器: opencv_haartraining 和 opencv_traincascade 。 opencv_traincascade 是一個新程序,根據OpenCV 2.x API 用C++ 編寫。這二者主要的區別是opencv_traincascade 支持 Haar[Viola2001] 和 LBP[Liao2007] (Local Binary Patterns) 兩種特徵,並易於增加其他的特徵。與Haar特徵相比,LBP特徵是整數特徵,因此訓練和檢測過程都會比Haar特徵快幾倍。LBP和Haar特徵用於檢 測的準確率,是依賴訓練過程中的訓練數據的質量和訓練參數。訓練一個與基於Haar特徵同樣準確度的LBP的分類器是可能的。

opencv_traincascadeopencv_haartraining 所輸出的分類器文件格式並不相同。注意,新的級聯檢測接口(參考objdetect 模塊中的 CascadeClassifier 類)支持這兩種格式。opencv_traincascade 可以舊格式導出選練好的級聯分類器。但是在訓練過程被中斷後再重啓訓練過程,opencv_traincascade andopencv_haartraining 不能裝載與中斷前不同的文件格式。

opencv_traincascade 程序使用TBB來處理多線程。如果希望使用多核並行運算加速,請使用TBB來編譯OpenCV。

還有一些與訓練相關的輔助程序。

  • opencv_createsamples 用來準備訓練用的正樣本數據和測試數據。opencv_createsamples 能夠生成能被opencv_haartrainingopencv_traincascade 程序支持的正樣本數據。它的輸出爲以 *.vec 爲擴展名的文件,該文件以二進制方式存儲圖像。

  • opencv_performance 可以用來評估分類器的質量,但只能評估opencv_haartraining 輸出的分類器。它讀入一組標註好的圖像,運行分類器並報告性能,如檢測到物體的數目,漏檢的數目,誤檢的數目,以及其他信息。

既然 opencv_haartraining 是一個將被棄用的程序,下面將不再介紹,而會主要介紹opencv_traincascadeopencv_createsamples 程序用來爲opencv_traincascade 準備訓練樣本,因此也會介紹它。

準備訓練數據

訓練需要一些列樣本。樣本分兩類:負樣本和正樣本。負樣本是指不包括物體的圖像。正樣本是待檢測的物體的圖像。負樣本必須手工準備,正樣本使用opencv_createsamples 創建。

負樣本

負樣本可以是任意圖像,但是這些圖像中不能包含待檢測的物體。用於摳取負樣本的圖像文 件名被列在一個文件中。這個文件是純文本文件,每行是一個文件名(包括相對目錄和文件名)。負樣本和樣本圖像也叫做背景樣本,或者背景樣本圖像,本文檔中 對之不予區分。這些圖像可以是不同的尺寸,但是圖像尺寸應該比訓練窗口的尺寸大,因爲這些圖像將被用於摳取負樣本,並將負樣本縮小到訓練窗口大小。

下面是一個描述文件的例子:

假如目錄結構如下:

/img
  img1.jpg
  img2.jpg
bg.txt

則bg.txt文件中的內容將如下所示:

img/img1.jpg
img/img2.jpg

正樣本

正樣本由 opencv_createsamples 生成。正樣本可以由包含待檢測物體的一張圖片生成,也可由一系列標記好的圖像生成。

請注意你需要一個很大的負樣本庫送給訓練程序進行訓練。如果是絕對剛性的物體,如OpenCV的標誌,你只有一張正樣本圖像;如果是人臉,你需要幾百甚至幾千個正樣本。在待檢測物體是人臉的情況下,你需要考慮所有的人種、年齡、表情甚至鬍子的樣式。

如果只有一張包含物體的圖像,如一個公司的標誌,那麼可以通過對物體圖像的隨機旋轉、改變標誌亮度以及將標誌放在任意的背景上而獲得大量的正樣本。生成的正樣本數目以及隨機的程度都可以通過opencv_createsamples 的命令行參數控制。

命令行參數:

  • -vec<vec_file_name>

    輸出文件,內含用於訓練的正樣本。

  • -img<image_file_name>

    輸入圖像文件名(例如一個公司的標誌)。

  • -bg<background_file_name>

    背景圖像的描述文件,文件中包含一系列的圖像文件名,這些圖像將被隨機選作物體的背景。

  • -num<number_of_samples>

    生成的正樣本的數目。

  • -bgcolor<background_color>

    背景顏色(目前爲灰度圖);背景顏色表示透明顏色。因爲圖像壓縮可造成顏色偏差,顏色的容差可以由 -bgthresh 指定。所有處於 bgcolor-bgthreshbgcolor+bgthresh 之間的像素都被設置爲透明像素。

  • -bgthresh<background_color_threshold>

  • -inv

    如果指定該標誌,前景圖像的顏色將翻轉。

  • -randinv

    如果指定該標誌,顏色將隨機地翻轉。

  • -maxidev<max_intensity_deviation>

    前景樣本里像素的亮度梯度的最大值。

  • -maxxangle<max_x_rotation_angle>

    X軸最大旋轉角度,必須以弧度爲單位。

  • -maxyangle<max_y_rotation_angle>

    Y軸最大旋轉角度,必須以弧度爲單位。

  • -maxzangle<max_z_rotation_angle>

    Z軸最大旋轉角度,必須以弧度爲單位。

  • -show

    很有用的調試選項。如果指定該選項,每個樣本都將被顯示。如果按下 Esc 鍵,程序將繼續創建樣本但不再顯示。

  • -w<sample_width>

    輸出樣本的寬度(以像素爲單位)。

  • -h<sample_height>

    輸出樣本的高度(以像素爲單位)。

創建樣本的流程如下: 輸入圖像沿着三個軸隨機旋轉。旋轉的角度由 -max?angle 限定。然後像素的亮度值位於 [bg_color-bg_color_threshold;bg_color+bg_color_threshold]範圍的像素被設置爲透明像素。將白噪聲加到前景圖像上。如果指定了-inv ,那麼前景圖像的顏色將被翻轉。如果指定了-randinv ,程序將隨機選擇是否將顏色進行翻轉。任選背景圖像,將獲得的前景圖像放到背景圖像上,並將圖像調整到-w-h 指定的大小。最後將圖像存入vec文件,vec文件名由命令行參數-vec 指定。

正樣本也可從一系列事先標記好的圖像中創建。標記信息可以存儲於一個文本文件,與背景描述文件類似。文件中的每行對應一個圖像文件。每行的第一個元素爲圖像文件名,後面是物體的數目,最後是物體位置和大小的描述 (x, y, width, height)。

下面是描述文件的例子:

假設目錄結構如下:

/img
  img_with_faces_1.jpg
  img_with_faces_2.jpg
info.dat

文件info.dat裏的內容如下:

img/img_with_faces_1.jpg  1  140 100 45 45
img/img_with_faces_2.jpg  2  100 200 50 50   50 30 25 25

圖像img_with_faces_1.jpg中包含一個物體實例(如人臉),標示其在圖像中的位置和大小的矩形爲(140, 100, 45, 45)。圖像img_with_faces_2.jpg包含兩個物體實例。

從這樣的一系列數據中創建正樣本,需要在命令行指定 -info 而非前面所用的 -img 參數:

  • -info<collection_file_name>

    描述物體所在圖像以及大小位置的描述文件。

此部分樣本創建過程如下:將物體實例從圖像中摳取出,然後將之調整尺寸到目標尺寸,然後保存到輸出的vec文件。在此過程中不會對圖像進行變形,所以有效的命令行參數僅有-w,-h,-show-num

opencv_createsamples 也可以用來查看和檢查保存於vec正樣本文件中的正樣本。這時只需指定-vec-w-h 三個參數則可。opencv_createsamples 將逐一顯示正樣本圖像。

在訓練中,訓練程序並不關心包含正樣本的vec文件如何生成的,你可以自己寫程序來生成vec文件。但是OpenCV提供的工具中,只有 opencv_createsamples 程序能夠創建包含正樣本的vec文件。

一個vec文件的例子位於 opencv/data/vec_files/trainingfaces_24-24.vec 。它可用來訓練人臉分類器,窗口大小爲:-w24-h24

訓練級聯分類器

下一步是訓練分類器。如前面所述, opencv_traincascadeopencv_haartraining 都可用來訓練一個級聯分類器,但是此處只介紹opencv_traincascadeopencv_haartraining 的用法與opencv_traincascade 類似。

下面是 opencv_traincascade 的命令行參數,以用途分組介紹:

  1. 通用參數:

  • -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>

    分類器的每一級希望得到的最小檢測率。總的檢測率大約爲 min_hit_rate^number_of_stages。

  • -maxFalseAlarmRate<max_false_alarm_rate>

    分類器的每一級希望得到的最大誤檢率。總的誤檢率大約爲 max_false_alarm_rate^number_of_stages.

  • -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度旋轉特徵。更多細節請參考[Rainer2002]

LBP特徵參數:LBP特徵無參數。

opencv_traincascade 程序訓練結束以後,訓練好的級聯分類器將存儲於文件cascade.xml中,這個文件位於-data 指定的目錄中。這個目錄中的其他文件是訓練的中間結果,當訓練程序被中斷後,再重新運行訓練程序將讀入之前的訓練結果,而不需從頭重新訓練。訓練結束後,你可以刪除這些中間文件。

訓練結束後,你就可以測試你訓練好的級聯分類器了!

[Viola2001]Paul Viola, Michael Jones. Rapid Object Detection using a Boosted Cascade of Simple Features. Conference on Computer Vision and Pattern Recognition (CVPR), 2001, pp. 511-518.

[Rainer2002]Rainer Lienhart and Jochen Maydt. An Extended Set of Haar-like Features for Rapid Object Detection. Submitted to ICIP2002.

[Liao2007]Shengcai Liao, Xiangxin Zhu, Zhen Lei, Lun Zhang and Stan Z. Li.Learning Multi-scale Block Local Binary Patterns for Face Recognition. International Conference on Biometrics (ICB), 2007, pp. 828-837.


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章