-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathbcresnet.py
More file actions
182 lines (160 loc) · 5.24 KB
/
bcresnet.py
File metadata and controls
182 lines (160 loc) · 5.24 KB
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
# Copyright (c) 2023 Qualcomm Technologies, Inc.
# All Rights Reserved.
import torch.nn.functional as F
from torch import nn
from subspectralnorm import SubSpectralNorm
class ConvBNReLU(nn.Module):
def __init__(
self,
in_plane,
out_plane,
idx,
kernel_size=3,
stride=1,
groups=1,
use_dilation=False,
activation=True,
swish=False,
BN=True,
ssn=False,
):
super().__init__()
def get_padding(kernel_size, use_dilation):
rate = 1 # dilation rate
padding_len = (kernel_size - 1) // 2
if use_dilation and kernel_size > 1:
rate = int(2**self.idx)
padding_len = rate * padding_len
return padding_len, rate
self.idx = idx
# padding and dilation rate
if isinstance(kernel_size, (list, tuple)):
padding = []
rate = []
for k_size in kernel_size:
temp_padding, temp_rate = get_padding(k_size, use_dilation)
rate.append(temp_rate)
padding.append(temp_padding)
else:
padding, rate = get_padding(kernel_size, use_dilation)
# convbnrelu block
layers = []
layers.append(
nn.Conv2d(in_plane, out_plane, kernel_size, stride, padding, rate, groups, bias=False)
)
if ssn:
layers.append(SubSpectralNorm(out_plane, 5))
elif BN:
layers.append(nn.BatchNorm2d(out_plane))
if swish:
layers.append(nn.SiLU(True))
elif activation:
layers.append(nn.ReLU(True))
self.block = nn.Sequential(*layers)
def forward(self, x):
return self.block(x)
class BCResBlock(nn.Module):
def __init__(self, in_plane, out_plane, idx, stride):
super().__init__()
self.transition_block = in_plane != out_plane
kernel_size = (3, 3)
# 2D part (f2)
layers = []
if self.transition_block:
layers.append(ConvBNReLU(in_plane, out_plane, idx, 1, 1))
in_plane = out_plane
layers.append(
ConvBNReLU(
in_plane,
out_plane,
idx,
(kernel_size[0], 1),
(stride[0], 1),
groups=in_plane,
ssn=True,
activation=False,
)
)
self.f2 = nn.Sequential(*layers)
self.avg_gpool = nn.AdaptiveAvgPool2d((1, None))
# 1D part (f1)
self.f1 = nn.Sequential(
ConvBNReLU(
out_plane,
out_plane,
idx,
(1, kernel_size[1]),
(1, stride[1]),
groups=out_plane,
swish=True,
use_dilation=True,
),
nn.Conv2d(out_plane, out_plane, 1, bias=False),
nn.Dropout2d(0.1),
)
def forward(self, x):
# 2D part
shortcut = x
x = self.f2(x)
aux_2d_res = x
x = self.avg_gpool(x)
# 1D part
x = self.f1(x)
x = x + aux_2d_res
if not self.transition_block:
x = x + shortcut
x = F.relu(x, True)
return x
def BCBlockStage(num_layers, last_channel, cur_channel, idx, use_stride):
stage = nn.ModuleList()
channels = [last_channel] + [cur_channel] * num_layers
for i in range(num_layers):
stride = (2, 1) if use_stride and i == 0 else (1, 1)
stage.append(BCResBlock(channels[i], channels[i + 1], idx, stride))
return stage
class BCResNets(nn.Module):
def __init__(self, base_c, num_classes=12):
super().__init__()
self.num_classes = num_classes
self.n = [2, 2, 4, 4] # identical modules repeated n times
self.c = [
base_c * 2,
base_c,
int(base_c * 1.5),
base_c * 2,
int(base_c * 2.5),
base_c * 4,
] # num channels
self.s = [1, 2] # stage using stride
self._build_network()
def _build_network(self):
# Head: (Conv-BN-ReLU)
self.cnn_head = nn.Sequential(
nn.Conv2d(1, self.c[0], 5, (2, 1), 2, bias=False),
nn.BatchNorm2d(self.c[0]),
nn.ReLU(True),
)
# Body: BC-ResBlocks
self.BCBlocks = nn.ModuleList([])
for idx, n in enumerate(self.n):
use_stride = idx in self.s
self.BCBlocks.append(BCBlockStage(n, self.c[idx], self.c[idx + 1], idx, use_stride))
# Classifier
self.classifier = nn.Sequential(
nn.Conv2d(
self.c[-2], self.c[-2], (5, 5), bias=False, groups=self.c[-2], padding=(0, 2)
),
nn.Conv2d(self.c[-2], self.c[-1], 1, bias=False),
nn.BatchNorm2d(self.c[-1]),
nn.ReLU(True),
nn.AdaptiveAvgPool2d((1, 1)),
nn.Conv2d(self.c[-1], self.num_classes, 1),
)
def forward(self, x):
x = self.cnn_head(x)
for i, num_modules in enumerate(self.n):
for j in range(num_modules):
x = self.BCBlocks[i][j](x)
x = self.classifier(x)
x = x.view(-1, x.shape[1])
return x