-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneural.py
345 lines (277 loc) · 10.7 KB
/
neural.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import pretrainedmodels
import torch
from torch import nn
import torch.nn.functional as F
from transformers import get_constant_schedule_with_warmup
from utils import get_learning_rate, isclose, Lookahead
from collections import OrderedDict
import torchlibrosa
def get_model_loss(args):
if args.model == "cnn14_att":
model = Cnn14_DecisionLevelAtt(args.num_classes, args.nmels).cuda()
state = torch.load("Cnn14_DecisionLevelAtt_mAP=0.425.pth")["model"]
new_state_dict = OrderedDict()
for k, v in state.items():
if ("att_block." in k) and v.dim() != 0:
print(k)
new_state_dict[k] = v[: args.num_classes]
elif "bn0" in k and v.dim() != 0 and args.nmels == 128:
new_state_dict[k] = torch.cat([v,v])
else:
new_state_dict[k] = v
model.load_state_dict(new_state_dict, strict=False)
loss_fn = PANNsLoss()
elif args.model == "b4_att":
from geffnet import tf_efficientnet_b4_ns
model = tf_efficientnet_b4_ns(
pretrained=True, num_classes=args.num_classes
).cuda()
loss_fn = PANNsLoss()
else:
model = torch.hub.load(
"rwightman/gen-efficientnet-pytorch",
"tf_efficientnet_%s_ns" % args.model,
pretrained=True,
num_classes=args.num_classes,
).cuda()
loss_fn = torch.nn.BCEWithLogitsLoss()
args.__dict__["sigmoid"] = True
return model, loss_fn
def get_optimizer_scheduler(model, args):
optimizer = torch.optim.AdamW(
model.parameters(),
lr=args.lr_base,
betas=args.betas,
eps=args.eps,
weight_decay=args.wd,
)
if args.opt_lookahead:
optimizer = Lookahead(optimizer)
scheduler_warmup = get_constant_schedule_with_warmup(
optimizer, num_warmup_steps=args.warmup
)
scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(
optimizer,
mode="max",
factor=args.lr_drop_rate,
patience=args.lr_patience,
threshold=0,
verbose=True,
)
return optimizer, scheduler_warmup, scheduler
class ResNet18(nn.Module):
def __init__(self, pretrained):
super(ResNet18, self).__init__()
if pretrained is True:
self.model = pretrainedmodels.__dict__["resnet18"](pretrained="imagenet")
else:
self.model = pretrainedmodels.__dict__["resnet18"](pretrained=None)
self.l0 = nn.Linear(512, args.num_classes)
def forward(self, x):
bs, _, _, _ = x.shape
x = self.model.features(x)
x = F.adaptive_avg_pool2d(x, 1).reshape(bs, -1)
x = self.l0(x)
return x
def init_layer(layer):
"""Initialize a Linear or Convolutional layer. """
nn.init.xavier_uniform_(layer.weight)
if hasattr(layer, "bias"):
if layer.bias is not None:
layer.bias.data.fill_(0.0)
def init_bn(bn):
"""Initialize a Batchnorm layer. """
bn.bias.data.fill_(0.0)
bn.weight.data.fill_(1.0)
class AttBlock(nn.Module):
def __init__(self, n_in, n_out, activation="linear", temperature=1.0):
super(AttBlock, self).__init__()
self.activation = activation
self.temperature = temperature
self.att = nn.Conv1d(
in_channels=n_in,
out_channels=n_out,
kernel_size=1,
stride=1,
padding=0,
bias=True,
)
self.cla = nn.Conv1d(
in_channels=n_in,
out_channels=n_out,
kernel_size=1,
stride=1,
padding=0,
bias=True,
)
self.bn_att = nn.BatchNorm1d(n_out)
self.init_weights()
def init_weights(self):
init_layer(self.att)
init_layer(self.cla)
init_bn(self.bn_att)
def forward(self, x):
# x: (n_samples, n_in, n_time)
norm_att = torch.softmax(torch.clamp(self.att(x), -10, 10), dim=-1)
cla = self.nonlinear_transform(self.cla(x))
x = torch.sum(norm_att * cla, dim=2)
return x, norm_att, cla
def nonlinear_transform(self, x):
if self.activation == "linear":
return x
elif self.activation == "sigmoid":
return torch.sigmoid(x)
class ConvBlock(nn.Module):
def __init__(self, in_channels, out_channels):
super(ConvBlock, self).__init__()
self.conv1 = nn.Conv2d(
in_channels=in_channels,
out_channels=out_channels,
kernel_size=(3, 3),
stride=(1, 1),
padding=(1, 1),
bias=False,
)
self.conv2 = nn.Conv2d(
in_channels=out_channels,
out_channels=out_channels,
kernel_size=(3, 3),
stride=(1, 1),
padding=(1, 1),
bias=False,
)
self.bn1 = nn.BatchNorm2d(out_channels)
self.bn2 = nn.BatchNorm2d(out_channels)
self.init_weight()
def init_weight(self):
init_layer(self.conv1)
init_layer(self.conv2)
init_bn(self.bn1)
init_bn(self.bn2)
def forward(self, input, pool_size=(2, 2), pool_type="avg"):
x = input
x = F.relu_(self.bn1(self.conv1(x)))
x = F.relu_(self.bn2(self.conv2(x)))
if pool_type == "max":
x = F.max_pool2d(x, kernel_size=pool_size)
elif pool_type == "avg":
x = F.avg_pool2d(x, kernel_size=pool_size)
elif pool_type == "avg+max":
x1 = F.avg_pool2d(x, kernel_size=pool_size)
x2 = F.max_pool2d(x, kernel_size=pool_size)
x = x1 + x2
else:
raise Exception("Incorrect argument!")
return x
def interpolate(x, ratio):
"""Interpolate data in time domain. This is used to compensate the
resolution reduction in downsampling of a CNN.
Args:
x: (batch_size, time_steps, classes_num)
ratio: int, ratio to interpolate
Returns:
upsampled: (batch_size, time_steps * ratio, classes_num)
"""
(batch_size, time_steps, classes_num) = x.shape
upsampled = x[:, :, None, :].repeat(1, 1, ratio, 1)
upsampled = upsampled.reshape(batch_size, time_steps * ratio, classes_num)
return upsampled
def pad_framewise_output(framewise_output, frames_num):
"""Pad framewise_output to the same length as input frames. The pad value
is the same as the value of the last frame.
Args:
framewise_output: (batch_size, frames_num, classes_num)
frames_num: int, number of frames to pad
Outputs:
output: (batch_size, frames_num, classes_num)
"""
pad = framewise_output[:, -1:, :].repeat(
1, frames_num - framewise_output.shape[1], 1
)
"""tensor for padding"""
output = torch.cat((framewise_output, pad), dim=1)
"""(batch_size, frames_num, classes_num)"""
return output
class LogMel(nn.Module):
def __init__(self, nmels):
super(LogMel, self).__init__()
self.spectrogram_extractor = torchlibrosa.stft.Spectrogram(n_fft=1024, hop_length=320)
self.logmel_extractor = torchlibrosa.stft.LogmelFilterBank(
sr=32000,
n_fft=1024,
n_mels=nmels,
top_db=None,
fmin=20,
fmax=16000,
is_log=False)
def forward(self, sound):
spec = self.spectrogram_extractor(sound)
spec = self.logmel_extractor(spec)
spec = torch.log(1e-8 + spec)
return spec / 100.
class Cnn14_DecisionLevelAtt(nn.Module):
def __init__(self, classes_num, input_size=64):
super(Cnn14_DecisionLevelAtt, self).__init__()
self.interpolate_ratio = 32 # Downsampled ratio
self.bn0 = nn.BatchNorm2d(input_size)
self.conv_block1 = ConvBlock(in_channels=1, out_channels=64)
self.conv_block2 = ConvBlock(in_channels=64, out_channels=128)
self.conv_block3 = ConvBlock(in_channels=128, out_channels=256)
self.conv_block4 = ConvBlock(in_channels=256, out_channels=512)
self.conv_block5 = ConvBlock(in_channels=512, out_channels=1024)
self.conv_block6 = ConvBlock(in_channels=1024, out_channels=2048)
self.fc1 = nn.Linear(2048, 2048, bias=True)
self.att_block = AttBlock(2048, classes_num, activation="sigmoid")
self.init_weight()
#self.logmel = LogMel(input_size)
def init_weight(self):
init_bn(self.bn0)
init_layer(self.fc1)
def forward(self, x):
x = x.transpose(2, 3)
frames_num = x.shape[2]
x = x.transpose(1, 3)
x = self.bn0(x)
x = x.transpose(1, 3)
x = self.conv_block1(x, pool_size=(2, 2), pool_type="avg")
x = F.dropout(x, p=0.2, training=self.training)
x = self.conv_block2(x, pool_size=(2, 2), pool_type="avg")
x = F.dropout(x, p=0.2, training=self.training)
x = self.conv_block3(x, pool_size=(2, 2), pool_type="avg")
x = F.dropout(x, p=0.2, training=self.training)
x = self.conv_block4(x, pool_size=(2, 2), pool_type="avg")
x = F.dropout(x, p=0.2, training=self.training)
x = self.conv_block5(x, pool_size=(2, 2), pool_type="avg")
x = F.dropout(x, p=0.2, training=self.training)
x = self.conv_block6(x, pool_size=(1, 1), pool_type="avg")
x = F.dropout(x, p=0.2, training=self.training)
x = torch.mean(x, dim=3)
x1 = F.max_pool1d(x, kernel_size=3, stride=1, padding=1)
x2 = F.avg_pool1d(x, kernel_size=3, stride=1, padding=1)
x = x1 + x2
x = F.dropout(x, p=0.5, training=self.training)
x = x.transpose(1, 2)
x = F.relu_(self.fc1(x))
x = x.transpose(1, 2)
x = F.dropout(x, p=0.5, training=self.training)
(clipwise_output, _, segmentwise_output) = self.att_block(x)
segmentwise_output = segmentwise_output.transpose(1, 2)
# Get framewise output
framewise_output = interpolate(segmentwise_output, self.interpolate_ratio)
framewise_output = pad_framewise_output(framewise_output, frames_num)
output_dict = {
"framewise_output": framewise_output,
"clipwise_output": clipwise_output,
}
# print(clipwise_output.min(), clipwise_output.max())
return clipwise_output, framewise_output
class PANNsLoss(nn.Module):
def __init__(self):
super().__init__()
self.bce = nn.BCELoss()
def forward(self, input_, target):
input_ = torch.where(torch.isnan(input_), torch.zeros_like(input_), input_)
input_ = torch.where(torch.isinf(input_), torch.zeros_like(input_), input_)
input_ = torch.clamp(input_, min=0.0, max=1.0)
target = target.float()
return self.bce(input_, target)