-
Notifications
You must be signed in to change notification settings - Fork 2
/
demo.m
69 lines (61 loc) · 2.28 KB
/
demo.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
clear;
rand('seed',1013);
BITs = [16 32 64 96 128 256 512];
bit_num = length(BITs);
%********************************************************************
% load database
% database should contain traindata, testdata, traingnd, testgnd
load('MNIST.mat');
trainnum = 5000;
% trainnum = 30000;
testnum = 1000;
% crop data by trainnum and testnum
traindata = traindata(1:trainnum,:);
traingnd = traingnd(1:trainnum,:);
testdata = testdata(1:testnum,:);
testgnd = testgnd(1:testnum,:);
cateTrainTest = bsxfun(@eq, traingnd, testgnd');
%***************************************************************
% Feature transformation
%***************************************************************
method = 'HCSDH';
feature = 'kernel';
n_anchors = 1000;
% Kernel trans
anchor = traindata(randsample(trainnum, n_anchors),:);
sigma = 0.4; % for normalized data
X = exp(-sqdist(traindata,anchor)/(2*sigma*sigma));
testX = exp(-sqdist(testdata,anchor)/(2*sigma*sigma));
%*************************************************************
%*********************************
% iterating by code length L
for i=1:bit_num
bit = BITs(i);
fprintf('bit =%d\n', bit);
%***************************************************************
% run FSDH
%***************************************************************
tic;
[~, R] = HCSDH(X, traingnd, bit);
COP = toc;
%****************************************************************
% Evaluation
%****************************************************************
B = X*R > 0;
tH = testX*R > 0;
H = B;
hammRadius = 2;
B = compactbit(H);
tB = compactbit(tH);
hammTrainTest = hammingDist (tB, B)';
Ret = (hammTrainTest <= hammRadius+1e-8);
[Pre, Rec] = evaluate_macro(cateTrainTest, Ret);
[~, HammingRank]=sort(hammTrainTest,1);
MAP = cat_apcal(traingnd,testgnd,HammingRank);
fprintf('Pre=%.3g Rec = %.3g MAP=%.3g training time=%.3g \n', Pre, Rec, MAP, COP);
rate = [0.0 0.005 0.01 0.02 0.04 0.08 0.1 0.2 0.4 0.8 1.0];
for h=1:11
[pre,rec] = cat_apcal2(traingnd,testgnd,HammingRank, trainnum * rate(h));
fprintf('Pre=%.3g Rec = %.3g\n', pre, rec);
end
end