-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdisc_resblocks.py
82 lines (62 loc) · 2.43 KB
/
disc_resblocks.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
import math
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn import init
from torch.nn import utils
class Block(nn.Module):
def __init__(self, in_ch, out_ch, h_ch=None, ksize=3, pad=1,
activation=F.relu, downsample=False, bottleneck=False):
super(Block, self).__init__()
self.activation = activation
self.downsample = downsample
self.learnable_sc = (in_ch != out_ch) or downsample
if h_ch is None:
h_ch = in_ch
else:
h_ch = out_ch
h_ch = h_ch//2 if bottleneck else h_ch
self.c1 = utils.spectral_norm(nn.Conv2d(in_ch, h_ch, ksize, 1, pad))
self.c2 = utils.spectral_norm(nn.Conv2d(h_ch, out_ch, ksize, 1, pad))
if self.learnable_sc:
self.c_sc = utils.spectral_norm(nn.Conv2d(in_ch, out_ch, 1, 1, 0))
self._initialize()
def _initialize(self):
init.orthogonal_(self.c1.weight.data)
init.orthogonal_(self.c2.weight.data)
if self.learnable_sc:
init.orthogonal_(self.c_sc.weight.data)
def forward(self, x):
return self.shortcut(x) + self.residual(x)
def shortcut(self, x):
if self.learnable_sc:
x = self.c_sc(x)
if self.downsample:
return F.avg_pool2d(x, 2)
return x
def residual(self, x):
h = self.c1(self.activation(x))
h = self.c2(self.activation(h))
if self.downsample:
h = F.avg_pool2d(h, 2)
return h
class OptimizedBlock(nn.Module):
def __init__(self, in_ch, out_ch, ksize=3, pad=1, activation=F.relu, bottleneck=False):
super(OptimizedBlock, self).__init__()
self.activation = activation
h_ch = out_ch//2 if bottleneck else out_ch
self.c1 = utils.spectral_norm(nn.Conv2d(in_ch, h_ch, ksize, 1, pad))
self.c2 = utils.spectral_norm(nn.Conv2d(h_ch, out_ch, ksize, 1, pad))
self.c_sc = utils.spectral_norm(nn.Conv2d(in_ch, out_ch, 1, 1, 0))
self._initialize()
def _initialize(self):
init.orthogonal_(self.c1.weight.data)
init.orthogonal_(self.c2.weight.data)
init.orthogonal_(self.c_sc.weight.data)
def forward(self, x):
return self.shortcut(x) + self.residual(x)
def shortcut(self, x):
return self.c_sc(F.avg_pool2d(x, 2))
def residual(self, x):
h = self.activation(self.c1(x))
return F.avg_pool2d(self.c2(h), 2)