-
Notifications
You must be signed in to change notification settings - Fork 1
/
SRHshowRetriImages.m
167 lines (158 loc) · 5.36 KB
/
SRHshowRetriImages.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
clear all;
clc;
path(path,'SRH'); % our method
path(path,'dataIO');
dataset='./data/Data_cifar.mat';
warning off;
c_num=1000;
epsilon=0.002;
if(exist('Xtraining','var')==0)
load(dataset, 'Xtraining');
end
if(exist('Xtest','var')==0)
load(dataset, 'Xtest');
end
if(exist('KNN_info','var')==0)
load(dataset, 'KNN_info');
end
loopbits=32;
param.block_size=1000;
param.nbits=loopbits;
Param=param;
Param.doMask= 0; %switch of indexing full OR part of dataset
Param.eta= 0.1; %trade off supervised data and regularizer
Param.L= 6; %the number of hash tables
Param.alpha=0.1; % alpha is a balance parater
Param.c_num=c_num; %the number of supervised points (should be large for large dataset)
Param.epsilon=epsilon; %the threshold to find the points to be indexed in the next hash table
Param = trainSRH(Xtraining, Param);
ntest=size(Xtest,1);
ntraining=size(Xtraining,1);
L=size(Param.A,2);
II=1:ntraining;
for j=1:L
B_tst{j} = compressSRH( Xtest, Param.A{j}, Param.B{j} );
B_trn{j} = compressSRH( Xtraining, Param.A{j}, Param.B{j} );
Mask{j}=setdiff(II,Param.Mask{j});
SizeL(j)=length(Param.Mask{j});
end
%i_block=1;
block_size=Param.block_size; %block_size=500 块的大小
block_num=ntest/block_size; %10000/500=20 分块的数目
for i_block=1:block_num
D=zeros(block_size,ntraining,'uint8'); %500*50000的0矩阵
D=D+inf; %500*50000元素全为255的矩阵
D2=zeros(block_size,ntraining,'single'); %500*50000的0矩阵
ibase=(i_block-1)*block_size; %0、500、1000、1500、2000、···、9500
imax=min(i_block*block_size, ntest); %i_block*500,10000; imax=i_block*500
BlockIdx=ibase+1:imax; %(i_block,BlockIdx),(1,1:500),(2,501:1500),
%(3,1001:2500),(4,1501:3500),(5,2001:4500), ```,(20,9501:19500)
for j=1:L
Dhamm = hammingDist(B_tst{j}(BlockIdx,:), B_trn{j}); %测试数据分块后块中数据点
% 与训练数据的汉明距离Dhamm BlockIdx的长度*5000
if(Param.doMask)
Dhamm(:,Mask{j})=inf;
end
D=min(Dhamm,D); %D BlockIdx的长度*5000 这一步的目的是使Dhamm
%的上限不要超过255(2^8=256)
D2=D2+single(Dhamm); %D2 BlockIdx的长度*5000 L个哈希表汉明距离累加
end
D2=D2/(max(max(D2))+1); %归一化???
D2=single(D)+D2;
[foo, Rank] = sort(D2, 2,'ascend'); %foo为汉明距离按每行由小到大排序
%Rank是对应排序前的位置
end
%% show retrieval images
load ./data/cifar-10-batches-mat/data_batch_1.mat;
data1=data;
labels1=labels;
clear data labels;
load ./data/cifar-10-batches-mat/data_batch_2.mat;
data2=data;
labels2=labels;
clear data labels;
load ./data/cifar-10-batches-mat/data_batch_3.mat;
data3=data;
labels3=labels;
clear data labels;
load ./data/cifar-10-batches-mat/data_batch_4.mat;
data4=data;
labels4=labels;
clear data labels;
load ./data/cifar-10-batches-mat/data_batch_5.mat;
data5=data;
labels5=labels;
clear data labels;
load ./data/cifar-10-batches-mat/test_batch.mat;
data6=data;
labels6=labels;
clear data labels;
database=[data1 labels1 ;data2 labels2;data3 labels3;data4 labels4;data5 labels5;data6 labels6];
cifar10labels=[labels1;labels2;labels3;labels4;labels5;labels6];
save('./data/cifar10labels.mat','cifar10labels');
%index=[50001,Rank(1,1:129)]'; %50001是猫
%index=[50002,Rank(2,1:129)]'; %50002是船
%index=[59004,Rank(4,1:129)]'; %59004是猫
%index=[59005,Rank(5,1:129)]'; %马
%index=[59006,Rank(6,1:129)]'; %狗
%index=[59018,Rank(18,1:129)]'; % 飞机
index=[59046,Rank(46,1:35)]'; % 飞机
%index=Rank(9,1:36)'; % 飞机
%index=[59018,Rank(18,1:129)]'; % 飞机
%index=[50007,Rank(7,1:129)]'; %50007是automobile
% DtrueTestTraining = distMat(Xtest(46,:), Xtraining(Rank(46,1:35),:)); % size = [Ntest x Ntraining]
% temp=[DtrueTestTraining;Rank(46,1:35)]';
% temp1=sort(temp);
% index=[59046,temp1(:,2)']';
rank=1;
%left=0.005;
left=0.1;
botton=0.8;
width=0.08;
height=0.08;
% for i=1:130
% j=index(i,1);
% image1r=database(j,1:1024);
% image1g=database(j,1025:2048);
% image1b=database(j,2049:end-1);
% image1rr=reshape(image1r,32,32);
% image1gg=reshape(image1g,32,32);
% image1bb=reshape(image1b,32,32);
% image1(:,:,1)=image1rr';
% image1(:,:,2)=image1gg';
% image1(:,:,3)=image1bb';
% image1=uint8(image1);
% if(mod(rank,13)~=0)
% hdl1=subplot(10,13,rank,'position',[left+0.07*(mod(rank,13)-1) botton-0.09*fix(rank/13) width height]);
% imshow(image1);
% else
% hdl1=subplot(10,13,rank,'position',[left+0.07*12 botton-0.09*fix(rank/14) width height]);
% imshow(image1);
% end
% % subplot(7,11,i);
% % imshow(image1);
% rank=rank+1;
% end
for i=1:36
j=index(i,1);
image1r=database(j,1:1024);
image1g=database(j,1025:2048);
image1b=database(j,2049:end-1);
image1rr=reshape(image1r,32,32);
image1gg=reshape(image1g,32,32);
image1bb=reshape(image1b,32,32);
image1(:,:,1)=image1rr';
image1(:,:,2)=image1gg';
image1(:,:,3)=image1bb';
image1=uint8(image1);
if(mod(rank,6)~=0)
hdl1=subplot(6,6,rank,'position',[left+0.07*(mod(rank,6)-1) botton-0.09*fix(rank/6) width height]);
imshow(image1);
else
hdl1=subplot(6,6,rank,'position',[left+0.07*5 botton-0.09*fix(rank/7) width height]);
imshow(image1);
end
% subplot(7,11,i);
% imshow(image1);
rank=rank+1;
end