OpenCV classification detector training

There are two programs in OpenCV to train cascade classifiers: opencv_haartraining and opencv_traincascade. opencv_traincascade is a new program written in C++ using the OpenCV 2.x API. The main difference between the two is that opencv_traincascade supports Haar, Hog and LBP (Local Binary Patterns) three features, and it is easy to add other features. Compared with Haar features, LBP features are integer features, so the training and detection process will be several times faster than Haar features. The accuracy of LBP and Haar features for detection depends on the quality of training data and training parameters in the training process. It is possible to train a classifier with the same accuracy as LBP based on Haar features.

The classifier file formats output by opencv_traincascade and opencv_haartraining are not the same. Note that the new cascade detection interface (see the CascadeClassifier class in the objdetect module) supports both formats. opencv_traincascade can export trained cascade classifiers in old format. But after the training process is interrupted and then restart the training process, opencv_traincascade and opencv_haartraining cannot load a file format different from that before the interruption.

The opencv_traincascade program uses TBB to handle multithreading. If you want to use multi-core parallel operation acceleration, please use TBB to compile OpenCV. There are also some auxiliary programs related to training.

opencv_createsamples is used to prepare positive sample data and test data for training. opencv_createsamples can generate positive sample data that can be supported by opencv_haartraining and opencv_traincascade programs. Its output is a file with a *.vec extension, which stores the image in binary.

opencv_performance can be used to evaluate the quality of classifiers. It reads in a set of annotated images, runs the classifier and reports performance, such as the number of detected objects, the number of missed detections, the number of false detections, and other information.

http://www.opencv.org.cn/opencvdoc/2.3.2/html/doc/user_guide/ug_traincascade.html
http://blog.csdn.net/delltdk/article/details/9186875

1 Create sample sets pos and neg


The positive sample image is stored in pos, which can be one image or multiple images. Neg stores images containing negative samples, which can be arbitrary images, but these images cannot contain objects to be detected.

Note:
Generally, a large negative sample library is required to be sent to the training program for training. If it is an absolutely rigid object, such as the OpenCV logo, and there is only one positive sample image, then a large number of positive samples can be obtained by randomly rotating the object image, changing the brightness of the logo, and placing the logo on any background; if it is a human face, hundreds or even thousands of positive samples are required. In the case where the object to be detected is a human face, all races, ages, expressions and even beard styles need to be considered.

2 Generate positive/negative sample description file negdata.txt


(1) Negative sample description file negdata.txt

Enter "cd d:\%....%\pos" in the command line window to switch to the neg folder, and enter "dir/b>negdata.txt" to generate a description file negdata.txt in the neg folder, storing the file names of all pictures in the neg. Be careful to delete the last negdata.txt.

Note:
If you save negdata.txt outside the neg folder, you need to add a relative path or absolute path outside each line to indicate the location of the sample. One method is to copy the contents of the negdata.txt file to word, and use the replace function to achieve quick modification. neg is replaced by neg\neg

(2) Positive sample description file posdata.txt

Create posdata.txt in the same way as creating negdata.txt, except that the number and position of the target in each sample needs to be pointed out in the positive sample description file, such as pos\1.bmp 1 x1 y1 x2 y2, where (x1,y1,x2,y2) is the rectangular box where the target is located, and pos\1.bmp2 x1 y1 x2 y2 x1' y1' x2' y2'

Because the positive samples we prepared are basically targets, we only need to add 1 0 0 width height after the file name.

Note:
1 If the positive sample images are of different sizes, on the one hand, you can use ImageResize or matlab to unify the images into the same size to generate posdata.txt, or program ImageToTxt to directly generate positive sample description files with images of different sizes. If it is not normalized at this stage, it will be automatically normalized in the program when the .vec file is generated later.

2 The sample description file must be consistent with the image. Images may exist but are not written in the description file, that is, there are redundant images, but do not write images that do not exist in the description file.

Question:
       Does the method of normalizing size affect the training results? What method does opencv_createsamples use for normalization?

sample = cvCreateImage( cvSize(winwidth, winheight ), IPL_DEPTH_8U, 1 );

