-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathcurves.py
318 lines (265 loc) · 12 KB
/
curves.py
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
import numpy as np
import math
import torch
import torch.nn.functional as F
from torch.nn import Module, Parameter
from torch.nn.modules.utils import _pair
from scipy.special import binom
class Bezier(Module):
def __init__(self, num_bends):
super(Bezier, self).__init__()
self.register_buffer(
'binom',
torch.Tensor(binom(num_bends - 1, np.arange(num_bends), dtype=np.float32))
)
self.register_buffer('range', torch.arange(0, float(num_bends)))
self.register_buffer('rev_range', torch.arange(float(num_bends - 1), -1, -1))
def forward(self, t):
return self.binom * \
torch.pow(t, self.range) * \
torch.pow((1.0 - t), self.rev_range)
class PolyChain(Module):
def __init__(self, num_bends):
super(PolyChain, self).__init__()
self.num_bends = num_bends
self.register_buffer('range', torch.arange(0, float(num_bends)))
def forward(self, t):
t_n = t * (self.num_bends - 1)
return torch.max(self.range.new([0.0]), 1.0 - torch.abs(t_n - self.range))
class CurveModule(Module):
def __init__(self, fix_points, parameter_names=()):
super(CurveModule, self).__init__()
self.fix_points = fix_points
self.num_bends = len(self.fix_points)
self.parameter_names = parameter_names
self.l2 = 0.0
def compute_weights_t(self, coeffs_t):
w_t = [None] * len(self.parameter_names)
self.l2 = 0.0
for i, parameter_name in enumerate(self.parameter_names):
for j, coeff in enumerate(coeffs_t):
parameter = getattr(self, '%s_%d' % (parameter_name, j))
if parameter is not None:
if w_t[i] is None:
w_t[i] = parameter * coeff
else:
w_t[i] += parameter * coeff
if w_t[i] is not None:
self.l2 += torch.sum(w_t[i] ** 2)
return w_t
class Linear(CurveModule):
def __init__(self, in_features, out_features, fix_points, bias=True):
super(Linear, self).__init__(fix_points, ('weight', 'bias'))
self.in_features = in_features
self.out_features = out_features
self.l2 = 0.0
for i, fixed in enumerate(self.fix_points):
self.register_parameter(
'weight_%d' % i,
Parameter(torch.Tensor(out_features, in_features), requires_grad=not fixed)
)
for i, fixed in enumerate(self.fix_points):
if bias:
self.register_parameter(
'bias_%d' % i,
Parameter(torch.Tensor(out_features), requires_grad=not fixed)
)
else:
self.register_parameter('bias_%d' % i, None)
self.reset_parameters()
def reset_parameters(self):
stdv = 1. / math.sqrt(self.in_features)
for i in range(self.num_bends):
getattr(self, 'weight_%d' % i).data.uniform_(-stdv, stdv)
bias = getattr(self, 'bias_%d' % i)
if bias is not None:
bias.data.uniform_(-stdv, stdv)
def forward(self, input, coeffs_t):
weight_t, bias_t = self.compute_weights_t(coeffs_t)
return F.linear(input, weight_t, bias_t)
class Conv2d(CurveModule):
def __init__(self, in_channels, out_channels, kernel_size, fix_points, stride=1,
padding=0, dilation=1, groups=1, bias=True):
super(Conv2d, self).__init__(fix_points, ('weight', 'bias'))
if in_channels % groups != 0:
raise ValueError('in_channels must be divisible by groups')
if out_channels % groups != 0:
raise ValueError('out_channels must be divisible by groups')
kernel_size = _pair(kernel_size)
stride = _pair(stride)
padding = _pair(padding)
dilation = _pair(dilation)
self.in_channels = in_channels
self.out_channels = out_channels
self.kernel_size = kernel_size
self.stride = stride
self.padding = padding
self.dilation = dilation
self.groups = groups
for i, fixed in enumerate(self.fix_points):
self.register_parameter(
'weight_%d' % i,
Parameter(
torch.Tensor(out_channels, in_channels // groups, *kernel_size),
requires_grad=not fixed
)
)
for i, fixed in enumerate(self.fix_points):
if bias:
self.register_parameter(
'bias_%d' % i,
Parameter(torch.Tensor(out_channels), requires_grad=not fixed)
)
else:
self.register_parameter('bias_%d' % i, None)
self.reset_parameters()
def reset_parameters(self):
n = self.in_channels
for k in self.kernel_size:
n *= k
stdv = 1. / math.sqrt(n)
for i in range(self.num_bends):
getattr(self, 'weight_%d' % i).data.uniform_(-stdv, stdv)
bias = getattr(self, 'bias_%d' % i)
if bias is not None:
bias.data.uniform_(-stdv, stdv)
def forward(self, input, coeffs_t):
weight_t, bias_t = self.compute_weights_t(coeffs_t)
return F.conv2d(input, weight_t, bias_t, self.stride,
self.padding, self.dilation, self.groups)
class _BatchNorm(CurveModule):
_version = 2
def __init__(self, num_features, fix_points, eps=1e-5, momentum=0.1, affine=True,
track_running_stats=True):
super(_BatchNorm, self).__init__(fix_points, ('weight', 'bias'))
self.num_features = num_features
self.eps = eps
self.momentum = momentum
self.affine = affine
self.track_running_stats = track_running_stats
self.l2 = 0.0
for i, fixed in enumerate(self.fix_points):
if self.affine:
self.register_parameter(
'weight_%d' % i,
Parameter(torch.Tensor(num_features), requires_grad=not fixed)
)
else:
self.register_parameter('weight_%d' % i, None)
for i, fixed in enumerate(self.fix_points):
if self.affine:
self.register_parameter(
'bias_%d' % i,
Parameter(torch.Tensor(num_features), requires_grad=not fixed)
)
else:
self.register_parameter('bias_%d' % i, None)
if self.track_running_stats:
self.register_buffer('running_mean', torch.zeros(num_features))
self.register_buffer('running_var', torch.ones(num_features))
self.register_buffer('num_batches_tracked', torch.tensor(0, dtype=torch.long))
else:
self.register_parameter('running_mean', None)
self.register_parameter('running_var', None)
self.register_parameter('num_batches_tracked', None)
self.reset_parameters()
def reset_running_stats(self):
if self.track_running_stats:
self.running_mean.zero_()
self.running_var.fill_(1)
self.num_batches_tracked.zero_()
def reset_parameters(self):
self.reset_running_stats()
if self.affine:
for i in range(self.num_bends):
getattr(self, 'weight_%d' % i).data.uniform_()
getattr(self, 'bias_%d' % i).data.zero_()
def _check_input_dim(self, input):
raise NotImplementedError
def forward(self, input, coeffs_t):
self._check_input_dim(input)
exponential_average_factor = 0.0
if self.training and self.track_running_stats:
self.num_batches_tracked += 1
if self.momentum is None: # use cumulative moving average
exponential_average_factor = 1.0 / self.num_batches_tracked.item()
else: # use exponential moving average
exponential_average_factor = self.momentum
weight_t, bias_t = self.compute_weights_t(coeffs_t)
return F.batch_norm(
input, self.running_mean, self.running_var, weight_t, bias_t,
self.training or not self.track_running_stats,
exponential_average_factor, self.eps)
def extra_repr(self):
return '{num_features}, eps={eps}, momentum={momentum}, affine={affine}, ' \
'track_running_stats={track_running_stats}'.format(**self.__dict__)
def _load_from_state_dict(self, state_dict, prefix, metadata, strict,
missing_keys, unexpected_keys, error_msgs):
version = metadata.get('version', None)
if (version is None or version < 2) and self.track_running_stats:
# at version 2: added num_batches_tracked buffer
# this should have a default value of 0
num_batches_tracked_key = prefix + 'num_batches_tracked'
if num_batches_tracked_key not in state_dict:
state_dict[num_batches_tracked_key] = torch.tensor(0, dtype=torch.long)
super(_BatchNorm, self)._load_from_state_dict(
state_dict, prefix, metadata, strict,
missing_keys, unexpected_keys, error_msgs)
class BatchNorm2d(_BatchNorm):
def _check_input_dim(self, input):
if input.dim() != 4:
raise ValueError('expected 4D input (got {}D input)'
.format(input.dim()))
class CurveNet(Module):
def __init__(self, num_classes, curve, architecture, num_bends, fix_start=True, fix_end=True,
architecture_kwargs={}):
super(CurveNet, self).__init__()
self.num_classes = num_classes
self.num_bends = num_bends
self.fix_points = [fix_start] + [False] * (self.num_bends - 2) + [fix_end]
self.curve = curve
self.architecture = architecture
self.l2 = 0.0
self.coeff_layer = self.curve(self.num_bends)
self.net = self.architecture(num_classes, fix_points=self.fix_points, **architecture_kwargs)
self.curve_modules = []
for module in self.net.modules():
if issubclass(module.__class__, CurveModule):
self.curve_modules.append(module)
def import_base_parameters(self, base_model, index):
parameters = list(self.net.parameters())[index::self.num_bends]
base_parameters = base_model.parameters()
for parameter, base_parameter in zip(parameters, base_parameters):
parameter.data.copy_(base_parameter.data)
def import_base_buffers(self, base_model):
for buffer, base_buffer in zip(self.net._all_buffers(), base_model._all_buffers()):
buffer.data.copy_(base_buffer.data)
def export_base_parameters(self, base_model, index):
parameters = list(self.net.parameters())[index::self.num_bends]
base_parameters = base_model.parameters()
for parameter, base_parameter in zip(parameters, base_parameters):
base_parameter.data.copy_(parameter.data)
def init_linear(self):
parameters = list(self.net.parameters())
for i in range(0, len(parameters), self.num_bends):
weights = parameters[i:i+self.num_bends]
for j in range(1, self.num_bends - 1):
alpha = j * 1.0 / (self.num_bends - 1)
weights[j].data.copy_(alpha * weights[-1].data + (1.0 - alpha) * weights[0].data)
def weights(self, t):
coeffs_t = self.coeff_layer(t)
weights = []
for module in self.curve_modules:
weights.extend([w for w in module.compute_weights_t(coeffs_t) if w is not None])
return np.concatenate([w.detach().cpu().numpy().ravel() for w in weights])
def _compute_l2(self):
self.l2 = sum(module.l2 for module in self.curve_modules)
def forward(self, input, t=None):
if t is None:
t = input.data.new(1).uniform_()
coeffs_t = self.coeff_layer(t)
output = self.net(input, coeffs_t)
self._compute_l2()
return output
def l2_regularizer(weight_decay):
return lambda model: 0.5 * weight_decay * model.l2