-
Notifications
You must be signed in to change notification settings - Fork 10
/
resnet_block_fc.py
182 lines (156 loc) · 5.95 KB
/
resnet_block_fc.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
from torch import nn
import torch
# import torch_scatter
import torch.autograd.profiler as profiler
import utils.util as util
# Resnet Blocks
class ResnetBlockFC(nn.Module):
"""
Fully connected ResNet Block class.
Taken from DVR code.
:param size_in (int): input dimension
:param size_out (int): output dimension
:param size_h (int): hidden dimension
"""
def __init__(self, size_in, size_out=None, size_h=None, beta=0.0):
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)
# Init
nn.init.constant_(self.fc_0.bias, 0.0)
nn.init.kaiming_normal_(self.fc_0.weight, a=0, mode="fan_in")
nn.init.constant_(self.fc_1.bias, 0.0)
nn.init.zeros_(self.fc_1.weight)
if beta > 0:
self.activation = nn.Softplus(beta=beta)
else:
self.activation = nn.ReLU()
if size_in == size_out:
self.shortcut = None
else:
self.shortcut = nn.Linear(size_in, size_out, bias=False)
nn.init.constant_(self.shortcut.bias, 0.0)
nn.init.kaiming_normal_(self.shortcut.weight, a=0, mode="fan_in")
def forward(self, x):
with profiler.record_function("resblock"):
net = self.fc_0(self.activation(x))
dx = self.fc_1(self.activation(net))
if self.shortcut is not None:
x_s = self.shortcut(x)
else:
x_s = x
return x_s + dx
class ResnetFC(nn.Module):
def __init__(
self,
d_in,
d_out=4,
n_blocks=5,
d_latent=0,
d_hidden=128,
beta=0.0,
combine_layer=1000,
combine_type="average",
use_spade=False,
):
"""
:param d_in input size
:param d_out output size
:param n_blocks number of Resnet blocks
:param d_latent latent size, added in each resnet block (0 = disable)
:param d_hidden hiddent dimension throughout network
:param beta softplus beta, 100 is reasonable; if <=0 uses ReLU activations instead
"""
super().__init__()
if d_in > 0:
self.lin_in = nn.Linear(d_in, d_hidden)
nn.init.constant_(self.lin_in.bias, 0.0)
nn.init.kaiming_normal_(self.lin_in.weight, a=0, mode="fan_in")
self.lin_out = nn.Linear(d_hidden, d_out)
nn.init.constant_(self.lin_out.bias, 0.0)
nn.init.kaiming_normal_(self.lin_out.weight, a=0, mode="fan_in")
self.n_blocks = n_blocks
self.d_latent = d_latent
self.d_in = d_in
self.d_out = d_out
self.d_hidden = d_hidden
self.combine_layer = combine_layer
self.combine_type = combine_type
self.use_spade = use_spade
self.blocks = nn.ModuleList(
[ResnetBlockFC(d_hidden, beta=beta) for i in range(n_blocks)]
)
if d_latent != 0:
n_lin_z = min(combine_layer, n_blocks)
self.lin_z = nn.ModuleList(
[nn.Linear(d_latent, d_hidden) for i in range(n_lin_z)]
)
for i in range(n_lin_z):
nn.init.constant_(self.lin_z[i].bias, 0.0)
nn.init.kaiming_normal_(self.lin_z[i].weight, a=0, mode="fan_in")
if self.use_spade:
self.scale_z = nn.ModuleList(
[nn.Linear(d_latent, d_hidden) for _ in range(n_lin_z)]
)
for i in range(n_lin_z):
nn.init.constant_(self.scale_z[i].bias, 0.0)
nn.init.kaiming_normal_(self.scale_z[i].weight, a=0, mode="fan_in")
if beta > 0:
self.activation = nn.Softplus(beta=beta)
else:
self.activation = nn.ReLU()
def forward(self, zx, combine_inner_dims=(1,), combine_index=None, dim_size=None):
"""
:param zx (..., d_latent + d_in)
:param combine_inner_dims Combining dimensions for use with multiview inputs.
Tensor will be reshaped to (-1, combine_inner_dims, ...) and reduced using combine_type
on dim 1, at combine_layer
"""
with profiler.record_function("resnetfc_infer"):
assert zx.size(-1) == self.d_latent + self.d_in
if self.d_latent > 0:
z = zx[..., : self.d_latent]
x = zx[..., self.d_latent:]
else:
x = zx
if self.d_in > 0:
x = self.lin_in(x)
else:
x = torch.zeros(self.d_hidden, device=zx.device)
for blkid in range(self.n_blocks):
if blkid == self.combine_layer:
x = util.combine_interleaved(
x, combine_inner_dims, self.combine_type
)
if self.d_latent > 0 and blkid < self.combine_layer:
tz = self.lin_z[blkid](z)
if self.use_spade:
sz = self.scale_z[blkid](z)
x = sz * x + tz
else:
x = x + tz
x = self.blocks[blkid](x)
out = self.lin_out(self.activation(x))
return out
@classmethod
def from_conf(cls, conf, d_in, **kwargs):
# PyHocon construction
return cls(
d_in,
n_blocks=conf.get_int("n_blocks", 5),
d_hidden=conf.get_int("d_hidden", 128),
beta=conf.get_float("beta", 0.0),
combine_layer=conf.get_int("combine_layer", 1000),
combine_type=conf.get_string("combine_type", "average"), # average | max
use_spade=conf.get_bool("use_spade", False),
**kwargs
)