fscanf( info, "%d %d %d%d", &x, &y, &width, &height )

    cvSetImageROI( src, cvRect( x, y, width,height ) );

cvResize( src, sample,

width >=sample->width && height >= sample->height ? CV_INTER_AREA :CV_INTER_LINEAR );

CV_INTER_NN nearest neighbor interpolation

CV_INTER_LINER bilinear interpolation, the default

CV_INTER_AREA uses pixel relationship resampling to avoid ripples when the image is reduced, similar to CV_INTER_NN when the image method is used

CV_INTER_CUBIC cubic interpolation

3 Generate .vec file


The number of positive samples generated and the degree of randomness can be controlled by the command line parameters of opencv_createsamples.

Use create.dat to call %Opencv%\vs2008\bin\Release\ opencv_createsamples.exe

Check the parameter settings in createsamples.cpp

-info Input positive sample description file, default NULL

-img input image file name, default NULL

-bg Negative sample description file, the file contains a series of image file names randomly selected as the object background, the default is NULL

-num The number of positive samples generated, the default is 1000

-bgcolor background color, which means transparent color, default 0

-bgthresh color tolerance, all pixels between bgcolor-bgthresh and bgcolor+bgthresh are set as transparent pixels, that is, white noise is added to the foreground image, the default is 80

-inv foreground image color flip flag, if specified color flip, default 0 (no flip)

-randinv If the specified color will be flipped randomly, the default is 0

-maxidev The maximum brightness gradient of pixels in the foreground image, default 40

-maxxangle X-axis maximum rotation angle, in radians, default 1.1

-maxyangle Y-axis maximum rotation angle, in radians, default 1.1

-maxzangle Z-axis maximum rotation angle, in radians, default 0.5

                                                         The input image is rotated along three axes, and the rotation angle is defined by the above 3 values.

-show If specified, each sample will be displayed, press the Esc key, the program will continue to create samples without displaying, the default is 0 (not displayed)

-scale Display image scaling, default 4.0

-w output sample width, default 24

-h output sample height, default 24

-vec output .vec file for training, default NULL

Convert the positive sample in the positive sample description file to a grayscale image and scale it to the -wh size and save it in the vec file.

(1) If you set -img and -vec

Call cvCreateTrainingSamples to create training samples using an image

(2) If you set -img, -bg and -info

Call cvCreateTestSamples to use an image to create test samples. And what does -bg do here? The purpose is to create a test image as a background.

(3) If -info and -vec are set (use the images in the positive sample description file to create training samples)

Call cvCreateTrainingSamplesFromInfo, the samples will be read in cvCreateTrainingSamplesFromInfo, and after resize, call icvWriteVecHeader and icvWriteVecSample to create a vec file.

(4) If only -vec is set (only the samples in the vec file are displayed)

Call cvShowVecSamples to view and check the positive samples saved in the vec file

The above parameters are set in create.dat, and there is a pause at the end, waiting for the result to be displayed: Done.Created num samples

4 Training process


Use train.dat to call %Opencv%\vs2008\bin\Release\ opencv_traincascade.exe

Check the parameter settings in traincascade.cpp

1. Basic parameters

-data directory name, store the trained classifier, if there is no training program, create it yourself

-vec positive sample .vec file, generated by opencv_createsamples

-bg negative sample description file

-numPos The number of positive samples used for each classifier training

-numNeg The number of negative samples used in each classifier training can be greater than the number of pictures specified by -bg

 -numStages number of stages to train the classifier

 -precalcValBufSize cache size, used to store precalculated feature values, in MB

-precalcIdxBufSize cache size, used to store precalculated feature index, unit M coins

-baseFormatSave is only valid when using Haar features, if specified, the cascade classifier will be stored in the old format

2. Cascade parameter cascadeParams

-stageType cascade type, staticconst char* stageTypes[] = { CC_BOOST };

-featureType feature type, staticconst char* featureTypes[] = { CC_HAAR, CC_LBP, CC_HOG };

-w             

-h The size of the training sample must be consistent with the size of the training sample created using opencv_createsamples

