6

I am trying to train a haar-like classifier for pedestrians in OpenCV using 3340 positive images and 1224 negative images. (in a .txt file I keep the negative image names i.e negatives(1).bmp, and in a txt file I keep the positives i.e. picture(1).bmp 1 0 0 64 128. Actually positive examples are already cropped images of pedestrians so I only need specify one positive sample per image).

At some point during the training process it stops and says :

"Opencv Error: Assertion failed (elements_read==1)in unknown function, file c:\path\cvhaartraining.cpp, line 1858"

Any ideas as to what is causing this ?

Sirko
  • 72,589
  • 19
  • 149
  • 183
valentin
  • 1,103
  • 2
  • 16
  • 36
  • Which version of OpenCV are you using? It might be that the following assertion fails: `assert( img->rows * img->cols == ((CvVecFile*) userdata)->vecsize );` This means that the rows and columns dont correspond with the size of the vector. But I don;t know what could cause this. – diip_thomas Jun 04 '12 at 15:21
  • Hi, I am using opencv 2.4 with pre-cropped images 64X128. thank you – valentin Jun 06 '12 at 09:11
  • Can you find out if it is only one specific picture that causes this or if none of them work? If it is one specific image you can see hwat is different in this image. If all of them don't work it we can take a look at the images themselves. – diip_thomas Jun 06 '12 at 13:45
  • the unusual thing so far is that I have some repeating images (not many just 6) in positive samples – valentin Jun 06 '12 at 14:29

1 Answers1

12

this issue was answered by creater of the utility on the OpenCV DevZone site in June 2012.

To quote Maria:

The problem is that your vec-file has exactly the same samples count that you passed in command line -numPos 979. Training application used all samples from the vec-file to train 0-stage and it can not get new positive samples for the next stage training because vec-file is over. The bug of traincascade is that it had assert() in such cases, but it has to throw an exception with error message for a user. It was fixed in r8913. -numPose is a samples count that is used to train each stage. Some already used samples can be filtered by each previous stage (ie recognized as background), but no more than (1 - minHitRate) * numPose on each stage. So vec-file has to contain >= (numPose + (numStages-1) * (1 - minHitRate) * numPose) + S, where S is a count of samples from vec-file that can be recognized as background right away. I hope it can help you to create vec-file of correct size and chose right numPos value.

It worked for me. I also had same problem, I was following the famous tutorial on HAAR training but wanted to try the newer training utility with -npos 7000 -nneg 2973

so i did following calcs:

vec-file has to contain >= (numPos + (numStages-1) * (1 - minHitRate) * numPos) + S

7000 >= (numPos + (20-1) * (1 - 0.999) * numPos) + 2973

(7000 - 2973)/(1 + 19*0.001) >= numPos

numPos <= 4027/1.019

numPos <= 3951 ~~ 3950

and used:

-npos 3950 -nneg 2973

It works. I also noticed that others have also had success with reducing numPos : here

Community
  • 1
  • 1
mayank
  • 186
  • 3
  • 12
  • 2
    does anyone else find -numPos and -numPose being two different things confusing? Or is just a typo? numPose does not exist in the documentation here: http://docs.opencv.org/doc/user_guide/ug_traincascade.html#cascade-training I understand it is a direct qoute, but for clarity it might be nice to edit it – EdgeCaseBerg Aug 06 '14 at 17:31
  • yes, the name of the variables are very wrong. besides that the total of negative images must be smaller than the positive ones (by the equation), but this information is nowhere. – elton fernando Oct 01 '21 at 12:41