-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdoanet_model.py
213 lines (160 loc) · 7.65 KB
/
doanet_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
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
#
# The SELDnet architecture
#
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from IPython import embed
class MultiHeadAttentionLayer(nn.Module):
def __init__(self, hid_dim, n_heads, dropout):
super().__init__()
assert hid_dim % n_heads == 0
self.hid_dim = hid_dim
self.n_heads = n_heads
self.head_dim = hid_dim // n_heads
self.fc_q = nn.Linear(hid_dim, hid_dim)
self.fc_k = nn.Linear(hid_dim, hid_dim)
self.fc_v = nn.Linear(hid_dim, hid_dim)
self.fc_o = nn.Linear(hid_dim, hid_dim)
self.dropout = nn.Dropout(dropout)
def forward(self, query, key, value, mask=None):
batch_size = query.shape[0]
#query = [batch size, query len, hid dim]
#key = [batch size, key len, hid dim]
#value = [batch size, value len, hid dim]
Q = self.fc_q(query)
K = self.fc_k(key)
V = self.fc_v(value)
#Q = [batch size, query len, hid dim]
#K = [batch size, key len, hid dim]
#V = [batch size, value len, hid dim]
Q = Q.view(batch_size, -1, self.n_heads, self.head_dim).permute(0, 2, 1, 3)
K = K.view(batch_size, -1, self.n_heads, self.head_dim).permute(0, 2, 1, 3)
V = V.view(batch_size, -1, self.n_heads, self.head_dim).permute(0, 2, 1, 3)
#Q = [batch size, n heads, query len, head dim]
#K = [batch size, n heads, key len, head dim]
#V = [batch size, n heads, value len, head dim]
energy = torch.div(torch.matmul(Q, K.permute(0, 1, 3, 2)), np.sqrt(self.head_dim))
#energy = [batch size, n heads, query len, key len]
if mask is not None:
energy = energy.masked_fill(mask == 0, -1e10)
attention = torch.softmax(energy, dim = -1)
#attention = [batch size, n heads, query len, key len]
x = torch.matmul(self.dropout(attention), V)
#x = [batch size, n heads, query len, head dim]
x = x.permute(0, 2, 1, 3).contiguous()
#x = [batch size, query len, n heads, head dim]
x = x.view(batch_size, -1, self.hid_dim)
#x = [batch size, query len, hid dim]
x = self.fc_o(x)
#x = [batch size, query len, hid dim]
return x
class AttentionLayer(nn.Module):
def __init__(self, in_channels, out_channels, key_channels):
super(AttentionLayer, self).__init__()
self.conv_Q = nn.Conv1d(in_channels, key_channels, kernel_size=1, bias=False)
self.conv_K = nn.Conv1d(in_channels, key_channels, kernel_size=1, bias=False)
self.conv_V = nn.Conv1d(in_channels, out_channels, kernel_size=1, bias=False)
def forward(self, x):
Q = self.conv_Q(x)
K = self.conv_K(x)
V = self.conv_V(x)
A = Q.permute(0, 2, 1).matmul(K).softmax(2)
x = A.matmul(V.permute(0, 2, 1)).permute(0, 2, 1)
return x
def __repr__(self):
return self._get_name() + \
'(in_channels={}, out_channels={}, key_channels={})'.format(
self.conv_Q.in_channels,
self.conv_V.out_channels,
self.conv_K.out_channels
)
class ConvBlock(torch.nn.Module):
def __init__(self, in_channels, out_channels, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)):
super().__init__()
self.conv = torch.nn.Conv2d(in_channels=in_channels,
out_channels=out_channels,
kernel_size=kernel_size,
stride=stride,
padding=padding)
self.bn = torch.nn.BatchNorm2d(out_channels)
def forward(self, x):
x = torch.relu_(self.bn(self.conv(x)))
return x
class CRNN(torch.nn.Module):
def __init__(self, in_feat_shape, out_shape, params):
super().__init__()
self.use_hnet = params['use_hnet']
self.use_activity_out = not params['use_dmot_only']
self.conv_block_list = torch.nn.ModuleList()
if len(params['f_pool_size']):
for conv_cnt in range(len(params['f_pool_size'])):
self.conv_block_list.append(
ConvBlock(
in_channels=params['nb_cnn2d_filt'] if conv_cnt else in_feat_shape[1],
out_channels=params['nb_cnn2d_filt']
)
)
self.conv_block_list.append(
torch.nn.MaxPool2d((params['t_pool_size'][conv_cnt], params['f_pool_size'][conv_cnt]))
)
self.conv_block_list.append(
torch.nn.Dropout2d(p=params['dropout_rate'])
)
if params['nb_rnn_layers']:
self.in_gru_size = int(params['nb_cnn2d_filt'] * (in_feat_shape[-1] / np.prod(params['f_pool_size'])))
self.gru = torch.nn.GRU(input_size=self.in_gru_size, hidden_size=params['rnn_size'],
num_layers=params['nb_rnn_layers'], batch_first=True,
dropout=params['dropout_rate'], bidirectional=True)
self.attn = None
if params['self_attn']:
# self.attn = AttentionLayer(params['rnn_size'], params['rnn_size'], params['rnn_size'])
self.attn = MultiHeadAttentionLayer(params['rnn_size'], params['nb_heads'], params['dropout_rate'])
self.fnn_list = torch.nn.ModuleList()
if params['nb_rnn_layers'] and params['nb_fnn_layers']:
for fc_cnt in range(params['nb_fnn_layers']):
self.fnn_list.append(
torch.nn.Linear(params['fnn_size'] if fc_cnt else params['rnn_size'] , params['fnn_size'], bias=True)
)
self.fnn_list.append(
torch.nn.Linear(params['fnn_size'] if params['nb_fnn_layers'] else params['rnn_size'], out_shape[-1], bias=True)
)
# Branch for activity detection
self.fnn_act_list = torch.nn.ModuleList()
if self.use_hnet and self.use_activity_out:
for fc_cnt in range(params['nb_fnn_act_layers']):
self.fnn_act_list.append(
torch.nn.Linear(params['fnn_act_size'] if fc_cnt else params['rnn_size'] , params['fnn_act_size'], bias=True)
)
self.fnn_act_list.append(
torch.nn.Linear(params['fnn_act_size'] if params['nb_fnn_act_layers'] else params['rnn_size'], params['unique_classes'], bias=True)
)
def forward(self, x):
'''input: (batch_size, mic_channels, time_steps, mel_bins)'''
for conv_cnt in range(len(self.conv_block_list)):
x = self.conv_block_list[conv_cnt](x)
'''(batch_size, feature_maps, time_steps, mel_bins)'''
x = x.transpose(1, 2).contiguous()
x = x.view(x.shape[0], x.shape[1], -1).contiguous()
''' (batch_size, time_steps, feature_maps):'''
(x, _) = self.gru(x)
x = torch.tanh(x)
x = x[:, :, x.shape[-1]//2:] * x[:, :, :x.shape[-1]//2]
'''(batch_size, time_steps, feature_maps)'''
if self.attn is not None:
x = self.attn.forward(x, x, x)
# out - batch x hidden x seq
x = torch.tanh(x)
x_rnn = x
for fnn_cnt in range(len(self.fnn_list)-1):
x = torch.relu_(self.fnn_list[fnn_cnt](x))
doa = torch.tanh(self.fnn_list[-1](x))
'''(batch_size, time_steps, label_dim)'''
if self.use_hnet and self.use_activity_out:
for fnn_cnt in range(len(self.fnn_act_list)-1):
x_rnn = torch.relu_(self.fnn_act_list[fnn_cnt](x_rnn))
activity = torch.tanh(self.fnn_act_list[-1](x_rnn))
return doa, activity
else:
return doa