-
Notifications
You must be signed in to change notification settings - Fork 12
/
DA_LPP.m
66 lines (66 loc) · 2.64 KB
/
DA_LPP.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
% =====================================================================
% Code for conference paper:
% Qian Wang, Penghui Bu, Toby Breckon, Unifying Unsupervised Domain
% Adaptation and Zero-Shot Visual Recognition, IJCNN 2019
% By Qian Wang, qian.wang173@hotmail.com
% =====================================================================
function acc_per_class = DA_LPP(domainS_features,domainS_labels,domainT_features,domainT_labels)
num_iter = 10;
options.NeighborMode='KNN';
options.WeightMode = 'HeatKernel';
options.k = 30;
options.t = 1;
options.ReducedDim = 128;
options.alpha = 1;
num_class = length(unique(domainS_labels));
W_all = zeros(size(domainS_features,1)+size(domainT_features,1));
W_s = constructW1(domainS_labels);
W = W_all;
W(1:size(W_s,1),1:size(W_s,2)) = W_s;
% looping
p = 1;
fprintf('d=%d\n',options.ReducedDim);
for iter = 1:num_iter
P = LPP([domainS_features;domainT_features],W,options);
%P = LPP(domainS_features,W_s,options);
domainS_proj = domainS_features*P;
domainT_proj = domainT_features*P;
proj_mean = mean([domainS_proj;domainT_proj]);
domainS_proj = domainS_proj - repmat(proj_mean,[size(domainS_proj,1) 1 ]);
domainT_proj = domainT_proj - repmat(proj_mean,[size(domainT_proj,1) 1 ]);
domainS_proj = L2Norm(domainS_proj);
domainT_proj = L2Norm(domainT_proj);
distances = EuDist2(domainT_proj,domainS_proj);
%% distance to class means
classMeans = zeros(num_class,options.ReducedDim);
for i = 1:num_class
classMeans(i,:) = mean(domainS_proj(domainS_labels==i,:));
end
classMeans = L2Norm(classMeans);
distClassMeans = EuDist2(domainT_proj,classMeans);
expMatrix = exp(-distClassMeans);
probMatrix = expMatrix./repmat(sum(expMatrix,2),[1 num_class]);
[prob,predLabels] = max(probMatrix');
p=1-iter/num_iter;
[sortedProb,index] = sort(prob);
sortedPredLabels = predLabels(index);
trustable = zeros(1,length(prob));
for i = 1:num_class
thisClassProb = sortedProb(sortedPredLabels==i);
if length(thisClassProb)>0
trustable = trustable+ (prob>thisClassProb(floor(length(thisClassProb)*p)+1)).*(predLabels==i);
end
end
pseudoLabels = predLabels;
pseudoLabels(~trustable) = -1;
W = constructW1([domainS_labels,pseudoLabels]);
%% calculate ACC
acc = sum(predLabels==domainT_labels)/length(domainT_labels);
for i = 1:num_class
acc_per_class(i) = sum((predLabels == domainT_labels).*(domainT_labels==i))/sum(domainT_labels==i);
end
fprintf('Iteration=%d, Acc:%0.3f,Mean acc per class: %0.3f\n', iter, acc, mean(acc_per_class));
if sum(trustable)>=length(prob)
break;
end
end