forked from tsungyu/o2dp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
initializeNetFromEncoder.m
182 lines (143 loc) · 5.86 KB
/
initializeNetFromEncoder.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
function net = initializeNetFromEncoder(encoder)
% -------------------------------------------------------------------------
if isfield(encoder, 'netb')
net = buildDagNN(encoder);
else
if isfield(encoder, 'layera')
net = addBilinearSimpleNN(encoder);
else
if isa(encoder.neta, 'dagnn.DagNN')
idx = find(arrayfun(@(x) isa(x.block, 'BilinearClPooling') || isa(x.block, 'BilinearPooling'), encoder.neta.layers), 1);
assert(~isempty(idx), 'no bilinear layer in the network')
net = encoder.neta;
else
idx = find(cellfun(@(x) sum(strcmp(x.type, {'bilinearpool', 'bilinearclpool'})), encoder.neta.layers), 1);
assert(~isempty(idx), 'no bilinear layer in the network')
net = encoder.neta;
end
end
end
if isa(net, 'dagnn.DagNN')
net.mode = 'test';
net.vars(net.getVarIndex('l_1')).precious = 1;
end
%{
assert(~isempty(encoderOpts.modela) && ~isempty(encoderOpts.modelb), 'Error: at least one of the network is not specified')
% load the pre-trained models
encoder.neta = load(encoderOpts.modela);
encoder.neta.layers = encoder.neta.layers(1:encoderOpts.layera);
encoder.netb = load(encoderOpts.modelb);
encoder.netb.layers = encoder.netb.layers(1:encoderOpts.layerb);
encoder.regionBorder = 0.05;
encoder.type = 'bcnn';
encoder.normalization = 'sqrt_L2';
% move models to GPU
if ~isempty(opts.useGpu)
encoder.neta = vl_simplenn_move(encoder.neta, 'gpu') ;
encoder.netb = vl_simplenn_move(encoder.netb, 'gpu') ;
else
encoder.neta = vl_simplenn_move(encoder.neta, 'cpu') ;
encoder.netb = vl_simplenn_move(encoder.netb, 'cpu') ;
end
for l=numel(encoder.neta.layers):-1:1
if strcmp(encoder.neta.layers{l}.type, 'conv')
encoder.neta.layers{l}.opts = {'CudnnWorkspaceLimit', opts.cudnnWorkspaceLimit};
end
end
for l=numel(encoder.netb.layers):-1:1
if strcmp(encoder.netb.layers{l}.type, 'conv')
encoder.netb.layers{l}.opts = {'CudnnWorkspaceLimit', opts.cudnnWorkspaceLimit};
end
end
%}
function net = buildDagNN(encoder)
% transform network to dagnn
% ------------------------------------------------------------------------------------
net = dagnn.DagNN();
net = net.fromSimpleNN(encoder.neta, 'CanonicalNames', true);
meta.meta1 = encoder.neta.meta;
netb = dagnn.DagNN();
netb = netb.fromSimpleNN(encoder.netb, 'CanonicalNames', true);
meta.meta2 = encoder.netb.meta;
net.meta = meta;
for i=1:numel(netb.layers)
layerName = strcat('netb_', netb.layers(i).name);
input = strcat('netb_', netb.layers(i).inputs);
output = strcat('netb_', netb.layers(i).outputs);
params = strcat('netb_', netb.layers(i).params);
% net.layers(end+1) = netb.layers(i);
net.addLayer(layerName, netb.layers(i).block, input, output, params);
for f = 1:numel(params)
varId = net.getParamIndex(params{f});
varIdb = netb.getParamIndex(netb.layers(i).params{f});
if strcmp(net.device, 'gpu')
net.params(varId).value = gpuArray(netb.params(varIdb).value);
else
net.params(varId).value = netb.params(varIdb).value;
end
end
end
clear netb
% ------------------------------------------------------------------------------------
% Add bilinearpool layer
bp_layer = {encoder.neta.layers{end}.name, strcat('netb_', encoder.netb.layers{end}.name)};
inputLayerIndex = net.getLayerIndex(bp_layer);
in1 = net.layers(inputLayerIndex(1)).outputs;
assert(length(in1) == 1);
in2 = net.layers(inputLayerIndex(2)).outputs;
assert(length(in2) == 1);
input = cat(2, in1, in2);
layerName = 'bilr_1';
output = 'b_1';
net.addLayer(layerName, BilinearClPooling('normalizeGradients', false), ...
input, output);
% Square-root layer
layerName = sprintf('sqrt_1');
input = output;
output = 's_1';
net.addLayer(layerName, SquareRoot(), {input}, output);
% L2 normalization layer
layerName = 'l2_1';
input = output;
bpoutput = 'l_1';
net.addLayer(layerName, L2Norm(), {input}, bpoutput);
% ------------------------------------------------------------------------------------
% % Rename classes
% net.meta.meta1.classes.name = imdb.classes.name;
% net.meta.meta1.classes.description = imdb.classes.name;
% net.meta.meta2.classes.name = imdb.classes.name;
% net.meta.meta2.classes.description = imdb.classes.name;
% add give border for translation data jittering
% if(~strcmp(opts.dataAugmentation{1}, 'f2') && ~strcmp(opts.dataAugmentation{1}, 'none'))
% net.meta.meta1.normalization.border = 256 - net.meta.meta1.normalization.imageSize(1:2) ;
% net.meta.meta2.normalization.border = 256 - net.meta.meta2.normalization.imageSize(1:2) ;
% end
function net = addBilinearSimpleNN(encoder)
% -------------------------------------------------------------------------
%{
% network setting
net = vl_simplenn_tidy(net) ;
for l=numel(net.layers):-1:1
if strcmp(net.layers{l}.type, 'conv')
net.layers{l}.opts = {'CudnnWorkspaceLimit', opts.cudnnWorkspaceLimit};
end
end
%}
net = encoder.neta;
% stack bilinearpool layer
if isfield(encoder, 'layera') && ~isfield(encoder, 'layerb')
net.layers{end+1} = struct('type', 'bilinearpool', 'name', 'blp');
else
assert(isfield(encoder, 'layera') && isfield(encoder, 'layera'), 'Specify both layera and layerb for cross layer bcnn with shared parameters')
net.layers{end+1} = struct('type', 'bilinearclpool', 'layer1', encoder.layera, 'layer2', encoder.layerb, 'name', 'blcp');
end
% stack normalization
net.layers{end+1} = struct('type', 'sqrt', 'name', 'sqrt_norm');
net.layers{end+1} = struct('type', 'l2norm', 'name', 'l2_norm');
% % Rename classes
% net.meta.classes.name = imdb.classes.name;
% net.meta.classes.description = imdb.classes.name;
% add border for translation data jittering
% if(~strcmp(opts.dataAugmentation{1}, 'f2') && ~strcmp(opts.dataAugmentation{1}, 'none'))
% net.meta.normalization.border = 256 - net.meta.normalization.imageSize(1:2) ;
% end