-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlayers.py
More file actions
366 lines (286 loc) · 14.5 KB
/
layers.py
File metadata and controls
366 lines (286 loc) · 14.5 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import torch
import numpy as np
import torch.nn as nn
import patterns
class PatternConv2d(nn.Module):
def __init__(self, conv_layer):
super(PatternConv2d, self).__init__()
if conv_layer.dilation != (1,1):
def dilation_mask(kernel_size, dilation):
mask = torch.zeros(kernel_size[0], kernel_size[1],
kernel_size[2]+(dilation[0]-1)*(kernel_size[2]-1),
kernel_size[3]+(dilation[1]-1)*(kernel_size[3]-1))
locs_x = np.arange(0, mask.shape[2], dilation[0])
locs_y = np.arange(0, mask.shape[3], dilation[1])
inds_x, inds_y = np.meshgrid(locs_x, locs_y)
mask[:,:,inds_x, inds_y] = 1
return mask
self.dil_mask = lambda ks: dilation_mask(ks, conv_layer.dilation)
self.forward_layer = conv_layer # kernels size of forward layer:
# self.forward_layer.kernel_size
padding_f = np.array(conv_layer.padding)
ks = np.array(self.forward_layer.kernel_size)
padding_b = tuple(-padding_f + ks - 1)
if conv_layer.dilation != (1,1):
ks_dil = np.array(self.dil_mask(conv_layer.weight.shape).shape[-2:])
padding_b = tuple(-padding_f + ks_dil - 1)
self.backward_layer = nn.Conv2d(
conv_layer.out_channels,
conv_layer.in_channels,
ks,
stride=conv_layer.stride,
dilation=conv_layer.dilation,
padding=padding_b,
bias=False,
)
self.statistics = None
self.patterns = None
def forward(self, input):
''' perform forward computations of forward_layer, return not only new
output, but also output without bias, if the forward layer has a
bias parameter.
'''
def expand_bias(bias, size):
new_tensor = torch.zeros((size))
for i in range(bias.shape[0]):
new_tensor[:, i, :, :] = bias[i]
return new_tensor
output = self.forward_layer(input)
# what if the layer does not have a bias?
if self.forward_layer.bias is None:
return output
bias = expand_bias(self.forward_layer.bias.data, output.data.shape)
output_wo_bias = output - bias
return output, output_wo_bias
def backward(self, input, normalize_output=True):
''' compute a backward step (for signal computation).
'''
output = self.backward_layer(input)
# if the dilation is not none the output has to be
# dilated to the original input size
# if self.forward_layer.dilation != (1,1):
# output_mask = self.dil_mask(output.shape)
# print('Dilaten output shape:',output_mask.shape, 'Current output shape:', output.shape)
# output_dilated = torch.zeros(output_mask.shape)
# output_dilated[output_mask == 1] = torch.flatten(output)
# output = output_dilated
if normalize_output:
# rescale output to be between -1 and 1
absmax = torch.abs(output.data).max()
if absmax > 0.000001:
output.data /= absmax
output.data[output.data > 1] = 1
output.data[output.data < -1] = -1
return output
def compute_statistics(self, input, output, output_wo_bias=None):
''' compute statistics for this layer given the input, output and
output without bias. Initialize statistics if none there yet,
otherwise update statistics with new values.
If the forward layer does not use a bias term, then the output
without bias, i.e. the layer's output, is in output and there is
no tensor in output_wo_bias.
'''
kernel_size = self.forward_layer.kernel_size
if self.forward_layer.dilation != (1,1):
dilation = self.forward_layer.dilation
kernel_size = tuple((kernel_size[0]+(dilation[0]-1)*(kernel_size[0]-1),
kernel_size[1]+(dilation[1]-1)*(kernel_size[1]-1)))
# print(output)
if output_wo_bias is None:
inp_dense, out_dense = patterns._conv_maps_to_dense(input, output,
kernel_size)
if self.forward_layer.dilation != (1,1):
inp_mask = torch.flatten(self.dil_mask(self.forward_layer.weight.shape)[0])
inp_dense = inp_dense[:, inp_mask==1]
if self.statistics is None:
self.statistics = patterns.compute_statistics(inp_dense,
out_dense,
out_dense)
else:
self.statistics = patterns.update_statistics(inp_dense,
out_dense,
out_dense,
self.statistics)
else:
inp_dense, out_wo_bias_dense = patterns._conv_maps_to_dense(input,
output_wo_bias,
kernel_size)
_, out_dense = patterns._conv_maps_to_dense(input, output,
kernel_size)
if self.forward_layer.dilation != (1,1):
inp_mask = torch.flatten(self.dil_mask(self.forward_layer.weight.shape)[0])
inp_dense = inp_dense[:, inp_mask==1]
if self.statistics is None:
self.statistics = patterns.compute_statistics(inp_dense,
out_wo_bias_dense,
out_dense)
else:
self.statistics = patterns.update_statistics(inp_dense,
out_wo_bias_dense,
out_dense,
self.statistics)
def compute_patterns(self):
''' Compute patterns from the computed statistics.
'''
kernel = self.forward_layer.weight.data
self.patterns = patterns.compute_patterns_conv(self.statistics,
kernel)
def set_patterns(self, pattern_type='relu'):
''' Sets the computed patterns as the kernel of the backward layer.
pattern_type can be 'relu' or 'linear'
'''
if pattern_type == 'relu':
self.backward_layer.parameters().__next__().data = self.patterns['A_plus']
elif pattern_type == 'linear':
self.backward_layer.parameters().__next__().data = self.patterns['A_linear']
class PatternLinear(nn.Module):
def __init__(self, linear_layer):
super(PatternLinear, self).__init__()
self.forward_layer = linear_layer
self.backward_layer = nn.Linear(linear_layer.out_features,
linear_layer.in_features,
bias=False)
self.statistics = None
self.patterns = None
def forward(self, input):
''' perform forward computations of forward_layer, return not only new
output, but also output without bias, if the forward layer has a
bias parameter.
'''
def expand_bias(bias, size):
new_tensor = torch.zeros((size))
for i in range(bias.shape[0]):
new_tensor[:, i] = bias[i]
return new_tensor
output = self.forward_layer(input)
# TODO: what if the layer does not have a bias?
if self.forward_layer.bias is None:
return output
bias = expand_bias(self.forward_layer.bias.data, output.data.shape)
output_wo_bias = output - bias
return output, output_wo_bias
def backward(self, input, normalize_output=True):
''' compute a backward step (for signal computation).
'''
output = self.backward_layer(input)
if normalize_output:
# rescale output to be between -1 and 1
absmax = torch.abs(output.data).max()
if absmax > 0.000001:
output.data /= absmax
output.data[output.data > 1] = 1
output.data[output.data < -1] = -1
return output
def compute_statistics(self, input, output, output_wo_bias=None):
''' compute statistics for this layer given the input, output and
output without bias. Initialize statistics if none there yet,
otherwise update statistics with new values.
If the forward layer does not use a bias term, then the output
without bias, i.e. the layer's output, is in output and there is
no tensor in output_wo_bias.
'''
if output_wo_bias is None:
if self.statistics is None:
self.statistics = patterns.compute_statistics(input,
output,
output)
else:
self.statistics = patterns.update_statistics(input,
output,
output,
self.statistics)
else:
if self.statistics is None:
self.statistics = patterns.compute_statistics(input,
output_wo_bias,
output)
else:
self.statistics = patterns.update_statistics(input,
output_wo_bias,
output,
self.statistics)
def compute_patterns(self):
''' Compute patterns from the computed statistics.
'''
w = self.forward_layer.weight.data
self.patterns = patterns.compute_patterns_linear(self.statistics, w)
def set_patterns(self, pattern_type='relu'):
''' Sets the computed patterns as the kernel of the backward layer.
pattern_type can be 'relu' or 'linear'
'''
if pattern_type == 'relu':
# self.backward_layer.weight.data = self.patterns['A_plus'].permute(1,0)
self.backward_layer.parameters().__next__().data = self.patterns['A_plus'].permute(1,0)
elif pattern_type == 'linear':
self.backward_layer.parameters().__next__().data = self.patterns['A_linear'].permute(1,0)
class PatternReLU(nn.Module):
def __init__(self):
super(PatternReLU, self).__init__()
self.forward_layer = nn.ReLU()
def forward(self, input):
indices = input <= 0
output = self.forward_layer(input)
return output, indices
def backward(self, input, indices):
# copy the input
input = input.clone().detach()
input[indices] = 0
return input
class PatternMaxPool2d(nn.Module):
def __init__(self, pool_layer):
super(PatternMaxPool2d, self).__init__()
# create a new pooling layer to use a different instance with
# return_indices=True without changing the original layer's
# settings
self.forward_layer = nn.MaxPool2d(pool_layer.kernel_size,
pool_layer.stride,
pool_layer.padding,
pool_layer.dilation,
return_indices=True)
# kernel_size[0]+(dilation[0]-1)*(kernel_size[0]-1)
ks = np.array(pool_layer.kernel_size)
dil = np.array(pool_layer.dilation)
ks_dil = ks + (dil - 1) * (ks - 1)
self.backward_layer = nn.MaxUnpool2d(ks_dil,
pool_layer.stride,
pool_layer.padding)
def forward(self, input):
return self.forward_layer(input)
def backward(self, input, switches, output_size=None):
if output_size is None:
return self.backward_layer(input, switches)
else:
return self.backward_layer(input, switches, output_size=output_size)
class PatternBatchNorm2d(torch.nn.Module):
def __init__(self, bn_layer):
super(PatternBatchNorm2d, self).__init__()
self.forward_layer = bn_layer
# TODO: use running mean and running var if existant
# else need to compute new mean and var for each call
if bn_layer.track_running_stats:
self.mean = bn_layer.running_mean
self.var = bn_layer.running_var
else:
self.mean = None
self.var = None
def forward(self, inp):
# TODO: check if my own computation (if no running stats) is really the same as from the layer
# --> did not check, but backward is just reversed computation and leads to same result as
# what's passed to the layer (up to 10^-5)
if self.forward_layer.track_running_stats:
# print('Using original layer for forward pass')
return self.forward_layer(inp)
else:
self.mean = torch.mean(inp, dim=(0,2,3), keepdim=True)
self.var = torch.var(inp, dim=(0,2,3), unbiased=False, keepdim=True)
return (inp - self.mean) / (torch.sqrt(self.var + self.forward_layer.eps)) * \
self.forward_layer.weight.data + self.forward_layer.bias.data
def backward(self, out):
# forward computation: (x - mean) /(sqrt(var + eps))*gamma + beta
# --> backward computation: (out - beta) / gamma * sqrt(var+eps) + mean
tmp = out - self.forward_layer.bias.data[None,:,None,None]
tmp /= self.forward_layer.weight.data[None,:,None,None]
tmp *= torch.sqrt(self.var + self.forward_layer.eps)[None,:,None,None]
tmp += self.mean[None,:,None,None]
result = tmp
return result