Skip to content

Commit

Permalink
实现了最简单的框架
Browse files Browse the repository at this point in the history
  • Loading branch information
lineclappe committed Nov 8, 2015
1 parent f315e70 commit 98fab95
Show file tree
Hide file tree
Showing 12 changed files with 271 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Data_Analyse/CalculateMAP.asv
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
%Calculate the MAP value under certain truncated level T
function value = CalculateMAP(rank,data,T)
num_rel = 0;
num_img = size(rank,1);
map = 0;
count = 0;
T = min(T,num_img);
for i = 1:T
seq = rank(i,1);
rel = data(seq,3);
count = count + 1;
num_rel = num_rel + rel;
if rel == 1
map = map + num_rel/count;
end
end
value = map / num_rel;
17 changes: 17 additions & 0 deletions Data_Analyse/CalculateMAP.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
%Calculate the MAP value under certain truncated level T
function value = CalculateMAP(rank,data,T)
num_rel = 0;
num_img = size(rank,1);
map = 0;
count = 0;
T = min(T,num_img);
for i = 1:T
seq = rank(i,1);
rel = data(seq,3);
count = count + 1;
num_rel = num_rel + rel;
if rel == 1
map = map + num_rel/count;
end
end
value = map / num_rel;
12 changes: 12 additions & 0 deletions Data_Analyse/GData.asv
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
%Analyze the data
%function GData
label_dir = 'G:/LINBIN_DATA/labels.txt';
Table = load(label_dir);
query_set = unique(Table(:,1));
for i = 1:length(query_set)
query = query_set(i);
%Extract the given query from the table
corres = find(Table(:,1) == query);
query_part = Table(corres(1):corres(length(corres)));
end

11 changes: 11 additions & 0 deletions Data_Analyse/GData.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
%Analyze the data
%function GData
function rec_query = GData(query)
label_dir = 'G:/LINBIN_DATA/labels.txt';
Table = load(label_dir);
%Extract the given query from the table
corres = find(Table(:,1) == query);
query_part = Table(corres(1):corres(length(corres)),:);
%order the set according to the number of the image
rec_query = sortrows(query_part,2);

5 changes: 5 additions & 0 deletions Similarity/CalculateSim.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
%Calculate the similarity value between two features
function value = CalculateSim(fea1,fea2)
%Calculate the distance between two features
dis =(norm(fea1-fea2))^2;
value = 1/(dis+0.5);
16 changes: 16 additions & 0 deletions Similarity/Fea2Sim.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
%Turning Feature Matrix into Similarity Matrix
function sim = Fea2Sim(fea)
num_img = size(fea,1);
dim = size(fea,2);
sim = zeros(num_img,num_img);
for i = 1:num_img
fea1 = fea(i,:);
for j = i+1 : nu_img
fea2 = fea(j,:);
simvalue = CalculateSim(fea1,fea2);
sim(i,j) = simvalue;
sim(j,i) = simvalue;
end
end


Binary file added Visual Rank/.VisualRank.m.swp
Binary file not shown.
44 changes: 44 additions & 0 deletions Visual Rank/QDE_Estimation.asv
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
%The procedure of query difficulty estimation using coherence score
function cos_score = QDE_Estimation(sim_mat, T)
%%
%%%Calculate the Trsim
%simpath = 'G:\LINBIN_DATA\simmat';
%v = SimThresh(simpath);
thresh = 0.6294;
%%
num_img = size(sim_mat,1);
if T > num_img
cos_score = -1;
else
for i = 1:








function value = SimThresh(sim_path)
%load the similarity value of the dataset
%For web353, the value equals to 0.6294
fea_dir = dir(sim_path);
sim_pairs = [];
for i = 1:length(fea_dir)
fea_name = fea_dir(i).name;
if(strcmp(fea_name,'.') == true || strcmp(fea_name, '..')==true)
continue;
end
fea_path = fullfile(sim_path,fea_name);
load(fea_path);
num_img = size(sim_mat,1);
for ii = 1:num_img
for jj = ii+1:num_img
sim_pairs = [sim_pairs; sim_mat(ii,jj)];
end
end
end
[Y,I] = sort(sim_pairs,1,'ascend');
num_pairs = length(sim_pairs);
value_index = floor(num_pairs*0.8);
value = sim_pairs(value_index);
53 changes: 53 additions & 0 deletions Visual Rank/QDE_Estimation.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
%The procedure of query difficulty estimation using coherence score
function cos_score = QDE_Estimation(sim_mat, T)
%%
%%%Calculate the Trsim
%simpath = 'G:\LINBIN_DATA\simmat';
%v = SimThresh(simpath);
thresh = 0.6294;
%%
num_img = size(sim_mat,1);
if T > num_img
cos_score = -1;
else
cos_score = 0;
for i = 1:T
for j = 1:T
sim = sim_mat(i,j);
if sim > thresh
cos_score = cos_score + 1;
end
end
end
cos_score = cos_score / (T*(T-1));

