-
Notifications
You must be signed in to change notification settings - Fork 667
/
Copy pathResCNN.py
50 lines (42 loc) · 1.93 KB
/
ResCNN.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
# AUTOGENERATED! DO NOT EDIT! File to edit: ../../nbs/041_models.ResCNN.ipynb.
# %% auto 0
__all__ = ['ResCNN']
# %% ../../nbs/041_models.ResCNN.ipynb 3
from ..imports import *
from ..utils import *
from .layers import *
# %% ../../nbs/041_models.ResCNN.ipynb 5
class _ResCNNBlock(Module):
def __init__(self, ni, nf, kss=[7, 5, 3], coord=False, separable=False, zero_norm=False):
self.convblock1 = ConvBlock(ni, nf, kss[0], coord=coord, separable=separable)
self.convblock2 = ConvBlock(nf, nf, kss[1], coord=coord, separable=separable)
self.convblock3 = ConvBlock(nf, nf, kss[2], act=None, coord=coord, separable=separable, zero_norm=zero_norm)
# expand channels for the sum if necessary
self.shortcut = ConvBN(ni, nf, 1, coord=coord)
self.add = Add()
self.act = nn.ReLU()
def forward(self, x):
res = x
x = self.convblock1(x)
x = self.convblock2(x)
x = self.convblock3(x)
x = self.add(x, self.shortcut(res))
x = self.act(x)
return x
class ResCNN(Module):
def __init__(self, c_in, c_out, coord=False, separable=False, zero_norm=False):
nf = 64
self.block1 = _ResCNNBlock(c_in, nf, kss=[7, 5, 3], coord=coord, separable=separable, zero_norm=zero_norm)
self.block2 = ConvBlock(nf, nf * 2, 3, coord=coord, separable=separable, act=nn.LeakyReLU, act_kwargs={'negative_slope':.2})
self.block3 = ConvBlock(nf * 2, nf * 4, 3, coord=coord, separable=separable, act=nn.PReLU)
self.block4 = ConvBlock(nf * 4, nf * 2, 3, coord=coord, separable=separable, act=nn.ELU, act_kwargs={'alpha':.3})
self.gap = nn.AdaptiveAvgPool1d(1)
self.squeeze = Squeeze(-1)
self.lin = nn.Linear(nf * 2, c_out)
def forward(self, x):
x = self.block1(x)
x = self.block2(x)
x = self.block3(x)
x = self.block4(x)
x = self.squeeze(self.gap(x))
return self.lin(x)