Ask Your Question
3

trouble when use opencv_traincascade.exe

asked 2013-03-11 02:11:49 -0600

JoeyChu gravatar image

hi all

I am tring to train my owen XM with following key steps

1, create vec data with createsamples.exe with some main para posNum 5709, negNum 4381, -w 20, -h 20

this runs OK, then the vec data be created, which size is about 4M

2, when i use below cmd firstly, opencv_traincascade.exe -data D:\traincascade -vec D:\data\pos.vec -bg D:\bg.txt -numPos 5709-numNeg 4381 -numStages 22 -precalcValBufSize 200 -precalcIdxBufSize 1000 -featureType LBP -w 20 -h 20, this runs NOK, with assert pops out, opencv_traincascade.exe OpenCV Error: Assertion failed (elements_read == 1)

so then i changed para numPos to 5000, numNeg to 4350 then redo this, all works ok, but seems stuck all the time at Stage 8, this keeps 2 days so far

so, my question is what the relationship with numPos, numNeg and numStages ?

how can i train this XML with above para?

edit retag flag offensive close merge delete

Comments

btw, is there a blank missing between 5709 and -numNeg ?

berak gravatar imageberak ( 2013-03-11 04:30:15 -0600 )edit

@ berak

nope, no blank missing actually in the cmd that I run if it is, i think errors will occur

JoeyChu gravatar imageJoeyChu ( 2013-03-11 08:15:58 -0600 )edit

just note, that those tools are very bad at reporting errors with any of those 10000 params.

and another thing, if you get stuck, and kill it, it will happily reuse all existing stages. so you can restart it with -numStages 8, and generate a cascade.xml from that ( to see the current state of affairs ) and then restart with -numStages 22 to proceed calculating the others

berak gravatar imageberak ( 2013-03-11 08:28:22 -0600 )edit

berak,

I cannot understand u absolutely, u mean when i want to train 22 stages, if the cmd stuck at stage 8, then i need to stop the train procedure, and restart with what ? just stage 8?

i have no idea about that, with that, i can get the XML that i really want?

JoeyChu gravatar imageJoeyChu ( 2013-03-11 08:53:37 -0600 )edit

sorry for the cunfusion. all i wanted to say is that you can restart it without having to do it ALL over again

berak gravatar imageberak ( 2013-03-11 10:31:36 -0600 )edit

@ berak,

acturally, that i restart it for many times, still not work out yet, so i post this question here, thank u

JoeyChu gravatar imageJoeyChu ( 2013-03-11 21:08:43 -0600 )edit

4 answers

Sort by » oldest newest most voted
6

answered 2013-03-12 03:39:33 -0600

So to answer your questions as posted in my comment

FIRST COMMENT POST

  1. With vec-file I mean the actual amount of elements in the vector file. This vector file is created by the create_samples.exe application and contains all positive detections. Do not misunderstand this with the amount of images used for positive samples, it is actually all single detections. So if you have multiple detections in each image, this will result in a single element for the vector!
  2. What confuses me is the S factor, I guess that is the actual number of negative elements you assign to the process, but I am not certain there.
  3. Number of negatives defines how specific you train your classifier for background noise. You should see it as follows. Negative images can have any size you prefer, however by selecting a width and a height factor, you assign the sample size that is randomly picked from these images. For example, having 200 negatives of 500500 pixels and a window size of 4545, will not get you a numNeg = 200 but could give you easily 5000 samples by randomly picking negatives out of the images.
  4. Take into account that the more negatives and positives you feed to the trainer, the longer it will actually take to reach a result. Each sample needs to be evaluated over and over again using the haar-wavelike feature set, so it is actually a computational expensive thing.

SECOND COMMENT POST

  1. Looking at your second comment, I am wondering if the vector file is actually filled with the correct information. Can you post the command you feed to the create_samples utility? Try to add the -show command and look if the samples actually make sense.

  2. Looking at the error form, it seems you are doing something wrong when storing the actual image information, posting your code could help us to find the problem.

THIRD COMMENT POST

  1. You are actually doing the correct thing
  2. It is not stuck, you should take into account the amount of time it takes to train HAAR like wavelet feature cascades. With those number of samples, it wouldn't surprise me if the time to compute it actually was a week or two. It is a slow process of calculating all features each time and evaluating them correctly for each sample.
  3. If you want to see if your data works out, use the traincascade algorithm with LBP functionality. It actually runs way faster, same training in about a day, and gives you idea about the possibilities.
  4. Once finished, and good data set is created, you could set a desktop running on the actual HAAR classifier and let it run for multiple days, maybe even weeks.

