Opencv study notes - train face recognition classifier by yourself

       After using the classifier haarcascade_frontalface_alt.xml that comes with opencv for the basic understanding of face recognition, I decided to train a classifier myself to see the effect. The process can be roughly divided into three stages: sample collection, classifier training and face detection using the trained classifier.

1. Collection of samples

       Before training, we need to collect positive samples and negative samples.

       The positive samples use some images in the ORL face database. A total of 63 images are selected this time, and the initial image size is 92*112, but there is a problem of insufficient memory during training, so the size is normalized to 20 *20. Some screenshots are as follows:


       The negative samples are the gray images in the image segmentation database on the website of the weizmann team http://www.wisdom.weizmann.ac.il/~vision/Seg_Evaluation_DB/dl.html, with a total of 200 images and a size of about 300* 200 pixels, the screenshot is as follows:


       After the images are acquired, they are placed under two folders, pos_img (positive samples) and neg_img (negative samples). Create a new xml folder, the xml folder stores the data model generated during the training process, and finally opencv will convert it to generate an xml file, which is the final classifier.

       Then, find the following two exe executable files from the OpenCv installation directory:


opencv_createsamples.exe: used to create sample description files, the suffix is ​​.vec. Specifically prepared for OpenCV training, only positive samples are required, and negative samples are not required. 

opencv_haartraining.exe: It is a tool that comes with OpenCV, which encapsulates the haar feature extraction and the adaboost classifier training process. 

       Generally speaking, when the ratio of positive and negative samples is 1:3, the training results are better, but not absolute. Due to the differences of each sample, there is no absolute proportional relationship. However, negative samples need to be more than positive samples, because in principle, the greater the diversity of negative samples, the better, so that we can effectively reduce the false detection rate, not just through the training of positive samples to allow them to recognize objects. In this training, I selected 63 positive samples and 200 negative samples, all grayscale images.

       After the sample is prepared, open the command line window cmd under Windows and enter the specified directory.


Generate a pos.txt in the current pos_img directory to record the names of all images. 


Open the txt file, remove pos.txt, and change pgm to pgm 1 0 0 20 20.


Here 1 means that the number of times the current image is repeated is 1, and 0 0 20 20 means that the size of the target image is a rectangular box from (0, 0) to (20, 20). 

In the same way, enter the neg_img directory, generate neg.txt, then open the file to remove neg.txt, and leave the rest unchanged.



After the end, copy out pos.txt and neg.txt and put them in a directory with the opencv_createsamples.exe file:


2. Use opencv_createsamples.exe to create a list of parameters required for training

       The Windows console enters the specified directory. We have put the opencv_createsamples.exe file in the directory before. Enter opencv_createsamples.exe in the console to get the parameter information: 


In the command line window enter:


Then in the current directory, a pos.vec file is generated.


Instruction introduction:

-vec pos.vec: Specify the generated file, the final generated file is pos.vec; 
-info pos_img\pos.txt: target image description file, in pos\pos.txt; 
-bg neg_img\neg.txt: background image description file, in neg\neg.txt; 
-w 20: width of output sample, 20; 
-h 20: height of output sample, 20; 

-num 63: the number of positive samples to generate, 63;

3. Training the model

       The Windows console enters the specified directory. We have put the opencv_haartraining.exe file in the directory before. Enter opencv_haartraining.exe in the console to get the parameter information: 


Then enter the command to train:


Instruction introduction:

-vec pos.vec: positive sample file name; 
-bg neg_img\neg.txt: background description file; 
-data xml: specify the path name for storing the trained classifier, which is the xml folder created earlier; 
-w 20 : sample image width, 20; 
-h 20: sample image height, 20; 
-mem 1024: provided memory in MB, obviously, the larger the value, the more memory provided and the faster the operation; 
- npos 45: take 45 positive samples, less than the total number of positive samples; 
-neg 180: take 180 negative samples, less than the total number of negative samples; 
-nstages 5: specify the number of training layers, the higher the number of layers, the longer the time-consuming; 

-nsplits 5: The number of split child nodes, the default value is 2; (originally set to 5, but there has been an error during training, after changing to the default value, training can be performed normally).

Here, if the two parameters of -npos -neg are directly the same as the total number of positive and negative samples, an error will occur during the training process: the program terminates suddenly in the middle of training, prompting "OpenCV Error: Assertion failed (elements_read == 1) in icvGetHaarTraininDataFromVecCallback, file ..\..\..\..\opencv\apps\haartraining\cvhaartraining.cpp, line 1861". -npos means randomly select npos positive samples from the .vec file for each training. Due to the existence of false alarms, after each training of a strong classifier, the wrongly classified ones will be removed from the entire sample library, and the total samples will remain CountVec = CountVec - (1 - minhitrate) * npos, in the second The training process of a strong classifier is to sample from the remaining Countvec, and keep doing this nstage times, so there is CountVec >= (npos + (nstages - 1)*(1 -minhitrate) * npos ) + nneg . When npos is set to be the same as the total number of samples in vec, an error will be reported when the second strong classifier is trained, indicating that the number of samples is insufficient. Just change the value to a smaller value.

The next thing to do is to wait for the training to finish:



Explanation of training process parameters:


N: Number of layers %
SMP: The usage rate of the sample 
F: + means to pass the flip, otherwise it is -
ST.THR: The threshold of 
the classifier HR: The current classifier recognizes the correct probability of positive samples
FA: The current classifier recognizes negative samples probability of error

EXP.ERR : the expected error rate of the classifier

After training, the xml.xml file will be generated in the root directory, and the final classifier will be generated in the xml folder:


4. Test

       Replace the classifier that comes with opencv with your own trained classifier for face recognition:


Because the training samples are too few and the number of layers of the classifier is not many, the classification effect is not good and needs to be improved.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325792440&siteId=291194637