From 61515f78c3c7626a28eb17b2315ec97ca9cd2735 Mon Sep 17 00:00:00 2001 From: Kirill Kornyakov Date: Thu, 10 Nov 2011 12:03:44 +0000 Subject: [PATCH] user guide on cascade training a bit updated --- doc/user_guide/ug_traincascade.rst | 124 ++++++++++++++++--------------------- 1 file changed, 54 insertions(+), 70 deletions(-) diff --git a/doc/user_guide/ug_traincascade.rst b/doc/user_guide/ug_traincascade.rst index b207303..80047e1 100644 --- a/doc/user_guide/ug_traincascade.rst +++ b/doc/user_guide/ug_traincascade.rst @@ -6,53 +6,36 @@ Cascade Classifier Training Introduction ============ -Usage of a cascade classifier consists of two main stages: a training and a detection. -Detection stage with some base information about a cascade classifier are described in -a documentation of ``objdetect`` module of general OpenCV documentation. Current section -will describe how to prepare a training data and train a cascade classifier. +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. -Importent notes +Important notes --------------- -In OpenCV there are two applications to train cascade classifier: ``opencv_haartraining`` -and ``opencv_traincascade``. ``opencv_traincascade`` is a newer version, written on C++. But the main -versions difference is that ``opencv_traincascade`` supports both Haar [Viola2001]_ and LBP [Liao2007]_ (Local Binary Patterns) -features. LBP features are integer in contrast with Haar features, so both a training and -a detection with LBP are several times faster then with Haar features. As regards a comparison of LBP -and Haar detection quality, it depends on a cascade training: a quality of training dataset first of all -and training parameters too. It's possible to train a LBP-based classifier that will give -almost the same quality as Haar-based one. +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`` stores the trained classifier in different file formats. -Note, the newer cascade detection interfaces (see ``CascadeClassifier`` class in ``objdetect`` module) supportes -both formats. ``opencv_traincascade`` can save a trained cascade in the older format. But ``opencv_traincascade`` -and ``opencv_haartraining`` can not load a classifier in another format for the futher training. +``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. -Also there are some auxilary utilities related to the cascade classifier training. +Note that ``opencv_traincascade`` application can use TBB for multi-threading. To use it in multicore mode OpenCV must be built with TBB. - * ``opencv_createsamples`` is used to prepare the training base of positive samples and the test samples too. ``opencv_createsamples`` produces the positive samples dataset in a format that applicable (supported) both in ``opencv_haartraining`` and ``opencv_traincascade`` applications. +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 the classifier trained by ``opencv_haartraining`` application only. It takes a collection of marked up images, applies the classifier and outputs the performance, i.e. number of found objects, number of missed objects, number of false alarms and other information. + * ``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 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. +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 creation -====================== -For training a training samples must be collected. There are two sample types: negative samples and positive samples. Negative samples -correspond to non-object images. Positive samples correspond to object images. Negative samples set must be prepared manually, whereas -positive samples set are created using ``opencv_createsamples`` utility. +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 object representations. Negative samples are passed through -background description file. It is a text file in which each text line contains the filename (relative to the directory of the description file) -of negative sample image. This file must be created manually. Note that the 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 training window size, because these images are used to subsample negative image of training size. - +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. -Example of negative description file: +An example of description file: Directory structure: @@ -72,10 +55,11 @@ File bg.txt: Positive Samples ---------------- -Positive samples are created by createsamples utility. They may be created from single object image or from collection of previously marked up images. +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. -The single object image may for instance contain a company logo. Then are large set of positive samples are created from the given object image by randomly rotating, changing the logo color as well as placing the logo on arbitrary background. -The amount and range of randomness can be controlled by command line arguments. +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: @@ -89,7 +73,7 @@ Command line arguments: * ``-bg `` - Background description file; contains a list of images into which randomly distorted versions of the object are pasted for positive sample generation. + Background description file; contains a list of images which are used as a background for randomly distorted versions of the object. * ``-num `` @@ -97,21 +81,21 @@ Command line arguments: * ``-bgcolor `` - 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 between ``bgcolor-bgthresh`` and ``bgcolor+bgthresh`` are regarded as transparent. + 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 `` * ``-inv`` - If specified, the colors will be inverted. + If specified, colors will be inverted. * ``-randinv`` - If specified, the colors will be inverted randomly. + If specified, colors will be inverted randomly. * ``-maxidev `` - Maximal intensity deviation of foreground samples pixels. + Maximal intensity deviation of pixels in foreground samples. * ``-maxxangle `` @@ -119,11 +103,11 @@ Command line arguments: * ``-maxzangle `` - Maximum rotation angles in radians. + Maximum rotation angles must be given in radians. * ``-show`` - If specified, each sample will be shown. Pressing ``Esc`` will continue creation process without samples showing. Useful debugging option. + Useful debugging option. If specified, each sample will be shown. Pressing ``Esc`` will continue the samples creation process without. * ``-w `` @@ -134,11 +118,11 @@ Command line arguments: Height (in pixels) of the output samples. For following procedure is used to create a sample object instance: -The source image is rotated random around all three axes. The chosen angle is limited my ``-max?angle``. Next pixels of intensities in the range of [``bg_color-bg_color_threshold``; ``bg_color+bg_color_threshold``] are regarded as transparent. White noise is added to the intensities of the foreground. If ``-inv`` key is specified then foreground pixel intensities are inverted. If ``-randinv`` key is specified then it is randomly selected whether for this sample inversion will be applied. Finally, the obtained image is placed onto arbitrary background from the background description file, resized to the pixel size specified by ``-w`` and ``-h`` and stored into the file specified by the ``-vec`` command line parameter. +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 text file similar to background description file. Each line of this file corresponds to collection image. The first element of the line is image file name. It is followed by number of object instances. The following numbers are the coordinates of bounding rectangles (x, y, width, height). +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). -Example of description file: +An example of description file: Directory structure: @@ -156,23 +140,23 @@ 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 bounding rectangle (140, 100, 45, 45). Image img2.jpg contains two object instances. +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``: +In order to create positive samples from such collection, ``-info`` argument should be specified instead of ``-img``: * ``-info `` Description file of marked up images collection. -The scheme of sample creation in this case is as follows. The object instances are taken from images. Then they are resized to samples size and stored in output file. No distortion is applied, so the only affecting arguments are ``-w``, ``-h``, ``-show`` and ``-num``. +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``. -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. +``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 positive samples files are generated. So the createsamples utility is only one way to collect/create a vector file of positive samples. +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. Cascade Training ================ -The next step after samples creation is 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. +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: @@ -182,11 +166,11 @@ Command line arguments of ``opencv_traincascade`` application grouped by purpose * ``-data `` - Directory name in which the trained classifier is stored. + Where the trained classifier should be stored. * ``-vec `` - File name of positive sample file (created by trainingsamples utility or by any other means). + vec-file with positive samples (created by ``opencv_createsamples`` utility). * ``-bg `` @@ -196,19 +180,19 @@ Command line arguments of ``opencv_traincascade`` application grouped by purpose * ``-numNeg `` - Number of positive/negative samples used in training of each classifier stage. + Number of positive/negative samples used in training for every classifier stage. * ``-numStages `` - Number of stages to be trained. + Number of cascade stages to be trained. * ``-precalcValBufSize `` - Size of buffer of precalculated feature values (in Mb). + Size of buffer for precalculated feature values (in Mb). * ``-precalcIdxBufSize `` - Size of buffer of precalculated feature indices (in Mb). The more memory you have the faster the training process. + Size of buffer for precalculated feature indices (in Mb). The more memory you have the faster the training process. * ``-baseFormatSave`` @@ -220,7 +204,7 @@ Command line arguments of ``opencv_traincascade`` application grouped by purpose * ``-stageType `` - Type of stages. Only boosted classifier are supported as stage type yet. + Type of stages. Only boosted classifier are supported as a stage type at the moment. * ``-featureType<{HAAR(default), LBP}>`` @@ -230,7 +214,7 @@ Command line arguments of ``opencv_traincascade`` application grouped by purpose * ``-h `` - Size of training samples (in pixels). Must have exactly the same values as used during training samples creation (utility ``opencv_createsamples``). + Size of training samples (in pixels). Must have exactly the same values as used during training samples creation (``opencv_createsamples`` utility). #. @@ -242,23 +226,23 @@ Command line arguments of ``opencv_traincascade`` application grouped by purpose * ``-minHitRate `` - Minimal desired hit rate for each stage classifier. Overall hit rate may be estimated as (min_hit_rate^number_of_stages). + Minimal desired hit rate for each stage of the classifier. Overall hit rate may be estimated as (min_hit_rate^number_of_stages). * ``-maxFalseAlarmRate `` - Maximal desired false alarm rate for each stage classifier. Overall false alarm rate may be estimated as (max_false_alarm_rate^number_of_stages). + 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 `` - Specifies wheter and how much weight trimming should be used. A decent choice is 0.95. + Specifies whether trimming should be used and its weight. A decent choice is 0.95. * ``-maxDepth `` - Maximal depth of weak tree. A decent choice is 1 that is case of stumps. + Maximal depth of a weak tree. A decent choice is 1, that is case of stumps. * ``-maxWeakCount `` - Maximal count of weak trees for each cascade stage. The boosted classifier (stage) has so many weak trees (``<=maxWeakCount``), so need to achieve the given ``-maxFalseAlarmRate``. + 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``. #. @@ -266,17 +250,17 @@ Command line arguments of ``opencv_traincascade`` application grouped by purpose * ``-mode `` - 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. + 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 have not parameters. + Local Binary Patterns don't have parameters. -After the ``opencv_traincascade`` application has finished its work, the trained cascade will be saved in file cascade.xml in the folder which was passed as ``-data`` parameter. Other files in this folder are created for possibility of discontinuous training and you may delete them after training completion. +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. -Note ``opencv_traincascade`` application is TBB-parallelized. To use it in multicore mode OpenCV must be built with TBB. +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. -- 2.7.4