FOURTH COMMENT POST

  1. As you see there, it is still processing, please be patient enough and read my comments.

FINAL CONCLUSION

I see to many people sticking to the old haartraining algorithm. Go with the newer traincascade and see if LBP training actually works for your problem. It is ... (more)

edit flag offensive delete link more

Comments

And actually, i feel appreciated if people accept the answers I have been posting, if it actually turns out to help you in some kind. Feel free to ask for more input :)

StevenPuttemans gravatar imageStevenPuttemans ( 2013-03-12 03:41:52 -0600 )edit

@ Steven

Really appreciate for your answer,

1, Sorry, seems that I cannot re-organized the comments structure right now, "Interanl server Error" occurs, Later, i will try it again

2, Here still sth confused about the vec-file that created by below create sampels application that i used actually, the Number of all the positive images is 5706, the positive detections are face and eyes, all the positive images contain face and eyes of one person, If I want to detect face and eyes both, how can I use the application to create samples? Is it correct that I only want to detect face?

opencv_createsamples.exe -vec E:\data\pos2.vec -info E:\faces\info.dat -num 5706 -w 20 -h 20

JoeyChu gravatar imageJoeyChu ( 2013-03-12 09:19:23 -0600 )edit

Yes basically, you make a model for each element you want to detect. First create manual annotations (bounding box coordinates in a textfile) of face and eye regions in your positive images. Then feed the textfile/annotations for faces as the -info parameter for the createsamples utility, with numPos the amount of actual faces you have annotated. -w and -h define the resizing of the original images to a standard size. If you want an eye detector, you have to repeat the same progress again. Go to this link, it shows you how the txt files should look like: http://note.sonots.com/SciSoftware/haartraining.html

StevenPuttemans gravatar imageStevenPuttemans ( 2013-03-12 09:27:49 -0600 )edit

3, I cannot get you what Negative images that you define above, I am not sure that I get it right, please figure me out, If I collect 4381 negative images which all are 20 pixel height and 20 pixel width, What it the negateive image parameter should I use? 4381 or any larger value than 4381?

Still, when I collect the negative images, not focus on the size, right? no need to re-size them all as 20*20 pixel which In order to reduce the calculation?

If, as you metioned, when I collect 200 negatives of 500*500 pixels, then how, what numNeg value should I use ? any randomly value that larger than 200 is OK?

JoeyChu gravatar imageJoeyChu ( 2013-03-12 09:28:14 -0600 )edit
1

if your negative images are the exact window size (20x20 pixels) than you do not have to give up a larger number. However, mostly people just use random images, with random size. By sampling randomly i mean that at random positions 20x20 pixel windows are grabbed and used as negative images. A set of 100 large images can have more than 100 of those samples. And yes, any number is good. Knowing you have 500*500 pixels gives you more than 2500 possible windows. Multiplying that with 250 would get you high numbers. So even picking 10.000 samples as numNeg would actually work in this case. However it will take longer for training but will result in better detector.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-03-12 09:31:59 -0600 )edit

OK,

For me, now the current status is that I collected 5706 positive images and 4381 Negative images that both size are 20* 20 pixels and all are normalization

Q1: Is the vec-file is correct when I use below cmd? opencv_createsamples.exe -vec E:\data\pos2.vec -info E:\faces\info.dat -num 5706 -w 20 -h 20

Q2: If the vec-file is correct, In order to avoid the assert failure about "OpenCV Error: Assertion failed (elements_read == 1) in unknown function" I follow the formula to calculate the numPos as 1188 to train, is this correct? 5706 >= x + (24-1)(1-0.995)x + 4381

x <=1188

If all above operations are correct, now that I only have to do is to be patient about the training process, right?

JoeyChu gravatar imageJoeyChu ( 2013-03-12 09:42:30 -0600 )edit

Q1: Correct IF info.dat contains the correct information as in my link provided. Q2: Yes this is correct :) Just be patient and accept that haartraining can take weeks. Overall: Consider to take a look at cascade training by LBP, it will be more beneficial to you. And: Could you accept some answers? I have been helping out, so some acknowledgement would be nice :)

StevenPuttemans gravatar imageStevenPuttemans ( 2013-03-12 09:46:16 -0600 )edit

Aha, OK very helpful and nice answers, I will mark them as correct answers,

BTW, If later I still need sth to ask you, How can I contact you? an MSN or Skype account will be very nice

JoeyChu gravatar imageJoeyChu ( 2013-03-12 09:49:48 -0600 )edit

Just put new questions on the forum. Using the @ symbol you can always add a current members name to your post, informing them that they are actually wanted for their answer.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-03-12 09:53:33 -0600 )edit

@steven Puttemans as discussed last week, at cascade training stage 6, this process lasts almost 6 days, and still keep that way, seems stuck Then unfortunetly, my PC system Win 7 with many applications running stuck really at that way, so, I have to reboot it and re-run cascade app, 5 stages fast be loaded, then still at stage 6

I have no idear about that any results I can get with this way

JoeyChu gravatar imageJoeyChu ( 2013-03-19 04:21:17 -0600 )edit
0

answered 2013-03-24 09:46:51 -0600

JoeyChu gravatar image

@ Steven,

any update about my trouble? I am still waiting here, aha...:)

edit flag offensive delete link more

Comments

I have set up some trainers myself, taking lots of time. Will report back once they finish. Hopefully when I arrive at work tomorrow, they will have finished!

StevenPuttemans gravatar imageStevenPuttemans ( 2013-03-24 16:04:20 -0600 )edit

@ steven

@herezt

the 2 PCs that I took in parallel both stuck at stage 6, the process both take almost one week now

what is the next action?

JoeyChu gravatar imageJoeyChu ( 2013-03-26 03:54:05 -0600 )edit

Like I said, be patient, I have similar problems, not finding any solution for the moment ...

StevenPuttemans gravatar imageStevenPuttemans ( 2013-03-26 09:28:05 -0600 )edit

@StevenPuttemans

It is 2 weeks now, still at stage 6, which the whole stage is 24 :(

OK, I will still keep this running and waiting for your solution :)

JoeyChu gravatar imageJoeyChu ( 2013-04-01 21:51:22 -0600 )edit

I have skipped looking into this problem for some time. Have spent more than 2 weeks of work on it, boss not exciting to get stuck at this, so moved on to some more urgent matters now. However, will get back to this in some weeks :)

StevenPuttemans gravatar imageStevenPuttemans ( 2013-04-02 02:41:22 -0600 )edit

@StevenPuttemans

some weeks :(

here is an question about eye detection, could you give me some advice?

when I use the original eye detect cascade xml to detect eye area, when I get these, How can I judge the eyes state, open or close?

here is an idear but I think it is not good enough, binaryzation the eyes, then calculate the the toppest and lowest black dots? then compare with the one default vlaue? sound Ok, but these are more limitations about this, like glasses, the eye original status..etc..

JoeyChu gravatar imageJoeyChu ( 2013-04-02 04:17:08 -0600 )edit

No experience in that region, but I think that modelling closed versus open eyes with a simple naive bayes classifier could solve the problem.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-04-02 04:27:04 -0600 )edit

@StevenPuttemans

sigh...

ONE month, still stucking at stage 6, My boss told me yesterday, If the demo workaround still NOK, he maybe cancel the project...sigh..sigh..

JoeyChu gravatar imageJoeyChu ( 2013-04-16 21:22:53 -0600 )edit

If it has been stuck for one month, then kill it. Not even a HAAR classifier is doing so long. After tons of other topics about this training, it seems that success depends on all parameters combinations and the data you feed to it. So basically start with 5 stages, try different parameter settings like minhitrate and such, then look for best result.

Also if your project depends on this, look into other classifiers like SVM, naive bayes, kNN, random forests, ...

This project has smiling detection, look at the code which is in comment, it might get you to a solution for the eyes open and eyes closed part. http://www.youtube.com/watch?v=AA78saEk29E

StevenPuttemans gravatar imageStevenPuttemans ( 2013-04-17 00:52:06 -0600 )edit

@StevenPuttemans

because the training process is so long, so If just use different parameters to try again, I do not think it is ok for me, what is the root cause?

right now, just familiar with HAAR classfier, not involve the other classifiers,

JoeyChu gravatar imageJoeyChu ( 2013-04-18 03:29:48 -0600 )edit
0

answered 2013-03-19 03:58:17 -0600

herezt gravatar image

updated 2013-03-19 04:02:35 -0600

Hi, joey

For training stuck, you may meet the same problem like mine.

The problem is caused because opencv_traincascade.exe doesn't get the image width and height correctly or the original image width and height are smaller than training window size.

You can add two lines pointed by arrow in the follow code to opencv/appa/traincascade/imagestorage.cpp to solve the problem.

