-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
1251 lines (1064 loc) · 46.7 KB
/
main.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
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# all main command line tooling
#
# All trainer logic is abstracted in the generic trainer.
# During the initial start, the trainer takes specs i.e., the yaml file invokes
# the factory method. That creates a model, a model-specific optimizer, and a scheduler.
#
# Note my assumption that the model can be stacked. Hence internally, it queues.
# So, for example, if you have two models and you train, you can define two sub-layers.
#
# A good example is if you want to train DTC with a different Vocoder,
# or, for instance, Tacotron 2 and WaveGlow
#
# Note right trainer is logically executed in a sequence generally backward
# in torch implementation and can not be executed in parallel anyway.
#
# Thus, queue is just FIFO.
#
# Ray loaded optionally.
# Mus
import argparse
import logging
import os
import random
import signal
import socket
import sys
import warnings
from pathlib import Path
from typing import Optional
import scipy
import torch
import time
import librosa
import matplotlib.pyplot as plt
import numpy as np
import torch.distributed as dist
from loguru import logger
import soundfile as sf
try:
import ray
from ray import tune
from ray.tune import CLIReporter
from ray.tune.schedulers import ASHAScheduler
from tunner import Trainable
except ImportError as err:
print(f"Ray tunner disabled.{err}")
pass
from tqdm import tqdm
from inference_tools import plot_spectrogram
from model_loader.dataset_stft25 import SFTF2Dataset
from model_loader.dataset_stft30 import SFTF3Dataset
from model_loader.ds_util import md5_checksum
from model_loader.stft_dataloader import SFTFDataloader
from model_trainer.internal.save_best import CheckpointBest
from model_trainer.plotting_utils import plot_spectrogram_to_numpy
from model_trainer.specs.model_tacotron25_spec import SpectrogramLayerSpec, ModelSpecTacotron25
from model_trainer.trainer import Trainer, TrainerError
from model_trainer.trainer_specs import ExperimentSpecs, TrainerSpecError
os.environ["NCCL_DEBUG"] = "INFO"
os.environ["NCCL_IB_DISABLE"] = "1"
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
os.environ["LOCAL_RANK"] = "0"
os.environ["RANK"] = "0"
os.environ["WORLD_SIZE"] = "2"
os.environ["TUNE_DISABLE_AUTO_CALLBACK_SYNCER"] = "1"
warnings.filterwarnings("ignore")
class ConverterError(Exception):
"""Base class for other exceptions"""
pass
def convert_mel_to_data(encoder_spec: SpectrogramLayerSpec,
dataset: SFTF2Dataset,
target_dir: Optional[str] = "",
meta_file: Optional[str] = "",
dataset_name: Optional[str] = "default",
data_type: Optional[str] = "all",
version: Optional[int] = 3,
post_check: Optional[bool] = True,
verbose: Optional[bool] = True):
"""
Convert audio dataset to MEL tensor representation.
:param version:
:param encoder_spec: all parameter for SFTS encoder
:param dataset: list.
:param meta_file: a meta file used to generate a dataset.
:param target_dir: where we will put our final file.
:param dataset_name: a name of dataset that will be in file name.
:param data_type: just a name train, validate, test , dev etc.
:param post_check: if we do sanity check post.
:param verbose:
:return:
"""
# all data how SFT's generated in meta
meta = dict(filter_length=encoder_spec.filter_length(),
hop_length=encoder_spec.hop_length(),
win_length=encoder_spec.win_length(),
n_mel_channels=encoder_spec.n_mel_channels(),
sampling_rate=encoder_spec.sampling_rate(),
mel_fmin=encoder_spec.mel_fmin(),
mel_fmax=encoder_spec.mel_fmax())
ds_size = len(dataset)
data = []
for i in tqdm(range(0, ds_size), desc="Converting"):
data.append(dataset[i])
meta['data'] = data
meta['meta_file'] = meta_file
meta['version'] = version
file_name = Path(target_dir) / f'{dataset_name}_{data_type}_num_sam_' \
f'{len(dataset)}_filter_' \
f'{encoder_spec.n_mel_channels()}_{version}.pt'
md5_sig = Path(target_dir) / f'{dataset_name}_{data_type}_num_sam_' \
f'{len(dataset)}_filter_' \
f'{encoder_spec.n_mel_channels()}_{version}.sig'
print("Saving ", file_name)
torch.save(meta, str(file_name))
md5checksum = md5_checksum(str(file_name))
print("MD5 checksum", md5checksum)
with open(md5_sig, 'w', encoding='utf-8') as f:
f.write(md5checksum)
if not post_check:
return
ds = torch.load(file_name)
print("Loading back and checking.")
if verbose:
logger.info(f"Dataset saved to a file: {file_name}")
logger.info(f"Dataset filter length: {ds['filter_length']}")
logger.info(f"Dataset mel channels: {ds['n_mel_channels']}")
logger.info(f"Dataset contains records: {len(ds['data'])}")
d = ds['data']
for i, (data_tuple) in tqdm(enumerate(d), total=len(ds['data']), desc="Validating"):
if version == 2:
txt_original, mel_from_ds = dataset[i]
one_hot, mel = data_tuple
elif version == 3:
txt_original, mel_from_ds, sft_ds = dataset[i]
one_hot, mel, stft = data_tuple
if not torch.equal(stft, sft_ds):
raise ConverterError("data mismatched.")
else:
raise ConverterError("Unknown version.")
if not torch.equal(mel, mel_from_ds):
raise ConverterError("mel data mismatched.")
if not torch.equal(one_hot, txt_original):
raise ConverterError("one hot vector data mismatched.")
print("Done.")
def plot_example(trainer_spec, version=3, dataset_name=None,
verbose=True, target_dir=None, sr=22050, num_examples=1):
"""
Routine plot dataset example for inspection.
Each MEl and STFT generated in result folder. SampleS TFT reconstructed by inverse
function and serialized in result directory.
:param sr:
:param target_dir: where to write plots
:param version: a dataset version. since we're serializing a tensor or numpy we need know what feature
on top of MEL we extract.
:param trainer_spec: a trainer spec object.
:param verbose: verbose output
:param dataset_name: if empty will use current active one. whatever in config use_dataset: 'mydataset'
:param num_examples: by default we plot one example
:return:
"""
# trainer_spec = ExperimentSpecs(verbose=verbose)
if dataset_name is None or len(dataset_name) == 0:
ds_spec = trainer_spec.get_active_dataset_spec()
# if not trainer_spec.is_audio_raw(ds_spec):
# print("Please check configuration, the active dataset not raw audio.")
# return
data = None
if dataset_name is None or len(dataset_name) == 0:
data = trainer_spec.get_audio_dataset()
else:
dataset_names = trainer_spec.get_dataset_names()
ds_name = dataset_name.strip()
if ds_name in dataset_names:
data = trainer_spec.get_audio_dataset(dataset_name=ds_name)
if data is None:
raise ConverterError("Dataset not found.")
start_time = time.time()
dataloader = SFTFDataloader(trainer_spec, batch_size=2, verbose=True)
data_loaders, collate_fn = dataloader.get_all()
_train_loader = data_loaders['train_set']
print(f"Train datasize {dataloader.get_train_dataset_size()}")
print("--- %s load time, seconds ---" % (time.time() - start_time))
fig = plt.figure()
default_dir = "results"
if target_dir is not None:
default_dir = target_dir
p = Path(default_dir).expanduser().resolve()
if not p.is_dir():
print(f"Target {target_dir} is not a dir.")
start_time = time.time()
# take one batch example.
for batch_idx, batch in enumerate(_train_loader):
if batch_idx == num_examples:
break
text_padded, input_lengths, mel_padded, gate_padded, out_len, stft_padded = batch
plot_spectrogram_to_numpy(mel_padded[0], file_name=f"{default_dir}/default_mel{batch_idx}.png")
plot_spectrogram(stft_padded[0],
y_axis_label="mel freq",
file_name=f"{default_dir}/default_stft_{batch_idx}.png")
# MEL
S = librosa.feature.inverse.mel_to_stft(mel_padded[0].numpy())
y = librosa.griffinlim(S)
sf.write(f'{default_dir}/default_{batch_idx}.wav', y, sr, 'PCM_24')
# iSTFT
y_numpy = stft_padded.numpy()
for i in range(0, y_numpy.shape[0]):
n = (y_numpy[batch_idx].shape[i] * y_numpy[i].shape[1])
y_out = librosa.istft(y_numpy[i], length=n)
sf.write(f'results/default_stft_{batch_idx}_{i}.wav', y_out, sr, 'PCM_24')
fig.show()
print("--- %s Single batch memory load, load time, seconds ---" % (time.time() - start_time))
def convert(trainer_spec, version=3,
dataset_name=None, merge=True, verbose=True, target_dir=None,
ds_ratio=10, exclude=None) -> None:
"""
Routine convert dataset to native torch tensor representation. It takes the entire audio dataset.
And create a single dat file. It supports both Tacotron2 format and DTC.
Each file is serialized to disk, and md5 hash is provided. In case we need to provide a download option.
BaseDataset class provides a list of URLs that provide the option to fetch data from the web, and each has a URL
and dataset. Each entry requires a mirror and md5 hash.
I used this method in my training procedure. It significantly increases
batch load to GPU time.
:param exclude: if we reduce a size by default validation dataset will exclude from reduction.
(default dataset used for this model has only 100 record, if you have large validation dataset
you can include in reduction as well).
:param ds_ratio: a ration. ( percentage 0.5 will drop 50 percent)
:param target_dir: where to write a file.
:param version: a dataset version. since we're serializing a tensor or numpy
we need know what feature on top of MEL we extract.
:param trainer_spec: a trainer spec object.
:param dataset_name: if empty will use current active one. whatever
in config use_dataset: 'mydataset'
:param merge: if true merge all datasets to single file..
:param verbose: verbose output
:return: None
"""
# trainer_spec = ExperimentSpecs(verbose=verbose)
if exclude is None:
exclude = ['validation_set']
# will read from config spec.
if dataset_name is None or len(dataset_name) == 0:
ds_spec = trainer_spec.get_active_dataset_spec()
if not trainer_spec.is_audio_raw(ds_spec):
print("Please check configuration, the "
"active dataset must be raw audio dataset.")
return
data = None
if dataset_name is None or len(dataset_name) == 0:
data = trainer_spec.get_audio_dataset()
else:
dataset_names = trainer_spec.get_dataset_names()
ds_name = dataset_name.strip()
if ds_name in dataset_names:
data = trainer_spec.get_audio_dataset(dataset_name=ds_name)
if data is None:
raise ConverterError("Dataset not found.")
if 'data' in data:
raise ConverterError("Data has no data key, active dataset must be raw audio.")
test_set = data['test_set']
training_set = data['train_set']
validation_set = data['validation_set']
model_spec: ModelSpecTacotron25 = trainer_spec.get_model_spec()
encoder_spec = model_spec.get_spectrogram()
train_listified = list(training_set.values())
test_listified = list(test_set.values())
val_listified = list(validation_set.values())
if ds_ratio > 0:
if 'train_set' not in exclude:
old_sz = len(train_listified)
train_listified = train_listified[0: int(len(train_listified) * (ds_ratio / 100))]
print(f"Train set reduced from {old_sz} to {len(train_listified)}.")
if 'validation_set' not in exclude:
old_sz = len(val_listified)
val_listified = val_listified[0: int(len(val_listified) * (ds_ratio / 100))]
print(f"Train set reduced from {old_sz} to {len(val_listified)}.")
if 'test_set' not in exclude:
old_sz = len(test_listified)
test_listified = test_listified[0: int(len(test_listified) * (ds_ratio / 100))]
print(f"Test set reduced from {old_sz} to {len(test_listified)}.")
listified = [train_listified, val_listified, test_listified]
if merge:
listified = [*train_listified, *val_listified, *test_listified]
datasets = []
if version == 2:
for data_split in listified:
ds = SFTF2Dataset(model_spec=encoder_spec,
data=data_split,
data_format="audio_raw",
verbose=verbose)
datasets.append(ds)
else:
ds = SFTF3Dataset(model_spec=encoder_spec,
data=listified,
data_format="audio_raw",
verbose=verbose)
datasets.append(ds)
#
if verbose:
logging.info(f"filter_length {encoder_spec.filter_length()}")
logging.info(f"hop_length {encoder_spec.hop_length()}")
logging.info(f"win_length {encoder_spec.win_length()}")
logging.info(f"n_mel_channels {encoder_spec.n_mel_channels()}")
logging.info(f"sampling_rate {encoder_spec.sampling_rate()}")
logging.info(f"mel_fmin {encoder_spec.mel_fmin()}")
logging.info(f"mel_fmax {encoder_spec.mel_fmax()}")
# by default target we read form specs
if target_dir is not None:
p = Path(target_dir)
expanded = p.expanduser()
resolved = expanded.resolve()
if target_dir.exists() and target_dir.is_dir():
final_dir = resolved
else:
raise ConverterError("Can't resolve target dir.")
else:
final_dir = trainer_spec.get_dataset_dir()
print("Going to write to dir", final_dir)
ds_spec = trainer_spec.get_dataset_spec(dataset_name=dataset_name)
files = [
Path(ds_spec['dir']).expanduser() / ds_spec['training_meta'],
Path(ds_spec['dir']).expanduser() / ds_spec['validation_meta'],
Path(ds_spec['dir']).expanduser() / ds_spec['test_meta']
]
keys = ['train', 'validation', 'test']
for i, ds in enumerate(datasets):
convert_mel_to_data(encoder_spec, ds,
dataset_name=dataset_name,
meta_file=data[f'{keys[i]}_meta'],
target_dir=final_dir,
data_type=f"{keys[i]}")
# def handler(a,b=None):
# """
#
# :param a:
# :param b:
# :return:
# """
# sys.exit(1)
#
# def install_handler():
# """
#
# :return:
# """
# if sys.platform == "win32":
# import win32api
# win32api.SetConsoleCtrlHandler(handler, True)
def cleanup(is_dist: bool) -> None:
"""
:param is_dist:
:return:
"""
if is_distributed:
dist.destroy_process_group()
def signal_handler(sig, frame) -> None:
if is_distributed:
dist.destroy_process_group()
print("handling signal")
sys.exit(0)
def setup_handler(handler):
"""
:return:
"""
if sys.platform == "win32":
import win32api
win32api.SetConsoleCtrlHandler(handler, True)
signal.signal(signal.SIGINT, signal_handler)
if os.name != 'nt':
signal.signal(signal.SIGTSTP, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
def init_distributed(spec=None, rank=0, world_size=0) -> None:
"""
Routine for distributed training.
:param spec:
:param rank:
:param world_size:
:return:
"""
if spec is None:
print("Empty trainer spec.")
sys.exit()
# if rank != 0:
os.environ['MASTER_ADDR'] = spec.get_master_address()
os.environ['MASTER_PORT'] = spec.get_master_port()
# os.environ["PL_TORCH_DISTRIBUTED_BACKEND"] = "nccl"
assert torch.cuda.is_available(), "Distributed mode requires CUDA."
logger.info("Distributed Available".format(torch.cuda.device_count()))
logger.info("Distribute protocol nccl available {}".format(torch.distributed.is_nccl_available()))
logger.info("Distribute protocol mpi available {}".format(torch.distributed.is_mpi_available()))
logger.info("Distribute protocol glow available {}".format(torch.distributed.is_gloo_available()))
logger.info("Distribute endpoint {} my rank {}".format(spec.get_backend(), rank))
# Set cuda device so everything is done on the right GPU.
# torch.cuda.set_device(self.rank % torch.cuda.device_count())
logger.info("Set cuda device".format(rank % torch.cuda.device_count()))
# Initialize distributed communication
if rank == 0:
host = socket.gethostname()
address = socket.gethostbyname(host)
logger.info("resolve hostname {}".format(host))
logger.info("resolve hostname {}".format(address))
torch.distributed.init_process_group(
backend=spec.get_backend(),
init_method=spec.dist_url(),
world_size=world_size,
rank=rank)
print("Done init")
logger.debug("Done initializing distributed")
def tune_hyperparam(spec=None, cmd_args=None, device=None, cudnn_bench=False):
"""
Note Ray tunned pulled to a separate class and conditionally include
since ray has an issue with Python 3.10, I test code 3.9 and 3.10.
Specs for ray in same trainer spec.
Example will test batch size variation, lr , grad clip rate.
ray:
batch_size: [32, 64]
lr_min: 1e-4
lr_max: 1e-1
num_samples: 10
checkpoint_freq: 4
resources:
cpu: 4
gpu: 1
attention_location_filters: 32
attention_kernel_size: 31
grad_clip:
min: 0.5
max: 1.0
:param spec:
:param cmd_args:
:param device:
:param cudnn_bench:
:return:
"""
spec.set_logger(False)
if int(cmd_args.rank) == 0:
logger.info("Staring rank zero node.")
if spec.is_distributed_run():
logger.info("Staring training in distributed settings. "
"rank {} world size {}".format(args.rank, args.world_size))
init_distributed(spec, int(args.rank), int(args.world_size))
dist.barrier()
if cmd_args.overfit:
spec.set_overfit()
torch.backends.cudnn.enabled = True
if cudnn_bench:
torch.backends.cudnn.benchmark = True
if args.verbose:
logger.debug(f"Torch allow matmul fp16 "
f"{torch.backends.cuda.matmul.allow_fp16_reduced_precision_reduction}")
logger.debug(f"Torch cudnn version, "
f"{torch.backends.cudnn.version}")
logger.debug(f"Torch backend openmp "
f"{torch.backends.openmp}")
logger.debug(f"Torch backend openmp "
f"{torch.version.cuda}")
logger.debug(f"Torch backend openmp"
f"{torch.backends.cudnn.version()}")
logger.debug(f"Torch backend openmp "
f"{torch.__version__}")
logger.debug(f"Torch backend openmp "
f"{torch.cuda.get_device_name(0)}")
logger.debug(f"Torch backend openmp "
f"{torch.cuda.get_device_properties(0)}")
try:
_metric = 'mean_train_loss'
scheduler = ASHAScheduler(
metric=_metric,
mode="min",
max_t=100,
grace_period=1,
reduction_factor=2)
reporter = CLIReporter(
metric_columns=[metric, "training_iteration"])
ray_spec = spec.get_tuner_spec()
config = {
"spec": spec,
"world_size": int(0),
"device": device,
}
if 'batch_size' in ray_spec:
config['batch_size'] = tune.choice(ray_spec['batch_size'])
if 'lr_min' in ray_spec and 'lr_max' in ray_spec:
config['lr'] = tune.loguniform(ray_spec['lr_min'], ray_spec['lr_max'])
if 'num_samples' not in ray_spec:
print("You need indicate num_samples in spec. It mandatory for Ray to function. ")
return
if 'checkpoint_freq' not in ray_spec:
print("Please checkpoint_freq, It essential for Ray to checkpoint.")
return
if 'resources' not in ray_spec:
print("Please indicate resources ray can use.")
return
resources = ray_spec['resources']
# optional
if 'grad_clip' in ray_spec:
grad_clip_spec = ray_spec['grad_clip']
if 'min' in grad_clip_spec and 'max' in grad_clip_spec:
config['grad_clip'] = tune.loguniform(float(grad_clip_spec['min']),
float(grad_clip_spec['max']))
tuner_result = tune.run(Trainable,
resources_per_trial={"cpu": int(resources['cpu']),
"gpu": int(resources['gpu'])},
config=config,
num_samples=ray_spec['num_samples'],
scheduler=scheduler,
checkpoint_freq=ray_spec['checkpoint_freq'],
local_dir=spec.model_files.get_tuner_log_dir(),
stop={
# "mean_accuracy": 0.95,
"training_iteration": 2 if cmd_args.smoke_test else 20,
},
max_concurrent_trials=1,
progress_reporter=reporter)
best_trial = tuner_result.get_best_trial(_metric, "min", "last")
print("Best trial config: {}".format(best_trial.config))
print("Best trial final train loss: {}".format(best_trial.last_result[_metric]))
except TrainerError as e:
print("Error: trainer error: ", e)
cleanup(spec.is_distributed_run())
sys.exit(10)
except Exception as other:
print(other)
raise other
def train(spec=None, cmd_args=None, device=None, cudnn_bench=False):
"""
Main routine for to train a models.
:param cmd_args:
:param spec: trainer spec, a config
:param cudnn_bench: if we need run cudnn bench
:param device: device where run
:return:
"""
if int(cmd_args.rank) == 0:
logger.info("Staring rank zero node.")
if spec.is_distributed_run():
logger.info("Staring training in distributed settings. "
"rank {} world size {}".format(args.rank, args.world_size))
init_distributed(spec, int(args.rank), int(args.world_size))
# device = torch.device(f"cuda:{int(0)}")
# device = torch.device(f"cuda:{dist.get_rank()}")
# device = torch.device(device)
dist.barrier()
if cmd_args.overfit:
spec.set_overfit()
torch.backends.cudnn.enabled = True
if cudnn_bench:
torch.backends.cudnn.benchmark = True
if args.verbose:
logger.debug(f"Torch allow matmul fp16 "
f"{torch.backends.cuda.matmul.allow_fp16_reduced_precision_reduction}")
logger.debug(f"Torch cudnn version, "
f"{torch.backends.cudnn.version()}")
logger.debug(f"Torch backend openmp "
f"{torch.backends.openmp}")
try:
dataloader = SFTFDataloader(spec, rank=cmd_args.rank,
world_size=cmd_args.world_size,
verbose=args.verbose)
trainer = Trainer(spec,
dataloader,
rank=int(args.rank),
world_size=int(cmd_args.world_size),
verbose=args.verbose, device=device,
callback=[CheckpointBest()])
dataloader.set_logger(is_enable=cmd_args.verbose)
trainer.set_logger(is_enable=cmd_args.verbose)
trainer.metric.set_logger(is_enable=cmd_args.verbose)
trainer.train()
except TrainerError as e:
print("Error: trainer error: ", e)
cleanup(spec.is_distributed_run())
sys.exit(10)
except Exception as other:
print(other)
raise other
def dataloader_dry(cmd_args, trainer_specs):
"""
Routine pass dry run over dataset and read time.
# TODO add batch size.
:return:
"""
data_loader = SFTFDataloader(trainer_specs, verbose=cmd_args.verbose)
if cmd_args.benchmark:
data_loader.benchmark_read()
def set_random_seeds(random_seed=1234):
"""
Routine called when we run in DDP mode.
It fixes all seed values.
:param random_seed:
:return:
"""
logger.info("Setting random seed for torch.")
torch.manual_seed(random_seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
np.random.seed(random_seed)
random.seed(random_seed)
def inference(spec: ExperimentSpecs, cmd_args, device, sigma=1.0,
sampling_rate=22050, denoiser_strength=0.2,
max_wav_values=32768.0):
"""
Main routine for inference.
:param max_wav_values:
:param denoiser_strength:
:param sampling_rate:
:param sigma:
:param spec:
:param cmd_args:
:param device:
:return:
"""
if cmd_args.model_file:
model_path = Path(cmd_args.model_file)
if model_path.exists() and model_path.is_file():
trainer = Trainer(spec, rank=int(args.rank),
world_size=int(cmd_args.world_size),
verbose=args.verbose, device=device, is_inference=True)
trainer.load_for_inference(model_name="dtc",
layer_name="spectrogram_layer",
model_file=str(model_path.resolve()))
logger.info("Model loaded.")
# text = "Hello world, I missed you so much."
text = "Meat was no longer issued raw, to be imperfectly cooked before " \
"a ward fire and bolted gluttonously, the whole two pounds at one sitting."
mel_outputs, mel_outputs_postnet, alignments = \
trainer.inference(input_seq=text,
model_name="dtc",
mel_output_path=str(spec.model_files.get_figure_dir() / "mel_out.png"),
mel_post_path=str(spec.model_files.get_figure_dir() / "mel_post.png"),
mel_alignment_path=str(spec.model_files.get_figure_dir() / "mel_alignment.png"),
plot=True)
glow_path = f"{spec.model_files.get_model_dir()}/waveglow_256channels_universal_v5.pt"
model_file = Path(glow_path)
if not model_file.exists():
print("Please download glow model and put to a results/model dir.")
print("url: https://drive.google.com/u/0/uc?id=1rpK8CzAAirq9sWZhe9nlfvxMF1dRgFbF&export=download")
return
from waveglow.denoiser import Denoiser
sys.path.insert(0, './waveglow')
waveglow = torch.load(str(model_file))['model']
waveglow = waveglow.remove_weightnorm(waveglow)
waveglow.cuda().eval()
denoiser = None
if denoiser_strength > 0:
denoiser = Denoiser(waveglow).cuda()
mel = torch.autograd.Variable(mel_outputs_postnet.cuda())
# mel = torch.unsqueeze(mel_outputs, 0)
with torch.no_grad():
audio = waveglow.infer(mel, sigma=sigma)
if denoiser is not None:
audio = denoiser(audio, denoiser_strength)
audio = audio * max_wav_values
audio = audio.squeeze()
audio = audio.cpu().numpy()
audio = audio.astype('int16')
audio_path = os.path.join(spec.model_files.get_results_dir(), "{}_synthesis.wav".format("test.wav"))
scipy.io.wavfile.write(audio_path, sampling_rate, audio)
# from scipy.io import wavfile
# from pesq import pesq
#
# rate, ref = wavfile.read(audio_path)
# rate, deg = wavfile.read("./audio/speech_bab_0dB.wav")
# print(pesq(rate, ref, deg, 'wb'))
# print(pesq(rate, ref, deg, 'nb'))
print(audio_path)
else:
print("Error: File does not exist {}".format(cmd_args.model_file))
sys.exit()
def load_glow(spec: ExperimentSpecs, default_filename="waveglow_256channels_universal_v5.pt"):
"""
Load Wave glow model.
:param default_filename:
:param spec:
:return:
"""
glow_path = f"{spec.model_files.get_model_dir()}/{default_filename}"
model_file = Path(glow_path)
if not model_file.exists():
print("Please download a glow model file and put to a results/model directory.")
print("url: https://drive.google.com/u/0/uc?id=1rpK8CzAAirq9sWZhe9nlfvxMF1dRgFbF&export=download")
return
from waveglow.denoiser import Denoiser
sys.path.insert(0, './waveglow')
waveglow = torch.load(str(model_file))['model']
waveglow = waveglow.remove_weightnorm(waveglow)
waveglow.cuda().eval()
return waveglow
def glow_inference(waveglow: torch.nn.Module, mel: torch.Tensor,
sigma: Optional[float] = 1.0, denoiser_strength: Optional[float] = 0.2,
max_wav_value: Optional[float] = 32768.0):
"""
Perform glow inference for input given mel spectrogram.
:param waveglow:
:param mel:
:param max_wav_value:
:param denoiser_strength:
:param sigma:
:return:
"""
from waveglow.denoiser import Denoiser
denoiser = None
if denoiser_strength > 0:
denoiser = Denoiser(waveglow).cuda()
mel = torch.autograd.Variable(mel.cuda())
# mel = torch.unsqueeze(mel_outputs, 0)
with torch.no_grad():
audio = waveglow.infer(mel, sigma=sigma)
if denoiser is not None:
audio = denoiser(audio, denoiser_strength)
audio = audio * max_wav_value
audio = audio.squeeze()
audio = audio.cpu().numpy()
audio = audio.astype('int16')
return audio
def load_dtc(spec: ExperimentSpecs, device,
model_path: Optional[str],
model_name: Optional[str] = "dtc",
layer_name: Optional[str] = "spectrogram_layer"):
"""
:param model_path:
:param device:
:param model_name:
:param layer_name:
:param spec:
:return:
"""
trainer = Trainer(spec, verbose=False, device=device, is_inference=True)
trainer.load_for_inference(model_name=model_name,
layer_name=layer_name,
model_file=model_path)
return trainer
def metric(spec: ExperimentSpecs, cmd_args, device,
model_name: Optional[str] = "dtc",
layer_name: Optional[str] = "spectrogram_layer",
num_sample: Optional[int] = 10,
glow_file_name: Optional[str] = "waveglow_256channels_universal_v5.pt",
sampling_rate: Optional[int] = 22050):
"""
Main routine for PESQ computation.
:param layer_name:
:param model_name:
:param glow_file_name:
:param num_sample:
:param sampling_rate:
:param spec:
:param cmd_args:
:param device:
:return:
"""
if not cmd_args.model_file:
print("Please indicate model file in argument --model_file.")
return
model_path = Path(cmd_args.model_file)
if model_path.exists() and model_path.is_file():
trainer = load_dtc(spec=spec, model_name=model_name,
layer_name=layer_name,
device=device, model_path=str(model_path))
glow_path = f"{spec.model_files.get_model_dir()}/{glow_file_name}"
model_file = Path(glow_path)
if not model_file.exists():
print("Please download glow model and put to a results/model dir.")
print("url: https://drive.google.com/u/0/uc?id=1rpK8CzAAirq9sWZhe9nlfvxMF1dRgFbF&export=download")
return
waveglow = None
try:
waveglow = load_glow(spec)
except Exception as err:
print(f"Failed to load glow model. err {err}")
dataset_files = spec.get_audio_dataset()
ds_keys = spec.get_audio_dataset_keys()
assert len(ds_keys) > 0
assert len(dataset_files) > 0
# for keys ['train_set', 'validation_set', 'test_set'] we sample K times
for _, k in enumerate(ds_keys):
if k not in dataset_files:
continue
ds = list(dataset_files[k].values())
pesq_wbs = np.zeros((num_sample, 1))
pesq_nss = np.zeros((num_sample, 1))
for i, dataset_rec in enumerate(ds):
if i == num_sample:
break
print(f"Sampled for {k} {i}-th example")
text_seq = dataset_rec['meta']
print(f"Generating inference for {text_seq.strip()} "
f"original path {dataset_rec['path']}")
file_name = Path(dataset_rec['path'])
mel_outputs, mel_outputs_post, alignments = \
trainer.inference(input_seq=text_seq, model_name=model_name, plot=False)
audio = glow_inference(waveglow, mel_outputs_post)
audio_path = os.path.join(spec.model_files.get_generated_dir(),
"{}_synthesis.wav".format(file_name.name))
scipy.io.wavfile.write(audio_path, sampling_rate, audio)
print(f"Saving audio {audio_path}")
# down sample since peqq need either 8k or 16k
ref, s = librosa.load(dataset_rec['path'], sr=16000)
deg, s = librosa.load(audio_path, sr=16000)
# now we read both files.
from pesq import pesq
pesq_wb = pesq(16000, ref, deg, 'wb')
pesw_nb = pesq(16000, ref, deg, 'nb')
pesq_wbs[i] = pesq_wb
pesq_nss[i] = pesw_nb
print(f"{file_name.name} : {pesq_wb}")
print(f"{file_name.name} : {pesw_nb}")
print(f"Average pesq for {k} : {pesq_wbs.mean()} {pesq_wbs.mean()}")
else:
print("Error: File does not exist {}".format(cmd_args.model_file))
sys.exit()
def main(cmd_args):
"""
:param cmd_args:
:return:
"""
if cmd_args.device_id >= 0:
logger.info("Manually setting cuda device.")
_device = torch.device(f"cuda:{int(cmd_args.device_id)}" if torch.cuda.is_available() else "cpu")
else:
_device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
_start_time = time.time()
trainer_spec = ExperimentSpecs(spec_config=cmd_args.config, verbose=cmd_args.verbose)
trainer_spec.set_logger(cmd_args.verbose)
print("--- %s Parser load time, seconds ---" % (time.time() - _start_time))
# cmd overwrite batch size from spec.
if cmd_args.batch_size is not None:
trainer_spec.set_batch_size(int(cmd_args.batch_size))
if cmd_args.batch_size is not None:
trainer_spec.set_epochs(int(cmd_args.epochs))
# similarly active dataset
if cmd_args.dataset_name is not None and len(cmd_args.dataset_name) > 0:
trainer_spec.set_active_dataset(dataset_name=str(cmd_args.dataset_name))
logger.add(trainer_spec.model_files.get_model_log_file_path(remove_old=True),
format="{elapsed} {level} {message}",
filter="model_trainer.trainer_metrics", level="INFO", rotation="1h")
logger.add(trainer_spec.model_files.get_trace_log_file("loader"),
format="{elapsed} {level} {message}",
filter="model_loader.stft_dataloader", level="INFO", rotation="1h")
logger.add(trainer_spec.model_files.get_trace_log_file("trainer"),
format="{elapsed} {level} {message}",
filter="model_trainer.trainer", level="INFO", rotation="1h")