VOC-release4.01 DPM训练的model(mat)转为OpenCV latentsvm可以加载的model(xml)

摘自:http://www.xuebuyuan.com/601093.html


转换后的single component person model检测效果不错,而VOC2007 2009中的three-component person model的检测效果则非常差,可能是OpenCV对混合模型的加载不太支持。(我只关心person的检测,所以只测试了person的model转换后的效果)

function MAT2XMLmodel_401(matmodel, xmlfile)
% jelly 2013-08-12

% Convert *.mat format model in the source example in
% Discriminatively Trained Deformable Part Models "voc-release4.01"
% to opencv's latentSVM detect input format *.xml
if(nargin < 2)
    matmodel = 'INRIA/inriaperson_final';
    xmlfile = 'INRIA/inriaperson_final.xml';
end
load(matmodel);
fid = fopen(xmlfile, 'w');

fprintf(fid, '<Model>\n');
%%
%获取组件数
ncom = length(model.rules{model.start});
fprintf(fid, '\t<!-- Number of components -->\n');
fprintf(fid, '\t<NumComponents>%d</NumComponents>\n', ncom);
%获取特征维数,固定值31维。model中没有记录
nfeature = 31;
fprintf(fid, '\t<!-- Number of features -->\n');
fprintf(fid, '\t<P>%d</P>\n', nfeature);
%Score threshold=model.thresh
fprintf(fid, '\t<!-- Score threshold -->\n');
fprintf(fid, '\t<ScoreThreshold>%.16f</ScoreThreshold>\n', model.thresh);
layer = 1;
%对于每一个组件分别获取它的root filter、part filter、deformation filter
for icom = 1:ncom
    fprintf(fid, '\t<Component>\n');
    
    fprintf(fid, '\t\t<!-- Root filter description -->\n');
    fprintf(fid, '\t\t<RootFilter>\n');
    
    % attention: X,Y swap
    rhs = model.rules{model.start}(icom).rhs;
    % assume the root filter is first on the rhs of the start rules
    if model.symbols(rhs(1)).type == 'T'
        % handle case where there's no deformation model for the root
        root = model.symbols(rhs(1)).filter;
    else
        % handle case where there is a deformation model for the root
        root = model.symbols(model.rules{rhs(1)}(layer).rhs).filter;
    end
    
    filternum = root;
    sizeX = model.filters(filternum).size(2);
    sizeY = model.filters(filternum).size(1);
    fprintf(fid, '\t\t\t<!-- Dimensions -->\n');
    fprintf(fid, '\t\t\t<sizeX>%d</sizeX>\n', sizeX);
    fprintf(fid, '\t\t\t<sizeY>%d</sizeY>\n', sizeY);
    
    fprintf(fid, '\t\t\t<!-- Weights (binary representation) -->\n');
    fprintf(fid, '\t\t\t<Weights>');
    for iY = 1:sizeY
        for iX = 1:sizeX
            % original mat has 32 which is larger than nfeature=31 by 1
            fwrite(fid, model.filters(filternum).w(iY,iX,1:nfeature), 'double'); % need verify
        end
    end
    fprintf(fid, '\t\t\t</Weights>\n'); 
    
    fprintf(fid, '\t\t\t<!-- Linear term in score function -->\n');
    fprintf(fid, '\t\t\t<LinearTerm>%.16f</LinearTerm>\n', ...  % need verify
        model.rules{model.start}(icom).offset.w);
    
    fprintf(fid, '\t\t</RootFilter>\n');
    
    fprintf(fid, '\t\t<!-- Part filters description -->\n');
    fprintf(fid, '\t\t<PartFilters>\n');
    %在每个component内,获取part filter的个数,并获取每个part的参数
    npart = length(model.rules{model.start}(icom).rhs) -1 ;
    fprintf(fid, '\t\t\t<NumPartFilters>%d</NumPartFilters>\n', npart);
    %%获取每个part的相关参数[dx,dy,ds]和penalty[dx dy dxx dyy]
    for ipart = 2: npart+1
        fprintf(fid, '\t\t\t<!-- Part filter ? description -->\n');
        fprintf(fid, '\t\t\t<PartFilter>\n');
        
        irule = model.rules{model.start}(icom).rhs(ipart);
        filternum = model.symbols(model.rules{irule}.rhs).filter;
        sizeX = model.filters(filternum).size(2);
        sizeY = model.filters(filternum).size(1);
        fprintf(fid, '\t\t\t\t<sizeX>%d</sizeX>\n', sizeX);
        fprintf(fid, '\t\t\t\t<sizeY>%d</sizeY>\n', sizeY);
        fprintf(fid, '\t\t\t\t<!-- Weights (binary representation) -->\n');
        fprintf(fid, '\t\t\t\t<Weights>');
        for iY = 1:sizeY
            for iX = 1:sizeX
                % original mat has 32 which is larger than nfeature=31 by 1
                fwrite(fid, model.filters(filternum).w(iY,iX,1:nfeature), 'double'); % need verify
            end
        end
        fprintf(fid, '\t\t\t\t</Weights>\n');        
        fprintf(fid, '\t\t\t\t<!-- Part filter offset -->\n');        
        fprintf(fid, '\t\t\t\t<V>\n');        
        fprintf(fid, '\t\t\t\t\t<Vx>%d</Vx>\n',model.rules{model.start}(icom).anchor{ipart}(1)+1); %[dx,dy,ds]
        fprintf(fid, '\t\t\t\t\t<Vy>%d</Vy>\n',model.rules{model.start}(icom).anchor{ipart}(2)+1);       
        fprintf(fid, '\t\t\t\t</V>\n');              
        fprintf(fid, '\t\t\t\t<!-- Quadratic penalty function coefficients -->\n');        
        fprintf(fid, '\t\t\t\t<Penalty>\n');
        fprintf(fid, '\t\t\t\t\t<dx>%.16f</dx>\n',model.rules{irule}.def.w(2));
        fprintf(fid, '\t\t\t\t\t<dy>%.16f</dy>\n',model.rules{irule}.def.w(4));
        fprintf(fid, '\t\t\t\t\t<dxx>%.16f</dxx>\n',model.rules{irule}.def.w(1));
        fprintf(fid, '\t\t\t\t\t<dyy>%.16f</dyy>\n',model.rules{irule}.def.w(3));       
        fprintf(fid, '\t\t\t\t</Penalty>\n');     
        fprintf(fid, '\t\t\t</PartFilter>\n');
    end
    
    fprintf(fid, '\t\t</PartFilters>\n');
    fprintf(fid, '\t</Component>\n');
end
fprintf(fid, '</Model>\n');
fclose(fid);
end


你可能感兴趣的:(opencv,DPM,voc-release4.01)