Ask Your Question
0

C++ error in haartraining attempts

asked 2013-10-17 10:23:27 -0600

mswcpt gravatar image

updated 2020-10-23 07:10:36 -0600

Hi -

I'm receiving the following error from terminal during training phase. This is a line of code from within one of the OpenCV C++ includes it seems which is very odd since these lines are not edited in anyway during the process.

Error:

OpenCV Error: Assertion failed (elements_read == 1) in icvGetHaarTraininDataFromVecCallback, file /opt/local/var/macports/build/_opt_mports_dports_graphics_opencv/opencv/work/opencv-2.4.6.1/apps/haartraining/cvhaartraining.cpp, line 1859 libc++abi.dylib: terminate called throwing an exception Abort trap: 6 -MacBook-Pro:newpos …$

Code:

Perhaps you might know what is wrong in the code? I have no idea…

int icvGetHaarTraininDataFromVecCallback( CvMat* img, void* userdata ) { uchar tmp = 0; int r = 0; int c = 0;

assert( img->rows * img->cols == ((CvVecFile*) userdata)->vecsize );

size_t elements_read = fread( &tmp, sizeof( tmp ), 1, ((CvVecFile*) userdata)->input );
CV_Assert(elements_read == 1);
elements_read = fread( ((CvVecFile*) userdata)->vector, sizeof( short ),
       ((CvVecFile*) userdata)->vecsize, ((CvVecFile*) userdata)->input );
CV_Assert(elements_read == (size_t)((CvVecFile*) userdata)->vecsize);

if( feof( ((CvVecFile*) userdata)->input ) ||
    (((CvVecFile*) userdata)->last)++ >= ((CvVecFile*) userdata)->count )
{
    return 0;
}

for( r = 0; r < img->rows; r++ )
{
    for( c = 0; c < img->cols; c++ )
    {
        CV_MAT_ELEM( *img, uchar, r, c ) =
            (uchar) ( ((CvVecFile*) userdata)->vector[r * img->cols + c] );
    }
}

return 1;

}

Regards,

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
1

answered 2013-10-17 10:42:11 -0600

updated 2013-10-17 10:43:00 -0600

To solve that problem you have to train your classifier with slightly less positive samples than you actually have.

So, if you generated a .vec file (with the createsamples.exe utility) with, lets say, 500 positive samples, you must call haartraining.exe with input parameter -npos 450 (for example).

edit flag offensive delete link more

Comments

Thanks - I will give this a try and let you know if it solves my problem tomorrow.

mswcpt gravatar imagemswcpt ( 2013-10-17 10:58:34 -0600 )edit

This seemed to work (but only trained for 2 stages for 450 pos images), then it completed. I will test the xml soon, and I will post again if that work. In meantime - thank you!

mswcpt gravatar imagemswcpt ( 2013-10-18 10:09:28 -0600 )edit

That has to do with the rest of the parameters you chose for the cascade.

If the answer solved your problem, please set the question as solved.

Good luck

Pedro Batista gravatar imagePedro Batista ( 2013-10-18 10:16:56 -0600 )edit

I specified 10 stages but it stopped after two with:

Tree Classifier Stage +---+---+---+ | 0| 1| 2| +---+---+---+

0---1---2

Cascade performance POS: 450 452 0.995575 NEG: 400 6.00415e-06 BACKGROUND PROCESSING TIME: 1435.00

mswcpt gravatar imagemswcpt ( 2013-10-18 10:41:27 -0600 )edit

It is likely that the classifier didn't need the 10 stages to classify the data you used as input. You used 450 positives and 400 negatives, and it got a hit rate of 0.995 with 2 stages. That is why it stopped.

To train a good classifier, you should provide the training tool with more challenging data.

Pedro Batista gravatar imagePedro Batista ( 2013-10-18 10:49:03 -0600 )edit

Thanks - closed/solved the thread

mswcpt gravatar imagemswcpt ( 2013-10-18 10:54:32 -0600 )edit

Question Tools

Stats

Asked: 2013-10-17 10:23:27 -0600

Seen: 1,325 times

Last updated: Oct 17 '13