end






function value = SimThresh(sim_path)
%load the similarity value of the dataset
%For web353, the value equals to 0.6294
fea_dir = dir(sim_path);
sim_pairs = [];
for i = 1:length(fea_dir)
fea_name = fea_dir(i).name;
if(strcmp(fea_name,'.') == true || strcmp(fea_name, '..')==true)
continue;
end
fea_path = fullfile(sim_path,fea_name);
load(fea_path);
num_img = size(sim_mat,1);
for ii = 1:num_img
for jj = ii+1:num_img
sim_pairs = [sim_pairs; sim_mat(ii,jj)];
end
end
end
[Y,I] = sort(sim_pairs,1,'ascend');
num_pairs = length(sim_pairs);
value_index = floor(num_pairs*0.8);
value = sim_pairs(value_index);
23 changes: 23 additions & 0 deletions Visual Rank/VisualRank.asv
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
function rank = VisualRank(sim,d,rel)
%Applying VisualRank Algorithm
num = size(sim,1);
rank = ones(num,1);
iter_time = 100;
%column normalize the matrix
for i = 1:num
fea = sim(:,i);
fea = fea./sum(fea);
sim(:,i) = fea;
end
%parameters settings
p = zeros(num,1);
for i = 1:rel
p(i) = 1/rel;
end
i = 0;
while i < iter_time
rank = d*sim*rank + (1-d)*p;
end



25 changes: 25 additions & 0 deletions Visual Rank/VisualRank.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function rank = VisualRank(sim,d,rel)
num = size(sim,1);
rank = ones(num,1);
iter_time = 100;
%column normalize the matrix
for i = 1:num
fea = sim(:,i);
fea = fea./sum(fea);
sim(:,i) = fea;
end
%parameters settings
p = zeros(num,1);
%In case when num is smaller than rel
rnum = min(rel,num);
for i = 1:rnum
p(i) = 1/rnum;
end
i = 0;
while i < iter_time
rank = d*sim*rank + (1-d)*p;
i = i+ 1;
end



48 changes: 48 additions & 0 deletions main.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
%The main function
addpath('Visual Rank');
addpath('Feature');
addpath('Similarity');
addpath('Data_Analyse');
img_path = 'G:\LINBIN_DATA\DCNN_O';
sim_path = 'G:\LINBIN_DATA\simmat';
%%
%load the similarity matrix
sim_dir = dir(sim_path);
sum_ap0 = 0;
sum_ap1 = 0;
for i = 1:length(sim_dir)
file_name = sim_dir(i).name;
if(strcmp(file_name,'.') == false && strcmp(file_name,'..')==false)
sim_file = fullfile(sim_path,file_name);
%%
%Get the index of the query
index1 = strfind(file_name,'_');
index2 = strfind(file_name,'.');
query = str2num(file_name(index1+1:index2-1))
%load the GroundTruth Data
gdata = GData(query);
load(sim_file);
%%
%1. Don't re-rank the images
num_img = size(sim_mat,1);
gindex = zeros(num_img,1);
for ii = 1:num_img
gindex(ii) = ii;
end
%2.Using Visual_Rank to re-rank the images
%Baseline method
rankscore = VisualRank(sim_mat,0.85,30);
[rank,index] = sort(rankscore,'descend');
%%
%Caluclate the MAP
AP0 = CalculateMAP(gindex,gdata,num_img);
AP1 = CalculateMAP(index,gdata,num_img);
sum_ap0 = AP0 + sum_ap0;
sum_ap1 = AP1 + sum_ap1;
end
end

sum_ap0 = sum_ap0 / 353
sum_ap1 = sum_ap1 / 353


0 comments on commit 98fab95

Please sign in to comment.