# Error in parameter of traincascade?

Hi,

I'm trying to train new detectors, with previous OpenCV version 2.3.1, my parameters had worked nice. I have discarded the previous detectors, so I needed to train again in the new version OpenCV 2.4.2. But with the same parameters, it had the following error:

===== TRAINING 2-stage =====
<BEGIN
OpenCV Error: Bad argument (Can not get new positive sample. The most possible reason is
insufficient count of samples in given vec-file.
) in get, file /home/user/opencv/apps/traincascade/imagestorage.cpp, line 159
terminate called after throwing an instance of 'cv::Exception'
what():  /home/user/opencv/apps/traincascade/imagestorage.cpp:159: error: (-5) Can not
get new positive sample. The most possible reason is insufficient count of samples in given
vec-file.
in function get


The parameters that I used:

    ./opencv_traincascade -data mix25x15 -vec mix.vec -bg negatives.txt -numStages 15
-minHitRate 0.999 -maxFalseAlarmRate 0.5 -numPos 3600 -numNeg 3045 -w 25 -h 15
-precalcValBufSize 2048 -precalcIdxBufSize 2048 -mode ALL


But I have put the exact number of positive samples, I have looked on the vec file to be sure. I also put the correct number of negatives images. I've tried to put the same number of pos and neg, for example 1200 (of course I've created the corresponding vec file) for each, but it is not working. I have tried also with 1200 pos and 3045 neg.

I'm not sure if is something in the code or in my parameters. Any idea? Thanks!

edit retag close merge delete

Sort by » oldest newest most voted

Most likely that your problem is that you take too many numPos and too large minHitRate. How many unique positive samples do you have in your vec-file? Please note that you need unique samples, you can't take one object and then warp it in different ways to get many samples. So, if you have at least 5000 samples, your parameters are probably OK, but if less than 4000, then traincascade tool is right, it can't get enough samples to satisfy you minHitRate which is 99.9% in your case.

So, I would recommend you to decrease the minHitRate to something reasonable and check that your training set (vec-file) is large enough.

more

Thanks for the answer. The vec is from unique samples, from a image dir with info. I've tried to increase the number of pos samples, to 5100 and decreased the minHitRate to 0.99, but it not worked. I've found the following issue: http://code.opencv.org/issues/1834 -> so it mean that the previous version were training incorrectly? Anyway, I will try to tweak a bit more on the parameters.

( 2012-07-26 03:59:34 -0600 )edit

I've read carefully the issue, and found the following: "vec-file has to contain >= (numPose + (numStages-1) * (1 - minHitRate) * numPose) + S". I'm a bit confused here... so, instead of putting the total number of pos in the argument numPos, I should put a number that is not the total? Or decrease the minHitRate to achieve the limit?

( 2012-07-26 04:21:33 -0600 )edit

Yes. You should decrease numPos, not increase. Try to set numPos = 0.9 * number_of_positive_samples and 0.99 as a minHitRate. If the training will work, you can continue to tune your parameters.

( 2012-07-26 11:07:01 -0600 )edit
1

Now it worked, I have decreased the numPos argument. I prefer to use the formula mentioned above, but using 0.9*TotalPos also works. Thanks!

( 2012-07-26 12:13:31 -0600 )edit

You're right, formula above is the right method, 0.9 is a fast, but fragile hack :) BTW, it is a good practice to accept answers, if the issue is resolved. And could you please provide a more descriptive subject for your question?

( 2012-07-28 03:22:02 -0600 )edit

I have edited the subject but don't know if it is good enough. Anyway, I accept suggestions or you can edit :)

( 2012-07-30 10:59:56 -0600 )edit

@icedecker You wrote that you prefer to use the formula, but a bit confused with it. I tried to describe it in more details here (http://answers.opencv.org/question/4368/traincascade-error-bad-argument-can-not-get-new/). Please check this if you're still interested.

( 2012-11-23 02:52:58 -0600 )edit

hi you need to try to debug train-cascade solution to see the issue it seems to stack inside

int CvCascadeClassifier::fillPassedSamples( int first, int count, bool isPositive, int64& consumed )
{
int getcount = 0;
for( int i = first; i < first + count; i++ )
{
for( ; ; )
{
**bool isGetImg = isPositive ? imgReader.getPos( img ) :
if( !isGetImg )
return getcount;
consumed++;

featureEvaluator->setImage( img, isPositive ? 1 : 0, i );
if( predict( i ) == 1.0F )
{
getcount++;
printf("%s current samples: %d\r", isPositive ? "POS":"NEG", getcount);
break;
}
}
}
return getcount;
}

more

        if( predict( i ) == 1.0F )
{
getcount++;
printf("%s current samples: %d\r", isPositive ? "POS":"NEG", getcount);
break;
}
else if (isPositive)//mic
{
i++;
}

( 2015-01-28 12:34:13 -0600 )edit

Does anybody know how exactly "S" can be calculated? I'm using the good old 0.85 percent of all positives but still I'm wondering, how?!

( 2016-11-01 18:08:41 -0600 )edit

@amahta, please don't post answers, if you have a comment or question.

( 2016-11-02 01:07:41 -0600 )edit

Hi All: Here: http://code.opencv.org/issues/1834 Maria Dimashova She is giving the formula : vec-file has to contain >= (numPose + (numStages-1) * (1 - minHitRate) * numPose) + S, where S is a count of samples from vec-file . First I would like to know where to find this formula, in which document . Second She is writing :Bug "It was fixed in r8913", What daes it mean r8913? Is it Opencv actual version 2.4.3 ? Thank You.

more