Shishir Joshi
Shishir Joshi

Reputation: 347

Error "Train dataset for temp stage can not be filled." while using traincascade tool of OpenCV

I'm building the opencv_traincascade.exe tool from source code (OpenCV 2.4.0) using vs2010 on windows 7. I want to train a LBP classifier, so I'm using opencv_traincascade.exe with the following arguments

-data D:\OpenCV\Classifiers\lbpclassifier1\temp\data -vec D:\OpenCV\Classifiers\lbpclassifier1\temp\samples.vec -bg D:\OpenCV\Classifiers\lbpclassifier1\temp\negative\infofile.txt -numPos 40 -numNeg 40 -precalcIdxBufSize 500 -precalcValBufSize 500 -stageType BOOST -featureType LBP -w 24 -h 24

The error message that I get is:

PARAMETERS:
cascadeDirName: D:\OpenCV\Classifiers\lbpclassifier1\temp\data
vecFileName: D:\OpenCV\Classifiers\lbpclassifier1\temp\samples.vec
bgFileName: D:\OpenCV\Classifiers\lbpclassifier1\temp\negative\infofile.txt
numPos: 2
numNeg: 40
numStages: 20
precalcValBufSize[Mb] : 500
precalcIdxBufSize[Mb] : 500
stageType: BOOST
featureType: LBP
sampleWidth: 24
sampleHeight: 24
boostType: GAB
minHitRate: 0.995
maxFalseAlarmRate: 0.5
weightTrimRate: 0.95
maxDepth: 1
maxWeakCount: 100

===== TRAINING 0-stage =====
<BEGIN
POS count : consumed   2 : 2
Train dataset for temp stage can not be filled. Branch training terminated.

This question has been asked before : Error while using opencv_train cascade , and the solution suggested there viz. making the number of positive and negative samples equal doesn't help in this case.

I thought that the error might be related to this bug : http://code.opencv.org/issues/1834 So, I downloaded the latest release of imagestorage.cpp from that page and rebuilt the opencv_traincascades tool. Also, the total number of samples in the .vec file is 45. So as suggested in this comment I have changed the numPos argument to 2, so that I don't run out of samples in the .vec file. The number of samples in the .vec file is 45.

Any help is much appreciated. Thanks.

Upvotes: 12

Views: 17502

Answers (10)

Damon
Damon

Reputation: 1

i also have this problem before. but after i reduce the param [maxFalseAlarmRate] ,like set small than 0.1 , it works ok. hope this have some help.

Upvotes: 0

ConcernedHobbit
ConcernedHobbit

Reputation: 874

I’ve had this issue several times, and it ended up being caused by me generating my .vec file with the wrong dimensions specified for my positive images.

For example, I had 700 positive images that were 100×100 pixels, and I successfully generated my .vec with them. So my text file looked like this:

/filepath/directory/image1.jpg 1 0 0 100 100
/filepath/directory/image2.jpg 1 0 0 100 100
…

But then when I tried to train my cascade HAAR file, the images were too big. So I resized them to 50×50, and recreated my .vec (specifying in the command line the new size) with the downsized images.

The .vec supposedly successfully created, but when I then try to train, I get the error. Once I recreated my text file with the correct dimensions, recreated my .vec file, and tried to train again, the error went away. The other issue I've had is when the negatives file has errors in it, like a bad path or some other issue; all the other comments have good suggestions for figuring out what might be wrong with the -bg file too. Hope this helps.

Upvotes: 1

Kiran Kumar
Kiran Kumar

Reputation: 41

opencv_traincascade is trying to read the Negative folder images in the current folder. Hence you have to transfer the opencv_traincascade.exe into negative/ images folder.

For example:

- opencv_traincascade.exe -data C:\Users\home\Documents\Face_Detection\HaarTrain\cascades -vec C:\Users\home\Documents\Face_Detection\HaarTrain\vector\vector.vec -bg bg.txt -numPos 130 -numNeg 500 -numStages 10 -precalcValBufSize 1024 -precalcIdxBufSize 1024

Upvotes: 3

Jayhello
Jayhello

Reputation: 6602

As to me(ubuntu16) it is file path issue.Below is my file structure.

xy@xy:~/face_sample/face_csdn$ ll
total 7144
drwx------ 5 xy xy    4096 Jun  6 17:58 ./
drwxrwxr-x 3 xy xy    4096 Jun  5 19:03 ../
drwx------ 2 xy xy  376832 Jun  6 17:50 negdata/
drwx------ 2 xy xy  516096 Jun  6 17:11 posdata/
-rw-rw-r-- 1 xy xy 6408012 Jun  6 17:15 pos.vec
drwxrwxr-x 2 xy xy    4096 Jun  6 17:54 xml/

posdata is my positive image dir which contains pathpos.txt(image absolute path)

xy@xy:~/face_sample/face_csdn$ ll -th  posdata/ | head - 5
==> standard input <==
total 75M
drwx------ 5 xy xy 4.0K Jun  6 17:58 ../
drwx------ 2 xy xy 504K Jun  6 17:11 ./
-rw-rw-r-- 1 xy xy 1.1M Jun  6 17:11 pathpos.txt
-rw-rw-r-- 1 xy xy  938 Mar 24  2014 18540.jpg
-rw-rw-r-- 1 xy xy  919 Mar 24  2014 18541.jpg
-rw-rw-r-- 1 xy xy  922 Mar 24  2014 18542.jpg
-rw-rw-r-- 1 xy xy  931 Mar 24  2014 18543.jpg

