From 0c95be16249db7613f7ad40cf027682c83eed71e Mon Sep 17 00:00:00 2001 From: James Betker Date: Sun, 12 Jun 2022 14:41:06 -0600 Subject: [PATCH] Fix MDF evaluator for current generation of --- .../audio/music/transformer_diffusion11.py | 9 +++--- codes/trainer/eval/audio_diffusion_fid.py | 17 ++++------ codes/trainer/eval/music_diffusion_fid.py | 32 +++++++++++-------- 3 files changed, 29 insertions(+), 29 deletions(-) diff --git a/codes/models/audio/music/transformer_diffusion11.py b/codes/models/audio/music/transformer_diffusion11.py index 659f8361..3da9b6b9 100644 --- a/codes/models/audio/music/transformer_diffusion11.py +++ b/codes/models/audio/music/transformer_diffusion11.py @@ -355,8 +355,7 @@ def register_transformer_diffusion11_with_ar_prior(opt_net, opt): def test_quant_model(): - clip = torch.randn(2, 256, 400) - cond = torch.randn(2, 256, 400) + clip = torch.randn(2, 100, 400) ts = torch.LongTensor([600, 600]) """ @@ -371,19 +370,19 @@ def test_quant_model(): """ # For TTS: - model = TransformerDiffusionWithQuantizer(in_channels=256, model_channels=1024, + model = TransformerDiffusionWithQuantizer(in_channels=100, out_channels=200, model_channels=1024, prenet_channels=1024, num_heads=4, input_vec_dim=1024, num_layers=12, prenet_layers=10, quantizer_dims=[1024,768,512], quantizer_codebook_size=64, quantizer_codebook_groups=4, dropout=.1) - quant_weights = torch.load('X:\\dlas\\experiments\\train_tts_quant_64\\models\\15500_generator.pth') + quant_weights = torch.load('X:\\dlas\\experiments\\train_tts_quant_128\\models\\4000_generator.pth') model.quantizer.load_state_dict(quant_weights, strict=False) torch.save(model.state_dict(), 'sample.pth') print_network(model) - o = model(clip, ts, clip, cond) + o = model(clip, ts, clip) model.get_grad_norm_parameter_groups() diff --git a/codes/trainer/eval/audio_diffusion_fid.py b/codes/trainer/eval/audio_diffusion_fid.py index 232d96e7..b9f61e1f 100644 --- a/codes/trainer/eval/audio_diffusion_fid.py +++ b/codes/trainer/eval/audio_diffusion_fid.py @@ -20,7 +20,7 @@ from models.clip.mel_text_clip import MelTextCLIP from models.audio.tts.tacotron2 import text_to_sequence from scripts.audio.gen.speech_synthesis_utils import load_discrete_vocoder_diffuser, wav_to_mel, load_speech_dvae, \ convert_mel_to_codes, load_univnet_vocoder, wav_to_univnet_mel, load_clvp -from trainer.injectors.audio_injectors import denormalize_mel, TorchMelSpectrogramInjector +from trainer.injectors.audio_injectors import denormalize_mel, TorchMelSpectrogramInjector, normalize_mel from utils.util import ceil_multiple, opt_get, load_model_from_config, pad_or_truncate @@ -214,12 +214,9 @@ class AudioDiffusionFid(evaluator.Evaluator): 'conditioning_input': None, 'disable_diversity': True}) - # denormalize mel - gen_mel = denormalize_mel(gen_mel) - - gen_wav = self.local_modules['vocoder'].inference(gen_mel) - real_dec = self.local_modules['vocoder'].inference(mel) - return gen_wav.float(), real_dec, SAMPLE_RATE + gen_wav = self.local_modules['vocoder'].inference(denormalize_mel(gen_mel)) + real_dec = self.local_modules['vocoder'].inference(denormalize_mel(mel)) + return gen_wav.float(), real_dec, gen_mel, mel, SAMPLE_RATE def load_projector(self): """ @@ -337,12 +334,12 @@ if __name__ == '__main__': if __name__ == '__main__': - diffusion = load_model_from_config('X:\\dlas\\experiments\\train_speech_diffusion_from_ctc_und10\\train.yml', 'generator', + diffusion = load_model_from_config('X:\\dlas\\experiments\\train_tts_diffusion_tfd11_quant\\train.yml', 'generator', also_load_savepoint=False, - load_path='X:\\dlas\\experiments\\train_speech_diffusion_from_ctc_und10\\models\\43000_generator_ema.pth').cuda() + load_path='X:\\dlas\\experiments\\train_tts_diffusion_tfd11_quant\\models\\14500_generator_ema.pth').cuda() opt_eval = {'eval_tsv': 'Y:\\libritts\\test-clean\\transcribed-oco-realtext.tsv', 'diffusion_steps': 100, 'conditioning_free': False, 'conditioning_free_k': 1, - 'diffusion_schedule': 'linear', 'diffusion_type': 'tfd'} + 'diffusion_schedule': 'cosine', 'diffusion_type': 'tfd'} env = {'rank': 0, 'base_path': 'D:\\tmp\\test_eval', 'step': 100, 'device': 'cuda', 'opt': {}} eval = AudioDiffusionFid(diffusion, opt_eval, env) print(eval.perform_eval()) diff --git a/codes/trainer/eval/music_diffusion_fid.py b/codes/trainer/eval/music_diffusion_fid.py index 8d7a1100..415f48b0 100644 --- a/codes/trainer/eval/music_diffusion_fid.py +++ b/codes/trainer/eval/music_diffusion_fid.py @@ -48,6 +48,9 @@ class MusicDiffusionFid(evaluator.Evaluator): self.diffuser = SpacedDiffusion(use_timesteps=space_timesteps(4000, [diffusion_steps]), model_mean_type='epsilon', model_var_type='learned_range', loss_type='mse', betas=get_named_beta_schedule(diffusion_schedule, 4000), conditioning_free=conditioning_free_diffusion_enabled, conditioning_free_k=conditioning_free_k) + self.spectral_diffuser = SpacedDiffusion(use_timesteps=space_timesteps(4000, [100]), model_mean_type='epsilon', + model_var_type='learned_range', loss_type='mse', betas=get_named_beta_schedule('linear', 4000), + conditioning_free=False, conditioning_free_k=1) self.dev = self.env['device'] mode = opt_get(opt_eval, ['diffusion_type'], 'tts') @@ -106,7 +109,7 @@ class MusicDiffusionFid(evaluator.Evaluator): gen_mel_denorm = denormalize_mel(gen_mel) output_shape = (1,16,audio.shape[-1]//16) self.spec_decoder = self.spec_decoder.to(audio.device) - gen_wav = self.diffuser.p_sample_loop(self.spec_decoder, output_shape, + gen_wav = self.spectral_diffuser.p_sample_loop(self.spec_decoder, output_shape, model_kwargs={'aligned_conditioning': gen_mel_denorm}) gen_wav = pixel_shuffle_1d(gen_wav, 16) @@ -127,14 +130,14 @@ class MusicDiffusionFid(evaluator.Evaluator): # x = x.clamp(-s, s) / s # return x gen_mel = self.diffuser.p_sample_loop(self.model, mel_norm.shape, #denoised_fn=denoising_fn, clip_denoised=False, - model_kwargs={'truth_mel': mel, - 'conditioning_input': torch.zeros_like(mel_norm[:,:,:390]), + model_kwargs={'truth_mel': mel_norm, + 'conditioning_input': None, 'disable_diversity': True}) gen_mel_denorm = denormalize_mel(gen_mel) output_shape = (1,16,audio.shape[-1]//16) self.spec_decoder = self.spec_decoder.to(audio.device) - gen_wav = self.diffuser.p_sample_loop(self.spec_decoder, output_shape, + gen_wav = self.spectral_diffuser.p_sample_loop(self.spec_decoder, output_shape, model_kwargs={'aligned_conditioning': gen_mel_denorm}) gen_wav = pixel_shuffle_1d(gen_wav, 16) @@ -160,7 +163,7 @@ class MusicDiffusionFid(evaluator.Evaluator): gen_mel_denorm = denormalize_mel(gen_mel) output_shape = (1,16,audio.shape[-1]//16) self.spec_decoder = self.spec_decoder.to(audio.device) - gen_wav = self.diffuser.p_sample_loop(self.spec_decoder, output_shape, + gen_wav = self.spectral_diffuser.p_sample_loop(self.spec_decoder, output_shape, model_kwargs={'aligned_conditioning': gen_mel_denorm}) gen_wav = pixel_shuffle_1d(gen_wav, 16) @@ -236,7 +239,7 @@ class MusicDiffusionFid(evaluator.Evaluator): for i in tqdm(list(range(0, len(self.data), self.skip))): path = self.data[(i + self.env['rank']) % len(self.data)] audio = load_audio(path, 22050).to(self.dev) - audio = audio[:, :22050*10] + audio = audio[:, :100000] sample, ref, sample_mel, ref_mel, sample_rate = self.diffusion_fn(audio) gen_projections.append(self.project(sample, sample_rate).cpu()) # Store on CPU to avoid wasting GPU memory. @@ -266,16 +269,17 @@ class MusicDiffusionFid(evaluator.Evaluator): if __name__ == '__main__': - diffusion = load_model_from_config('X:\\dlas\\experiments\\train_music_diffusion_ar_prior.yml', 'generator', + diffusion = load_model_from_config('X:\\dlas\\experiments\\train_music_diffusion_tfd_quant.yml', 'generator', also_load_savepoint=False, - load_path='X:\\dlas\\experiments\\train_music_diffusion_ar_prior\\models\\22000_generator_ema.pth' + load_path='X:\\dlas\\experiments\\train_music_diffusion_tfd11\\models\\24000_generator_ema.pth' ).cuda() - opt_eval = {#'path': 'Y:\\split\\yt-music-eval', - 'path': 'E:\\music_eval', - 'diffusion_steps': 100, + opt_eval = {'path': 'Y:\\split\\yt-music-eval', # eval music, mostly electronica. :) + #'path': 'E:\\music_eval', # this is music from the training dataset, including a lot more variety. + 'diffusion_steps': 200, 'conditioning_free': False, 'conditioning_free_k': 1, - 'diffusion_schedule': 'linear', 'diffusion_type': 'partial_from_codes_quant', - 'partial_low': 128, 'partial_high': 192} - env = {'rank': 0, 'base_path': 'D:\\tmp\\test_eval_music', 'step': 504, 'device': 'cuda', 'opt': {}} + 'diffusion_schedule': 'cosine', 'diffusion_type': 'from_codes_quant', + #'partial_low': 128, 'partial_high': 192 + } + env = {'rank': 0, 'base_path': 'D:\\tmp\\test_eval_music', 'step': 600, 'device': 'cuda', 'opt': {}} eval = MusicDiffusionFid(diffusion, opt_eval, env) print(eval.perform_eval())