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"?>\n');
fprintf(f, '<opencv_storage>\n');
fprintf(f, '<SBin>%d</SBin>\n', csc_model.sbin);
fprintf(f, '<NumComponents>%d</NumComponents>\n', csc_model.numcomponents);
fprintf(f, '<NumFeatures>%d</NumFeatures>\n', num_feat);
fprintf(f, '<Interval>%d</Interval>\n', csc_model.interval);
fprintf(f, '<MaxSizeX>%d</MaxSizeX>\n', maxsizex);
fprintf(f, '<MaxSizeY>%d</MaxSizeY>\n', maxsizey);
%the pca coeff
fprintf(f, '<PCAcoeff type_id="opencv-matrix">\n');
fprintf(f, '\t<rows>%d</rows>\n', pca_rows);
fprintf(f, '\t<cols>%d</cols>\n', pca_cols);
fprintf(f, '\t<dt>d</dt>\n');
fprintf(f, '\t<data>\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</data>\n');
fprintf(f, '</PCAcoeff>\n');
fprintf(f, '<PCADim>%d</PCADim>\n', pca_cols);
fprintf(f, '<ScoreThreshold>%.16f</ScoreThreshold>\n', csc_model.thresh);
fprintf(f, '<Bias>\n');
for c = 1:csc_model.numcomponents
fprintf(f, '%f ', offset{c});
end
fprintf(f, '\n</Bias>\n');
fprintf(f, '<RootFilters>\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<rows>%d</rows>\n', rows);
fprintf(f, '\t<cols>%d</cols>\n', cols*depth);
fprintf(f, '\t<dt>d</dt>\n');
fprintf(f, '\t<data>\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</data>\n');
fprintf(f, '\t</_>\n');
end
fprintf(f, '</RootFilters>\n');
fprintf(f, '<RootPCAFilters>\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<rows>%d</rows>\n', rows);
fprintf(f, '\t<cols>%d</cols>\n', cols*depth);
fprintf(f, '\t<dt>d</dt>\n');
fprintf(f, '\t<data>\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</data>\n');
fprintf(f, '\t</_>\n');
end
fprintf(f, '</RootPCAFilters>\n');
fprintf(f, '<PartFilters>\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<rows>%d</rows>\n', rows);
fprintf(f, '\t<cols>%d</cols>\n', cols*depth);
fprintf(f, '\t<dt>d</dt>\n');
fprintf(f, '\t<data>\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</data>\n');
fprintf(f, '\t</_>\n');
end
end
fprintf(f, '</PartFilters>\n');
fprintf(f, '<PartPCAFilters>\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<rows>%d</rows>\n', rows);
fprintf(f, '\t<cols>%d</cols>\n', cols*depth);
fprintf(f, '\t<dt>d</dt>\n');
fprintf(f, '\t<data>\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</data>\n');
fprintf(f, '\t</_>\n');
end
end
fprintf(f, '</PartPCAFilters>\n');
fprintf(f, '<PrunThreshold>\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, '</PrunThreshold>\n');
fprintf(f, '<Anchor>\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, '</Anchor>\n');
fprintf(f, '<Deformation>\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, '</Deformation>\n');
fprintf(f, '<NumParts>\n');
for c = 1:csc_model.numcomponents
fprintf(f, '%f ', numparts{c});
end
fprintf(f, '</NumParts>\n');
fprintf(f, '<PartOrder>\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, '</PartOrder>\n');
fprintf(f, '<LocationWeight>\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, '</LocationWeight>\n');
fprintf(f, '</opencv_storage>');
fclose(f);
DPM(voc-release5) Matlab模型文件 Mat转XML的更多相关文章
- python——读取MATLAB数据文件 *.mat
鉴于以后的目标主要是利用现有的Matlab数据(.mat或者.txt),主要考虑python导入Matlab数据的问题.以下代码可以解决python读取.mat文件的问题.主要使用sicpy.io即可 ...
- Matlab之文件读写
读文件: (0)自己添加 你可以将txt的一些文本数据直接拷贝到matlab窗口,然后保存为mat文件,下次就可以直接采用load函数了. (1)Load load 从Matlab的数据文件.mat ...
- Matlab立体标定mat转换成Opencv的CvMat
最近在做基于双目视觉的三维重建.比较opencv和matlab工具箱的立体标定结果精度时,发现貌似如果手工选取角点不那么离谱的话,matlab标定结果精度更高也更鲁棒.就想先用matlab标定好相机, ...
- MATLAB中文件的读写和数据的导入导出
http://blog.163.com/tawney_daylily/blog/static/13614643620111117853933/ 在编写一个程序时,经常需要从外部读入数据,或者将程序运行 ...
- tensorflow c++ API加载.pb模型文件并预测图片
tensorflow python创建模型,训练模型,得到.pb模型文件后,用c++ api进行预测 #include <iostream> #include <map> # ...
- Matlab 读取文件夹中所有的bmp文件
将srcimg文件下的bmp文件转为jpg图像,存放在dstimg文件夹下 str = 'srcimg'; dst = 'dstimg'; file=dir([str,'\*.bmp']); :len ...
- matlab提速技巧(自matlab帮助文件)
matlab提速技巧(自matlab帮助文件) 1.首先要学会用profiler.1.1. 打开profiler.To open the Profiler, select View -> Pro ...
- TF的模型文件
TF的模型文件 标签(空格分隔): TensorFlow Saver tensorflow模型保存函数为: tf.train.Saver() 当然,除了上面最简单的保存方式,也可以指定保存的步数,多长 ...
- Away3D 学习笔记(一): 加载3DS格式的模型文件
加载外部的3DS文件分为两种: 1: 模型与贴图独立于程序的,也就是从外部的文件夹中读取 private function load3DSFile():Loader3D { loader = new ...
随机推荐
- 关于HTML面试题汇总之H5
一.H5有哪些新特性,移除了哪些元素?如何处理h5新标签的浏览器兼容性问题,如何区分html和html5 1. html5不在是SGL(通用标记语言)的一个子集,而包含了:图像.位置.存储.多任务等功 ...
- Java2_JDK的安装和配置
什么是JDK JDK就是Java Development Kit,java开发工具包,由sun公司开发. JDK的三个版本 桌面系统或应用程序的标准版(Java 2 Platform Standard ...
- go语言 类型:字符串
示例 package main import ( "fmt" ) func main() { var str1 string // 声明一个字符串变量 str1 = "H ...
- LayoutInflater的infalte()
其中: resource:是布局文件ID root:是父ViewGroup对象, attachToRoot:是是否将“翻译”出来的View添加到上面的root中 root和attachToRoot是共 ...
- Android SQL语句实现数据库的增删改查
本文介绍android中的数据库的增删改查 复习sql语法: * 增 insert into info (name,phone) values ('wuyudong','111') * 删 delet ...
- Android ListView添加多种类型的ItemView
一般复杂的ListView都会重写BaseAdapter,通过重用convertView来减少inflate,通过setTag()和ViewHolder改变ItemView的内容. 重写BaseAda ...
- 浅谈Java五大设计原则之代理模式
我们来定义一下 AOP(面向切面编程) 它是面向对象的一种补充或者是一种增强,它在这基础上增加了一些 而外的功能增强. 它可以在原有的行为不改变的前提,在这之前或者之后完成一些而外 的事情. 而AO ...
- C#复习⑦
C#复习⑦ 2016年6月22日 11:50 Main Exception & Namespaces & Assemblies 异常 & 命名空间 & 程序集 1.tr ...
- hibernate总记录数查询和分页查询
//参考代码 //第一种方法: String hql = "select count(*) from User as user"; Integer count = (Integer ...
- Visual Studio 2012中文旗舰版(序列号和下载地址)
序列号:YKCW6-BPFPF-BT8C9-7DCTH-QXGWC 链接: http://pan.baidu.com/s/1pLGhDjl 密码: 3udq