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.


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