Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
jantic
GitHub Repository: jantic/deoldify
Path: blob/master/fastai/layers.py
781 views
1
"`fastai.layers` provides essential functions to building and modifying `model` architectures"
2
from .torch_core import *
3
4
__all__ = ['AdaptiveConcatPool2d', 'BCEWithLogitsFlat', 'BCEFlat', 'MSELossFlat', 'CrossEntropyFlat', 'Debugger',
5
'Flatten', 'Lambda', 'PoolFlatten', 'View', 'ResizeBatch', 'bn_drop_lin', 'conv2d', 'conv2d_trans', 'conv_layer',
6
'embedding', 'simple_cnn', 'NormType', 'relu', 'batchnorm_2d', 'trunc_normal_', 'PixelShuffle_ICNR', 'icnr',
7
'NoopLoss', 'WassersteinLoss', 'SelfAttention', 'SequentialEx', 'MergeLayer', 'res_block', 'sigmoid_range',
8
'SigmoidRange', 'PartialLayer', 'FlattenedLoss', 'BatchNorm1dFlat', 'LabelSmoothingCrossEntropy', 'PooledSelfAttention2d']
9
10
class Lambda(Module):
11
"Create a layer that simply calls `func` with `x`"
12
def __init__(self, func:LambdaFunc): self.func=func
13
def forward(self, x): return self.func(x)
14
15
class View(Module):
16
"Reshape `x` to `size`"
17
def __init__(self, *size:int): self.size = size
18
def forward(self, x): return x.view(self.size)
19
20
class ResizeBatch(Module):
21
"Reshape `x` to `size`, keeping batch dim the same size"
22
def __init__(self, *size:int): self.size = size
23
def forward(self, x): return x.view((x.size(0),) + self.size)
24
25
class Flatten(Module):
26
"Flatten `x` to a single dimension, often used at the end of a model. `full` for rank-1 tensor"
27
def __init__(self, full:bool=False): self.full = full
28
def forward(self, x): return x.view(-1) if self.full else x.view(x.size(0), -1)
29
30
def PoolFlatten()->nn.Sequential:
31
"Apply `nn.AdaptiveAvgPool2d` to `x` and then flatten the result."
32
return nn.Sequential(nn.AdaptiveAvgPool2d(1), Flatten())
33
34
NormType = Enum('NormType', 'Batch BatchZero Weight Spectral Group Instance SpectralGN')
35
36
def batchnorm_2d(nf:int, norm_type:NormType=NormType.Batch):
37
"A batchnorm2d layer with `nf` features initialized depending on `norm_type`."
38
bn = nn.BatchNorm2d(nf)
39
with torch.no_grad():
40
bn.bias.fill_(1e-3)
41
bn.weight.fill_(0. if norm_type==NormType.BatchZero else 1.)
42
return bn
43
44
def bn_drop_lin(n_in:int, n_out:int, bn:bool=True, p:float=0., actn:Optional[nn.Module]=None):
45
"Sequence of batchnorm (if `bn`), dropout (with `p`) and linear (`n_in`,`n_out`) layers followed by `actn`."
46
layers = [nn.BatchNorm1d(n_in)] if bn else []
47
if p != 0: layers.append(nn.Dropout(p))
48
layers.append(nn.Linear(n_in, n_out))
49
if actn is not None: layers.append(actn)
50
return layers
51
52
def conv1d(ni:int, no:int, ks:int=1, stride:int=1, padding:int=0, bias:bool=False):
53
"Create and initialize a `nn.Conv1d` layer with spectral normalization."
54
conv = nn.Conv1d(ni, no, ks, stride=stride, padding=padding, bias=bias)
55
nn.init.kaiming_normal_(conv.weight)
56
if bias: conv.bias.data.zero_()
57
return spectral_norm(conv)
58
59
class PooledSelfAttention2d(Module):
60
"Pooled self attention layer for 2d."
61
def __init__(self, n_channels:int):
62
self.n_channels = n_channels
63
self.theta = spectral_norm(conv2d(n_channels, n_channels//8, 1)) # query
64
self.phi = spectral_norm(conv2d(n_channels, n_channels//8, 1)) # key
65
self.g = spectral_norm(conv2d(n_channels, n_channels//2, 1)) # value
66
self.o = spectral_norm(conv2d(n_channels//2, n_channels, 1))
67
self.gamma = nn.Parameter(tensor([0.]))
68
69
def forward(self, x):
70
# code borrowed from https://github.com/ajbrock/BigGAN-PyTorch/blob/7b65e82d058bfe035fc4e299f322a1f83993e04c/layers.py#L156
71
theta = self.theta(x)
72
phi = F.max_pool2d(self.phi(x), [2,2])
73
g = F.max_pool2d(self.g(x), [2,2])
74
theta = theta.view(-1, self.n_channels // 8, x.shape[2] * x.shape[3])
75
phi = phi.view(-1, self.n_channels // 8, x.shape[2] * x.shape[3] // 4)
76
g = g.view(-1, self.n_channels // 2, x.shape[2] * x.shape[3] // 4)
77
beta = F.softmax(torch.bmm(theta.transpose(1, 2), phi), -1)
78
o = self.o(torch.bmm(g, beta.transpose(1,2)).view(-1, self.n_channels // 2, x.shape[2], x.shape[3]))
79
return self.gamma * o + x
80
81
class SelfAttention(Module):
82
"Self attention layer for nd."
83
def __init__(self, n_channels:int):
84
self.query = conv1d(n_channels, n_channels//8)
85
self.key = conv1d(n_channels, n_channels//8)
86
self.value = conv1d(n_channels, n_channels)
87
self.gamma = nn.Parameter(tensor([0.]))
88
89
def forward(self, x):
90
#Notation from https://arxiv.org/pdf/1805.08318.pdf
91
size = x.size()
92
x = x.view(*size[:2],-1)
93
f,g,h = self.query(x),self.key(x),self.value(x)
94
beta = F.softmax(torch.bmm(f.permute(0,2,1).contiguous(), g), dim=1)
95
o = self.gamma * torch.bmm(h, beta) + x
96
return o.view(*size).contiguous()
97
98
def conv2d(ni:int, nf:int, ks:int=3, stride:int=1, padding:int=None, bias=False, init:LayerFunc=nn.init.kaiming_normal_) -> nn.Conv2d:
99
"Create and initialize `nn.Conv2d` layer. `padding` defaults to `ks//2`."
100
if padding is None: padding = ks//2
101
return init_default(nn.Conv2d(ni, nf, kernel_size=ks, stride=stride, padding=padding, bias=bias), init)
102
103
def conv2d_trans(ni:int, nf:int, ks:int=2, stride:int=2, padding:int=0, bias=False) -> nn.ConvTranspose2d:
104
"Create `nn.ConvTranspose2d` layer."
105
return nn.ConvTranspose2d(ni, nf, kernel_size=ks, stride=stride, padding=padding, bias=bias)
106
107
def relu(inplace:bool=False, leaky:float=None):
108
"Return a relu activation, maybe `leaky` and `inplace`."
109
return nn.LeakyReLU(inplace=inplace, negative_slope=leaky) if leaky is not None else nn.ReLU(inplace=inplace)
110
111
def conv_layer(ni:int, nf:int, ks:int=3, stride:int=1, padding:int=None, bias:bool=None, is_1d:bool=False,
112
norm_type:Optional[NormType]=NormType.Batch, use_activ:bool=True, leaky:float=None,
113
transpose:bool=False, init:Callable=nn.init.kaiming_normal_, self_attention:bool=False):
114
"Create a sequence of convolutional (`ni` to `nf`), ReLU (if `use_activ`) and batchnorm (if `bn`) layers."
115
if padding is None: padding = (ks-1)//2 if not transpose else 0
116
bn = norm_type in (NormType.Batch, NormType.BatchZero)
117
if bias is None: bias = not bn
118
conv_func = nn.ConvTranspose2d if transpose else nn.Conv1d if is_1d else nn.Conv2d
119
conv = init_default(conv_func(ni, nf, kernel_size=ks, bias=bias, stride=stride, padding=padding), init)
120
if norm_type==NormType.Weight: conv = weight_norm(conv)
121
elif norm_type==NormType.Spectral: conv = spectral_norm(conv)
122
layers = [conv]
123
if use_activ: layers.append(relu(True, leaky=leaky))
124
if bn: layers.append((nn.BatchNorm1d if is_1d else nn.BatchNorm2d)(nf))
125
if self_attention: layers.append(SelfAttention(nf))
126
return nn.Sequential(*layers)
127
128
class SequentialEx(Module):
129
"Like `nn.Sequential`, but with ModuleList semantics, and can access module input"
130
def __init__(self, *layers): self.layers = nn.ModuleList(layers)
131
132
def forward(self, x):
133
res = x
134
for l in self.layers:
135
res.orig = x
136
nres = l(res)
137
#print(l. + ' mean: ' + str(nres.abs().mean()))
138
#print(' max: ' + str(nres.abs().max()))
139
# We have to remove res.orig to avoid hanging refs and therefore memory leaks
140
res.orig = None
141
res = nres
142
return res
143
144
def __getitem__(self,i): return self.layers[i]
145
def append(self,l): return self.layers.append(l)
146
def extend(self,l): return self.layers.extend(l)
147
def insert(self,i,l): return self.layers.insert(i,l)
148
149
class MergeLayer(Module):
150
"Merge a shortcut with the result of the module by adding them or concatenating thme if `dense=True`."
151
def __init__(self, dense:bool=False): self.dense=dense
152
def forward(self, x): return torch.cat([x,x.orig], dim=1) if self.dense else (x+x.orig)
153
154
def res_block(nf, dense:bool=False, norm_type:Optional[NormType]=NormType.Batch, bottle:bool=False, **conv_kwargs):
155
"Resnet block of `nf` features. `conv_kwargs` are passed to `conv_layer`."
156
norm2 = norm_type
157
if not dense and (norm_type==NormType.Batch): norm2 = NormType.BatchZero
158
nf_inner = nf//2 if bottle else nf
159
return SequentialEx(conv_layer(nf, nf_inner, norm_type=norm_type, **conv_kwargs),
160
conv_layer(nf_inner, nf, norm_type=norm2, **conv_kwargs),
161
MergeLayer(dense))
162
163
def sigmoid_range(x:Tensor, low:int, high:int):
164
"Sigmoid function with range `(low, high)`"
165
return torch.sigmoid(x) * (high - low) + low
166
167
class SigmoidRange(Module):
168
"Sigmoid module with range `(low,x_max)`"
169
def __init__(self, low:int, high:int): self.low,self.high = low,high
170
def forward(self, x): return sigmoid_range(x, self.low, self.high)
171
172
class PartialLayer(Module):
173
"Layer that applies `partial(func, **kwargs)`."
174
def __init__(self, func, **kwargs): self.repr,self.func = f'{func}({kwargs})', partial(func, **kwargs)
175
def forward(self, x): return self.func(x)
176
def __repr__(self): return self.repr
177
178
class AdaptiveConcatPool2d(Module):
179
"Layer that concats `AdaptiveAvgPool2d` and `AdaptiveMaxPool2d`."
180
def __init__(self, sz:Optional[int]=None):
181
"Output will be 2*sz or 2 if sz is None"
182
self.output_size = sz or 1
183
self.ap = nn.AdaptiveAvgPool2d(self.output_size)
184
self.mp = nn.AdaptiveMaxPool2d(self.output_size)
185
186
def forward(self, x): return torch.cat([self.mp(x), self.ap(x)], 1)
187
188
class Debugger(Module):
189
"A module to debug inside a model."
190
def forward(self,x:Tensor) -> Tensor:
191
set_trace()
192
return x
193
194
def icnr(x, scale=2, init=nn.init.kaiming_normal_):
195
"ICNR init of `x`, with `scale` and `init` function."
196
ni,nf,h,w = x.shape
197
ni2 = int(ni/(scale**2))
198
k = init(torch.zeros([ni2,nf,h,w])).transpose(0, 1)
199
k = k.contiguous().view(ni2, nf, -1)
200
k = k.repeat(1, 1, scale**2)
201
k = k.contiguous().view([nf,ni,h,w]).transpose(0, 1)
202
x.data.copy_(k)
203
204
class PixelShuffle_ICNR(Module):
205
"Upsample by `scale` from `ni` filters to `nf` (default `ni`), using `nn.PixelShuffle`, `icnr` init, and `weight_norm`."
206
def __init__(self, ni:int, nf:int=None, scale:int=2, blur:bool=False, norm_type=NormType.Weight, leaky:float=None):
207
nf = ifnone(nf, ni)
208
self.conv = conv_layer(ni, nf*(scale**2), ks=1, norm_type=norm_type, use_activ=False)
209
icnr(self.conv[0].weight)
210
self.shuf = nn.PixelShuffle(scale)
211
# Blurring over (h*w) kernel
212
# "Super-Resolution using Convolutional Neural Networks without Any Checkerboard Artifacts"
213
# - https://arxiv.org/abs/1806.02658
214
self.pad = nn.ReplicationPad2d((1,0,1,0))
215
self.blur = nn.AvgPool2d(2, stride=1)
216
self.relu = relu(True, leaky=leaky)
217
218
def forward(self,x):
219
x = self.shuf(self.relu(self.conv(x)))
220
return self.blur(self.pad(x)) if self.blur else x
221
222
class FlattenedLoss():
223
"Same as `func`, but flattens input and target."
224
def __init__(self, func, *args, axis:int=-1, floatify:bool=False, is_2d:bool=True, **kwargs):
225
self.func,self.axis,self.floatify,self.is_2d = func(*args,**kwargs),axis,floatify,is_2d
226
functools.update_wrapper(self, self.func)
227
228
def __repr__(self): return f"FlattenedLoss of {self.func}"
229
@property
230
def reduction(self): return self.func.reduction
231
@reduction.setter
232
def reduction(self, v): self.func.reduction = v
233
234
def __call__(self, input:Tensor, target:Tensor, **kwargs)->Rank0Tensor:
235
input = input.transpose(self.axis,-1).contiguous()
236
target = target.transpose(self.axis,-1).contiguous()
237
if self.floatify: target = target.float()
238
input = input.view(-1,input.shape[-1]) if self.is_2d else input.view(-1)
239
return self.func.__call__(input, target.view(-1), **kwargs)
240
241
def CrossEntropyFlat(*args, axis:int=-1, **kwargs):
242
"Same as `nn.CrossEntropyLoss`, but flattens input and target."
243
return FlattenedLoss(nn.CrossEntropyLoss, *args, axis=axis, **kwargs)
244
245
def BCEWithLogitsFlat(*args, axis:int=-1, floatify:bool=True, **kwargs):
246
"Same as `nn.BCEWithLogitsLoss`, but flattens input and target."
247
return FlattenedLoss(nn.BCEWithLogitsLoss, *args, axis=axis, floatify=floatify, is_2d=False, **kwargs)
248
249
def BCEFlat(*args, axis:int=-1, floatify:bool=True, **kwargs):
250
"Same as `nn.BCELoss`, but flattens input and target."
251
return FlattenedLoss(nn.BCELoss, *args, axis=axis, floatify=floatify, is_2d=False, **kwargs)
252
253
def MSELossFlat(*args, axis:int=-1, floatify:bool=True, **kwargs):
254
"Same as `nn.MSELoss`, but flattens input and target."
255
return FlattenedLoss(nn.MSELoss, *args, axis=axis, floatify=floatify, is_2d=False, **kwargs)
256
257
class NoopLoss(Module):
258
"Just returns the mean of the `output`."
259
def forward(self, output, *args): return output.mean()
260
261
class WassersteinLoss(Module):
262
"For WGAN."
263
def forward(self, real, fake): return real.mean() - fake.mean()
264
265
def simple_cnn(actns:Collection[int], kernel_szs:Collection[int]=None,
266
strides:Collection[int]=None, bn=False) -> nn.Sequential:
267
"CNN with `conv_layer` defined by `actns`, `kernel_szs` and `strides`, plus batchnorm if `bn`."
268
nl = len(actns)-1
269
kernel_szs = ifnone(kernel_szs, [3]*nl)
270
strides = ifnone(strides , [2]*nl)
271
layers = [conv_layer(actns[i], actns[i+1], kernel_szs[i], stride=strides[i],
272
norm_type=(NormType.Batch if bn and i<(len(strides)-1) else None)) for i in range_of(strides)]
273
layers.append(PoolFlatten())
274
return nn.Sequential(*layers)
275
276
def trunc_normal_(x:Tensor, mean:float=0., std:float=1.) -> Tensor:
277
"Truncated normal initialization."
278
# From https://discuss.pytorch.org/t/implementing-truncated-normal-initializer/4778/12
279
return x.normal_().fmod_(2).mul_(std).add_(mean)
280
281
def embedding(ni:int,nf:int) -> nn.Module:
282
"Create an embedding layer."
283
emb = nn.Embedding(ni, nf)
284
# See https://arxiv.org/abs/1711.09160
285
with torch.no_grad(): trunc_normal_(emb.weight, std=0.01)
286
return emb
287
288
class BatchNorm1dFlat(nn.BatchNorm1d):
289
"`nn.BatchNorm1d`, but first flattens leading dimensions"
290
def forward(self, x):
291
if x.dim()==2: return super().forward(x)
292
*f,l = x.shape
293
x = x.contiguous().view(-1,l)
294
return super().forward(x).view(*f,l)
295
296
class LabelSmoothingCrossEntropy(Module):
297
def __init__(self, eps:float=0.1, reduction='mean'): self.eps,self.reduction = eps,reduction
298
299
def forward(self, output, target):
300
c = output.size()[-1]
301
log_preds = F.log_softmax(output, dim=-1)
302
if self.reduction=='sum': loss = -log_preds.sum()
303
else:
304
loss = -log_preds.sum(dim=-1)
305
if self.reduction=='mean': loss = loss.mean()
306
return loss*self.eps/c + (1-self.eps) * F.nll_loss(log_preds, target, reduction=self.reduction)
307
308