-
Notifications
You must be signed in to change notification settings - Fork 3
/
LearnPerImageKernels.m
executable file
·160 lines (123 loc) · 4.09 KB
/
LearnPerImageKernels.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
function LearnPerImageKernels(Dataset, outputFile)
% function LearnPerImageKernels(Dataset, outputFile);
%
% Learns kernel combinations for images
%
% Input:
% Dataset = Dataset structure
% outputFile = file to which the krenel weights are saved
% Alexander Vezhnevets, 2012
load(Dataset.ImageIndexFile);
ImagesDB_train = ImagesDB(Dataset.TrainImageIdx);
n = length(ImagesDB_train);
feat_len = length(ImagesDB_train{1}.Features);
c_w = zeros(n,21);
total_c = zeros(1,21);
% calculating label weights
for im = 1 : length(ImagesDB_train)
cur_im = ImagesDB_train{im};
for c = cur_im.labels
if(c ~= 0)
total_c(c) = total_c(c) + 1;
end
end
end
n_plus = 1 ./ total_c;
n_minus = 1 ./ (n - total_c);
dist = zeros(feat_len,n,n);
y = zeros(n,n);
bin_labels = zeros(n,21);
for im_idx = 1 : length(ImagesDB_train)
im_e = ImagesDB_train{im_idx};
for im_j_idx = 1 : length(ImagesDB_train)
if(im_j_idx == im_idx)
continue;
end
im_j = ImagesDB_train{im_j_idx};
total_dist = zeros(1,length(im_j.Features));
for f = 1 : length(im_j.Features)
loc_dist = im_e.Features{f} - im_j.Features{f};
total_dist(f) = norm(loc_dist);
if(f == 1)
total_dist(f) = norm(loc_dist);
elseif(f > 2 && f < 9)
total_dist(f) = 0.5 * sum(((loc_dist).^2) ./ (im_e.Features{f} + im_j.Features{f} + eps));
%total_dist(f) = norm(loc_dist);
else
total_dist(f) = norm(loc_dist);
%total_dist(f) = 0.5 * sum(((loc_dist).^2) ./ (im_e.Features{f} + im_j.Features{f} + eps));
end
end
dist(:, im_idx, im_j_idx) = total_dist;
common = intersect(im_e.labels, im_j.labels);
common = setdiff(common, 0);
y(im_idx, im_j_idx) = length(common) / length(setdiff(im_e.labels, 0));
for c = 1 : size(c_w,2)
if(ismember(c, im_e.labels))
c_w(im_idx, c) = n_plus(c);
else
c_w(im_idx, c) = n_minus(c);
end
end
bin_labels(im_idx, setdiff(im_e.labels, 0)) = 1;
end
end
%%
bin_labels( bin_labels == 0) = eps;
bin_labels( bin_labels == 1) = 1 - eps;
w = 5*(ones(feat_len,n) + rand(feat_len,n) / 5);
W = sum(c_w');
pi = zeros(size(dist,2), size(dist,3));
ro = zeros(size(dist,2), size(dist,3));
for iter = 1 : 100
pi_old = pi;
prediction = zeros(n, 21);
for i = 1 : size(pi,1)
%norm = 0;
for j = 1 : size(pi,2)
if i ~= j
pi(i,j) = exp(-dist(:,i,j)' * w(:,j));
%ro(i,j) = c_w(i,:) / W(i) * bin_labels(i,:)';
end
end
nor = sum(pi(i,:));
pi(i,:) = pi(i,:) / nor;
for c = 1 : 21
prediction(i,c) = pi(i,:) * bin_labels(:,c);
end
for j = 1 : size(pi,2)
if i ~= j
p_jw = bin_labels(j,:) ./ prediction(i,:) / n;
ro(i,j) = c_w(i,:) / W(i) * p_jw';
end
end
end
grad = 0 * w;
L = 0;
for i = 1 : size(prediction,1)
for l = 1 : 21
if(bin_labels(i,l) > 0.5)
L = L + c_w(i,l) * log( prediction(i,l));
else
L = L + c_w(i,l) * log( 1 - prediction(i,l));
end
end
end
Grad = pi - ro;
%mean(mean(pi - pi_old))
for l = 1 : feat_len
Grad_l = Grad .* reshape(dist(l,:,:), n, n);
for i = 1 : n
grad(l, i) = W(i) * sum(Grad_l(:,i));
%grad(l, i) = sum(W' .* Grad_l(:,i));
end
end
grad;
w;
for i = 1 : n
w(:,i) = w(:,i) + 0.01 * grad(:,i) / mean(abs(grad(:,i)));
end
% w(w < 0) = 0;
% w = w / sum(w);
end
save (outputFile, 'w');