第二十五篇:traincascade训练报错:Train dataset for temp stage can not be filled.

Train dataset for temp stage can not be filled (solution)    


Yesterday, I tried to use opencv_traincascade to train a classifier, as shown on this website: 
http://docs.opencv.org/doc/user_guide/ug_traincascade.html
But, I always encountered the same error as:
Problem: 
l@DEL1207W118-Ubuntu $ opencv_traincascade -data data/ -vec signsPositive.vec -bg negSample.txt -numPos 10 -numNeg 200
PARAMETERS:
cascadeDirName: data/
vecFileName: signsPositive.vec
bgFileName: negSample.txt
numPos: 10
numNeg: 200
numStages: 20
precalcValBufSize[Mb] : 256
precalcIdxBufSize[Mb] : 256
stageType: BOOST
featureType: HAAR
sampleWidth: 24
sampleHeight: 24
boostType: GAB
minHitRate: 0.995
maxFalseAlarmRate: 0.5
weightTrimRate: 0.95
maxDepth: 1
maxWeakCount: 100
mode: BASIC

Stage 0 is loaded

===== TRAINING 1-stage =====
<BEGIN
POS count : consumed   10 : 10
Train dataset for temp stage can not be filled. Branch training terminated.
Cascade classifier can't be trained. Check the used training parameters.


I took me the whole afternoon to find the solution by google, some other guys have the same problem like me as discussed in this topic:  http://answers.opencv.org/question/16868/error-in-train-casacde/

Why?
The reason is very simple, in your opencv folder, find "opencv-2.4.6.1 -> apps -> traincascade ", open " imagestorage.cpp", in function "bool CvCascadeImageReader::NegReader::create( const String _filename, Size _winSize )", we can see that all the image file names in negInfo.txt are read into  imgFilenames by std:getline. It seems right! But only if you have used ubuntu to generate the negInfo.txt. If you have used windows ( what I did) to generate this text file, there would be two chars at the end of each line  \r\n; on linux there is just one,  \n.  So, with the original code in opencv, the  \r is kept at the end of each file name. Then, in Line 60, "src = imread( imgFilenames[last++], 0 )", the image file can't be loaded. 

So, there are two  solutions:
1) generate your bt.txt (negative sample file) on Ubuntu, make sure that there is a newline for each image file no empty line in the whole file;
2) or generate your bt.txt (negative sample file) on Windows, but add one line in the original opencv code " imagestorage.cpp":
before line 45, add "
if (str[str.size() - 1] == '\r')
       str.resize(str.size() - 1); // to erase \r if the file is generated by windows
"; then, recompile (cmake .. -> make -> make install) your opencv for a new opencv_traincascade application.

Now, it works:
@DEL1207W118-Ubuntu  $ opencv_traincascade -data data/ -vec signsPositive.vec -bg negSample.txt -numPos 10 -numNeg 200
PARAMETERS:
cascadeDirName: data/
vecFileName: signsPositive.vec
bgFileName: negSample.txt
numPos: 10
numNeg: 200
numStages: 20
precalcValBufSize[Mb] : 256
precalcIdxBufSize[Mb] : 256
stageType: BOOST
featureType: HAAR
sampleWidth: 24
sampleHeight: 24
boostType: GAB
minHitRate: 0.995
maxFalseAlarmRate: 0.5
weightTrimRate: 0.95
maxDepth: 1
maxWeakCount: 100
mode: BASIC

Stage 0 is loaded

===== TRAINING 1-stage =====
<BEGIN
POS count : consumed   10 : 10
NEG count : acceptanceRatio    200 : 0.0311284
Precalculation time: 2
+----+---------+---------+
|  N |    HR   |    FA   |
+----+---------+---------+
|   1|        1|        0|
+----+---------+---------+
END>

===== TRAINING 2-stage =====
<BEGIN
POS count : consumed   10 : 10
NEG count : acceptanceRatio    200 : 0.000914256
Precalculation time: 3
+----+---------+---------+
|  N |    HR   |    FA   |
+----+---------+---------+
|   1|        1|     0.01|
+----+---------+---------+
END>

===== TRAINING 3-stage =====
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值