bool CvCascadeImageReader::NegReader::nextImg()
{
    Point _offset = Point(0,0);
    size_t count = imgFilenames.size();
    for( size_t i = 0; i < count; i++ )
    {
        src = imread( imgFilenames[last++], 0 );
        if(src.rows<winSize.height || src.cols < winSize.width)   <-----------
            continue;                                             <-----------
        if( src.empty() )
            continue;
....

Hope this solution will help you.

edit flag offensive delete link more

Comments

maybe it should even bark, if the image was empty, like the posreader does. (had a case, where it did not find the negatives, but never said so .. )

berak gravatar imageberak ( 2013-03-19 04:02:54 -0600 )edit

@ herezt

thanks for your advice, I will add this code and re-compile it, then redo this with all parameter not changed, BTW, my opencv version is 2.4.0

keep u informed no matter any results

JoeyChu gravatar imageJoeyChu ( 2013-03-19 04:39:12 -0600 )edit

@ herezt

All above is ready, now the training process still came to stage 6, I will keep it at least 3 days then,

JoeyChu gravatar imageJoeyChu ( 2013-03-19 05:17:01 -0600 )edit

Not to ruin the possible solution, but if you did the create_samples vector step correctly AND if you gave the correct -w and -h parameters to the training algorithm, then this should not be a problem. Every time negatives or positives gave me a problem, it was because I manually made mistakes in the data creation. The haartraining algorithm has a lot of problems with false formatting of models, data, files, ... It is the main problem with many errors. As a reference, I have a LBP trainer running now, which should be faster, stage 20 took more than a day but it is still progressing, so be patient, thats the biggest step with model training!

StevenPuttemans gravatar imageStevenPuttemans ( 2013-03-19 08:20:59 -0600 )edit

@ Steven

I have 2 PCs, In order to compare, I intend to work on cascade training application in parallel, one is no code changed, the other is from herezt,

So, again, I output the all the cmd and parameters here, Please piont it out when any find any errors,

1, create_samples

all 5706 positive images with size 20* 20 pixels were collected, so

opencv_createsamples.exe -vec E:data\pos.vec -info E:\info.dat -num 5706 -w 20 -h 20

the pos.vec was created by manual, the info.dat file is fill with all 5706 file name and parameter like below

10010.bmp 1 0 0 20 20

10011.bmp 1 0 0 20 20

10012.bmp 1 0 0 20 20

10013.bmp 1 0 0 20 20

10014.bmp 1 0 0 20 20

all above is correct?

JoeyChu gravatar imageJoeyChu ( 2013-03-19 09:39:30 -0600 )edit

2, cascade train with opencv_traincascade.exe,

"traincascade" direction was created by manual,

pos.vec is the file that created by create_samples application above,

the "bg.txt" file is about negative images that account is 4381, and still, all the size is 20*20

-numPos value was calculted as 1188 by the "famous" formula which discussed before,

training featrue type is LBP.

opencv_traincascade.exe -data D:\traincascade -vec D:\data\pos.vec -bg D:\bg.txt -numPos 1188 numNeg 4381 -numStages 22 -precalcValBufSize 200 -precalcIdxBufSize 1000 -featureType LBP -w 20 -h 20

is this correct?

JoeyChu gravatar imageJoeyChu ( 2013-03-19 09:46:29 -0600 )edit

Yes, the training of the vector file is correct. Negatives (bg.txt) do not need an explicit mentioning of sizes, so basically a list of filenames. I already see a first thing that bothers me, only 200 mb assigned for precalcValBufSize and cldxBufSize. To speed up that increase both to 1024 MB which is a 2 GB of memory assigned to the process, 1GB each step. And for the rest your commands seem correct!

StevenPuttemans gravatar imageStevenPuttemans ( 2013-03-19 11:28:15 -0600 )edit

@ Steven

1, yes, just image names in the bg.txt, without size, like

f002549.bmp

f002831.bmp

f003621.bmp

f004901.bmp

f006151.bmp

f006201.bmp

....

2, Then increace the precalcValBufSize value and cldxBufSize value both to 1024, Still, I add 2 printf funtion to print "src.rows, src.cols winSize.height and winSize.width" in NegReader::nextImg() to check the training process whether ongoing at stage 6,

then re-compile, now at stage 6, all the printf information output here, it does continue keep calculating.

JoeyChu gravatar imageJoeyChu ( 2013-03-20 00:35:27 -0600 )edit

I just ran into a similar problem, however, getting stuck for no single reason at level 21. It did jump from level 20 to level 21 and it is still calculating, but this is strange for LBP. I am going to check what could be wrong, maybe it adds up to solve your problem also.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-03-20 02:32:57 -0600 )edit

@joerChu, You can add the following code to C:\opencv\apps\traincascade\cascadeclassifier.cpp to monitor the training image collocting progress. And can you attach your training log?

int CvCascadeClassifier::fillPassedSamples( int first, int count, bool isPositive, int64& consumed ) { … featureEvaluator->setImage( img, isPositive ? 1 : 0, i ); if( predict( i ) == 1.0F ) { getcount++; printf("%s getcount: %d\r", isPositive ? "pos":"neg", getcount); break; } … }

herezt gravatar imageherezt ( 2013-03-20 04:10:52 -0600 )edit
0

answered 2013-03-11 10:59:56 -0600

These problems are generated by using the old haartraining. The newer cascade training doesn't have this problematic need of a lower positive number than actually given to the algorithm in your input vector. http://docs.opencv.org/modules/objdetect/doc/cascade_classification.html?highlight=train%20cascade

Basicly the creator of the haartrainingalgorithm posted this function for calculating the correct amount of numPos.

vec-file has to contain >= (numPos) + (numStages-1)(1-minHitRate)numPos- + S where S is the number of samples that can be directly as background :)