3. Boosted classifier parameter stageParams

-bt Boosted classifier type

DAB-discrete Adaboost, RAB-RealAdaboost, LB-LogiBoost, GAB-Gentle Adaboost

-minHitRate The minimum detection rate that each level of the classifier expects to get, the total maximum detection rate is about

min_hit_rate^number_of_stages

-maxFalseAlarmRate The maximum false positive rate that each level of the classifier expects to get, the total false positive rate is about

                                                        max_false_rate^number_of_stages

-weightTrimRate Specifies whether trimming should be used and its weight. A good value is 0.95

-maxDepth The maximum depth of the weak classifier, a good value is 1, binary tree

-maxWeightCount maximum number of weak classifiers in each level

4. Haar feature parameter featureParams

-mode The Haar feature type used in the training process, CORE-Allupright ALL-All Features BASIC-Viola

After the above parameters are set, call CvCascadeClassifier::train for training

Edit the above content in train.dat and run it. The training finally generates a -data folder specifying the cascade classifier and a cascade.xml file. The rest of the files are intermediate results. When the training program is interrupted, re-running the training program will read the previous training results without retraining from scratch. These files can be deleted after the training.

       In the cascade.xml file, there are mainly stageType, featureType, width, height, stageParams, featureParams, stageNum, stages and features nodes.

The number of stages in stages is set by yourself, and each stage contains multiple weakClassifiers, and each weakClassifier contains an internalNodes and a leafValues. The four variables in internalNodes represent a node, which are the left/right mark in the node, ID and threshold in the feature pool. Two variables in leafValues ​​represent a node, which are leftleaf and right leaf values.

       features is the feature pool of the classifier, each Haar feature contains a rectangle rect and the feature number to be extracted, and each Hog feature/LBP feature contains a rectangle.

                                  

Notice:


1. The image containing negative samples must not be smaller than the size set in create


Negative sample images can be of different sizes, but the image size should be larger than the size of the training window. When using negative sample images, OpenCV automatically cuts out an area of ​​the same size as the positive sample from the negative sample image as a negative sample. For details, please refer to icvGetNextFromBackgroundData. The specific cutout process is:
1) Determine the coordinates of the upper left corner of the cutout area (Point.x, Point.y)
2) Determine a minimum zoom ratio so that the original negative sample image is scaled to include the selected negative sample area
3) Scale the original negative sample image according to the calculated zoom ratio
4) Cut out the negative sample on the zoomed image.


2. –numPos is generally 200-300 less than the actual number of positive samples. Is there the same situation with -numNeg? Positive and negative sample selection rules?


If there is: the training stays in a classifier for several hours without response, the problem occurs in the function icvGetHaarTrainingDataFromBG that takes the negative samples; only when the previous strong classifier correctly classifies all the samples in the negative sample set will there be an infinite loop, because as long as one sample is misclassified as a positive sample, then count negative samples can be obtained by scanning the entire negative sample set count times. Of course, these count negative samples are actually count copies of a negative sample. To avoid this situation, the number of samples in the negative sample set needs to be large enough.

However, the classifier at this time has been fully rated and can be used, because its false detection rate is already very low, and there is no problem in terms of practicability. So we can limit the number of classifier stages by setting the -nstages parameter, stop and generate an xml file when appropriate.

Check it out from CvCascadeBoost::train

The function poscount= icvGetHaarTrainingDataFromVec( training_data, 0, npos,
                    (CvIntHaarClassifier*)tcc, vecfilename, &consumed ) is responsible for loading count (npos) positive samples from the positive sample set *.vec file. When the program runs to this point for the first time (that is, before training the first classifier), as long as there are count samples in the positive sample set, count positive samples will be taken out. At this point in the future, it is possible that count samples cannot be obtained, because
the samples that must be classified as positive samples (that is, correctly classified samples) by the previous cascade strong classifier ((CvIntHaarClassifier*) tcc) will be taken out as the next strong classifier training samples. For details, refer to the icvGetHaarTrainingData and icvEvalTreeCascadeClassifierFilter functions.

