-
Notifications
You must be signed in to change notification settings - Fork 5
/
models.py
332 lines (273 loc) · 11.4 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
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
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn.utils import weight_norm
import random
class residual_stack(nn.Module):
def __init__(self, size, dilation):
super().__init__()
self.block = nn.Sequential(
nn.LeakyReLU(0.2),
nn.ReflectionPad1d(dilation),
weight_norm(nn.Conv1d(size, size, kernel_size=3, dilation=dilation)),
nn.LeakyReLU(0.2),
weight_norm(nn.Conv1d(size, size, kernel_size=1))
)
self.shortcut = weight_norm(nn.Conv1d(size, size, kernel_size=1))
def forward(self, x):
return self.block(x) + self.shortcut(x)
def encoder_sequential(input_size, output_size, *args, **kwargs):
return nn.Sequential(
nn.LeakyReLU(0.2),
weight_norm((nn.ConvTranspose1d(input_size, output_size, *args, **kwargs)))
)
#-------------melgan генератор-----------------
class GeneratorMel(nn.Module):
def __init__(self, mel_dim):
super().__init__()
factor = [8, 8, 2, 2]
layers = [
nn.ReflectionPad1d(3),
weight_norm(nn.Conv1d(mel_dim, 512, kernel_size=7)),
]
input_size = 512
for f in factor:
layers += [encoder_sequential(input_size,
input_size // 2,
kernel_size=f * 2,
stride=f,
padding=f // 2 + f % 2)]
input_size //= 2
for d in range(3):
layers += [residual_stack(input_size, 3 ** d)]
layers += [
nn.LeakyReLU(0.2),
nn.ReflectionPad1d(3),
weight_norm(nn.Conv1d(32, 1, kernel_size=7)),
nn.Tanh(),
]
self.generator = nn.Sequential(*layers)
def forward(self, x):
return self.generator(x)
def decoder_sequential(input_size, output_size, *args, **kwargs):
return nn.Sequential(
weight_norm((nn.Conv1d(input_size, output_size, *args, **kwargs))),
nn.LeakyReLU(0.2, inplace=True)
)
#-------------Дискриминатор-----------------
class Discriminator(nn.Module):
def __init__(self):
super().__init__()
# Каждый Дискриминатор
self.discriminator = nn.ModuleList([
# Получаем 16 фич
nn.Sequential(
nn.ReflectionPad1d(7),
weight_norm(nn.Conv1d(1, 16, kernel_size=15)),
nn.LeakyReLU(0.2, inplace=True) # изменить вход
),
# Четыре понижения размерности в 4 раза
decoder_sequential(16, 64, kernel_size=41, stride=4, padding=20, groups=4),
decoder_sequential(64, 256, kernel_size=41, stride=4, padding=20, groups=16),
decoder_sequential(256, 1024, kernel_size=41, stride=4, padding=20, groups=64),
decoder_sequential(1024, 1024, kernel_size=41, stride=4, padding=20, groups=256),
# Фичи 1024*32(16,8)
nn.Sequential(
weight_norm(nn.Conv1d(1024, 1024, kernel_size=5, padding=2)),
nn.LeakyReLU(0.2, inplace=True)
),
# Выход 1*32(16,8)
weight_norm(nn.Conv1d(1024, 1, kernel_size=3, padding=1))
])
def forward(self, x):
feature_map = []
for module in self.discriminator:
x = module(x)
feature_map.append(x)
return feature_map
#-------------3 Дискриминатора-----------------
class MultiScale(nn.Module):
def __init__(self):
super().__init__()
# Каждому Дискриминатору свой блок
self.block = nn.ModuleList([
Discriminator() for _ in range(3)
])
# Блок понижающий размерность сигнала
self.avgpool = nn.AvgPool1d(kernel_size=4, stride=2, padding=1, count_include_pad=False)
def forward(self, x):
result = []
# Посчитать выходы 3 дискриминаторов
for idx, module in enumerate(self.block):
result.append(module(x))
if idx <= 1:
# понизить размерность сигнала
x = self.avgpool(x)
return result
#------------Генератор для wavgan------------------
class GeneratorWav(nn.Module):
def __init__(self):
super().__init__()
self.Conv_1 = nn.Sequential(
nn.ReflectionPad1d(7),
weight_norm(nn.Conv1d(1, 16, kernel_size=15)),
nn.LeakyReLU(0.2)
)
self.Conv_2 = nn.Sequential(
weight_norm(nn.Conv1d(16, 64, kernel_size=41, stride=4, padding=20, groups=4)),
nn.LeakyReLU(0.2)
)
self.Conv_3 = nn.Sequential(
weight_norm(nn.Conv1d(64, 256, kernel_size=41, stride=4, padding=20, groups=16)),
nn.LeakyReLU(0.2)
)
self.Conv_4 = nn.Sequential(
weight_norm(nn.Conv1d(256, 1024, kernel_size=41, stride=4, padding=20, groups=64)),
residual_stack(1024, 3),
residual_stack(1024, 9),
nn.LeakyReLU(0.2)
)
self.ConvTrans_4 = nn.Sequential(
weight_norm(nn.ConvTranspose1d(1024, 256, kernel_size=16, stride=4, padding=6)),
residual_stack(256, 3),
residual_stack(256, 9),
nn.LeakyReLU(0.2)
)
self.ConvTrans_3 = nn.Sequential(
weight_norm(nn.ConvTranspose1d(256, 64, kernel_size=16, stride=4, padding=6)),
residual_stack(64, 3),
residual_stack(64, 9),
nn.LeakyReLU(0.2)
)
self.ConvTrans_2 = nn.Sequential(
weight_norm(nn.ConvTranspose1d(64, 16, kernel_size=16, stride=4, padding=6)),
residual_stack(16, 3),
residual_stack(16, 9),
nn.LeakyReLU(0.2)
)
self.ConvTrans_1 = nn.Sequential(
nn.ReflectionPad1d(3),
weight_norm(nn.Conv1d(16, 1, kernel_size=7)),
nn.Tanh()
)
def forward(self, x):
self.x1 = self.Conv_1(x)
self.x2 = self.Conv_2(self.x1)
self.x3 = self.Conv_3(self.x2)
self.x4 = self.Conv_4(self.x3)
self.x4 = self.ConvTrans_4(self.x4) + self.x3
self.x3 = self.ConvTrans_3(self.x4) + self.x2
self.x2 = self.ConvTrans_2(self.x3) + self.x1
self.x1 = self.ConvTrans_1(self.x2)
return self.x1
#-------------------------AGAIN-----------------------------
# Нормирующий блок и вычисляющий среднее значение и дисперсию
class InstanceNorm(nn.Module):
def __init__(self, eps=1e-5):
super().__init__()
self.eps = eps
def calc_mean_std(self, x, mask=None):
B, C = x.shape[:2]
mn = x.view(B, C, -1).mean(-1)
sd = (x.view(B, C, -1).var(-1) + self.eps).sqrt()
mn = mn.view(B, C, *((len(x.shape) - 2) * [1]))
sd = sd.view(B, C, *((len(x.shape) - 2) * [1]))
return mn, sd
def forward(self, x, return_mean_std=False):
# Вычисляем среднее значение и дисперсию
mean, std = self.calc_mean_std(x)
# Нормируем
x = (x - mean) / std
# Возвращаем нормированный выход
if return_mean_std:
return x, mean, std
else:
return x
# Сверточный блок в кодировщике
class ConvBlock(nn.Module):
def __init__(self, c_h):
super().__init__()
self.seq = nn.Sequential(
nn.Conv1d(c_h, c_h, kernel_size=3, padding=1),
nn.BatchNorm1d(c_h),
nn.LeakyReLU()
)
def forward(self, x):
y = self.seq(x)
return x + y
# Кодировщик
class Encoder(nn.Module):
def __init__(self, c_in, c_out, n_conv_blocks, c_h):
super().__init__()
self.inorm = InstanceNorm()
self.in_layer = nn.Conv1d(c_in, c_h, kernel_size=1)
self.conv1d_blocks = nn.ModuleList([ConvBlock(c_h)
for _ in range(n_conv_blocks)
])
self.out_layer = nn.Conv1d(c_h, c_out, kernel_size=1)
self.alpha = 0.1
def forward(self, x):
# Входная свертка
y = self.in_layer(x)
mns = []
sds = []
for block in self.conv1d_blocks:
# Ценртальные свертки
y = block(y)
# Нормируем и запоминаем среднее и дисперсию
y, mn, sd = self.inorm(y, return_mean_std=True)
mns.append(mn)
sds.append(sd)
# Выходная свертка
y = self.out_layer(y)
# Активация
y = 1 / (1+torch.exp(-self.alpha*y))
return (y, mns, sds)
# Декодер
class Decoder(nn.Module):
def __init__(self, c_in, c_h, c_out, n_conv_blocks):
super().__init__()
self.in_layer = nn.Conv1d(c_in, c_h, kernel_size=3, padding=1)
self.conv_blocks = nn.ModuleList([ConvBlock(c_h)
for _ in range(n_conv_blocks)
])
self.rnn = nn.GRU(c_h, c_h, 2)
self.out_layer = nn.Linear(c_h, c_out)
self.act = nn.LeakyReLU()
self.inorm = InstanceNorm()
def forward(self, enc, cond):
y1, _, _ = enc
y2, mns, sds = cond
# Нормируем сигнал передаём ему характеристики другого сигнала
mn, sd = self.inorm.calc_mean_std(y2)
c = self.inorm(y1)
c_affine = c * sd + mn
# Входная свертка
y = self.in_layer(c_affine)
y = self.act(y)
# Нормируем сигнал и передаём ему характеристики другого сигнала
for i, (block, mn, sd) in enumerate(zip(self.conv_blocks, mns, sds)):
y = block(y)
y = self.inorm(y)
y = y * sd + mn
# Пропускаем через RNN
y = torch.cat((mn, y), dim=2)
y = y.transpose(1,2)
y, _ = self.rnn(y)
y = y[:,1:,:]
# Выходная свертка
y = self.out_layer(y)
y = y.transpose(1,2)
return y
# Модель Again
class AgainModel(nn.Module):
def __init__(self):
super().__init__()
self.encoder = Encoder(c_in = 80, c_h = 256, c_out = 4, n_conv_blocks = 6)
self.decoder = Decoder(c_in = 4, c_h = 256, c_out = 80, n_conv_blocks = 6)
def forward(self, source, target = None):
if target is None:
a = random.randint(0, source.shape[2])
target = torch.cat((source[:,:,a:], source[:,:,:a]), axis=2)
y = self.decoder(self.encoder(source), self.encoder(target))
return y