-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_expret_networks_autoendoer_hard_gate.m
153 lines (118 loc) · 4.47 KB
/
test_expret_networks_autoendoer_hard_gate.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
%@Rahaf Aljundi
%demonstrates expert gate system on a sequence of six tasks
%activates one task only
%nets_path: path to each task expert network
%imdbs: path to each task dataset
%encoders_imdbs_path: path to each of the autoencoders trained on each
%previous task
function [accuracy] = test_expret_networks_autoendoer_hard_gate(nets_path,imdbs,encoders_path,output,encoders_imdbs_path)
% setup MatConvNet
cd ..
run matlab/vl_setupnn
if(~exist('th','var'))
th=0.1;
end
T=2;
% load the pre-trained CNN
for(i=1:numel(nets_path))
load(nets_path{i}) ;
net.layers{end}.type='softmax';
nets{i}=net;
clear net
end
%load the data set
if(~exist('encoders_imdbs_path','var'))
input_net=load('/users/visics/raljundi/Code/MyOwnCode/MatConvNet/imagenet-caffe-alex.mat');
input_size= 43264;
output_layer_id= 15;
else
for i=1:numel(encoders_imdbs_path)
encoders_imdbs{i}=load(encoders_imdbs_path{i});
end
end
%load the standarization statistics
load('autoencoder/imagenet_mean.mat');
load('autoencoder/imagenet_std.mat');
%initialization
imdb.images.data=[];
imdb.images.set=[];
imdb.images.labels=[];
%the labels should change. each dataset labels starts from the last label of the previous dataset
init_lb=0;
%no augmentations for test
opts.numAugments=1;
opts.transformation='none';
all_val=[];
%load the encoders
for(i=1:numel(encoders_path))
encoders{i}=load(encoders_path{i})
if(isfield( encoders{i},'net'))
encoders{i}=encoders{i}.net;
end
error_rate{i}=[];
success_rate{i}=[];
encoders{i}.layers{end} = struct('type', 'euclideanloss');
end
%start the test
for imdb_ind=1:numel(imdbs)
this_imdb=load(imdbs{imdb_ind});
this_imdb.images.labels=this_imdb.images.labels+init_lb;
numOfClasses=numel(unique(this_imdb.images.labels));
inds=find(this_imdb.images.set==3); %get test images
if(ndims(this_imdb.images.data)==4)
im_links= this_imdb.images.data(:,:,:,inds);
else
im_links= this_imdb.images.data(inds);
end
accuracy=0;
for i=1:numel(im_links)
%get images
image = cnn_imagenet_get_batch(im_links(i),opts);
origin_res = vl_simplenn_autoencoder(input_net, image) ;
input=origin_res(output_layer_id);
%reshape%
input=reshape(input.x,1,1,input_size,[]);
%------standarization----------
input = bsxfun(@minus, input, imagenet_mean);
input = bsxfun(@rdivide, input,imagenet_std);
%--------------------------------------
input=sigmoid(input);
%---
for enc_ind=1:numel(encoders)
this_input=input;
this_input = gpuArray(this_input);
encoders{enc_ind}.layers{end}.class = this_input;
res = vl_simplenn_autoencoder(encoders{enc_ind}, this_input, [], [], 'disableDropout', true);
reconstruction_err(enc_ind) = gather(res(end).x);
end
%pass to the softmax
reconstruction_err1=-reconstruction_err;
reconstruction_err1=reshape(reconstruction_err1,1,1,numel(imdbs),1);
soft_rec=tempy(reconstruction_err1,T);
soft_rec=reshape(soft_rec,1,numel(imdbs));
all_val=[all_val;soft_rec reconstruction_err];
[x,min_ind]=max(soft_rec);
%if correctly predicted task pass it to the expert model
if((min_ind==imdb_ind ))
%---------------------------------------------------------------------------------------
smopts.disableDropout=1;
for j=1:size(image,4)
res = vl_simplenn(nets{imdb_ind}, image(:,:,:,j),[], [],smopts) ;
% show the classification result
scores = squeeze(gather(res(end).x)) ;
dec=zeros(numel(scores),1);
[bestScore, best] = max(scores) ;
dec(best)=dec(best)+1;
end
[votes,best]=max(dec);
predicted{i}=best;
if(best==labels(i))
accuracy=accuracy+1;
end
end
% fprintf('the predicted labels is %g the actual label is %g \n',best, labels(i));
end
accuracy= (accuracy*100)/numel(im_links);
acc{imdb_ind}=accuracy;
end
save(strcat('results/',output,'_accuracy'),'acc');