Ask Your Question

Error in parameter of traincascade?

asked 2012-07-25 19:27:35 -0500

icedecker gravatar image

updated 2012-07-31 15:18:17 -0500

Kirill Kornyakov gravatar image


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 =====
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 
  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 flag offensive close merge delete

3 answers

Sort by ยป oldest newest most voted

answered 2012-07-26 02:34:20 -0500

Kirill Kornyakov gravatar image

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.

edit flag offensive delete link 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: -> so it mean that the previous version were training incorrectly? Anyway, I will try to tweak a bit more on the parameters.

icedecker gravatar imageicedecker ( 2012-07-26 03:59:34 -0500 )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?

icedecker gravatar imageicedecker ( 2012-07-26 04:21:33 -0500 )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.

Kirill Kornyakov gravatar imageKirill Kornyakov ( 2012-07-26 11:07:01 -0500 )edit

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!

icedecker gravatar imageicedecker ( 2012-07-26 12:13:31 -0500 )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?

Kirill Kornyakov gravatar imageKirill Kornyakov ( 2012-07-28 03:22:02 -0500 )edit

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

icedecker gravatar imageicedecker ( 2012-07-30 10:59:56 -0500 )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 ( Please check this if you're still interested.

Maria Dimashova gravatar imageMaria Dimashova ( 2012-11-23 02:52:58 -0500 )edit

answered 2015-01-28 12:24:06 -0500

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;
    Mat img(cascadeParams.winSize, CV_8UC1);
    for( int i = first; i < first + count; i++ )
        for( ; ; )
            **bool isGetImg = isPositive ? imgReader.getPos( img ) :
                                           imgReader.getNeg( img );**
            if( !isGetImg )
                return getcount;

            featureEvaluator->setImage( img, isPositive ? 1 : 0, i );
            if( predict( i ) == 1.0F )
                printf("%s current samples: %d\r", isPositive ? "POS":"NEG", getcount);
    return getcount;
edit flag offensive delete link more


i added printf after imgreader seems reading more than images in the folder ?

now i add protection

        if( predict( i ) == 1.0F )
            printf("%s current samples: %d\r", isPositive ? "POS":"NEG", getcount);
        else if (isPositive)//mic
michaels gravatar imagemichaels ( 2015-01-28 12:34:13 -0500 )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?!

amahta gravatar imageamahta ( 2016-11-01 18:08:41 -0500 )edit

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

berak gravatar imageberak ( 2016-11-02 01:07:41 -0500 )edit

answered 2012-11-20 08:26:32 -0500

marcoromagnoli gravatar image

Hi All: Here: 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.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2012-07-25 19:27:35 -0500

Seen: 12,316 times

Last updated: Nov 01 '16