-
Notifications
You must be signed in to change notification settings - Fork 22
/
trainTestDA.m
executable file
·133 lines (114 loc) · 3.25 KB
/
trainTestDA.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
function [meanAcc, stdAcc] = trainTestDA(opt, annotations)
dataset = opt.dataset;
featureType = opt.featureType;
modelType = opt.modelType;
sourceName = annotations.prm.sourceName;
targetName = annotations.prm.targetName;
nclasses = opt.nclasses;
ntrials = opt.ntrials;
if opt.nclasstrain < 0
ntrials = 1;
end
cachedir = opt.cachedir;
EN_NA = 1;
EN_NTSL = 1;
EN_TAISL = 1;
% load source features
source = load( ...
fullfile( ...
cachedir, ...
[dataset '_' sourceName '_' modelType '_features_' featureType] ...
) ...
);
% extract target features
target = load( ...
fullfile( ...
cachedir, ...
[dataset '_' targetName '_' modelType '_features_' featureType] ...
) ...
);
% parse features
for i = 1:nclasses
source.features{i} = cat(4, source.features{i}{:});
target.features{i} = cat(4, target.features{i}{:});
end
% train & test
idxSourceTrials = annotations.train.source;
idxTargetTrials = annotations.test.target;
acc_na = zeros(1, ntrials);
acc_ntsl = zeros(1, ntrials);
acc_taisl = zeros(1, ntrials);
tic;
for i = 1:ntrials
% warning('off') %#ok<WNOFF>
fprintf('.')
idxSource = idxSourceTrials{i};
idxTarget = idxTargetTrials{i};
% prase training and test data from both domain
feats_source = [];
feats_target = [];
labels_source = [];
labels_target = [];
for j = 1:nclasses
% index
idxClassSource = idxSource{j};
idxClassTarget = idxTarget{j};
% features
feats_source = cat( ...
4, ...
feats_source, ...
source.features{j}(:, :, :, idxClassSource) ...
);
feats_target = cat( ...
4, ...
feats_target, ...
target.features{j}(:, :, :, idxClassTarget) ...
);
% labels
labels_source = cat( ...
1, ...
labels_source, ...
source.labels{j}(idxClassSource) ...
);
labels_target = cat( ...
1, ...
labels_target, ...
target.labels{j}(idxClassTarget) ...
);
end
% ----------------------------------------------
% baseline no alignment
% ----------------------------------------------
if EN_NA
[Xs, Xt] = hl_na(feats_source, feats_target, opt);
Ys = labels_source;
Yt = labels_target;
C = learnPredictSVM(Xs, Xt, Ys, Yt);
acc_na(i) = normAcc(Yt, C);
end
% ----------------------------------------------
% tensor subspace learning
% ----------------------------------------------
if EN_NTSL
[Xs, Xt] = hl_ntsl(feats_source, feats_target, opt);
Ys = labels_source;
Yt = labels_target;
C = learnPredictSVM(Xs, Xt, Ys, Yt);
acc_ntsl(i) = normAcc(Yt, C);
end
% ----------------------------------------------
% tensor alignment and subspace learning
% ----------------------------------------------
if EN_TAISL
[Xs, Xt] = hl_taisl(feats_source, feats_target, labels_source, labels_target, opt);
Ys = labels_source;
Yt = labels_target;
C = learnPredictSVM(Xs, Xt, Ys, Yt);
acc_taisl(i) = normAcc(Yt, C);
end
end
time = toc;
fprintf('\nAverage time for each trial is %4.2f\n', time / ntrials)
meanAcc.na = mean(acc_na); stdAcc.na = std(acc_na);
meanAcc.ntsl = mean(acc_ntsl); stdAcc.ntsl = std(acc_ntsl);
meanAcc.taisl = mean(acc_taisl); stdAcc.taisl = std(acc_taisl);