-
Notifications
You must be signed in to change notification settings - Fork 661
/
ResNet.py
48 lines (41 loc) · 1.46 KB
/
ResNet.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
# AUTOGENERATED! DO NOT EDIT! File to edit: ../../nbs/033_models.ResNet.ipynb.
# %% auto 0
__all__ = ['ResBlock', 'ResNet']
# %% ../../nbs/033_models.ResNet.ipynb 3
from ..imports import *
from .layers import *
from .utils import *
# %% ../../nbs/033_models.ResNet.ipynb 4
class ResBlock(Module):
def __init__(self, ni, nf, kss=[7, 5, 3]):
self.convblock1 = ConvBlock(ni, nf, kss[0])
self.convblock2 = ConvBlock(nf, nf, kss[1])
self.convblock3 = ConvBlock(nf, nf, kss[2], act=None)
# expand channels for the sum if necessary
self.shortcut = BN1d(ni) if ni == nf else ConvBlock(ni, nf, 1, act=None)
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 ResNet(Module):
def __init__(self, c_in, c_out):
nf = 64
kss=[7, 5, 3]
self.resblock1 = ResBlock(c_in, nf, kss=kss)
self.resblock2 = ResBlock(nf, nf * 2, kss=kss)
self.resblock3 = ResBlock(nf * 2, nf * 2, kss=kss)
self.gap = nn.AdaptiveAvgPool1d(1)
self.squeeze = Squeeze(-1)
self.fc = nn.Linear(nf * 2, c_out)
def forward(self, x):
x = self.resblock1(x)
x = self.resblock2(x)
x = self.resblock3(x)
x = self.squeeze(self.gap(x))
return self.fc(x)