For training negative samples, please refer to the icvGetHaarTrainingDataFromBG and icvEvalTreeCascadeClassifierFilter functions for details.

int icvGetHaarTrainingDataFromBG(CvHaarTrainingData* data, int first, int count,
                                  CvIntHaarClassifier*cascade, double* acceptance_ratio, const char * filename = NULL ) The
  acceptance_ratio parameter of the return value records the number of negative samples actually taken out and the number of negative samples queried (if passed through the previous cascade When the number of negative samples of the stage strong classifier is very small, the program will repeatedly read the negative samples and use the thread_consumed_count count) ratio (acceptance_ratio = ((double) count) / consumed_count), that is, the false alarm rate, which is used to judge whether the trained cascade classifier reaches the target. If the target is reached, the training process is stopped. 

 
  Pay attention to a main For loop in the function icvGetHaarTrainingData:
        for( i = first; i < first +count; i++ ) //A total of count negative samples are read, and when less than {
        //so many negative samples are read, an infinite loop will occur!
 
  It is necessary to further explain the comments in the above code: only when the previous strong classifier correctly classifies all the samples in the negative sample set will there be an infinite loop. Because as long as one sample is misclassified as a positive sample, count negative samples can be obtained by scanning the entire negative sample set count times. Of course, these count negative samples are actually count copies of a negative sample. In order to avoid these situations, the number of samples in the negative sample set needs to be large enough.
  When the size of the negative sample image is exactly the same as the size of the positive sample, assuming that the final false alarm rate requirement of the classifier is falsealarm, and the number of negative samples participating in the training is count, the total number of negative samples required can be calculated as follows: TotalCount = count / falsearm 0.5^20)= 3,145,728,000=3.1
  billion
.

The function icvGetHaarTrainingDataFromBG () is responsible for loading count negative samples from the negative sample set. When the program runs to this point for the first time (that is, before training the first classifier), as long as there are count samples in the negative sample set, count negative samples must be taken out. At this point in the future, it is possible that count samples cannot be obtained, because the samples that must be classified as positive samples by the previous cascaded strong classifier (that is, the wrongly classified samples) will be taken out as the negative sample input of the next strong classifier.
 

对于int icvGetHaarTrainingData( CvHaarTrainingData* data,int first, int count,
                            CvIntHaarClassifier*cascade,
                            CvGetHaarTrainingDataCallbackcallback, void* userdata,
                            int*consumed, double* acceptance_ratio )

 Explanation of this function:

This is a general function for reading positive and negative samples, the difference lies in the call of callback. In this function, there is a variable thread_getcount, which indicates the number of samples divided into positive samples (whether the sample is a negative sample or a positive sample).
  The Consumed parameter passing the return value means that count positive samples are taken, and the total number of positive samples that have been queried. For negative samples is empty (null), no return value. 

3. Have you encountered a 10*20 classifier that cannot train Hog ​​features? Is there a size limit for Hog features?
         Check the calculation method of Hog features, HogDescriptor in Opencv

          The training methods are similar, but the extracted features are different, and the process is the same

4. When using opencv_traincascade to train the classifier, I encountered the following error:

Train dataset for temp stage can not be filled. Branch training terminated.

After checking on stackoverflow, the root of the problem lies in the failure to read negative samples. The reasons for the problem are:

1) The negative sample description file neg.txt cannot have a path name, that is: -bg neg.txt is legal, and -bg negdata/neg.txt is illegal. So you must put the neg.txt file and the exe file in the same directory

2) When the operating system is switched, the reading of negative samples will fail due to the format of the txt file. For example: neg.txt was born under the windows operating system, but training is performed under ubuntu, which will cause errors, because the newline character '\r' of the txt file under windows cannot be recognized under ubuntu

3) The problem of negative sample images. The reason for the error I encountered was that I did not check whether the negative sample was empty when saving it after processing the negative sample with opencv, that is, the empty mat was directly written into the file, resulting in an error

5. Other Notes

