-
Notifications
You must be signed in to change notification settings - Fork 4
/
sample.py
executable file
·670 lines (572 loc) · 28 KB
/
sample.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
# TODO(@tbazin, 2022/07/04): import TorchScript interfaces for the models used
from enum import Enum, auto
from typing import Union, Optional, Iterable, Tuple, Mapping
import argparse
import pathlib
import os
import json
from datetime import datetime
import uuid
import soundfile
from tqdm import tqdm
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
from sklearn.preprocessing import LabelEncoder
import torch
import torchaudio
from torch import nn
from torch.nn import functional as F
from torchvision.utils import save_image
class Seq2SeqInputKind(Enum):
"""Types of input sequences for seq2seq models"""
Source = auto()
Target = auto()
# use matplotlib without an X server
# on desktop, this prevents matplotlib windows from popping around
mpl.use('Agg')
def top_k_top_p_filtering(logits, top_k=0, top_p=0.0, filter_value=-float('Inf')):
""" Filter a distribution of logits using top-k and/or nucleus (top-p) filtering
Args:
logits: logits distribution shape (batch size x sequence_duration x vocabulary size)
top_k > 0: keep only top k tokens with highest probability (top-k filtering).
top_p > 0.0: keep the top tokens with cumulative probability >= top_p (nucleus filtering).
Nucleus filtering is described in Holtzman et al. (http://arxiv.org/abs/1904.09751)
From: https://gist.github.com/thomwolf/1a5a29f6962089e871b94cbd09daf317
"""
top_k = min(top_k, logits.size(-1)) # Safety check
if top_k > 0:
# Remove all tokens with a probability less than the last token of the top-k
indices_to_remove = logits < torch.topk(logits, top_k)[0][..., -1, None]
logits[indices_to_remove] = filter_value
if top_p > 0.0:
sorted_logits, sorted_indices = torch.sort(logits, descending=True)
cumulative_probs = torch.cumsum(F.softmax(sorted_logits, dim=-1), dim=-1)
# Remove tokens with cumulative probability above the threshold
sorted_indices_to_remove = cumulative_probs > top_p
# Shift the indices to the right to keep also the first token above the threshold
sorted_indices_to_remove[..., 1:] = sorted_indices_to_remove[..., :-1].clone()
sorted_indices_to_remove[..., 0] = 0
# scatter sorted tensors to original indexing
indices_to_remove = sorted_indices_to_remove.scatter(
dim=-1, index=sorted_indices, src=sorted_indices_to_remove)
logits[indices_to_remove] = filter_value
return logits
def make_conditioning_tensors(
class_conditioning: Mapping[str, Union[int, str, Tuple[int, int]]],
label_encoders_per_conditioning: Mapping[str, LabelEncoder],
) -> Mapping[str, torch.Tensor]:
class_conditioning_tensors = {}
def make_conditioning_tensor(value: Union[int, str, Tuple[int, int]],
modality: str) -> torch.Tensor:
label_encoder = label_encoders_per_conditioning[modality]
try:
# check if value is a 2-uple (useful for pitch ranges)
range_min, range_max = tuple(int(x) for x in value)
assert range_min < range_max, (
"Provide increasing range for range conditioning")
encoded_label = label_encoder.transform(
list(range(range_min, range_max))).transpose()
except BaseException:
encoded_label = label_encoder.transform([value])
return torch.from_numpy(encoded_label).long()
# for value, modality, location in zip(
# [args.instrument_family_conditioning_top,
# args.instrument_family_conditioning_bottom,
# args.pitch_conditioning_top, args.pitch_conditioning_bottom,
# ],
# ['instrument_family_str', 'instrument_family_str', 'pitch', 'pitch'],
# ['top', 'bottom', 'top', 'bottom']
# ):
# maybe_add_conditioning(value, modality, location)
for modality, value in class_conditioning.items():
class_conditioning_tensors[modality] = (
make_conditioning_tensor(value, modality))
return class_conditioning_tensors
ConditioningMap = Union[Iterable[Iterable[str]],
Iterable[Iterable[int]]]
def make_conditioning_map(class_conditioning: Mapping[str, ConditioningMap],
label_encoders_per_conditioning: Mapping[
str, LabelEncoder],
) -> Mapping[str, torch.Tensor]:
def map_to_tensor(conditioning_map: ConditioningMap, modality: str):
label_encoder = label_encoders_per_conditioning[modality]
num_rows = len(conditioning_map)
num_columns = len(conditioning_map[0])
conditioning_tensor = torch.zeros(num_rows, num_columns).long()
for row_index, row in enumerate(conditioning_map):
encoded_row = label_encoder.transform(row)
conditioning_tensor[row_index] = torch.from_numpy(encoded_row)
return conditioning_tensor.unsqueeze(0) # prepare in batched format
return {modality: map_to_tensor(conditioning_map, modality)
for modality, conditioning_map in class_conditioning.items()}
@torch.inference_mode()
def sample_model(model: nn.Module, device: Union[torch.device, str],
batch_size: int, codemap_size: Iterable[int],
temperature: float, condition: Optional[torch.Tensor] = None,
constraint: Optional[torch.Tensor] = None,
class_conditioning: Mapping[str, Iterable[int]] = {},
initial_code: Optional[torch.Tensor] = None,
mask: Optional[torch.BoolTensor] = None,
local_class_conditioning_map: Optional[Mapping[str, Iterable[int]]] = None,
time_indexes_source: Optional[Iterable[int]] = None,
time_indexes_target: Optional[Iterable[int]] = None,
top_k_sampling_k: int = 0,
top_p_sampling_p: float = 0.0,
# this option allows to drop-in tqdm_notebook when needed
progressbar_decorator=tqdm,
use_predictive_sampling: bool = False, # https://arxiv.org/abs/2002.09928
use_parallel_model: bool = True,
inference_model: Optional[torch.ScriptModule] = None
):
"""Generate a sample from the provided PixelSNAIL
Arguments:
model (PixelSNAIL)
device (torch.device or str):
The device on which to perform the sampling
batch_size (int)
codemap_size (Iterable[int]):
The size of the codemap to generate
temperature (float):
Sampling temperature (lower means the model is more conservative)
condition (torch.Tensor, optional, default None):
Another codemap to use as hierarchical conditionning for sampling.
If not provided, sampling is unconditionned.
constraint_2D (torch.Tensor, optional, default None):
If provided, fixes the top-left part of the generated 2D codemap
to be the given Tensor.
`constraint_2D.size` should be less or equal to codemap_size.
"""
if initial_code is None:
codemap = (torch.full([batch_size] + list(codemap_size),
fill_value=0,
dtype=torch.int64,
device=device))
if model.self_conditional_model and condition is None:
condition = (
torch.full([batch_size] + list(codemap_size),
fill_value=model.mask_token_index if model.self_conditional_model else 0,
dtype=torch.int64,
device=device)
)
else:
codemap = initial_code.to(device)
if not model.local_class_conditioning or (
local_class_conditioning_map is None):
class_conditioning_tensors = {
conditioning_modality: (
conditioning_tensor.unsqueeze(1).long()
.expand(batch_size, -1)
.to(device))
for conditioning_modality, conditioning_tensor
in class_conditioning.items()
}
else:
class_conditioning_tensors = local_class_conditioning_map
if device != 'cpu' and use_parallel_model:
# TODO(@tbazin, 2022/01/28): pass appropriate devices_t to DataParallel
# or remove usage of DataParallel alltogether?
parallel_model = nn.DataParallel(model)
parallel_model.eval()
else:
parallel_model = None
constraint_height = 0
constraint_width = 0
if constraint is not None:
raise NotImplementedError
if list(constraint.shape) > codemap_size:
raise ValueError("Incorrect size of constraint, constraint "
"should be smaller than the target codemap size")
else:
_, constraint_height, constraint_width = constraint.shape
padding_left = padding_top = 0
padding_bottom = codemap_size[0] - constraint_height
padding_right = codemap_size[1] - constraint_width
padder = torch.nn.ConstantPad2d(
(padding_left, padding_right, padding_top, padding_bottom),
value=0).to(device)
channel_dim = 1
codemap = (padder(constraint.unsqueeze(channel_dim))
.detach()
.squeeze(channel_dim)
)
if model.self_conditional_model and condition is None:
condition = codemap
source_sequence, target_sequence = model.to_sequences(
codemap, condition,
class_conditioning=class_conditioning_tensors,
mask=mask,
time_indexes_source=time_indexes_source,
time_indexes_target=time_indexes_target
)
kind = Seq2SeqInputKind.Target
input_sequence = target_sequence
condition_sequence = source_sequence
sequence_duration_without_start_symbol: int = (
model.target_transformer_sequence_length)
source_start_symbol_duration = model.source_start_symbol.shape[1]
target_start_symbol_duration = model.target_start_symbol.shape[1]
codemap_as_sequence = model.target_codemaps_helper.to_sequence(codemap)
if mask is not None:
mask_sequence = model.target_codemaps_helper.to_sequence(mask).squeeze(0)
else:
mask_sequence = torch.full((sequence_duration_without_start_symbol, ),
True, dtype=bool)
mask_sequence: np.ndarray = mask_sequence.cpu().numpy()
class_condition_sequence = None
if model.local_class_conditioning:
class_condition_sequence = (
model.make_class_conditioning_sequence(class_conditioning_tensors)
)
if use_predictive_sampling:
# sample Gumbel noise for categorical reparametrization trick
# standard Gumbel distribution
standard_gumbel_distribution = torch.distributions.gumbel.Gumbel(
torch.zeros(1, device=device),
torch.ones(1, device=device))
gumbel_noise_shape: Tuple[int] = (
codemap_as_sequence.shape
+ (model.n_class_target,))
standard_gumbel_sample = standard_gumbel_distribution.sample(
gumbel_noise_shape)
else:
standard_gumbel_sample = None
memory = None
prediction_was_correct = False
correct_predictions = 0
sample = None
previous_codemap_as_sequence = codemap_as_sequence
for i, is_masked in progressbar_decorator(enumerate(mask_sequence)):
if not is_masked:
continue
if (use_predictive_sampling
and sample is not None
and prediction_was_correct):
prediction_was_correct = torch.all(
sample[:, i] == previous_codemap_as_sequence[:, i])
if prediction_was_correct:
correct_predictions += 1
continue
sampling_model = model
if parallel_model is not None:
sampling_model = parallel_model
if inference_model is not None:
sampling_model = inference_model
logits_sequence_out, memory = sampling_model.forward(
input_sequence,
(condition_sequence if not model.self_conditional_model
else input_sequence),
class_condition_sequence,
memory=memory)
# apply temperature and filter logits
logits_sequence_out = logits_sequence_out / temperature
logits_sequence_out = top_k_top_p_filtering(logits_sequence_out,
top_k=top_k_sampling_k,
top_p=top_p_sampling_p)
next_step_probabilities = torch.softmax(
logits_sequence_out, dim=-1)
if not use_predictive_sampling:
sample = torch.multinomial(next_step_probabilities[:, i, :], 1
).squeeze(-1)
codemap_as_sequence[:, i] = sample.long()
embedded_sample = model.embed_data(sample, kind)
# translate to account for the added start_symbol!
input_sequence[:, i+target_start_symbol_duration,
:model.embeddings_effective_dim] = (
embedded_sample)
else:
# Gumbel reparametrization trick
gumble_softmax = torch.argmax(
torch.log(next_step_probabilities) + standard_gumbel_sample.squeeze(-1),
dim=-1)
sample = gumble_softmax
prediction_was_correct = torch.all(
sample[:, i].long() == codemap_as_sequence[:, i])
previous_codemap_as_sequence = codemap_as_sequence.clone()
causal_and_inpainting_mask = (
torch.as_tensor(mask_sequence).bool()
* (torch.arange(mask_sequence.shape[-1]) >= i).unsqueeze(0)
).bool()
codemap_as_sequence[causal_and_inpainting_mask] = sample[
causal_and_inpainting_mask]
embedded_sample = model.embed_data(sample, kind)
# translate to account for the added start_symbol!
embeddings_causal_and_inpainting_mask = (
causal_and_inpainting_mask.unsqueeze(-1)
.expand(-1, -1, model.embeddings_effective_dim))
input_sequence[:, target_start_symbol_duration:,
:model.embeddings_effective_dim][
embeddings_causal_and_inpainting_mask] = (
embedded_sample[embeddings_causal_and_inpainting_mask])
if use_predictive_sampling and False:
correct_predictions_ratio = (correct_predictions
/ mask_sequence.sum())
print(f"Ratio of correct predictions: "
f"{correct_predictions_ratio:.2f}"
" ===> Relative speedup: "
f"{1 / (1 - correct_predictions_ratio):.2f}")
codemap = model.target_codemaps_helper.to_time_frequency_map(
codemap_as_sequence).long()
return codemap
def plot_codes(top_codes: torch.LongTensor,
bottom_codes: torch.LongTensor,
codes_dictionary_dim_top: int,
codes_dictionary_dim_bottom: int,
cmap='viridis', plots_per_row: int = 12) -> None:
assert (len(top_codes)
== len(bottom_codes))
num_maps = len(top_codes)
num_groups = 2
plots_per_row = min(num_maps, plots_per_row)
num_rows_per_codemaps_group = int(np.ceil(num_maps / plots_per_row))
num_rows = num_groups * num_rows_per_codemaps_group
figure, subplot_axs = plt.subplots(num_rows, plots_per_row,
figsize=(10 * plots_per_row/12,
2*num_rows))
for ax in subplot_axs.ravel().tolist():
ax.set_axis_off()
def get_ax(codemap_group_index: int, codemap_index: int):
start_row = codemap_group_index * num_rows_per_codemaps_group
row = start_row + codemap_index // plots_per_row
ax = subplot_axs[row][codemap_index % plots_per_row]
return ax
for group_index, (maps_group, codes_dictionary_dim) in enumerate(
zip([top_codes, bottom_codes],
[codes_dictionary_dim_top,
codes_dictionary_dim_bottom])):
for map_index, codemap in enumerate(maps_group):
ax = get_ax(group_index, map_index)
im = ax.matshow(codemap.cpu().numpy(), vmin=0,
vmax=codes_dictionary_dim-1,
cmap=cmap)
figure.tight_layout()
# add colorbar for codemaps
figure.colorbar(im,
ax=(subplot_axs.ravel().tolist()))
return figure, subplot_axs
if __name__ == '__main__':
from interactive_spectrogram_inpainting.utils.datasets.label_encoders import (
load_label_encoders_from_file)
from interactive_spectrogram_inpainting.vqvae.vqvae import VQVAE
from interactive_spectrogram_inpainting.priors.transformer import (
VQNSynthTransformer,
SelfAttentiveVQTransformer, UpsamplingVQTransformer,
Seq2SeqInputKind)
from interactive_spectrogram_inpainting.utils.misc import (
get_spectrograms_helper)
parser = argparse.ArgumentParser()
parser.add_argument('--batch_size', type=int, default=8)
parser.add_argument('--dataset', type=str, choices=['nsynth', 'imagenet'],
required=True)
parser.add_argument('--model_type_top', type=str,
choices=['Transformer'],
default='Transformer')
parser.add_argument('--model_type_bottom', type=str,
choices=['Transformer'],
default='Transformer')
parser.add_argument('--vqvae_training_parameters_path', type=str, required=True)
parser.add_argument('--vqvae_model_parameters_path', type=str, required=True)
parser.add_argument('--vqvae_weights_path', type=str, required=True)
parser.add_argument('--prediction_top_parameters_path', type=str,
required=True)
parser.add_argument('--prediction_top_weights_path', type=str,
required=True)
parser.add_argument('--prediction_bottom_parameters_path', type=str,
required=True)
parser.add_argument('--prediction_bottom_weights_path', type=str,
required=True)
def key_value(arg: str) -> Iterable[Tuple[str, str]]:
key, value = arg.split(',')
if len(value.split('...')) == 2:
value = value.split('...')
return key, value
parser.add_argument('--class_conditioning', type=key_value, nargs='*',
default=[])
parser.add_argument('--class_conditioning_top', type=key_value, nargs='*',
default=[])
parser.add_argument('--keep_same_top', action='store_true')
parser.add_argument('--class_conditioning_bottom', type=key_value, nargs='*',
default=[])
# TODO(theis): change this, store label encoders inside the VQNSynthTransformer model class
parser.add_argument('--label_encoders_path', type=str)
parser.add_argument('--temperature', type=float, default=1.0)
parser.add_argument('--top_p_sampling_p', type=float, default=0.0)
parser.add_argument('--top_k_sampling_k', type=int, default=0)
parser.add_argument('--hop_length', type=int, default=512)
parser.add_argument('--n_fft', type=int, default=2048)
parser.add_argument('--sample_rate_hz', type=int, default=16000)
parser.add_argument('--condition_top_audio_path', type=str)
parser.add_argument('--constraint_top_audio_path', type=str)
parser.add_argument('--constraint_top_num_timesteps', type=int)
parser.add_argument('--output_directory', type=str, default='./')
args = parser.parse_args()
def expand_path(path: str) -> pathlib.Path:
return pathlib.Path(path).expanduser().absolute()
OUTPUT_DIRECTORY = expand_path(args.output_directory)
run_ID = (datetime.now().strftime('%Y%m%d-%H%M%S-')
+ str(uuid.uuid4())[:6])
print("Sample ID: ", run_ID)
device = 'cuda' if torch.cuda.is_available() else 'cpu'
if args.model_type_top == 'Transformer':
ModelTop = SelfAttentiveVQTransformer
else:
raise ValueError(
f"Unexpected value {args.model_type_top} for option model_type_top")
if args.model_type_bottom == 'Transformer':
ModelBottom = UpsamplingVQTransformer
else:
raise ValueError(
f"Unexpected value {args.model_type_bottom} for option model_type_bottom")
model_vqvae = VQVAE.from_parameters_and_weights(
expand_path(args.vqvae_model_parameters_path),
expand_path(args.vqvae_weights_path),
device=device
).to(device).eval()
model_top = ModelTop.from_parameters_and_weights(
expand_path(args.prediction_top_parameters_path),
expand_path(args.prediction_top_weights_path),
device=device
).to(device).eval()
model_bottom = ModelBottom.from_parameters_and_weights(
expand_path(args.prediction_bottom_parameters_path),
expand_path(args.prediction_bottom_weights_path),
device=device
).to(device).eval()
VQVAE_TRAINING_PARAMETERS_PATH = expand_path(
args.vqvae_training_parameters_path)
# retrieve n_fft, hop length, window length parameters...
with open(VQVAE_TRAINING_PARAMETERS_PATH, 'r') as f:
vqvae_training_parameters = json.load(f)
spectrograms_helper = get_spectrograms_helper(
device=device, **vqvae_training_parameters)
def to_dictionary(key_value_list: Iterable[Tuple[any, any]]
) -> Mapping[any, any]:
return {key: value for key, value in key_value_list}
if len(args.class_conditioning_top) > 0:
assert len(args.class_conditioning_bottom) > 0
class_conditioning_top = to_dictionary(args.class_conditioning_top)
class_conditioning_bottom = to_dictionary(
args.class_conditioning_bottom)
else:
# use same conditioning for top and bottom
class_conditioning_top = to_dictionary(args.class_conditioning)
class_conditioning_bottom = class_conditioning_top
classes_for_conditioning = set()
classes_for_conditioning.update(class_conditioning_top.keys())
classes_for_conditioning.update(class_conditioning_bottom.keys())
# additional_modalities = set(modality
# for modality, _ in )
# classes_for_conditioning.update(additional_modalities)
if args.label_encoders_path is not None:
label_encoders_per_conditioning = load_label_encoders_from_file(
args.label_encoders_path)
else:
label_encoders_per_conditioning = (
model_top.class_conditioning_label_encoders_per_modality)
class_conditioning_tensors_top = make_conditioning_tensors(
class_conditioning_top,
label_encoders_per_conditioning)
class_conditioning_tensors_bottom = make_conditioning_tensors(
class_conditioning_bottom,
label_encoders_per_conditioning)
with torch.no_grad():
initial_code = None
if args.condition_top_audio_path is not None:
condition_mel_spec_and_IF = spectrograms_helper.from_wavfile(
args.condition_top_audio_path)
(_, _, _, condition_code_top, condition_code_bottom,
*_) = model_vqvae.encode(condition_mel_spec_and_IF.to(device))
# repeat condition for the whole batch
top_code = condition_code_top.repeat(args.batch_size, 1, 1)
initial_code = condition_code_bottom.repeat(args.batch_size, 1, 1)
elif args.constraint_top_audio_path is not None:
constraint_mel_spec_and_IF = spectrograms_helper.from_wavfile(
args.constraint_top_audio_path)
(_, _, _, constraint_code_top, *_) = model_vqvae.encode(
constraint_mel_spec_and_IF.to(device))
constraint_code_top_restrained = (
constraint_code_top[:, :args.constraint_top_num_timesteps-1])
top_code_sample = sample_model(
model_top, device, batch_size=1, codemap_size=model_top.shape,
temperature=args.temperature,
constraint=constraint_code_top_restrained,
class_conditioning=class_conditioning_tensors_top,
top_p_sampling_p=args.top_p_sampling_p,
top_k_sampling_k=args.top_k_sampling_k
)
# repeat condition for the whole batch
top_code = top_code_sample.repeat(args.batch_size, 1, 1)
else:
batch_size_top = args.batch_size
if args.keep_same_top:
batch_size_top = 1
top_code_sample = sample_model(
model_top, device, batch_size_top, model_top.shape,
args.temperature,
class_conditioning=class_conditioning_tensors_top,
top_p_sampling_p=args.top_p_sampling_p,
top_k_sampling_k=args.top_k_sampling_k)
top_code = top_code_sample
if args.keep_same_top:
top_code = top_code.repeat(args.batch_size, 1, 1)
# sample bottom code contitioned on the top code
bottom_sample = sample_model(
model_bottom, device, args.batch_size, model_bottom.shape,
args.temperature, condition=top_code,
class_conditioning=class_conditioning_tensors_bottom,
initial_code=initial_code,
top_p_sampling_p=args.top_p_sampling_p,
top_k_sampling_k=args.top_k_sampling_k
)
decoded_sample = model_vqvae.decode_code(top_code, bottom_sample)
codes_figure, _ = plot_codes(top_code, bottom_sample,
model_top.n_class,
model_bottom.n_class)
condition_top_audio = None
if args.condition_top_audio_path is not None:
CONDITION_TOP_AUDIO_PATH = expand_path(args.condition_top_audio_path)
import torchvision.transforms as transforms
sample_audio, fs_hz = torchaudio.load(CONDITION_TOP_AUDIO_PATH,
channels_first=True)
resampler = torchaudio.transforms.Resample(
orig_freq=fs_hz, new_freq=args.sample_rate_hz)
sample_audio = resampler(sample_audio.cuda())
condition_top_audio = sample_audio.flatten().cpu().numpy()
def make_audio(mag_and_IF_batch: torch.Tensor,
condition_audio: Optional[np.ndarray],
normalize: bool = False) -> np.ndarray:
audio_batch = spectrograms_helper.to_audio(mag_and_IF_batch)
if normalize:
normalized_audio_batch = (
audio_batch
/ audio_batch.abs().max(dim=1, keepdim=True)[0])
audio_batch = normalized_audio_batch
audio_mono_concatenated = audio_batch.flatten().cpu().numpy()
if condition_audio is not None:
audio_mono_concatenated = np.concatenate(
[condition_audio,
np.zeros(condition_audio.shape),
audio_mono_concatenated])
return audio_mono_concatenated
os.makedirs(OUTPUT_DIRECTORY, exist_ok=True)
with open(OUTPUT_DIRECTORY / f'{run_ID}-command_line_parameters.json', 'w') as f:
json.dump(args.__dict__, f, indent=4)
codes_figure.savefig(OUTPUT_DIRECTORY / f'{run_ID}-codemaps.png')
audio_sample_path = OUTPUT_DIRECTORY / f'{run_ID}.wav'
soundfile.write(audio_sample_path,
make_audio(decoded_sample, condition_top_audio),
samplerate=args.sample_rate_hz)
# write spectrogram and IF
channel_dim = 1
for channel_index, channel_name in enumerate(
['spectrogram', 'instantaneous_frequency']):
channel = decoded_sample.select(channel_dim, channel_index
).unsqueeze(channel_dim)
save_image(
channel,
os.path.join(args.output_directory, f'{run_ID}-{channel_name}.png'),
nrow=args.batch_size,
# normalize=True,
# range=(-1, 1),
# scale_each=True,
)