However, I find it confusing to use this formula.

Going to the traincascade algorithm, just use the amount of images in your vec file as numPos and define numNeg as large as possible. This just takes randomn samples of your negative image set. The better you model the background, the better your detection results will be.

edit flag offensive delete link more

Comments

Hi Steven,

thanks a lot about your answer above, but still sth confused here 1, about this formula vec-file has to contain >= (numPos) + (numStages-1)(1-minHitRate)numPos- + S what is the vec-file? the size that vec-file contains? here, 4MB? or what? numPos amount? 5706? btw, you said you are confused about this too. aha, if you know it, please tell me

2, about the solution, here, i still use the the numPos as 5706 that in my vec file, right? no chaned, but, what about negative image amout, here, the negetive image samples that I have is only 4831, u mean although that, i can still change the amount as large, right? no need to get more negative images, just change the numNeg as large when I run the cascade train cmd?

JoeyChu gravatar imageJoeyChu ( 2013-03-11 21:05:09 -0600 )edit

Hi Steven,

oops, what I not change the numPos param which is 5706, just enlarge the numNeg param as 8000, 10000, or 20000, without really get that number Negative image samples, then redo all the above

still below assert failure came out

OpenCV Error: Assertion failed (elements_read == 1) in unknown function, file .. ....\apps\traincascade\imagestorage.cpp, line 154

before, when I get the error, i only changed the numPos param, what about this now?

JoeyChu gravatar imageJoeyChu ( 2013-03-11 21:15:18 -0600 )edit

Hi Steven according this link http://code.opencv.org/issues/1834 this bug seems have the same issue with me so, I took the advice from Maria Dimashova mentioned which is still about the formula,

now, i do below steps 1, i use numPos 5706 to create samples, 2, In order to cal the numPos value, then i took it as unknown vec-file has to contain >= (numPos) + (numStages-1)(1-minHitRate)numPos- + S here, S is bg size, 4381

5706 >= x + (24-1)(1-0.995)x + 4381

x <=1188

then, re-train above opencv_traincascade.exe -data D:\traincascade -vec D:\data\pos.vec -bg D:\bg.txt -numPos 1188 numNeg 4381 -numStages 22 -precalcValBufSize 200 -precalcIdxBufSize 1000 -featureType LBP -w 20 -h 20

still, seems stuck at stage 6 here

===== TRAINING 6-stage ===== <BEGIN POS count : consumed 1188 : 119

JoeyChu gravatar imageJoeyChu ( 2013-03-12 02:46:30 -0600 )edit

BTW, my opencv version is 2.4.0

at Stage 5, the cal result is

===== TRAINING 5-stage ===== <BEGIN POS count : consumed 1188 : 1193 NEG count : acceptanceRatio 4381 : 0.00230658 Precalculation time: 2.158 +----+---------+---------+ | N | HR | FA | +----+---------+---------+ | 1| 1| 1| +----+---------+---------+ | 2| 1| 0| +----+---------+---------+ END>

JoeyChu gravatar imageJoeyChu ( 2013-03-12 02:52:32 -0600 )edit

Ok JoeyChu, first off all, in the feature try to keep your comments organized, I will reply to them in a new answer since this comment lines are hard to get something structured. Please feel free to accept input if it helped you :)

StevenPuttemans gravatar imageStevenPuttemans ( 2013-03-12 03:24:11 -0600 )edit

Question Tools

Stats

Asked: 2013-03-11 02:11:49 -0600

Seen: 6,238 times

Last updated: Mar 24 '13