-
Notifications
You must be signed in to change notification settings - Fork 11
/
bc_resnet_model.py
129 lines (97 loc) · 4.76 KB
/
bc_resnet_model.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
import torch
from torch import nn
import torch.nn.functional as F
from subspectral_norm import SubSpectralNorm
DROPOUT = 0.1
class NormalBlock(nn.Module):
def __init__(self, n_chan: int, *, dilation: int = 1, dropout: float = DROPOUT, use_subspectral: bool = True):
super().__init__()
norm_layer = SubSpectralNorm(n_chan, 5) if use_subspectral else nn.BatchNorm2d(n_chan)
self.f2 = nn.Sequential(
nn.Conv2d(n_chan, n_chan, kernel_size=(3, 1), padding="same", groups=n_chan),
norm_layer,
)
self.f1 = nn.Sequential(
nn.Conv2d(n_chan, n_chan, kernel_size=(1, 3), padding="same", groups=n_chan, dilation=(1, dilation)),
nn.BatchNorm2d(n_chan),
nn.SiLU(),
nn.Conv2d(n_chan, n_chan, kernel_size=1),
nn.Dropout2d(dropout)
)
self.activation = nn.ReLU()
def forward(self, x):
n_freq = x.shape[2]
x1 = self.f2(x)
x2 = torch.mean(x1, dim=2, keepdim=True)
x2 = self.f1(x2)
x2 = x2.repeat(1, 1, n_freq, 1)
return self.activation(x + x1 + x2)
class TransitionBlock(nn.Module):
def __init__(self, in_chan: int, out_chan: int, *, dilation: int = 1, stride: int = 1, dropout: float = DROPOUT, use_subspectral: bool = True):
super().__init__()
if stride == 1:
conv = nn.Conv2d(out_chan, out_chan, kernel_size=(3, 1), groups=out_chan, padding="same")
else:
conv = nn.Conv2d(out_chan, out_chan, kernel_size=(3, 1), stride=(stride, 1), groups=out_chan, padding=(1, 0))
norm_layer = SubSpectralNorm(out_chan, 5) if use_subspectral else nn.BatchNorm2d(out_chan)
self.f2 = nn.Sequential(
nn.Conv2d(in_chan, out_chan, kernel_size=(1, 1)),
nn.BatchNorm2d(out_chan),
nn.ReLU(),
conv,
norm_layer,
)
self.f1 = nn.Sequential(
nn.Conv2d(out_chan, out_chan, kernel_size=(1, 3), padding="same", groups=out_chan, dilation=(1, dilation)),
nn.BatchNorm2d(out_chan),
nn.SiLU(),
nn.Conv2d(out_chan, out_chan, kernel_size=1),
nn.Dropout2d(dropout)
)
self.activation = nn.ReLU()
def forward(self, x: torch.Tensor):
x = self.f2(x)
n_freq = x.shape[2]
x1 = torch.mean(x, dim=2, keepdim=True)
x1 = self.f1(x1)
x1 = x1.repeat(1, 1, n_freq, 1)
return self.activation(x + x1)
class BcResNetModel(nn.Module):
def __init__(self, n_class: int = 35, *, scale: int = 1, dropout: float = DROPOUT, use_subspectral: bool = True):
super().__init__()
self.input_conv = nn.Conv2d(1, 16*scale, kernel_size=(5, 5), stride=(2, 1), padding=2)
self.t1 = TransitionBlock(16*scale, 8*scale, dropout=dropout, use_subspectral=use_subspectral)
self.n11 = NormalBlock(8*scale, dropout=dropout, use_subspectral=use_subspectral)
self.t2 = TransitionBlock(8*scale, 12*scale, dilation=2, stride=2, dropout=dropout, use_subspectral=use_subspectral)
self.n21 = NormalBlock(12*scale, dilation=2, dropout=dropout, use_subspectral=use_subspectral)
self.t3 = TransitionBlock(12*scale, 16*scale, dilation=4, stride=2, dropout=dropout, use_subspectral=use_subspectral)
self.n31 = NormalBlock(16*scale, dilation=4, dropout=dropout, use_subspectral=use_subspectral)
self.n32 = NormalBlock(16*scale, dilation=4, dropout=dropout, use_subspectral=use_subspectral)
self.n33 = NormalBlock(16*scale, dilation=4, dropout=dropout, use_subspectral=use_subspectral)
self.t4 = TransitionBlock(16*scale, 20*scale, dilation=8, dropout=dropout, use_subspectral=use_subspectral)
self.n41 = NormalBlock(20*scale, dilation=8, dropout=dropout, use_subspectral=use_subspectral)
self.n42 = NormalBlock(20*scale, dilation=8, dropout=dropout, use_subspectral=use_subspectral)
self.n43 = NormalBlock(20*scale, dilation=8, dropout=dropout, use_subspectral=use_subspectral)
self.dw_conv = nn.Conv2d(20*scale, 20*scale, kernel_size=(5, 5), groups=20)
self.onexone_conv = nn.Conv2d(20*scale, 32*scale, kernel_size=1)
self.head_conv = nn.Conv2d(32*scale, n_class, kernel_size=1)
def forward(self, x: torch.Tensor):
x = self.input_conv(x)
x = self.t1(x)
x = self.n11(x)
x = self.t2(x)
x = self.n21(x)
x = self.t3(x)
x = self.n31(x)
x = self.n32(x)
x = self.n33(x)
x = self.t4(x)
x = self.n41(x)
x = self.n42(x)
x = self.n43(x)
x = self.dw_conv(x)
x = self.onexone_conv(x)
x = torch.mean(x, dim=3, keepdim=True)
x = self.head_conv(x)
x = x.squeeze()
return F.log_softmax(x, dim=-1)