bag of words matlab,Bag of words(matlab实现)
Bag of Word主要思想:将训练样本特征Kmeans聚类,对测试样本的每个特征,计算与其最近的类心,相应类别计数count加1,这样每个测试样本可以生成ncenter维的直方图。
比如:训练样本特征a、b、c、a、d、f、e、b、e、d、c、f,如果类别数ncenter为6,则可以聚成6类[a,b,c,d,e,f]注意实际聚类时类心不一定为训练样本中特征,因为kmeans聚类更新类心时都重新计算。
假如一个测试样本特征为:a、b、c、d.那么经过BoW生成6维的直方图[1,1,1,1,0,0].
其实前面就是kmeans,然后Hard voting。关于kmeans不细说了,就是更新类心的过程,一直到类心变化在误差范围内。
kmeans聚类时用的训练数据中center个随机数据初始化,后面用的欧氏距离度量,其中计算欧氏距离时用了矢量化编程,加速运算。
这是参考了别人的代码实现的,每个人针对自己的研究可能还需要小小修改。适合入门的看看。
function dic=CalDic(data,dicsize)
fprintf('Building Dictionary using Training Data\n\n');
dictionarySize = dicsize;
niters=100;%迭代次数
centres=zeros(dictionarySize,size(data,2));
[ndata,data_dim]=size(data);
[ncentres,dim]=size(centres);
%% initialization
perm = randperm(ndata);
perm = perm(1:ncentres);
centres = data(perm, :);
num_points=zeros(1,dictionarySize);
old_centres = centres;
display('Run k-means');
for n=1:niters
% Save old centres to check for termination
e2=max(max(abs(centres - old_centres)));
inError(n)=e2;
old_centres = centres;
tempc = zeros(ncentres, dim);
num_points=zeros(1,ncentres);
[ndata, data_dim] = size(data);
id = eye(ncentres);
d2 = EuclideanDistance(data,centres);
% Assign each point to nearest centre
[minvals, index] = min(d2', [], 1);
post = id(index,:); % matrix, if word i is in cluster j, post(i,j)=1, else 0;
num_points = num_points + sum(post, 1);
for j = 1:ncentres
tempc(j,:) = tempc(j,:)+sum(data(find(post(:,j)),:), 1);
end
for j = 1:ncentres
if num_points(j)>0
centres(j,:) = tempc(j,:)/num_points(j);
end
end
if n > 1
% Test for termination
%Threshold
ThrError=0.009;
if max(max(abs(centres - old_centres))) <0.009
dictionary= centres;
fprintf('Saving texton dictionary\n');
mkdir('data');%建立data文件夹
save ('data\dictionary','dictionary');%保存dictionary到data文件夹下。
break;
end
fprintf('The %d th interation finished \n',n);
end
end
下面是欧氏距离函数:
function d = EuclideanDistance(a,b)
% DISTANCE - computes Euclidean distance matrix
%
% E = EuclideanDistance(A,B)
%
% A - (MxD) matrix
% B - (NxD) matrix
%
% Returns:
% E - (MxN) Euclidean distances between vectors in A and B
%
%
% Description :
% This fully vectorized (VERY FAST!) m-file computes the
% Euclidean distance between two vectors by:
%
% ||A-B|| = sqrt ( ||A||^2 + ||B||^2 - 2*A.B )
%
% Example :
% A = rand(100,400); B = rand(200,400);
% d = EuclideanDistance(A,B);
% Author : Roland Bunschoten
% University of Amsterdam
% Intelligent Autonomous Systems (IAS) group
% Kruislaan 403 1098 SJ Amsterdam
% tel.(+31)20-5257524
% bunschot@wins.uva.nl
% Last Rev : Oct 29 16:35:48 MET DST 1999
% Tested : PC Matlab v5.2 and Solaris Matlab v5.3
% Thanx : Nikos Vlassis
% Copyright notice: You are free to modify, extend and distribute
% this code granted that the author of the original code is
% mentioned as the original author of the code.
if (nargin ~= 2)
b=a;
end
if (size(a,2) ~= size(b,2))
error('A and B should be of same dimensionality');
end
aa=sum(a.*a,2); bb=sum(b.*b,2); ab=a*b';
d = sqrt(abs(repmat(aa,[1 size(bb,1)]) + repmat(bb',[size(aa,1) 1]) - 2*ab));
下面是Hard Voting函数:
function His=HardVoting(data,dic)
ncentres=size(dic,1);
id = eye(ncentres);
d2 = EuclideanDistance(data,dic);% Assign each point to nearest centre
[minvals, index] = min(d2', [], 1);
post = id(index,:); % matrix, if word i is in cluster j, post(i,j)=1, else 0
His=sum(post, 1);
end
如果用于分类问题,可以尝试用LLC(CVPR2010) 一般比Hard Voting效果好。
bag of words matlab,Bag of words(matlab实现)相关推荐
- matlab ros 手势识别,使用MATLAB读取分析ros记录的.bag文件
/一定要关闭所有的防火墙 设置主机IP setenv('ROS_MASTER_URI','http://192.168.43.241;11311') 设置本机节点ip setenv('ROS_IP', ...
- 怎么把dll库写成MATLAB接口,如何在Matlab中应用动态连接库接口技术
1 引言 Matlab是当前应用最为广泛的数学软件,具有强大的数值计算.数据分析处理.系统 分析.图形显示甚至符号运算等功能.利用这一完整的数学平台,用户可以快速实现十分 复杂的功能,极大地提高工程分 ...
- matlab 汽车 流场,matlab画流场图
基于 Matlab 分布式工具箱的流场计算及其可视化 蔡群;周美莲;段杰峰;李青... 基于 MATLAB 和 CFD 数据库的流场可视化的实现 [J], 晏畅 5.基于 VB 与 MATLAB 混合 ...
- 目标层准则层MATLAB,层次分析法-MATLAB
层次分析法-MATLAB 第八章 层次分析法 层次分析法(Analytic Hierarchy Process,简称AHP)是对一些较为复杂.较为模糊的问题作出决策的简易方法,它特别适用于那些难于完全 ...
- matlab提excel文字,matlab读取excel文字
数据导入| 将excel数据导入matlab_计算机软件及应用_IT/计算机_专业资料.将 excel 数据导入 matlab 一. 将 excel 文本放在 matlab 的 work 文件夹里面. ...
- matlab中step_使用MATLAB中bertool进行通信仿真
MATLAB中的Communication System Toolbox中的bertool图像化工具bertool提供了极为方便的通信仿真环境以测试仿真模型的误比特率性能,并且可以很好地和理论值进行对 ...
- Matlab:序列分析法MATLAB代码
Matlab:序列分析法MATLAB代码 目录 输出结果 设计代码 输出结果 更新-- 设计代码 ###下面所有带代码中的n值需要以自己输入的数据为准###1.简单一次滑动平均法预测MATLAB程序代 ...
- Matlab学习一本通,matlab基础教程
链接:https://pan.baidu.com/s/1uTCbiRfIxcrt6lmiy6_QlQ 提取码:f2dn Matlab学习一本通,matlab基础教程 <MATLAB R201 ...
- matlab dsp实验报告,matlab实验报告14.pdf
matlab实验报告14 Matlab实验报告 院系名称 :信息科学与工程学院 专业班级 :通信工程 1303 指导老师 :陈科文 ,支国明 ,张金焕 ,周扬 学生姓名 : 学号 : 目录 实验一 熟 ...
- matlab完全自学一本通 pdf,matlab r2014a自学一本通 MATLAB R2014a完全自学一本通 中文pdf扫描版[125MB] 下载-脚本之家...
MATLAB R2014a完全自学一本通面向MATLAB 的初中级读者,在介绍MATLAB R2014a 集成环境的基础上,对MATLAB 使用中常用的知识和工具进行了详细的介绍,书中各章均提供了大量 ...
最新文章
- vba 编辑combobox内容_初识Visual Basic编辑器并建立一段简单的代码
- zzuli 20级新生周赛(1)题解
- HDFS文件上传、下载和删除案例
- getline及读文件总结
- Java基础:多线程
- 千万级日订单下,饿了么异地多活数据实施DRC的应用实践
- 【收藏】keepalived配置文件解读
- android禁用应用组件,Android彻底退出(关闭)应用程序.docx
- linux 进程间通信-信号量(semagpore)
- python处理大文件_Python如何处理大文件(知识整理)
- docker修改镜像的存储位置_win10家庭版Docker环境搭建步骤
- java所参数查询_Java程序查询系统参数
- 改变图像的对比度和亮度
- C Tricks(五)—— 比例和阈值的实现
- winform 图片压缩大小为原图的一半_图片压缩指定大小?!这款神奇的工具有必要了解一下...
- 黑客入门,从HTB开始
- Python视频剪辑Auto-Editor一键预处理口播无声片段
- 什么是时间戳?时间戳有什么用?(适合新人)
- Python不掉包实现矩阵分解及行列式转换,求秩等操作并使用QT可视化
- Python 基础之匿名函数 迭代器和高阶函数