-
Notifications
You must be signed in to change notification settings - Fork 6
/
style_transfer.py
417 lines (346 loc) · 12.9 KB
/
style_transfer.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import os
import glob
import torch
import auraloss
import torchaudio
import numpy as np
import matplotlib.pyplot as plt
from tqdm import tqdm
from typing import List, Optional
from dasp_pytorch import ParametricEQ, Compressor, NoiseShapedReverb, Gain
def plot_loss(log_dir, loss_history: List[float]):
fig, ax = plt.subplots()
ax.plot(loss_history)
ax.set_xlabel("Iteration")
ax.set_ylabel("Loss")
plt.grid(c="lightgray")
outfilepath = os.path.join(log_dir, "loss.png")
plt.savefig(outfilepath, dpi=300)
plt.close("all")
class TCNBlock(torch.nn.Module):
def __init__(
self,
in_channels: int,
out_channels: int,
kernel_size: int,
dilation: int = 1,
):
super().__init__()
self.conv1 = torch.nn.Conv1d(
in_channels,
out_channels,
kernel_size,
dilation=dilation,
stride=2,
)
self.relu1 = torch.nn.PReLU(out_channels)
self.bn1 = torch.nn.BatchNorm1d(out_channels)
self.conv2 = torch.nn.Conv1d(
out_channels,
out_channels,
kernel_size,
dilation=1,
)
self.relu2 = torch.nn.PReLU(out_channels)
self.bn2 = torch.nn.BatchNorm1d(out_channels)
def forward(self, x: torch.Tensor):
x = self.bn1(self.relu1(self.conv1(x)))
x = self.bn2(self.relu2(self.conv2(x)))
return x
class Encoder(torch.nn.Module):
def __init__(self, embed_dim: int, ch_dim: int = 256) -> None:
super().__init__()
self.embed_dim = embed_dim
# we will use a simple TCN to estimate a single conditioning parameter
self.blocks = torch.nn.ModuleList()
self.blocks.append(TCNBlock(1, ch_dim, 7, dilation=1))
self.blocks.append(TCNBlock(ch_dim, ch_dim, 7, dilation=2))
self.blocks.append(TCNBlock(ch_dim, ch_dim, 7, dilation=4))
self.blocks.append(TCNBlock(ch_dim, ch_dim, 7, dilation=8))
self.blocks.append(TCNBlock(ch_dim, ch_dim, 7, dilation=16))
self.blocks.append(TCNBlock(ch_dim, ch_dim, 7, dilation=1))
self.blocks.append(TCNBlock(ch_dim, ch_dim, 7, dilation=2))
self.blocks.append(TCNBlock(ch_dim, ch_dim, 7, dilation=4))
self.blocks.append(TCNBlock(ch_dim, ch_dim, 7, dilation=8))
self.blocks.append(TCNBlock(ch_dim, ch_dim, 7, dilation=16))
self.mlp = torch.nn.Sequential(
torch.nn.Linear(ch_dim, 256),
torch.nn.ReLU(),
torch.nn.Linear(256, 256),
torch.nn.ReLU(),
torch.nn.Linear(256, embed_dim),
)
def forward(self, x: torch.Tensor):
for block in self.blocks:
x = block(x)
x = x.mean(dim=-1) # aggregate over time
return self.mlp(x) # map to latent
class ParameterProjector(torch.nn.Module):
def __init__(self, embed_dim: int, num_control_params: int, num_hidden: int = 256):
super().__init__()
self.embed_dim = embed_dim
self.num_hidden = num_hidden
self.num_control_params = num_control_params
self.layers = torch.nn.Sequential(
torch.nn.Linear(embed_dim, num_hidden),
torch.nn.ReLU(),
torch.nn.Linear(num_hidden, num_hidden),
torch.nn.ReLU(),
torch.nn.Linear(num_hidden, num_control_params),
)
def forward(self, x: torch.Tensor):
return torch.sigmoid(self.layers(x))
class StyleTransferModel(torch.nn.Module):
def __init__(self, sample_rate: int) -> None:
super().__init__()
# create efffects
self.equalizer = ParametricEQ(sample_rate)
self.compressor = Compressor(sample_rate)
self.reverb = NoiseShapedReverb(sample_rate)
self.gain = Gain(sample_rate)
# create networks
self.encoder = Encoder(512)
self.equalizer_projector = ParameterProjector(
self.encoder.embed_dim * 2, self.equalizer.num_params
)
self.compressor_projector = ParameterProjector(
self.encoder.embed_dim * 2, self.compressor.num_params
)
self.reverb_projector = ParameterProjector(
self.encoder.embed_dim * 2, self.reverb.num_params
)
self.gain_projector = ParameterProjector(
self.encoder.embed_dim * 2, self.gain.num_params
)
def forward(self, input: torch.Tensor, ref: torch.Tensor):
# process the input and reference with encoder
z_input = self.encoder(input)
z_ref = self.encoder(ref)
# combine the input and reference embeddings
z = torch.cat((z_input, z_ref), dim=-1)
# estimate parameters for each effect
equalizer_params = self.equalizer_projector(z)
compressor_params = self.compressor_projector(z)
reverb_params = self.reverb_projector(z)
gain_params = self.gain_projector(z)
# process audio with estimated parameters
y = input.clone()
y = self.equalizer.process_normalized(y, equalizer_params)
y = self.compressor.process_normalized(y, compressor_params)
y = self.reverb.process_normalized(y, reverb_params)
y = self.gain.process_normalized(y, gain_params)
return y
class AudioFileDataset(torch.nn.Module):
def __init__(
self,
filepaths: List[str],
length: int = 131072,
) -> None:
super().__init__()
self.length = length
assert len(filepaths) > 0, "No files found."
self.examples = []
# create example of length `length` from each file
print("Creating dataset...")
for filepath in tqdm(filepaths):
md = torchaudio.info(filepath)
if md.num_frames < length:
continue
num_examples = md.num_frames // length
for n in range(num_examples):
frame_offset = n * length
frame, sr = torchaudio.load(
filepath,
frame_offset=frame_offset,
num_frames=length,
backend="soundfile",
)
# check for silence
if torch.max(torch.abs(frame)) < 1e-4:
continue
self.examples.append((filepath, frame_offset))
self.examples = self.examples
def __len__(self):
return len(self.examples)
def __getitem__(self, idx: int):
filepath, frame_offset = self.examples[idx]
# read segment of audio from file
x, sr = torchaudio.load(
filepath,
frame_offset=frame_offset,
num_frames=self.length,
backend="soundfile",
)
# clamp to [-1,1] to ensure within range
x = torch.clamp(x, -1, 1)
return x
def validate(
model: torch.nn.Module,
val_dataloader: torch.utils.data.DataLoader,
num_examples: int = 1,
use_gpu: bool = False,
epoch: int = 0,
log_dir: str = "outputs/style_transfer",
):
model.eval()
for batch_idx, batch in enumerate(val_dataloader):
if batch_idx >= num_examples:
break
input = batch
if use_gpu:
input = input.cuda()
with torch.no_grad():
input_a, input_b, ref_a, ref_b, output_a = step(input, model)
# save audio examples
input_a_filepath = os.path.join(
log_dir, "audio", f"epoch={epoch}_input_a_{batch_idx}.wav"
)
input_b_filepath = os.path.join(
log_dir, "audio", f"epoch={epoch}_input_b_{batch_idx}.wav"
)
ref_a_filepath = os.path.join(
log_dir, "audio", f"epoch={epoch}_ref_a_{batch_idx}.wav"
)
ref_b_filepath = os.path.join(
log_dir, "audio", f"epoch={epoch}_ref_b_{batch_idx}.wav"
)
output_a_filepath = os.path.join(
log_dir, "audio", f"epoch={epoch}_output_a_{batch_idx}.wav"
)
torchaudio.save(
input_a_filepath, input_a.cpu().squeeze(0), 44100, backend="soundfile"
)
torchaudio.save(
input_b_filepath, input_b.cpu().squeeze(0), 44100, backend="soundfile"
)
torchaudio.save(
ref_a_filepath, ref_a.cpu().squeeze(0), 44100, backend="soundfile"
)
torchaudio.save(
ref_b_filepath, ref_b.cpu().squeeze(0), 44100, backend="soundfile"
)
torchaudio.save(
output_a_filepath, output_a.cpu().squeeze(0), 44100, backend="soundfile"
)
def step(input: torch.Tensor, model: torch.nn.Module):
# generate reference by randomly processing input
# torch.manual_seed(1)
rand_equalizer_params = torch.rand(
input.shape[0],
model.equalizer.num_params,
).type_as(input)
rand_compressor_params = torch.rand(
input.shape[0],
model.compressor.num_params,
).type_as(input)
rand_reverb_params = torch.rand(
input.shape[0],
model.reverb.num_params,
).type_as(input)
rand_gain_params = torch.rand(
input.shape[0],
model.gain.num_params,
).type_as(input)
# process input with random parameters
# randomly disable the effects
ref = input.clone()
# if torch.rand(1) < 0.5:
ref = model.equalizer.process_normalized(ref, rand_equalizer_params)
# if torch.rand(1) < 0.5:
ref = model.compressor.process_normalized(ref, rand_compressor_params)
# if torch.rand(1) < 0.5:
ref = model.reverb.process_normalized(ref, rand_reverb_params)
# ref = model.gain.process_normalized(ref, rand_gain_params)
# if not stereo already, convert to stereo
# if ref.shape[1] == 1:
# ref = ref.repeat(1, 2, 1)
# peak normalize reference recordings
peak, _ = torch.max(torch.abs(ref), dim=-1, keepdim=True)
ref = ref / peak
# apply random gain from -24 dB to 0 dB
gain_db = torch.rand(input.shape[0], 1, 1).type_as(input) * 24
gain_lin = torch.pow(10, -gain_db / 20)
ref = ref * gain_lin
# apply random gain to input
gain_db = torch.rand(input.shape[0], 1, 1).type_as(input) * 24
gain_lin = torch.pow(10, -gain_db / 20)
input = input * gain_lin
# split into A and B sections
input_a, input_b = torch.chunk(input, 2, dim=-1)
ref_a, ref_b = torch.chunk(ref, 2, dim=-1)
# forward pass
output_a = model(input_a, torch.mean(ref_b, dim=1, keepdim=True))
return input_a, input_b, ref_a, ref_b, output_a
def train(
model: torch.nn.Module,
train_dataloader: torch.utils.data.DataLoader,
val_dataloader: Optional[torch.utils.data.DataLoader] = None,
lr: float = 1e-4,
epochs: int = 250,
use_gpu: bool = False,
log_dir: str = "outputs/style_transfer",
):
optimizer = torch.optim.Adam(model.parameters(), lr=lr)
loss_fn = auraloss.freq.MultiResolutionSTFTLoss()
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, epochs)
if use_gpu:
model = model.cuda()
loss_fn = loss_fn.cuda()
epoch_loss_history = []
for epoch in range(epochs):
pbar = tqdm(train_dataloader)
loss_history = []
model.train()
for batch in pbar:
input = batch
if use_gpu:
input = input.cuda()
# forward pass
input_a, input_b, ref_a, ref_b, output_a = step(input, model)
# compute loss on A section
loss = loss_fn(output_a, ref_a)
loss_history.append(loss.item())
pbar.set_description(f"Epoch {epoch} Loss: {np.mean(loss_history):.4f}")
optimizer.zero_grad()
loss.backward()
optimizer.step()
scheduler.step()
epoch_loss_history.append(np.mean(loss_history))
plot_loss(log_dir, epoch_loss_history)
validate(
model,
val_dataloader,
epoch=epoch + 1,
log_dir=log_dir,
use_gpu=use_gpu,
)
if __name__ == "__main__":
sample_rate = 44100
log_dir = "outputs/style_transfer"
os.makedirs(log_dir, exist_ok=True)
os.makedirs(os.path.join(log_dir, "audio"), exist_ok=True)
model = StyleTransferModel(sample_rate)
filepaths = glob.glob(
"/import/c4dm-datasets/VocalSet1-2/data_by_singer/**/*.wav",
recursive=True,
)
train_filepaths = filepaths[: int(len(filepaths) * 0.8)]
val_filepaths = filepaths[int(len(filepaths) * 0.8) :]
# train_filepaths = train_filepaths[:1]
# val_filepaths = train_filepaths[:1]
train_dataset = AudioFileDataset(train_filepaths, length=262144)
train_dataloader = torch.utils.data.DataLoader(
train_dataset,
batch_size=8,
shuffle=True,
num_workers=8,
)
val_dataset = AudioFileDataset(val_filepaths, length=262144)
val_dataloader = torch.utils.data.DataLoader(val_dataset, batch_size=1)
train(
model,
train_dataloader,
val_dataloader=val_dataloader,
log_dir=log_dir,
use_gpu=True,
)