Ao to pathpos.txt I use absolute path of the image

xy@xy:~/face_sample/face_csdn$ head -5 posdata/pathpos.txt 

/home/xy/face_sample/face_csdn/posdata/10000.jpg 1 0 0 20 20
/home/xy/face_sample/face_csdn/posdata/10001.jpg 1 0 0 20 20
/home/xy/face_sample/face_csdn/posdata/10002.jpg 1 0 0 20 20
/home/xy/face_sample/face_csdn/posdata/10003.jpg 1 0 0 20 20
/home/xy/face_sample/face_csdn/posdata/10004.jpg 1 0 0 20 20

enter image description here

Go back to parent dir, and have a look of negative txt file(negative image path)

enter image description here

Then have a train:

enter image description here

The above is just show how to use opencv_traincascade to train.

I also encounter the above problem mainly about image path.

Upvotes: 0

Mikro Koder
Mikro Koder

Reputation: 1096

CentOS Linux release 7.4.1708 (Core)

 opencv_traincascade -data haarcascadeHAAR 
-vec samples.vec -bg Bad.dat -numPos 1500 -numNeg 3000 
-numStages 35 -minHitRate 0.95 -maxFalseAlarmRate 0.5 
-mode ALL -w 20 -h 20 
-precalcValBufSize 2048 -precalcIdxBufSize 4096

System return responce: Train dataset for temp stage can not be filled. Branch training terminated. Cascade classifier can't be trained. Check the used training parameters.

This trouble was in OpenCV 2.4.6 and 3.1.0. Resolution next (for my case)- I create file Bad.dat on windows host, when I copy this file on unix I switch b-slash to slash (:%s/\// for Vi), but catch same result. When I change EOL from Win (CR LF) to Nix (LF) - traincascade launched successfuly.

 $ file Bad.dat
Bad.dat: ASCII text, with CRLF line terminators

$ dos2unix Bad.dat
dos2unix: converting file Bad.dat to Unix format ...

$ file Bad.dat
Bad.dat: ASCII text

The process works well reason - check you Bad.dat (file in -bg param)

Upvotes: 0

Jeff Camera
Jeff Camera

Reputation: 5544

I had the exact same problem. The issue for me was that I created my negatives.txt file using PowerShell which writes a BOM to the file. The BOM trips up the opencv_traincascade program when it tries to read the file and causes it to not find any of the images. I converted the file to UTF-8 using Notepad++ and that fixed the issue.

Upvotes: 1

etoropov
etoropov

Reputation: 1225

First, for me too, the error was with the bg file. I use Opencv 2.4.10.1.

I got the error when I specified the absolute path to the bg file in the command (like the author of the post). Looks like the authors did a really, really crappy job on figuring out the full negative image paths. One simple workaround is to simply CD to the dir of the .info file (note -bg infofile.txt)

cd D:\OpenCV\Classifiers\lbpclassifier1\temp\negative
train_cascade -data D:\OpenCV\Classifiers\lbpclassifier1\temp\data -vec D:\OpenCV\Classifiers\lbpclassifier1\temp\samples.vec -bg infofile.txt -numPos 40 -numNeg 40 -precalcIdxBufSize 500 -precalcValBufSize 500 -stageType BOOST -featureType LBP -w 24 -h 24

Upvotes: 15

ChiNhan
ChiNhan

Reputation: 109

I had this similar problem which gives error:

POS count : consumed   50 : 50
Train dataset for temp stage can not be filled. Branch training terminated.

The problem was that, my bg.txt was generated in a Windows system using \r for next line. When I tried to use opencv_traincascade in Ubuntu, it read '\r' into the string for filelist, thus in CvCascadeImageReader::NegReader::nextImg() , the line src = imread(imgFilenames[last++], 0); (as xeed mentioned) did not work.

My fix was to add str.erase(std::remove(str.begin(), str.end(), '\r'), str.end()); before imgFilenames.push_back(dirname + str); in imagestorage.cpp

I hope this helps, if anyone still struggles.

Upvotes: 3

xeed
xeed

Reputation: 947

I hope to close this question. For those who stumbled over this as well. I know its old. I debugged the traincascade and found, that at some point the following line:

src = imread( imgFilenames[last++], CV_LOAD_IMAGE_GRAYSCALE );

returns a null mat. This line should read the background images.

This bug occures, if you build traincascade.cpp as debug and are using the release libs and dlls of opencv. (The other way around shouldn't work as well.

Upvotes: 4

Hiro
Hiro

Reputation: 505

I am also learning how to use OpenCV object detection tools.

I noticed that if the bg file (in your case "D:\OpenCV\Classifiers\lbpclassifier1\temp\negative\infofile.txt") has a wrong format or contains a entry with non-existent file, opencv_traincascade produces error.

Hope it helps.

Upvotes: 8

Related Questions