现在的位置: 首页 > 综合 > 正文

How to train models of Object Detection with Discriminatively Trained Part Based Model

2012年11月20日 ⁄ 综合 ⁄ 共 4659字 ⁄ 字号 评论关闭

转至:http://blog.csdn.net/pozen/article/details/7103412

 

我把补充的内容放最前面,显眼!

补充:1 关于负样本。框架里面提取负样本的方式是选择样本库中不包含训练类型的图片为负样本。我直接用库里面的图片。我用1000张左右的负样本,自己准备的话也简单没太多好说的。提供图片路径就OK了,它自己会裁。

            2 关于自己准备正样本要注意的问题:

               (1)      pos(numpos).flip = false;
                               pos(numpos).trunc = 0; 

               (2)     最好不要自己手动裁正样本小图片当正样本图片,而应该将一张图片的中的正样本用坐标表示出来,做成类似原框架中的解析文件(用OPENCV之类的东西做 应该容易吧),我是写脚本解析的别人的样本库,我的解析文件格式是:x1 y1 x2 y2 filepath, 这样一行对应一个正样本,在MATLAB格式化读取也方便。注意!没有标LABEL,RESIZE之类的步骤!只需要指明正样本的框,和相应的图片路径就OK。

             3 关于pascal()第二个参数的问题:按作者的说法(组件的个数)不太好理解。我理解的意思是:Model中子模型的个数。这个值的设置比较重要,一般设置成1. 如果正样本比较多样化,比如行人样本中有两种情况:站立的,躺着的。这时应该设置成2。如果基本上只有一种情况,设成1就够了,因为设置成多个的话, 效果提 高不了多少,检测速度还慢了不少!看一下代码应该知道,组件分类的时候,只是简单的按SIZE的宽高比例分,并没有还可以分正面反面那么高级。

             4 part个数 part大小的设置:在pascal_train.m里面model_addparts函数的最后两个参数。目标不同,这两个参数也可能不一样,可以先目测一下,大约多少个part可以基本捕获目标的基本特征。可能需要实验多次。

             5 训练时间问题:刚开始调整训练参数的时候,只需要看大致的效果,所以只用很少的样本量就可以,还嫌慢的话,可以减少learn.cc里面的迭代次数(我就是这么干    的),还可以把pascal_train里面的maxneg的值改小,比如50。

             6 怎么生成PCA统计信息文件: 参见http://blog.csdn.net/pozen/article/details/7282124

 --------------------邪恶的补充完结分割线-----------------------------

我是在windows平台下训练模型的。因为Felzenszwalb的框架是不支持WINDOWS的,需要进行一些小的修改才可以,不过一些小错误好像是平台无关的,不清楚UNIX下是否同样存在。

首先要下载FelzenszwalbVOC-release4, 这里面包含有模型的训练脚本。再下载2011 PASCAL VOC devkit and dataset,这里面包含了样本图片和这些样本图片的解析信息,以及获取样本的相关脚本。重新指定pascal_init,global中的路径设置。

Felzenszwalb的代码中直接调用VOC devkit的脚本获取正负样本。如果你要训练的模型在样本库中不存在样本,需要自己准备OR需要使用其它样本库。那么你需要更改VOC-release4pascal_data.m脚本,它的功能是为训练准备正负样本,包含的信息比较简单主要就是样本相对于图片的框,以及你的本样图片的路径。

2 pascal_data.m中需要几行代码来防止因图片不存在而报错:

      if exist([VOCopts.datadir rec.imgname]) == 0

          continue;

      end

  VOCopts.datadir rec.imgname代表样本图片的绝对路径,创建正负样本集的时候  都应该进行判断,我下载的2011的库存在个别图片不存在的情况。

如果正样本个数太多的话(超过10000?)会报 out of memory错误。这个时候需要在pascal_data.m里面限制收集样本的数量。

