首先调用格式:
example:
pascal('person', 2); % train and evaluate a 2 component person model
pascal_train.m
function model = pascal_train(cls, n) % n=2
% model = pascal_train(cls)
% Train a model using the PASCAL dataset.
globals;
%----------读取正负样本-----------------------
% pos.im,neg.im存储了图像路径,pos.x1..pos.y2为box,负样本无box
[pos, neg] = pascal_data(cls);
% 按照长宽比,分成等量的两部分? 即将 component label 固定,phase2时,该值为latent variable。 spos为索引
spos = split(pos, n);
% -----------phase 1 : train root filters using warped positives & random negatives-----------
try
load([cachedir cls '_random']);
catch
% -----------------------------phas 1--------------------------------
% 初始化 rootfilters
for i=1:n
models{i} = initmodel(spos{i});
%---------train-------------
% model.rootfilters{i}.w
% model.offsets{i}.w
models{i} = train(cls, models{i}, spos{i}, neg, 1, 1, 1, 1, 2^28);
end
save([cachedir cls '_random'], 'models');
end
% -----------------phase2-------------------------------------------
% :merge models and train using latent detections & hard negatives
try
load([cachedir cls '_hard']);
catch
model = mergemodels(models);
model = train(cls, model, pos, neg(1:200), 0, 0, 2, 2, 2^28, true, 0.7);
save([cachedir cls '_hard'], 'model');
end
%----------------phase 3----------------------------------------------
% add parts and update models using latent detections & hard negatives.
try
load([cachedir cls '_parts']);
catch
for i=1:n
model = addparts(model, i, 6);
end
% use more data mining iterations in the beginning
model = train(cls, model, pos, neg(1:200), 0, 0, 1, 4, 2^30, true, 0.7);
model = train(cls, model, pos, neg(1:200), 0, 0, 6, 2, 2^30, true, 0.7, true);
save([cachedir cls '_parts'], 'model');
end
% update models using full set of negatives.
try
load([cachedir cls '_mine']);
catch
model = train(cls, model, pos, neg, 0, 0, 1, 3, 2^30, true, 0.7, true, ...
0.003*model.numcomponents, 2);
save([cachedir cls '_mine'], 'model');
end
% train bounding box prediction
try
load([cachedir cls '_final']);
catch
% 论文中说用最小二乘,怎么直接相除了,都不考虑矩阵的奇异性
model = trainbox(cls, model, pos, 0.7);
save([cachedir cls '_final'], 'model');
end
initmodel.m
function model = initmodel(pos, sbin, size)
% model = initmodel(pos, sbin, size)
% Initialize model structure.
%
% If not supplied the dimensions of the model template are computed
% from statistics in the postive examples.
%
% This should be documented! :-)
% model.sbin 8
% model.interval 10
% model.numblocks phase 1 :单独训练rootfilter时为2,offset,rootfilter;phase 2,为 4
% model.numcomponents 1
% model.blocksizes (1)=1,(2)= root.h*root.w/2*31
% model.regmult 0,1
% model.learnmult 20,1
% model.maxsize root 的size
% model.minsize
% model.rootfilters{i}
% .size 以sbin为单位,尺寸为综合各样本的h/w,area计算出来的
% .w
% .blocklabel blocklabel是为编号,offset(2),rootfilter(2),partfilter(12 or less),def (12 same as part)虽然意义不同但是放在一起统一编号
% model.partfilters{i}
% .w
% .blocklabel
% model.defs{i}
% .anchor
% .w
% .blocklabel
% model.offsets{i}
% .w 0
% .blocklabel 1
% model.components{i}
% .rootindex 1
% .parts{j}
% .partindex
% .defindex
% .offsetindex 1
% .dim 2 + model.blocksizes(1) + model.blocksizes(2)
% .numblocks 2
% pick mode of aspect ratios
h = [pos(:).y2]' - [pos(:).y1]' + 1;
w = [pos(:).x2]' - [pos(:).x1]' + 1;
xx = -2:.02:2;
filter = exp(-[-100:100].^2/400); % e^-25,e^25
aspects = hist(log(h./w), xx); %
aspects = convn(aspects, filter, 'same');
[peak, I] = max(aspects);
aspect = exp(xx(I)); %滤波后最大的h/w,作为最典型的h/w
% pick 20 percentile area
areas = sort(h.*w);
area = areas(floor(length(areas) * 0.2)); % 比它大的,可以缩放,比该尺寸小的呢?
area = max(min(area, 5000), 3000); %限制在 3000-5000
% pick dimensions
w = sqrt(area/aspect