1

📝笔记:使用vlfeat的Matlab接口简单实现BOW以及VLAD

 2 years ago
source link: https://vincentqin.tech/posts/vlfeat-feature-embedding/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

📝笔记:使用vlfeat的Matlab接口简单实现BOW以及VLAD

Posted on

2022-03-24

Views: 19 Comments: 0

本文使用 vlfeatMatlab 接口实现 BOW 以及 VLAD

先放出代码,细节后续补充。

close all
% add vlfeat toolbox
run('vlfeat-0.9.21\toolbox\vl_setup');

%% Train a codebook

% trainning data generation
numDatabase = 10000;
dimLocalDesc = 128;
X = uint8(floor(255*rand(dimLocalDesc,numDatabase))); % can be computed by vl_sift

% setting params to KMeans
K = 10; % number of clusters
[centers,assn] = vl_ikmeans(X,K,'verbose');

% build a kd-tree to softassignment
kdtree = vl_kdtreebuild(single(centers),'verbose');

%% Compute IDF for database
nn_kdtree = vl_kdtreequery(kdtree, single(centers), single(X));
assignments = zeros(K,size(X,2));
assignments(sub2ind(size(assignments), nn_kdtree, 1:length(nn))) = 1;
% or: assignments(sub2ind(size(assignments), assn, 1:length(nn))) = 1;

IDF = log(numDatabase*ones(K,1)./(sum(assignments,2) + 1) );


%% Encode with BOW and VLAD
numofLocalKPs = 100; % number of keypoints

query = uint8(floor(255*rand(dimLocalDesc,numofLocalKPs))); % can be computed by vl_sift
% todo: can be retrieval with vl_ikmeanspush(query,centers) ;
nn = vl_kdtreequery(kdtree, single(centers), single(query));

% soft assignment
query_assign = zeros(K,numofLocalKPs); %only 1 each col
query_assign(sub2ind(size(query_assign), nn, 1:length(nn))) = 1;

% encode with vlad and bow
enc_vlad = vl_vlad(single(query),single(centers),single(query_assign),'verbose');

enc_bow = sum(query_assign,2);
numTotalWords = sum(enc_bow);
TF = enc_bow / numTotalWords;
enc_bow = TF .* IDF;

% show
bar(enc_bow);
set(gcf,'color',[1 1 1]);

TF-IDF加权后的BOW向量长这个样子:

demo_01.png


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK