forked from taconite/PTF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayers.py
621 lines (494 loc) · 18.1 KB
/
layers.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
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
import torch
import torch.nn as nn
# Resnet Blocks
class ResnetBlockFC(nn.Module):
''' Fully connected ResNet Block class.
Args:
size_in (int): input dimension
size_out (int): output dimension
size_h (int): hidden dimension
'''
def __init__(self, size_in, size_out=None, size_h=None):
super().__init__()
# Attributes
if size_out is None:
size_out = size_in
if size_h is None:
size_h = min(size_in, size_out)
self.size_in = size_in
self.size_h = size_h
self.size_out = size_out
# Submodules
self.fc_0 = nn.Linear(size_in, size_h)
self.fc_1 = nn.Linear(size_h, size_out)
self.actvn = nn.ReLU()
if size_in == size_out:
self.shortcut = None
else:
self.shortcut = nn.Linear(size_in, size_out, bias=False)
# Initialization
nn.init.zeros_(self.fc_1.weight)
def forward(self, x):
net = self.fc_0(self.actvn(x))
dx = self.fc_1(self.actvn(net))
if self.shortcut is not None:
x_s = self.shortcut(x)
else:
x_s = x
return x_s + dx
class ResnetBlockGroupConv1d(nn.Module):
''' Fully connected ResNet Block imeplemented with group convolutions.
Args:
size_in (int): input dimension
size_out (int): output dimension
size_h (int): hidden dimension
'''
def __init__(self, size_in, groups, size_out=None, size_h=None):
super().__init__()
# Attributes
if size_out is None:
size_out = size_in
if size_h is None:
size_h = min(size_in, size_out)
self.size_in = size_in
self.size_h = size_h
self.size_out = size_out
# Submodules
self.fc_0 = nn.Conv1d(size_in, size_h, 1, groups=groups)
self.fc_1 = nn.Conv1d(size_h, size_out, 1, groups=groups)
self.actvn = nn.ReLU()
if size_in == size_out:
self.shortcut = None
else:
self.shortcut = nn.Conv1d(size_in, size_out, 1, bias=False, groups=groups)
# Initialization
nn.init.zeros_(self.fc_1.weight)
def forward(self, x):
net = self.fc_0(self.actvn(x))
dx = self.fc_1(self.actvn(net))
if self.shortcut is not None:
x_s = self.shortcut(x)
else:
x_s = x
return x_s + dx
class ResnetBlockGroupNormConv1d(nn.Module):
''' Fully connected ResNet Block imeplemented with group convolutions and group normalizations.
Args:
size_in (int): input dimension
groups (int): number of groups for group convolutions
gn_groups (int): number of groups for group normalizations
size_out (int): output dimension
size_h (int): hidden dimension
'''
def __init__(self, size_in, groups, gn_groups=4, size_out=None, size_h=None, dropout_prob=0.0, leaky=False):
super().__init__()
# Attributes
if size_out is None:
size_out = size_in
if size_h is None:
size_h = min(size_in, size_out)
if dropout_prob > 0.0:
self.dropout = nn.Dropout(dropout_prob, inplace=True)
else:
self.dropout = None
self.size_in = size_in
self.size_h = size_h
self.size_out = size_out
# Submodules
self.gn_0 = GroupNorm1d(groups * gn_groups, size_in)
self.gn_1 = GroupNorm1d(groups * gn_groups, size_h)
self.fc_0 = nn.Conv1d(size_in, size_h, 1, groups=groups, bias=False)
self.fc_1 = nn.Conv1d(size_h, size_out, 1, groups=groups, bias=False)
if not leaky:
self.actvn = nn.ReLU()
else:
self.actvn = nn.LeakyReLU(0.1)
if size_in == size_out:
self.shortcut = None
else:
self.shortcut = nn.Conv1d(size_in, size_out, 1, bias=False, groups=groups)
# Initialization
nn.init.zeros_(self.fc_1.weight)
def forward(self, x):
if self.dropout is not None:
net = self.fc_0(self.dropout(self.actvn(self.gn_0(x))))
dx = self.fc_1(self.dropout(self.actvn(self.gn_1(net))))
else:
net = self.fc_0(self.actvn(self.gn_0(x)))
dx = self.fc_1(self.actvn(self.gn_1(net)))
if self.shortcut is not None:
x_s = self.shortcut(x)
else:
x_s = x
return x_s + dx
class ResnetBlockGroupNormShallowConv1d(nn.Module):
''' Fully connected ResNet Block imeplemented with group convolutions and group normalizations.
Args:
size_in (int): input dimension
groups (int): number of groups for group convolutions
gn_groups (int): number of groups for group normalizations
size_out (int): output dimension
size_h (int): hidden dimension
'''
def __init__(self, size_in, groups, gn_groups=4, size_out=None, size_h=None, dropout_prob=0.0, leaky=False):
super().__init__()
# Attributes
if size_out is None:
size_out = size_in
if size_h is None:
size_h = min(size_in, size_out)
if dropout_prob > 0.0:
self.dropout = nn.Dropout(dropout_prob, inplace=True)
else:
self.dropout = None
self.size_in = size_in
self.size_h = size_h
self.size_out = size_out
# Submodules
self.gn_0 = GroupNorm1d(groups * gn_groups, size_in)
self.fc_0 = nn.Conv1d(size_in, size_h, 1, groups=groups, bias=False)
if not leaky:
self.actvn = nn.ReLU()
else:
self.actvn = nn.LeakyReLU(0.1)
if size_in == size_out:
self.shortcut = None
else:
self.shortcut = nn.Conv1d(size_in, size_out, 1, bias=False, groups=groups)
def forward(self, x):
if self.dropout is not None:
dx = self.fc_0(self.dropout(self.actvn(self.gn_0(x))))
else:
dx = self.fc_0(self.actvn(self.gn_0(x)))
if self.shortcut is not None:
x_s = self.shortcut(x)
else:
x_s = x
return x_s + dx
class ResnetBlockInplaceNormShallowConv1d(nn.Module):
''' Fully connected ResNet Block imeplemented with group convolutions and weight/spectral normalizations.
Args:
size_in (int): input dimension
groups (int): number of groups for group convolutions
size_out (int): output dimension
size_h (int): hidden dimension
'''
def __init__(self, size_in, groups, norm_method='weight_norm', size_out=None, size_h=None, dropout_prob=0.0, leaky=False):
super().__init__()
# Attributes
if size_out is None:
size_out = size_in
if size_h is None:
size_h = min(size_in, size_out)
if dropout_prob > 0.0:
self.dropout = nn.Dropout(dropout_prob, inplace=True)
else:
self.dropout = None
self.size_in = size_in
self.size_h = size_h
self.size_out = size_out
fc_0 = nn.Conv1d(size_in, size_h, 1, groups=groups, bias=False)
if norm_method == 'weight_norm':
self.fc_0 = nn.utils.weight_norm(fc_0)
elif norm_method == 'spectral_norm':
self.fc_0 = nn.utils.spectral_norm(fc_0)
else:
raise ValueError('Normalization method {} not supported.'.format(norm_method))
if not leaky:
self.actvn = nn.ReLU()
else:
self.actvn = nn.LeakyReLU(0.1)
if size_in == size_out:
self.shortcut = None
else:
self.shortcut = nn.Conv1d(size_in, size_out, 1, bias=False, groups=groups)
def forward(self, x):
if self.dropout is not None:
dx = self.fc_0(self.dropout(self.actvn(x)))
else:
dx = self.fc_0(self.actvn(x))
if self.shortcut is not None:
x_s = self.shortcut(x)
else:
x_s = x
return x_s + dx
class ResnetBlockGroupBatchNormConv1d(nn.Module):
''' Fully connected ResNet Block imeplemented with group convolutions and group batch normalizations.
Args:
size_in (int): input dimension
groups (int): number of groups for group convolutions
size_out (int): output dimension
size_h (int): hidden dimension
'''
def __init__(self, size_in, groups, size_out=None, size_h=None):
super().__init__()
# Attributes
if size_out is None:
size_out = size_in
if size_h is None:
size_h = min(size_in, size_out)
self.size_in = size_in
self.size_h = size_h
self.size_out = size_out
# Submodules
self.bn_0 = GBatchNorm1d(size_in, groups)
self.bn_1 = GBatchNorm1d(size_h, groups)
self.fc_0 = nn.Conv1d(size_in, size_h, 1, groups=groups, bias=False)
self.fc_1 = nn.Conv1d(size_h, size_out, 1, groups=groups, bias=False)
self.actvn = nn.ReLU()
if size_in == size_out:
self.shortcut = None
else:
self.shortcut = nn.Conv1d(size_in, size_out, 1, bias=False, groups=groups)
# Initialization
nn.init.zeros_(self.fc_1.weight)
def forward(self, x):
net = self.fc_0(self.actvn(self.bn_0(x)))
dx = self.fc_1(self.actvn(self.bn_1(net)))
if self.shortcut is not None:
x_s = self.shortcut(x)
else:
x_s = x
return x_s + dx
class CResnetBlockConv1d(nn.Module):
''' Conditional batch normalization-based Resnet block class.
Args:
c_dim (int): dimension of latend conditioned code c
size_in (int): input dimension
size_out (int): output dimension
size_h (int): hidden dimension
norm_method (str): normalization method
legacy (bool): whether to use legacy blocks
'''
def __init__(self, c_dim, size_in, size_h=None, size_out=None,
norm_method='batch_norm', legacy=False):
super().__init__()
# Attributes
if size_h is None:
size_h = size_in
if size_out is None:
size_out = size_in
self.size_in = size_in
self.size_h = size_h
self.size_out = size_out
# Submodules
if not legacy:
self.bn_0 = CBatchNorm1d(
c_dim, size_in, norm_method=norm_method)
self.bn_1 = CBatchNorm1d(
c_dim, size_h, norm_method=norm_method)
else:
self.bn_0 = CBatchNorm1d_legacy(
c_dim, size_in, norm_method=norm_method)
self.bn_1 = CBatchNorm1d_legacy(
c_dim, size_h, norm_method=norm_method)
self.fc_0 = nn.Conv1d(size_in, size_h, 1)
self.fc_1 = nn.Conv1d(size_h, size_out, 1)
self.actvn = nn.ReLU()
if size_in == size_out:
self.shortcut = None
else:
self.shortcut = nn.Conv1d(size_in, size_out, 1, bias=False)
# Initialization
nn.init.zeros_(self.fc_1.weight)
def forward(self, x, c):
net = self.fc_0(self.actvn(self.bn_0(x, c)))
dx = self.fc_1(self.actvn(self.bn_1(net, c)))
if self.shortcut is not None:
x_s = self.shortcut(x)
else:
x_s = x
return x_s + dx
class ResnetBlockConv1d(nn.Module):
''' 1D-Convolutional ResNet block class.
Args:
size_in (int): input dimension
size_out (int): output dimension
size_h (int): hidden dimension
'''
def __init__(self, size_in, size_h=None, size_out=None):
super().__init__()
# Attributes
if size_h is None:
size_h = size_in
if size_out is None:
size_out = size_in
self.size_in = size_in
self.size_h = size_h
self.size_out = size_out
# Submodules
self.bn_0 = nn.BatchNorm1d(size_in)
self.bn_1 = nn.BatchNorm1d(size_h)
self.fc_0 = nn.Conv1d(size_in, size_h, 1)
self.fc_1 = nn.Conv1d(size_h, size_out, 1)
self.actvn = nn.ReLU()
if size_in == size_out:
self.shortcut = None
else:
self.shortcut = nn.Conv1d(size_in, size_out, 1, bias=False)
# Initialization
nn.init.zeros_(self.fc_1.weight)
def forward(self, x):
net = self.fc_0(self.actvn(self.bn_0(x)))
dx = self.fc_1(self.actvn(self.bn_1(net)))
if self.shortcut is not None:
x_s = self.shortcut(x)
else:
x_s = x
return x_s + dx
# Utility modules
class AffineLayer(nn.Module):
''' Affine layer class.
Args:
c_dim (tensor): dimension of latent conditioned code c
dim (int): input dimension
'''
def __init__(self, c_dim, dim=3):
super().__init__()
self.c_dim = c_dim
self.dim = dim
# Submodules
self.fc_A = nn.Linear(c_dim, dim * dim)
self.fc_b = nn.Linear(c_dim, dim)
self.reset_parameters()
def reset_parameters(self):
nn.init.zeros_(self.fc_A.weight)
nn.init.zeros_(self.fc_b.weight)
with torch.no_grad():
self.fc_A.bias.copy_(torch.eye(3).view(-1))
self.fc_b.bias.copy_(torch.tensor([0., 0., 2.]))
def forward(self, x, p):
assert(x.size(0) == p.size(0))
assert(p.size(2) == self.dim)
batch_size = x.size(0)
A = self.fc_A(x).view(batch_size, 3, 3)
b = self.fc_b(x).view(batch_size, 1, 3)
out = p @ A + b
return out
class CBatchNorm1d(nn.Module):
''' Conditional batch normalization layer class.
Args:
c_dim (int): dimension of latent conditioned code c
f_dim (int): feature dimension
norm_method (str): normalization method
'''
def __init__(self, c_dim, f_dim, norm_method='batch_norm'):
super().__init__()
self.c_dim = c_dim
self.f_dim = f_dim
self.norm_method = norm_method
# Submodules
self.conv_gamma = nn.Conv1d(c_dim, f_dim, 1)
self.conv_beta = nn.Conv1d(c_dim, f_dim, 1)
if norm_method == 'batch_norm':
self.bn = nn.BatchNorm1d(f_dim, affine=False)
elif norm_method == 'instance_norm':
self.bn = nn.InstanceNorm1d(f_dim, affine=False)
elif norm_method == 'group_norm':
self.bn = nn.GroupNorm1d(f_dim, affine=False)
else:
raise ValueError('Invalid normalization method!')
self.reset_parameters()
def reset_parameters(self):
nn.init.zeros_(self.conv_gamma.weight)
nn.init.zeros_(self.conv_beta.weight)
nn.init.ones_(self.conv_gamma.bias)
nn.init.zeros_(self.conv_beta.bias)
def forward(self, x, c):
assert(x.size(0) == c.size(0))
assert(c.size(1) == self.c_dim)
# c is assumed to be of size batch_size x c_dim x T
if len(c.size()) == 2:
c = c.unsqueeze(2)
# Affine mapping
gamma = self.conv_gamma(c)
beta = self.conv_beta(c)
# Batchnorm
net = self.bn(x)
out = gamma * net + beta
return out
class GBatchNorm1d(nn.Module):
''' Group batch normalization layer class.
Args:
f_dim (int): feature dimension
'''
def __init__(self, f_dim, groups):
super().__init__()
self.f_dim = f_dim
self.groups = groups
assert (f_dim % groups == 0)
# Submodules
bn = [nn.BatchNorm1d(f_dim // groups) for _ in range(groups)]
self.bn = nn.ModuleList(bn)
def forward(self, x):
net = torch.split(x, self.f_dim // self.groups, 1)
out = torch.cat([self.bn[idx](net[idx]) for idx in range(len(self.bn))], dim=1)
return out
class GroupNorm1d(nn.Module):
''' Group normalization that does per-point group normalization.
Args:
groups (int): number of groups
f_dim (int): feature dimension, mush be divisible by groups
'''
def __init__(self, groups, f_dim, eps=1e-5, affine=True):
super().__init__()
self.groups = groups
self.f_dim = f_dim
self.affine = affine
self.eps = eps
assert (f_dim % groups == 0)
# Affine parameters
if affine:
self.gamma = nn.Parameter(torch.ones(1, f_dim, 1))
self.beta = nn.Parameter(torch.zeros(1, f_dim, 1))
def forward(self, x):
batch_size, D, T = x.size()
net = x.view(batch_size, self.groups, D // self.groups, T)
means = net.mean(2, keepdim=True)
variances = net.var(2, keepdim=True)
net = (net - means) / (variances + self.eps).sqrt()
net = net.view(batch_size, D, T)
if self.affine:
return net * self.gamma + self.beta
else:
return net
class CBatchNorm1d_legacy(nn.Module):
''' Conditional batch normalization legacy layer class.
Args:
c_dim (int): dimension of latent conditioned code c
f_dim (int): feature dimension
norm_method (str): normalization method
'''
def __init__(self, c_dim, f_dim, norm_method='batch_norm'):
super().__init__()
self.c_dim = c_dim
self.f_dim = f_dim
self.norm_method = norm_method
# Submodules
self.fc_gamma = nn.Linear(c_dim, f_dim)
self.fc_beta = nn.Linear(c_dim, f_dim)
if norm_method == 'batch_norm':
self.bn = nn.BatchNorm1d(f_dim, affine=False)
elif norm_method == 'instance_norm':
self.bn = nn.InstanceNorm1d(f_dim, affine=False)
elif norm_method == 'group_norm':
self.bn = nn.GroupNorm1d(f_dim, affine=False)
else:
raise ValueError('Invalid normalization method!')
self.reset_parameters()
def reset_parameters(self):
nn.init.zeros_(self.fc_gamma.weight)
nn.init.zeros_(self.fc_beta.weight)
nn.init.ones_(self.fc_gamma.bias)
nn.init.zeros_(self.fc_beta.bias)
def forward(self, x, c):
batch_size = x.size(0)
# Affine mapping
gamma = self.fc_gamma(c)
beta = self.fc_beta(c)
gamma = gamma.view(batch_size, self.f_dim, 1)
beta = beta.view(batch_size, self.f_dim, 1)
# Batchnorm
net = self.bn(x)
out = gamma * net + beta
return out