This repository has been archived by the owner on Aug 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
model.py
215 lines (187 loc) · 6.97 KB
/
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
214
215
import torch
import torch.nn.functional as F
from torch import nn
class ResnetBlock(nn.Module):
"""Residual Block
Args:
in_channels (int): number of channels in input data
out_channels (int): number of channels in output
"""
def __init__(self, in_channels, out_channels, kernel_size=3, one_d=False):
super(ResnetBlock, self).__init__()
self.build_conv_block(in_channels, out_channels, one_d, kernel_size=kernel_size)
def build_conv_block(self, in_channels, out_channels, one_d, kernel_size=3):
padding = (kernel_size -1)//2
if not one_d:
conv = nn.Conv2d
norm = nn.BatchNorm2d
else:
conv = nn.Conv1d
norm = nn.BatchNorm1d
self.conv1 = nn.Sequential(
conv(in_channels, out_channels, kernel_size=kernel_size, padding=padding),
norm(out_channels),
nn.ELU()
)
self.conv2 = nn.Sequential(
conv(out_channels, out_channels, kernel_size=kernel_size, padding=padding),
norm(out_channels),
)
if in_channels != out_channels:
self.down = nn.Sequential(
conv(in_channels, out_channels, kernel_size=1, bias=False),
norm(out_channels)
)
else:
self.down = None
self.act = nn.ELU()
def forward(self, x):
"""
Args:
x (Tensor): B x C x T
"""
residual = x
out = self.conv1(x)
out = self.conv2(out)
if self.down is not None:
residual = self.down(residual)
return self.act(out + residual)
class UpsamplingLayer(nn.Module):
"""Applies 1D upsampling operator over input tensor.
Args:
in_channels (int): number of input channels
out_channels (int): number of output channels
residuals (int, optional): number of residual blocks. Default=0
"""
def __init__(self, in_channels, out_channels, residuals=0):
super(UpsamplingLayer, self).__init__()
# TODO: try umsampling with bilinear interpolation
self.upsample = nn.Upsample(scale_factor=2, mode='linear')
self.conv = nn.Conv1d(in_channels, out_channels, kernel_size=3, padding=1)
torch.nn.init.xavier_uniform_(self.conv.weight)
self.bn = nn.BatchNorm1d(out_channels)
self.act = nn.ELU()
if residuals != 0:
# resnet blocks
layers = []
for _ in range(residuals):
layers.append(
ResnetBlock(out_channels, out_channels, one_d=True)
)
self.res_blocks = nn.Sequential(*layers)
else:
self.res_blocks = None
def forward(self, x):
"""
Args:
x (Tensor): B x in_channels x T
Returns:
Tensor of shape (B, out_channels, T x 2)
"""
# upsample network
B, C, T = x.shape
# upsample
# x = x.unsqueeze(dim=3)
# x = F.upsample(x, size=(T*2, 1), mode='bilinear').squeeze(3)
x = self.upsample(x)
# x = self.pad(x)
x = self.conv(x)
x = self.bn(x)
x = self.act(x)
# pass through resnet blocks to improve internal representations
# of data
if self.res_blocks != None:
x = self.res_blocks(x)
return x
class ConvBlock(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size=3, padding=1):
super(ConvBlock, self).__init__()
self.conv = nn.Conv2d(in_channels, out_channels, kernel_size=kernel_size, padding=padding)
self.bn = nn.BatchNorm2d(out_channels)
self.act = nn.ELU()
def forward(self, x):
x = self.conv(x)
x = self.bn(x)
x = self.act(x)
return x
class Head(nn.Module):
"""Head module
Args:
channels (list): list of #channels in each upsampling layer
pre_residuals (int, optional): number of residual blocks before upsampling. Default: 64
down_conv_channels (list): list of #channels in each down_conv blocks
up_residuals (int, optional): number of residual blocks in each upsampling module. Default: 0
"""
def __init__(self, channels,
pre_residuals=64,
pre_conv_channels=[64, 32, 16, 8, 4],
up_residuals=0,
post_residuals=2):
super(Head, self).__init__()
pre_convs = []
c0 = pre_conv_channels[0]
pre_convs.append(ConvBlock(1, c0, kernel_size=3, padding=1))
for _ in range(pre_residuals):
pre_convs.append(ResnetBlock(c0, c0))
for i in range(len(pre_conv_channels) -1):
in_c = pre_conv_channels[i]
out_c = pre_conv_channels[i + 1]
pre_convs.append(ResnetBlock(in_c, out_c))
for _ in range(pre_residuals):
pre_convs.append(ResnetBlock(out_c, out_c))
self.pre_conv = nn.Sequential(*pre_convs)
up_layers = []
for i in range(len(channels) - 1):
in_channels = channels[i]
out_channels = channels[i + 1]
layer = UpsamplingLayer(in_channels, out_channels, residuals=up_residuals)
up_layers.append(layer)
self.upsampling = nn.Sequential(*up_layers)
post_convs = []
last_channels = channels[-1]
for i in range(post_residuals):
post_convs.append(ResnetBlock(last_channels, last_channels, one_d=True, kernel_size=5))
self.post_conv = nn.Sequential(*post_convs)
def forward(self, x):
"""
forward pass
Args:
x (Tensor): B x C x T
Returns:
Tensor: B x C x (2^#channels * T)
"""
x = x.unsqueeze(1) # reshape to [B x 1 x C x T]
x = self.pre_conv(x)
s1, _, _, s4 = x.shape
x = x.reshape(s1, -1, s4)
x = self.upsampling(x)
x2 = self.post_conv(x)
return x, x2
DEFAULT_LAYERS_PARAMS = [80, 128, 128, 64, 64, 32, 16, 8, 1]
class CNNVocoder(nn.Module):
"""CNN Vocoder
Args:
n_heads (int): Number of heads
layer_channels (list): list of #channels of each layer
"""
def __init__(self, n_heads=3,
layer_channels=DEFAULT_LAYERS_PARAMS,
pre_conv_channels=[64, 32, 16, 8, 4],
pre_residuals=64,
up_residuals=0,
post_residuals=3):
super(CNNVocoder, self).__init__()
self.head = Head(layer_channels,
pre_conv_channels=pre_conv_channels,
pre_residuals=pre_residuals, up_residuals=up_residuals,
post_residuals=post_residuals)
self.linear = nn.Linear(layer_channels[-1], 1)
self.act_fn = nn.Softsign()
def forward(self, x):
b = x.shape[0]
pre, post = self.head(x)
rs0 = self.linear(pre.transpose(1, 2))
rs0 = self.act_fn(rs0).squeeze(-1)
rs1 = self.linear(post.transpose(1, 2))
rs1 = self.act_fn(rs1).squeeze(-1)
return rs0, rs1