Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

Error: Can not get new positive sample

📅 2014-Sep-01 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ cascade classifier, error, opencv ⬩ 📚 Archive

Problem

I was in the process of training a cascade classifier. I had exactly 354 positive samples and yet I got this error from opencv_traincascade when it entered stage 2:

$ opencv_traincascade -data car-classifier -vec pos-samples.vec -bg neg-image-filepaths.txt -precalcValBufSize 256 -precalcIdxBufSize 256 -numPos 354 -numNeg 4000 -nstages 20 -minhitrate 0.999 -maxfalsealarm 0.5 -w 50 -h 50 -nonsym -baseFormatSave 
PARAMETERS:
numPos: 354
numNeg: 4000
numStages: 20
stageType: BOOST
featureType: HAAR
sampleWidth: 50
sampleHeight: 50
boostType: GAB
minHitRate: 0.995
maxFalseAlarmRate: 0.5
weightTrimRate: 0.95
maxDepth: 1
maxWeakCount: 100
mode: BASIC

===== TRAINING 0-stage =====
<BEGIN
POS count : consumed   354 : 354
NEG count : acceptanceRatio    4000 : 1
Precalculation time: 100
+----+---------+---------+
|  N |    HR   |    FA   |
+----+---------+---------+
|   1|        1|        1|
+----+---------+---------+
|   2|        1|        1|
+----+---------+---------+
|   3|        1|        1|
+----+---------+---------+
|   4|        1|        1|
+----+---------+---------+
|   5| 0.997175|  0.40525|
+----+---------+---------+
END>
Training until now has taken 0 days 1 hours 39 minutes 31 seconds.

===== TRAINING 1-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 opencv-2.4.9/apps/traincascade/imagestorage.cpp, line 162
terminate called after throwing an instance of 'cv::Exception'
  what():  opencv-2.4.9/apps/traincascade/imagestorage.cpp:162: error: (-5) Can not get new positive sample. The most possible reason is insufficient count of samples in given vec-file.
 in function get

Solution

This problem seems to be very common. One possible solution is here. I took the hint from that and reduced the number of positive samples I provided as input to the program. Note that you need more positive samples to be present in the .vec file, just pass a smaller number to the program so it has some left over when it starts stage 1.

Here is the modified invocation I used:

$ opencv_traincascade -data car-classifier -vec pos-samples.vec -bg neg-image-filepaths.txt -precalcValBufSize 256 -precalcIdxBufSize 256 -numPos 200 -numNeg 2000 -nstages 20 -minhitrate 0.999 -maxfalsealarm 0.5 -w 50 -h 50 -nonsym -baseFormatSave

Tried with: OpenCV 2.4.9 and Ubuntu 14.04


© 2022 Ashwin Nanjappa • All writing under CC BY-SA license • 🐘 @codeyarns@hachyderm.io📧