My Blog List

Wednesday, April 18, 2012

最简单的方式使用Discriminatively Trained Deformable Part Models训练自己的模型 zz

最简单的方式使用Discriminatively Trained Deformable Part Models训练自己的模型


最简单的方式使用Discriminatively Trained Deformable Part Models训练自己的模型(原创,适合没有linux基础和matlab基础的人)

最近尝试使用Pedro FelzenszwalbDiscriminatively Trained Deformable Part Modelshttp://www.cs.brown.edu/~pff/)训练自己的模型,因为基本没用过matlablinux,所以开始比较糊涂,后来看了pozenhttp://blog.csdn.net/pozen/article/details/7023742,收获很大,所以自己尝试了一下,现在已经跑起来了。

Pozen的工作还是主要讲怎么在windows下训练,其中怎么准备数据说的不是很清楚,我这里稍微补充一下吧。(注意:我是在linux下跑的,windows下还要参考pozen的工作)


首先,当然是下载 voc-release4.01.tgz。因为voc-release4.01.tgz自带的训练是结合VOCdevkit,所以为了尽可能少的改动代码,我们还是把这个下载了吧,至少还可以用它的负样例不是。下载地址:http://pascallin.ecs.soton.ac.uk/challenges/VOC/voc2011/VOCdevkit_25-May-2011.tar,如果用VOC的数据,请自行下载。

OK,都下载完,解压缩完,就可以更改代码了。
首先,按照Felzenszwalb的说明,需要修改'globals.m'。这里,需要修改以下几个地方
14   VOCyear = '2011';%选择你想使用的VOC的数据的年份,或者你不需要使用,就默认2011吧。
17   % directory for caching models, intermediate data, and results
18   cachedir = ['. /TrainVal/VOCdevkit/result/2011/'];%这里是放训练好的models、中间数据和结果的地方,训练好的模型就在这里。
37   % directory with PASCAL VOC development kit and dataset
38   VOCdevkit = ['D:/data/TrainVal/VOCdevkit/'];%顾名思义


voc-release4.01.tgz加载数据都是在pascal_data.m中完成的,看一下代码,我们会发现
try
  load([cachedir cls '_train_' year]);%cls是种类名,例如人就是person
catch
  % positive examples from train+val
也就是说,如果你之前已经加载好了数据,并且命名格式是cls_train_year,则直接进行训练,不会再重新加载数据。因此,我们只需自己把需要训练的数据按照要求的格式准备好就行了。那么,cls_train_year是什么格式的呢,通过分析,发现包含两种类型,POSNEG
POS中存储了正样例的相关信息,主要包含图像地址,图像中正样例标注框的起始点和结束点,其他的我们训练自己的数据一般用不到。因此,我们只需要将我们训练的正样例数据保存在以下格式中:
x1 y1 x2 y2 path
说明:x1,y1为正样例的外接矩形的起始点,x2,y2为正样例的外接矩形的结束点,path为这个图片的路径。然后使用如下代码读入到POS中。
function [pos] = pascal_data2(clspos,numTrainPos,cls)
%clspos为正样例文件的地址,numTrainPos正样例数目,cls为正样例的名称
% Get training data from the PASCAL dataset.
 pos = [];
 numpos = 0;
 [a,b,c,d,e]=textread(clspos,'%d%d%d%d%s');
  for i = 1:numTrainPos;
      numpos = numpos+1;
      pos(numpos).im = [e(numpos)];
      pos(numpos).x1 = a(numpos);
      pos(numpos).y1 = b(numpos);
      pos(numpos).x2 = c(numpos);
      pos(numpos).y2 = d(numpos);
      pos(numpos).flip = false;
      pos(numpos).trunc = 0;
  end
  year = '2011';
 save([cls '_train_' year], 'pos');
end  
NEG中存放了负样例信息,其实只是负样例的图像地址。同样放到一个文本文件中,然后用以下代码读取
function [neg] = pascal_data2(clsneg,numTrainNeg,cls)
  % negative examples from train (this seems enough!)
   [e]=textread(clsneg,'%s');
 %fid2=fopen(clsneg,'r');%得到文件号
  neg = [];
  numneg = 0;
  for i = 1:numTrainNeg;
      numneg = numneg+1;
      neg(numneg).im = e(numneg);
      neg(numneg).flip = false;
  end
 save([cls '_train_20120212' ],  'neg');
end

最后保存成cls_train_year的数据文件就行了。
pos = pascal_data_pos('trainpos.txt',3776,'pos');
neg = pascal_data_neg('trainneg.txt',2315,'neg');
save('person_train_2011','pos','neg');
数据准备完成后,还需要把imreadx6行由ex.im改为ex.im{1},问题好像很简单,但是我是matlab白痴,所以就不解释了!

最后,按照作者的提示进行操作就行啦!
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


以上就是我作为一个对linuxmatlab都及其不熟悉的人使用Discriminatively Trained Deformable Part Models训练自己数据的方法,大家可以实践一下,有问题欢迎指出!

No comments:

Post a Comment