-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCRNN_baseline.py
136 lines (118 loc) · 4.39 KB
/
CRNN_baseline.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
"""
CRNN baseline taken from
- Castorena, C., Cobos, M., Lopez-Ballester, J., & Ferri, F. J. (2023). A safety-oriented framework for sound event detection in driving scenarios. Applied Acoustics, 215, 109719.
- Ronchini, F., Serizel, R., Turpault, N., & Cornell, S. (2021). The impact of non-target events in synthetic soundscapes for sound event detection. arXiv preprint arXiv:2109.14061.
"""
import torch
class ConvBlockCRNN(torch.nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, pool_size, do_pool=True):
super(ConvBlockCRNN, self).__init__()
self.conv = torch.nn.Conv2d(
in_channels=in_channels,
out_channels=out_channels,
kernel_size=kernel_size,
padding="same",
)
self.bn = torch.nn.BatchNorm2d(out_channels)
self.GLU = torch.nn.GLU()
self.pool = torch.nn.AvgPool2d(kernel_size=pool_size)
self.do_pool = do_pool
def forward(self, x):
x = self.conv(x)
x = self.bn(x)
x = torch.concatenate(
(x, x), dim=-1
)
x = self.GLU(x)
if self.do_pool:
x = self.pool(x)
return x
class CRNNBaseline(torch.nn.Module):
def __init__(
self,
n_mels,
kernel_size=3,
filters=[16, 32, 64, 128, 128, 128, 128],
pool_sizes=[(2, 2), (2, 2), (1, 2), (1, 2), (1, 2), (1, 2), (1, 2)],
):
super(CRNNBaseline, self).__init__()
if n_mels == 64:
do_pool_last = False
if n_mels == 128:
do_pool_last = True
self.filters = filters
self.kernel_size = kernel_size
self.pool_size = pool_sizes
# Conv Blocks
self.convblock1 = ConvBlockCRNN(
in_channels=1,
out_channels=filters[0],
kernel_size=kernel_size,
pool_size=self.pool_size[0],
)
self.convblock2 = ConvBlockCRNN(
in_channels=filters[0],
out_channels=filters[1],
kernel_size=kernel_size,
pool_size=self.pool_size[1],
)
self.convblock3 = ConvBlockCRNN(
in_channels=filters[1],
out_channels=filters[2],
kernel_size=kernel_size,
pool_size=self.pool_size[2],
)
self.convblock4 = ConvBlockCRNN(
in_channels=filters[2],
out_channels=filters[3],
kernel_size=kernel_size,
pool_size=self.pool_size[3],
)
self.convblock5 = ConvBlockCRNN(
in_channels=filters[3],
out_channels=filters[4],
kernel_size=kernel_size,
pool_size=self.pool_size[4],
)
self.convblock6 = ConvBlockCRNN(
in_channels=filters[4],
out_channels=filters[5],
kernel_size=kernel_size,
pool_size=self.pool_size[5],
)
self.convblock7 = ConvBlockCRNN(
in_channels=filters[5],
out_channels=filters[6],
kernel_size=kernel_size,
pool_size=self.pool_size[6],
do_pool=do_pool_last,
)
# Bidirectional GRU
self.GRU = torch.nn.GRU(
input_size=128, hidden_size=128, num_layers=2, bidirectional=True
)
# Now let's do the classification head
self.flatten = torch.nn.Flatten()
self.dense = torch.nn.Linear(in_features=2816, out_features=10)
def forward(self, x):
# INPUT SHOULD HAVE SIZE: BATCH X CHANNEL X TIME X FREQ (so we permute input tensor)
x_input = torch.permute(
x, (0, 1, 3, 2)
) # Luca: N.B. salamon applies 4X2 pooling over TXF axis, but specs are returned in FXT form, so the reshaping
x = self.convblock1(x_input)
x = self.convblock2(x)
x = self.convblock3(x)
x = self.convblock4(x)
x = self.convblock5(x)
x = self.convblock6(x)
x = self.convblock7(x)
# Output at this point must be BATCH X TIME X FREQ X CHANNEL
x = torch.permute(x, dims=(0, 2, 1, 3)).squeeze(-1)
# Let's apply bidirectional GRU
x, _ = self.GRU(x)
# Here is different from the paper N.B. the original baseline gives classification
# Output per time instant, here we want to give a single classification output for the
# whole audio track
x = self.flatten(x)
logits = self.dense(x)
return logits