1 Regarding positive samples, first of all, positive samples are not what some people say. You actually have 300 positive samples, which can be written as 3000 in traincascade. This kind of thinking is useless. When collecting positive samples, you must pay attention to keeping the aspect ratio of all samples roughly the same. If you take a screenshot yourself, it is recommended to use the light and shadow magic hand. Or you can write a gui screenshot tool yourself. In order to avoid opencv error, when using opencv_traincascade.exe, -numPos should be slightly lower than the actual number of positive samples, for example, if you have 2100, you can set numpos to 1900-2000,

2 Regardless of the positive sample or the negative sample, do not use special characters when naming the picture, and you should name it pos1.jpg in a proper manner. . . . . Wait, special characters include (), opencv error will appear, or cannot be recognized.

3 The ratio of positive and negative samples is 1:2.5~1:3. An article once said that in order to reduce false positives, the number of negative samples can be increased.

4 When there is insufficient memory, there are several methods: 1. You can run on a 64-pc. 2. Reduce the number of positive and negative samples. 3 Reduce the width and height of positive samples.

Reference link:


 https://blog.csdn.net/xidianzhimeng/article/details/10470839

Opencv trains its own xml classifier and how to get opencv_createsamples.exe and opencv_traincascade.exe_Lizaozao96's Blog-CSDN Blog



(Final Chapter) [Image Recognition] 13. OpenCV Case Custom Training Set Classifier Object Detection_opencv Object Recognition_ζั͡ ั͡fogั͡Wolfั͡✾'s Blog-CSDN Blog

[OpenCV Learning Record] Cascade Classifier Training and Testing_Silence Tu's Blog-CSDN Blog

[cascades training] Use Cascade Trainer GUI for cascade classifier training - Ten Years Dream Lab Blog - CSDN Blog

[Opencv] Cascade cascade classifier_cascade classifier_Chris_Liu_'s Blog-CSDN Blog

Cascade training method of cascade classifier in OpenCV (Linux)_opencv cascade classifier_Zhaozhuoqihua R Blog-CSDN Blog

common problem

I tried using opencv_traincascade.exe, but it crashes a few seconds after starting. This is how I proceed: opencv_traincascade crashes without explanation

This is because of the window size you choose. The memory used by traincascade.exe to train a classifier grows exponentially with the window size, and there are several home computers that can handle the 100x100 window size in traincascade.

Do this exercise: open task manager and monitor memory usage When you start training, you will notice that the program crashes when it cannot allocate more memory. To fix this, you have to choose a smaller window size.

You can use the parameters -precalcValBufSize 1024 and -precalcIdxBufSize 1024. You can increase the numbers if you like, but you need to put the values ​​below the RAM capacity

Hello, landlord. I am also training a classifier. The current problem is that only one layer is trained, the recognition rate reaches 1, FA=0, and then the training stage1 is infinitely stuck. Where is the problem? The training process command is as follows: opencv_traincascade -data classifier -vec pos.vec -vg neg.txt -numPos 11000 -numNeg 26545 -numStage 15 -precalcValBufSize 1024 0 -precalcIdxBufSize 10240 -w 64 -h 64 -mode ALL There are 12519 positive samples and 26545 negative samples, all of which are 64x64 in size, and the training stage is set to
15
layers

