DPM(voc-release5) Matlab模型文件 Mat转XML

(转载请注明作者和出处 楼燚(yì)航的blog :http://www.cnblogs.com/louyihang loves baiyan/ 未经允许请勿用于商业用途)

由于目前DPM模型训练的代码没有C++版本,至少我没看见opencv ccv conrib等一些库中都没有看到相关训练的部分倒都是有detector的部分),大部分人都是基于Matlab来做训练的,放到wnidows下用一些别的DPM的库或者自己C++实现,那么这些模型的文件类型大多都是XML的,网上现成的都比较混乱,看到opencv contrib库中的作者在他的另一个Git库中放了这个转换文件,没有公开说明。这个转换的文件可以达到目的,亲测可用。

function mat2opencvxml(fname_in, fname_out)
% Convert DPM (2007) model to cascade model and
% save in OpenCV file storage format (.xml)
%    mat2opencvxml(fname_in, fname_out)
%
%    e.g., mat2opencvxml('./INRIA/inriaperson_final.mat', 'inriaperson_cascade_cv.xml'
%
%    Arguments
%       fname_in    File name of the DPM VOC 2007 model
%       fname_out   File name of the OpenCV file storage model (.xml)

% load VOC2007 DPM model
load(fname_in);

thresh = model.thresh;
pca = 5;
csc_model = cascade_model(model, '2007', pca, thresh);

num_feat = 32;
rootfilters = [];
for i = 1:length(csc_model.rootfilters)
    rootfilters{i} = csc_model.rootfilters{i}.w;
end
partfilters = [];
for i = 1:length(csc_model.partfilters)
    partfilters{i} = csc_model.partfilters{i}.w;
end
for c = 1:csc_model.numcomponents
    ridx{c} = csc_model.components{c}.rootindex;
    oidx{c} = csc_model.components{c}.offsetindex;
    root{c} = csc_model.rootfilters{ridx{c}}.w;
    root_pca{c} = csc_model.rootfilters{ridx{c}}.wpca;
    offset{c} = csc_model.offsets{oidx{c}}.w;
    loc{c} = csc_model.loc{c}.w;
    rsize{c} = [size(root{c},1) size(root{c},2)];
    numparts{c} = length(csc_model.components{c}.parts);
    for j = 1:numparts{c}
        pidx{c,j} = csc_model.components{c}.parts{j}.partindex;
        didx{c,j} = csc_model.components{c}.parts{j}.defindex;
        part{c,j} = csc_model.partfilters{pidx{c,j}}.w;
        part_pca{c,j} = csc_model.partfilters{pidx{c,j}}.wpca;
        psize{c,j} = [size(part{c,j},1) size(part{c,j},2)];
    end
end

maxsizex = ceil(csc_model.maxsize(2));
maxsizey = ceil(csc_model.maxsize(1));

pca_rows = size(csc_model.pca_coeff, 1);
pca_cols = size(csc_model.pca_coeff, 2);

f = fopen(fname_out, 'wb');
fprintf(f, '\n');
fprintf(f, '\n');
fprintf(f, '%d\n', csc_model.sbin);
fprintf(f, '%d\n', csc_model.numcomponents);
fprintf(f, '%d\n', num_feat);
fprintf(f, '%d\n', csc_model.interval);
fprintf(f, '%d\n', maxsizex);
fprintf(f, '%d\n', maxsizey);
%the pca coeff
fprintf(f, '\n');
fprintf(f,  '\t%d\n', pca_rows);
fprintf(f,  '\t%d\n', pca_cols);
fprintf(f,  '\t
d
\n'); fprintf(f, '\t\n'); for i=1:pca_rows fprintf(f, '\t'); for j=1:pca_cols fprintf(f, '%f ', csc_model.pca_coeff(i, j)); end fprintf(f, '\n'); end fprintf(f, '\t\n'); fprintf(f, '
\n'); fprintf(f, '%d\n', pca_cols); fprintf(f, '%.16f\n', csc_model.thresh); fprintf(f, '\n'); for c = 1:csc_model.numcomponents fprintf(f, '%f ', offset{c}); end fprintf(f, '\n\n'); fprintf(f, '\n'); for c = 1:csc_model.numcomponents rootfilter = root{c}; rows = size(rootfilter,1); cols = size(rootfilter,2); depth = size(rootfilter,3); fprintf(f, '\t<_ type_id="opencv-matrix">\n'); fprintf(f, '\t%d\n', rows); fprintf(f, '\t%d\n', cols*depth); fprintf(f, '\t
d
\n'); fprintf(f, '\t\n'); for i=1:rows fprintf(f, '\t'); for j=1:cols for k=1:depth fprintf(f, '%f ', rootfilter(i, j, k)); end end fprintf(f, '\n'); end fprintf(f, '\t\n'); fprintf(f, '\t\n'); end fprintf(f, '
\n'); fprintf(f, '\n'); for c = 1:csc_model.numcomponents rootfilter_pca = root_pca{c}; rows = size(rootfilter_pca,1); cols = size(rootfilter_pca,2); depth = size(rootfilter_pca,3); fprintf(f, '\t<_ type_id="opencv-matrix">\n'); fprintf(f, '\t%d\n', rows); fprintf(f, '\t%d\n', cols*depth); fprintf(f, '\t
d
\n'); fprintf(f, '\t\n'); for i=1:rows fprintf(f, '\t'); for j=1:cols for k=1:depth fprintf(f, '%f ', rootfilter_pca(i, j, k)); end end fprintf(f, '\n'); end fprintf(f, '\t\n'); fprintf(f, '\t\n'); end fprintf(f, '
\n'); fprintf(f, '\n'); for c = 1:csc_model.numcomponents for p=1:numparts{c} partfilter = part{c,p}; rows = size(partfilter,1); cols = size(partfilter,2); depth = size(partfilter,3); fprintf(f, '\t<_ type_id="opencv-matrix">\n'); fprintf(f, '\t%d\n', rows); fprintf(f, '\t%d\n', cols*depth); fprintf(f, '\t
d
\n'); fprintf(f, '\t\n'); for i=1:rows fprintf(f, '\t'); for j=1:cols for k=1:depth fprintf(f, '%f ', partfilter(i, j, k)); end end fprintf(f, '\n'); end fprintf(f, '\t\n'); fprintf(f, '\t\n'); end end fprintf(f, '
\n'); fprintf(f, '\n'); for c = 1:csc_model.numcomponents for p=1:numparts{c} partfilter = part_pca{c,p}; rows = size(partfilter,1); cols = size(partfilter,2); depth = size(partfilter,3); fprintf(f, '\t<_ type_id="opencv-matrix">\n'); fprintf(f, '\t%d\n', rows); fprintf(f, '\t%d\n', cols*depth); fprintf(f, '\t
d
\n'); fprintf(f, '\t\n'); for i=1:rows fprintf(f, '\t'); for j=1:cols for k=1:depth fprintf(f, '%f ', partfilter(i, j, k)); end end fprintf(f, '\n'); end fprintf(f, '\t\n'); fprintf(f, '\t\n'); end end fprintf(f, '
\n'); fprintf(f, '\n'); for c = 1:csc_model.numcomponents fprintf(f, '\t<_>\n'); fprintf(f, '\t'); t = csc_model.cascade.t{ridx{c}}; for j=1:length(t) fprintf(f, '%f ', t(j)); end fprintf(f, '\n\t\n'); end fprintf(f, '\n'); fprintf(f, '\n'); for c = 1:csc_model.numcomponents for p=1:numparts{c} fprintf(f, '\t<_>\n'); fprintf(f, '\t'); anchor = csc_model.defs{didx{c,p}}.anchor; for j=1:length(anchor) fprintf(f, '%f ', anchor(j)); end fprintf(f, '\n\t\n'); end end fprintf(f, '\n'); fprintf(f, '\n'); for c = 1:csc_model.numcomponents for p=1:numparts{c} fprintf(f, '\t<_>\n'); fprintf(f, '\t'); def = csc_model.defs{didx{c,p}}.w; for j=1:length(def) fprintf(f, '%f ', def(j)); end fprintf(f, '\n\t\n'); end end fprintf(f, '\n'); fprintf(f, '\n'); for c = 1:csc_model.numcomponents fprintf(f, '%f ', numparts{c}); end fprintf(f, '\n'); fprintf(f, '\n'); for c = 1:csc_model.numcomponents fprintf(f, '\t<_>\n'); fprintf(f, '\t'); order = csc_model.cascade.order{c}; for i=1:length(order) fprintf(f, '%f ', order(i)); end fprintf(f, '\n\t\n'); end fprintf(f, '\n'); fprintf(f, '\n'); for c = 1:csc_model.numcomponents fprintf(f, '\t<_>\n'); fprintf(f, '\t'); loc_w = loc{c}; for i=1:length(loc_w) fprintf(f, '%f ', loc_w(i)); end fprintf(f, '\n\t\n'); end fprintf(f, '\n'); fprintf(f, '
'); fclose(f);

转载于:https://www.cnblogs.com/louyihang-loves-baiyan/p/4966006.html

你可能感兴趣的:(DPM(voc-release5) Matlab模型文件 Mat转XML)