4 compile learn.cc。如果是在UNIX/LINUX(OR 其模拟环境)下的话,可以直接MAKE生成可执行文件,windows下可以用nmake来生成。也可以用VS建一个工程来生成。当然会报一些错误,都可以通过GOOGLE解决。

5 compile c++ helper functions.参见:前一篇

6 WINDOWS下需要修正的错误(以pascal开头的几个脚本,还有rewritedat.m):

调用unix()的地方换成system() (还需要把mv 换成move, rm 换成delcp换成copy 等等)

调用./learn 的地方改成 learn

执行pascal('person', 3);   % train and evaluate a 6 component person model

 

8 我训练时出现过的错误和解决方法:

    把 unix 调用换成system调用 感觉运行不稳定,把rewritedat.m里面的 unix(['mv ' datfile ' ' oldfile]) 改成 movefile(datfile, oldfile)

    unix(['rm ' oldfile]) 改成delete(oldfile)。

    同样是rewritedat.m中还会出现下标越界的情况:

    把这段代码:

[plain] view
plain
copy

  1. I = sort(I);  
  2.   
  3. pos = 1;  
  4. for i = 1:length(I)  
  5.   cnt = I(i)-pos;  
  6.   while cnt > 0  
  7.     % + 2 to include the num non-zero blocks and example length  
  8.     info = fread(fin, labelsize+2, 'int32');  
  9.     dim = info(end);  
  10.     fseek(fin, dim*4, 0);  
  11.     cnt = cnt - 1;  
  12.   end  
  13.   y = fread(fin, labelsize+2, 'int32');  
  14.   dim = y(end);  
[plain] view
plain
copy

  1.   x = fread(fin, dim, 'single');  
  2.   fwrite(fout, y, 'int32');  
  3.   fwrite(fout, x, 'single');  
  4.   pos = I(i)+1;  
  5. end  

替换成:

[plain] view
plain
copy

  1. I = sort(I);  
  2.   
  3. pos = 1;  
  4. for i = 1:length(I)  
  5.   cnt = I(i)-pos;  
  6.   while cnt > 0  
  7.     % + 2 to include the num non-zero blocks and example length  
  8.     info = fread(fin, labelsize+2, 'int32');  
  9.     if length(info) == 0  
  10.         dim = 0;  
  11.     else  
  12.         dim = info(end);  
  13.     end  
  14.     %dim = info(end);  
  15.     fseek(fin, dim*4, 0);  
  16.     cnt = cnt - 1;  
  17.   end  
  18.   y = fread(fin, labelsize+2, 'int32');  
  19.   %//!  
  20.   if length(y) == 0  
  21.       dim = 0;  
  22.   else  
  23.       dim = y(end);  
  24.   end  
  25.   x = fread(fin, dim, 'single');  
  26.   fwrite(fout, y, 'int32');  
  27.   fwrite(fout, x, 'single');  
  28.   pos = I(i)+1;  
  29. end  

 

类似的越界错误在检测模块中,检测完成提取框时,如果检测个数为0的话也可能出现,不过影响不大。

 

Readme里面的原文:

Using the learning code

=======================

1. Download and install the 2006/2007/2008 PASCAL VOC devkit and dataset.

   (you should set VOCopts.testset='test' in VOCinit.m)

2. Modify 'globals.m' according to your configuration.

3. Run 'make' to compile learn.cc, the LSVM gradient descent code.

   (Run from a shell, not Matlab.)

4. Start matlab.

5. Run the 'compile' script to compile the helper functions.

   (you may need to edit compile.m to use a different convolution 

    routine depending on your system)

6. Use the 'pascal' script to train and evaluate a model. 

example:

> pascal('person', 3);   % train and evaluate a 6 component person model

The learning code saves a number of intermediate files in a cache

directory defined in 'globals.m'.  You should delete these files before

training models on different datasets, or when training new models after

modifing the code.

The code also generates some very large temporary files during training

(the default configuration produces files up to about 3GB).  They are

placed in a temporary directory defined in 'globals.m'.  This directory

should be in a local filesystem.

抱歉!评论已关闭.