-
Notifications
You must be signed in to change notification settings - Fork 3
/
models.py
182 lines (152 loc) · 6.82 KB
/
models.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
import torch.nn as nn
import torch.nn.functional as F
import torch
from torch.autograd import Variable
class CNNNet(nn.Module):
def __init__(self, n_frames=200, n_feats=40, kernel=5, max_pooling=2):
super(CNNNet, self).__init__()
self.conv1 = nn.Conv2d(1, 20, kernel_size=kernel)
conv1_outsize = (20, n_feats - kernel + 1, n_frames - kernel + 1)
self.conv2 = nn.Conv2d(20, 20, kernel_size=kernel)
conv2_outsize = (conv1_outsize[0], int(conv1_outsize[1] / max_pooling - kernel + 1),
int(conv1_outsize[2] / max_pooling - kernel + 1))
self.conv2_drop = nn.Dropout2d()
self.fc1 = nn.Linear(
int(conv2_outsize[0] * (conv2_outsize[1] / max_pooling) * (conv2_outsize[2] / max_pooling)), 500)
self.fc2 = nn.Linear(500, 248)
self.max_pooling = max_pooling
def forward(self, x):
x = self.conv1(x.unsqueeze(1))
x = F.relu(F.max_pool2d(x, self.max_pooling))
x = self.conv2(x)
x = F.relu(F.max_pool2d(self.conv2_drop(x), self.max_pooling))
x = x.view(x.size(0), -1)
x = F.relu(self.fc1(x))
x = F.dropout(x, training=self.training)
x = self.fc2(x)
# return torch.sigmoid(x)
return x
class _LayerNorm(nn.Module):
"""Layer Normalization base class."""
def __init__(self, channel_size):
super(_LayerNorm, self).__init__()
self.channel_size = channel_size
self.gamma = nn.Parameter(torch.ones(channel_size),
requires_grad=True)
self.beta = nn.Parameter(torch.zeros(channel_size),
requires_grad=True)
def apply_gain_and_bias(self, normed_x):
""" Assumes input of size `[batch, chanel, *]`. """
return (self.gamma * normed_x.transpose(1, -1) +
self.beta).transpose(1, -1)
EPS = 1e-8
class GlobLN(_LayerNorm):
"""Global Layer Normalization (globLN)."""
def forward(self, x):
""" Applies forward pass.
Works for any input size > 2D.
Args:
x (:class:`torch.Tensor`): Shape `[batch, chan, *]`
Returns:
:class:`torch.Tensor`: gLN_x `[batch, chan, *]`
"""
dims = list(range(1, len(x.shape)))
mean = x.mean(dim=dims, keepdim=True)
var = torch.pow(x - mean, 2).mean(dim=dims, keepdim=True)
return self.apply_gain_and_bias((x - mean) / (var + EPS).sqrt())
class Conv1DBlock(nn.Module):
"""One dimensional convolutional block, as proposed in [1].
Args:
in_chan (int): Number of input channels.
hid_chan (int): Number of hidden channels in the depth-wise
convolution.
skip_out_chan (int): Number of channels in the skip convolution.
If 0 or None, `Conv1DBlock` won't have any skip connections.
Corresponds to the the block in v1 or the paper. The `forward`
return res instead of [res, skip] in this case.
kernel_size (int): Size of the depth-wise convolutional kernel.
padding (int): Padding of the depth-wise convolution.
dilation (int): Dilation of the depth-wise convolution.
norm_type (str, optional): Type of normalization to use. To choose from
- ``'gLN'``: global Layernorm
- ``'cLN'``: channelwise Layernorm
- ``'cgLN'``: cumulative global Layernorm
References:
[1] : "Conv-TasNet: Surpassing ideal time-frequency magnitude masking
for speech separation" TASLP 2019 Yi Luo, Nima Mesgarani
https://arxiv.org/abs/1809.07454
"""
def __init__(self, in_chan, hid_chan, kernel_size, padding,
dilation, ):
super(Conv1DBlock, self).__init__()
conv_norm = GlobLN
in_conv1d = nn.Conv1d(in_chan, hid_chan, 1)
depth_conv1d = nn.Conv1d(hid_chan, hid_chan, kernel_size,
padding=padding, dilation=dilation,
groups=hid_chan)
self.shared_block = nn.Sequential(in_conv1d, nn.PReLU(),
conv_norm(hid_chan), depth_conv1d,
nn.PReLU(), conv_norm(hid_chan))
self.res_conv = nn.Conv1d(hid_chan, in_chan, 1)
def forward(self, x):
""" Input shape [batch, feats, seq]"""
shared_out = self.shared_block(x)
res_out = self.res_conv(shared_out)
return res_out
class TCN(nn.Module):
# n blocks --> receptive field increases , n_repeats increases capacity mostly
def __init__(self, in_chan=40, n_src=1, out_chan=248, n_blocks=5, n_repeats=2,
bn_chan=64, hid_chan=128, kernel_size=3, ):
super(TCN, self).__init__()
self.in_chan = in_chan
self.n_src = n_src
out_chan = out_chan if out_chan else in_chan
self.out_chan = out_chan
self.n_blocks = n_blocks
self.n_repeats = n_repeats
self.bn_chan = bn_chan
self.hid_chan = hid_chan
self.kernel_size = kernel_size
layer_norm = GlobLN(in_chan)
bottleneck_conv = nn.Conv1d(in_chan, bn_chan, 1)
self.bottleneck = nn.Sequential(layer_norm, bottleneck_conv)
# Succession of Conv1DBlock with exponentially increasing dilation.
self.TCN = nn.ModuleList()
for r in range(n_repeats): #ripetizioni 2
for x in range(n_blocks): #5 layers convoluzionali
padding = (kernel_size - 1) * 2 ** x // 2
self.TCN.append(Conv1DBlock(bn_chan, hid_chan,
kernel_size, padding=padding,
dilation=2 ** x))
out_conv = nn.Linear(bn_chan, n_src * out_chan)
self.out = nn.Sequential(nn.PReLU(), out_conv)
# self.out = nn.Linear(bn_chan, n_src*out_chan)
# self.out = nn.Linear(800, n_src*out_chan)
# Get activation function.
def forward(self, mixture_w):
output = self.bottleneck(mixture_w)
for i in range(len(self.TCN)):
residual = self.TCN[i](output)
output = output + residual
###provare max pool2D su ouput seguito de reshape .view(-1,1)
logits = self.out(output.mean(-1))
# output = F.max_pool2d(output, 4).view(output.size(0),-1)
# logits = self.out(output)
return logits
def get_config(self):
config = {
'in_chan': self.in_chan,
'out_chan': self.out_chan,
'bn_chan': self.bn_chan,
'hid_chan': self.hid_chan,
'kernel_size': self.kernel_size,
'n_blocks': self.n_blocks,
'n_repeats': self.n_repeats,
'n_src': self.n_src,
'norm_type': self.norm_type,
}
return config
if __name__ == "__main__":
inp = torch.rand(6, 40, 400)
m = TCN(width=400)
print(m(inp).shape)