• 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, '<?xml version="1.0"?>
    ');
    fprintf(f, '<opencv_storage>
    ');
    fprintf(f, '<SBin>%d</SBin>
    ', csc_model.sbin);
    fprintf(f, '<NumComponents>%d</NumComponents>
    ', csc_model.numcomponents);
    fprintf(f, '<NumFeatures>%d</NumFeatures>
    ', num_feat);
    fprintf(f, '<Interval>%d</Interval>
    ', csc_model.interval);
    fprintf(f, '<MaxSizeX>%d</MaxSizeX>
    ', maxsizex);
    fprintf(f, '<MaxSizeY>%d</MaxSizeY>
    ', maxsizey);
    %the pca coeff
    fprintf(f, '<PCAcoeff type_id="opencv-matrix">
    ');
    fprintf(f,  '	<rows>%d</rows>
    ', pca_rows);
    fprintf(f,  '	<cols>%d</cols>
    ', pca_cols);
    fprintf(f,  '	<dt>d</dt>
    ');
    fprintf(f,  '	<data>
    ');
    for i=1:pca_rows
        fprintf(f,  '	');
        for j=1:pca_cols
            fprintf(f,  '%f ', csc_model.pca_coeff(i, j));
        end
        fprintf(f,  '
    ');
    end
    fprintf(f,  '	</data>
    ');
    fprintf(f, '</PCAcoeff>
    ');
    fprintf(f, '<PCADim>%d</PCADim>
    ', pca_cols);
    fprintf(f, '<ScoreThreshold>%.16f</ScoreThreshold>
    ', csc_model.thresh);
    
    fprintf(f, '<Bias>
    ');
    for c = 1:csc_model.numcomponents
        fprintf(f,  '%f ', offset{c});
    end
    fprintf(f, '
    </Bias>
    ');
    
    fprintf(f, '<RootFilters>
    ');
    for c = 1:csc_model.numcomponents
        rootfilter = root{c};
        rows = size(rootfilter,1);
        cols = size(rootfilter,2);
        depth = size(rootfilter,3);
        fprintf(f, '	<_ type_id="opencv-matrix">
    ');
        fprintf(f,  '	<rows>%d</rows>
    ', rows);
        fprintf(f,  '	<cols>%d</cols>
    ', cols*depth);
        fprintf(f,  '	<dt>d</dt>
    ');
        fprintf(f,  '	<data>
    ');
        for i=1:rows
            fprintf(f,  '	');
            for j=1:cols
                for k=1:depth
                    fprintf(f,  '%f ', rootfilter(i, j, k));
                end
            end
            fprintf(f,  '
    ');
        end
        fprintf(f,  '	</data>
    ');
        fprintf(f, '	</_>
    ');
    end
    fprintf(f, '</RootFilters>
    ');
    
    fprintf(f, '<RootPCAFilters>
    ');
    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, '	<_ type_id="opencv-matrix">
    ');
        fprintf(f,  '	<rows>%d</rows>
    ', rows);
        fprintf(f,  '	<cols>%d</cols>
    ', cols*depth);
        fprintf(f,  '	<dt>d</dt>
    ');
        fprintf(f,  '	<data>
    ');
        for i=1:rows
            fprintf(f,  '	');
            for j=1:cols
                for k=1:depth
                    fprintf(f,  '%f ', rootfilter_pca(i, j, k));
                end
            end
            fprintf(f,  '
    ');
        end
        fprintf(f,  '	</data>
    ');
        fprintf(f, '	</_>
    ');
    end
    fprintf(f, '</RootPCAFilters>
    ');
    
    fprintf(f, '<PartFilters>
    ');
    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, '	<_ type_id="opencv-matrix">
    ');
            fprintf(f,  '	<rows>%d</rows>
    ', rows);
            fprintf(f,  '	<cols>%d</cols>
    ', cols*depth);
            fprintf(f,  '	<dt>d</dt>
    ');
            fprintf(f,  '	<data>
    ');
            for i=1:rows
                fprintf(f,  '	');
                for j=1:cols
                    for k=1:depth
                        fprintf(f,  '%f ', partfilter(i, j, k));
                    end
                end
                fprintf(f,  '
    ');
            end
            fprintf(f,  '	</data>
    ');
            fprintf(f, '	</_>
    ');
        end
    end
    fprintf(f, '</PartFilters>
    ');
    
    fprintf(f, '<PartPCAFilters>
    ');
    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, '	<_ type_id="opencv-matrix">
    ');
            fprintf(f,  '	<rows>%d</rows>
    ', rows);
            fprintf(f,  '	<cols>%d</cols>
    ', cols*depth);
            fprintf(f,  '	<dt>d</dt>
    ');
            fprintf(f,  '	<data>
    ');
            for i=1:rows
                fprintf(f,  '	');
                for j=1:cols
                    for k=1:depth
                        fprintf(f,  '%f ', partfilter(i, j, k));
                    end
                end
                fprintf(f,  '
    ');
            end
            fprintf(f,  '	</data>
    ');
            fprintf(f, '	</_>
    ');
        end
    end
    fprintf(f, '</PartPCAFilters>
    ');
    
    fprintf(f, '<PrunThreshold>
    ');
    for c = 1:csc_model.numcomponents
        fprintf(f, '	<_>
    ');
        fprintf(f,  '	');
        t = csc_model.cascade.t{ridx{c}};
        for j=1:length(t)
            fprintf(f,  '%f ', t(j));
        end
        fprintf(f, '
    	</_>
    ');
    end
    fprintf(f, '</PrunThreshold>
    ');
    
    fprintf(f, '<Anchor>
    ');
    for c = 1:csc_model.numcomponents
        for p=1:numparts{c}
            fprintf(f, '	<_>
    ');
            fprintf(f,  '	');
            anchor = csc_model.defs{didx{c,p}}.anchor;
            for j=1:length(anchor)
                fprintf(f,  '%f ', anchor(j));
            end
            fprintf(f, '
    	</_>
    ');
        end
    end
    fprintf(f, '</Anchor>
    ');
    
    fprintf(f, '<Deformation>
    ');
    for c = 1:csc_model.numcomponents
        for p=1:numparts{c}
            fprintf(f, '	<_>
    ');
            fprintf(f,  '	');
            def = csc_model.defs{didx{c,p}}.w;
            for j=1:length(def)
                fprintf(f,  '%f ', def(j));
            end
            fprintf(f, '
    	</_>
    ');
        end
    end
    fprintf(f, '</Deformation>
    ');
    
    fprintf(f, '<NumParts>
    ');
    for c = 1:csc_model.numcomponents
        fprintf(f,  '%f ', numparts{c});
    end
    fprintf(f, '</NumParts>
    ');
    
    fprintf(f, '<PartOrder>
    ');
    for c = 1:csc_model.numcomponents
        fprintf(f, '	<_>
    ');
        fprintf(f,  '	');
        order = csc_model.cascade.order{c};
        for i=1:length(order)
            fprintf(f,  '%f ', order(i));
        end
        fprintf(f, '
    	</_>
    ');
    end
    fprintf(f, '</PartOrder>
    ');
    
    fprintf(f, '<LocationWeight>
    ');
    for c = 1:csc_model.numcomponents
        fprintf(f, '	<_>
    ');
        fprintf(f,  '	');
        loc_w = loc{c};
        for i=1:length(loc_w)
            fprintf(f,  '%f ', loc_w(i));
        end
        fprintf(f, '
    	</_>
    ');
    end
    fprintf(f, '</LocationWeight>
    ');
    fprintf(f, '</opencv_storage>');
    fclose(f);
    
    
  • 相关阅读:
    BZOJ-3940:Censoring(AC自动机裸题)
    BZOJ-3881:Divljak (AC自动机+DFS序+树链求并+树状数组)
    CodeForces
    CodeForces 547E:Mike and Friends(AC自动机+DFS序+主席树)
    CodeForces -163E :e-Government (AC自动机+DFS序+树状数组)
    CodeForces
    CodeForces
    BZOJ2726:任务安排(DP+斜率优化+二分)
    bzoj 2049: [Sdoi2008]Cave 洞穴勘测
    [SDOI2009]Bill的挑战
  • 原文地址:https://www.cnblogs.com/louyihang-loves-baiyan/p/4966006.html
Copyright © 2020-2023  润新知