.
The parameters are relatively normal. Why does the result of training stage0 be HR=1, FA=0
and then get stuck in stage1, a headache? ?

    • The number of negative samples is set incorrectly, set it to half of the total, or other values, anyway, it should be less than the actual number

  • Hello...I see that setting the training parameters...just set the maximum number of weak classifiers maxWeakCount contained in each strong classifier...how is the number of weak classifiers contained in each strong classifier set?...

    • The sample path is wrong

    • The vCascadeClassifier::train() function provides an overview of the entire Cascade execution process. Including the initialization before training, the sample set update between the strong classifiers of each stage and the training of strong classifiers can be seen. The most conspicuous one is the for loop of Stage training.
      To update the samples between stages, it is necessary to ensure that the number of positive and negative samples is sufficient when training a new stage.
      if ( !updateTrainingSet( tempLeafFARate ) )
      { the number of samples is not enough, exit training cout << "Train dataset for temp stage can not be filled. Branch training terminated." << endl; break; } if ( tempLeafFARate <= requiredLeafFARate ) { The false alarm rate has reached the standard and no longer continue training cout << "Required leaf false alarm rate achieved. Branch training terminated." << endl; break; } ​Edit The function analysis in the web link says so










  • zhi_jin 2016.05.20

    Hello, landlord, I keep getting this error after changing the parameters, can you help me to find out where the problem is~?




















    minHitRate: 0.999
    maxFalseAlarmRate: 0.5
    weightTrimRate: 0.95
    maxDepth: 1
    maxWeakCount: 100
    mode: BASIC

    ===== TRAINING 0-stage =====
    <BEGIN
    POS count : consumed 2000 : 2000
    Train dataset for temp stage can not be filled. Branch training terminated.
    Cascade classifier can't be trained. Check the used training parameters.

    • Excuse me, did you succeed in changing it in the end? How did you change it, I have the same problem

    • Hello. Can you send me a positive sample and a negative sample. My email [email protected] Thank you

  • The problems I encounter now are:
    1. The false alarm rate is a bit high, and false detections are often made during detection;
    I use about 4000 positive samples and about 8000 negative samples, minHitRate=0.997, maxFalseRate=0.5
    My current hit rate is very high, how can I reduce the false detection rate?
    2. The second question is how to adjust each parameter? How to improve the quality of samples? How to evaluate the quality of the sample? Thank you!

    • Reply to wangyuezhuiyi2016.11.08

      I have been making mistakes OpenCV Error: Bad argument (Can not get new positive sample. The most possible reason is insufficient count of samples in given vec-file. Can you give me an
      address where I can download directly without restrictions, or send me email [email protected] Thank you

  • kissmemissme2016.05.09

    Hello, the landlord, how do I use the random forest algorithm to train the classifier?

  • Sir, can you please send me the source code, thank you very much!!

    • Reply to the second half price 2016.04.29

      The source code can be found from opencv, in the sources/apps/traincascade directory

  • Why I changed -numPos to 3200, the program can only recognize 2000, why is this, it happens every time, the landlord please answer, thank you very much. Negative samples can be identified by changing any number

    • Shallow_mo reply lee333333 2016.11.28

      It is possible that the p in the parameter name of your -numPos is not written in uppercase

  • opencv_traincascade.exe, can the landlord send me a copy, mine is version 2.4.6, why the computer can’t find it, there are many problems with version 2.4.9, my email is [email protected]

  • chen123123_sh 2014.06.11

    Are the modes of HOG and HAAR training the same?

    • The author of Xidian Dream replied chen123123_sh2014.10.13

      The training methods are similar, but the extracted features are different, and the process is the same

  • Hello landlord, I would like to ask about the leafvalue in xml, how are its leftvalue and rightvalue calculated? And what is the difference between featureIdex and featcomponent?

    • The author of Xidian Dream replied hwl567892014.10.13

      featureIndex indicates the Id number in the feature pool (corresponding to the number of features in features), and featureComponent indicates the feature serial number of the HOG feature (0-35)

  • a201c501ys2014.02.23

    Hello, I have 250 positive samples with a size of 10*40, and 1000 negative samples that are the same size as the positive samples and do not contain positive sample images. However, I am stuck after training to the fourth layer. I don’t know what’s going on?

    • Hello, is there an upper limit on the positive and negative samples for training?

      I think the number of training samples is too small, and it is trapped in the minimum value and cannot jump out. Try adding more samples. When using adaboost training, it is best to have enough samples.

  • Hello, the landlord, I just started using the opencv classifier, and I was a little confused when I read your article. What does "use create.dat to call %Opencv%\vs2008\bin\Release\ opencv_createsamples.exe" mean? ?

  • The meaning of that sentence is to write a file with an extension of dat, which contains the following parameter settings. I believe you will understand after looking at the source file of the opencv_createsamples program. The dat file is the argv parameter in the main function, argv[0]=application name, which is opencv_createsamples.exe

Guess you like

Origin blog.csdn.net/qq_42672770/article/details/131795143