diff --git a/codes/scripts/audio/gen/use_diffuse_tts.py b/codes/scripts/audio/gen/use_diffuse_tts.py index 2b5eb458..04363673 100644 --- a/codes/scripts/audio/gen/use_diffuse_tts.py +++ b/codes/scripts/audio/gen/use_diffuse_tts.py @@ -20,26 +20,28 @@ def ceil_multiple(base, multiple): if __name__ == '__main__': parser = argparse.ArgumentParser() - parser.add_argument('-opt', type=str, help='Path to options YAML file used to train the diffusion model', default='../options/train_diffusion_tts_medium.yml') + parser.add_argument('-opt', type=str, help='Path to options YAML file used to train the diffusion model', default='X:\\dlas\\experiments\\train_diffusion_tts_medium\\train_diffusion_tts_medium.yml') parser.add_argument('-diffusion_model_name', type=str, help='Name of the diffusion model in opt.', default='generator') - parser.add_argument('-diffusion_model_path', type=str, help='Path to saved model weights', default='X:\\dlas\\experiments\\train_diffusion_tts_medium\\models\\14800_generator_ema.pth') + parser.add_argument('-diffusion_model_path', type=str, help='Path to saved model weights', default='X:\\dlas\\experiments\\train_diffusion_tts_medium\\models\\22500_generator_ema.pth') parser.add_argument('-aligned_codes', type=str, help='Comma-delimited list of integer codes that defines text & prosody. Get this by apply W2V to an existing audio clip or from a bespoke generator.', default='0,0,0,0,10,10,0,4,0,7,0,17,4,4,0,25,5,0,13,13,0,22,4,4,0,21,15,15,7,0,0,14,4,4,6,8,4,4,0,0,12,5,0,0,5,0,4,4,22,22,8,16,16,0,4,4,4,0,0,0,0,0,0,0') # Default: 'i am very glad to see you', libritts/train-clean-100/103/1241/103_1241_000017_000001.wav. # -cond "Y:\libritts/train-clean-100/103/1241/103_1241_000017_000001.wav" parser.add_argument('-cond', type=str, help='Path to the conditioning input audio file.', default='Y:\\clips\\books1\\754_Dan Simmons - The Rise Of Endymion 356 of 450\\00026.wav') parser.add_argument('-diffusion_steps', type=int, help='Number of diffusion steps to perform to create the generate. Lower steps reduces quality, but >40 is generally pretty good.', default=100) parser.add_argument('-output_path', type=str, help='Where to store outputs.', default='../results/use_diffuse_tts') + parser.add_argument('-device', type=str, help='Device to run on', default='cpu') args = parser.parse_args() os.makedirs(args.output_path, exist_ok=True) print("Loading Diffusion Model..") - diffusion = load_model_from_config(args.opt, args.diffusion_model_name, also_load_savepoint=False, load_path=args.diffusion_model_path) + diffusion = load_model_from_config(args.opt, args.diffusion_model_name, also_load_savepoint=False, + load_path=args.diffusion_model_path, device=args.device) aligned_codes_compression_factor = 221 # Derived empirically for 11025Hz sample rate. Adjust if sample rate increases. print("Loading data..") - aligned_codes = torch.tensor([int(s) for s in args.aligned_codes.split(',')]).cuda() + aligned_codes = torch.tensor([int(s) for s in args.aligned_codes.split(',')]).to(args.device) diffuser = load_discrete_vocoder_diffuser(desired_diffusion_steps=args.diffusion_steps) - cond = load_audio(args.cond, 22050).cuda() + cond = load_audio(args.cond, 22050).to(args.device) if cond.shape[-1] > 88000: cond = cond[:,:88000] @@ -48,7 +50,7 @@ if __name__ == '__main__': diffusion.eval() output_shape = (1, 1, ceil_multiple(aligned_codes.shape[-1]*aligned_codes_compression_factor, 2048)) - output = diffuser.p_sample_loop(diffusion, output_shape, noise=torch.zeros(output_shape, device='cuda'), + output = diffuser.p_sample_loop(diffusion, output_shape, noise=torch.zeros(output_shape, device=args.device), model_kwargs={'tokens': aligned_codes.unsqueeze(0), 'conditioning_input': cond.unsqueeze(0)}) torchaudio.save(os.path.join(args.output_path, f'output_mean.wav'), output.cpu().squeeze(0), 11025) diff --git a/codes/scripts/audio/gen/use_gpt_tts.py b/codes/scripts/audio/gen/use_gpt_tts.py index 1ffb7b60..e2e1c9fe 100644 --- a/codes/scripts/audio/gen/use_gpt_tts.py +++ b/codes/scripts/audio/gen/use_gpt_tts.py @@ -98,6 +98,11 @@ if __name__ == '__main__': parser.add_argument('-opt_clip', type=str, help='Path to options YAML file used to train the CLIP model', default='X:\\dlas\\experiments\\train_clip_text_to_voice.yml') parser.add_argument('-clip_model_name', type=str, help='Name of the CLIP model in opt.', default='clip') parser.add_argument('-clip_model_path', type=str, help='CLIP model checkpoint to load.', default='X:\\dlas\\experiments\\train_clip_text_to_voice_masking_bigger_batch\\models\\23500_clip_ema.pth') + parser.add_argument('-opt_cond_clip', type=str, help='Path to options YAML file used to train the Conditioning CLIP model', default='D:\\dlas\\options\\train_clip_cond_to_voice.yml') + parser.add_argument('-cond_clip_model_name', type=str, help='Name of the CLIP model in opt.', default='clip') + parser.add_argument('-cond_clip_model_path', type=str, help='CLIP model checkpoint to load.', default='D:\\dlas\\experiments\\train_clip_cond_to_voice\\models\\42000_clip_ema.pth') + parser.add_argument('-cond_clip_weight', type=float, help='How much to weight the conditioning CLIP to the text CLIP. Lower means the sample sounds more like the text, higher means it sounds more like the conditioning.', + default=.3) parser.add_argument('-text', type=str, help='Text to speak.', default="I am a language model that has learned to speak.") parser.add_argument('-cond_preset', type=str, help='Use a preset conditioning voice (defined above). Overrides cond_path.', default='libri_test') parser.add_argument('-num_samples', type=int, help='How many total outputs the autoregressive transformer should produce.', default=128) @@ -141,6 +146,7 @@ if __name__ == '__main__': print("Loading CLIP..") clip = load_model_from_config(args.opt_clip, model_name=args.clip_model_name, also_load_savepoint=False, load_path=args.clip_model_path).eval() + cond_clip = load_model_from_config(args.opt_cond_clip, model_name=args.cond_clip_model_name, also_load_savepoint=False, load_path=args.cond_clip_model_path).eval() print("Performing CLIP filtering..") for i in range(samples.shape[0]): samples[i] = fix_autoregressive_output(samples[i], stop_mel_token) @@ -148,6 +154,9 @@ if __name__ == '__main__': torch.full((samples.shape[0],), fill_value=text.shape[1]-1, dtype=torch.long, device='cuda'), samples, torch.full((samples.shape[0],), fill_value=samples.shape[1]*1024, dtype=torch.long, device='cuda'), return_loss=False) + cond_clip_results = cond_clip(conds[:, -1], samples, torch.full((samples.shape[0],), fill_value=samples.shape[1]*1024, + dtype=torch.long, device='cuda'), return_loss=False) + clip_results = clip_results * (1-args.cond_clip_weight) + cond_clip_results * args.cond_clip_weight best_results = samples[torch.topk(clip_results, k=args.num_outputs).indices] # Delete the GPT TTS model to free up GPU memory diff --git a/codes/utils/util.py b/codes/utils/util.py index ff5267b7..92b84cf4 100644 --- a/codes/utils/util.py +++ b/codes/utils/util.py @@ -467,7 +467,8 @@ def clip_grad_norm(parameters: list, parameter_names: list, max_norm: float, nor Loader, Dumper = OrderedYaml() -def load_model_from_config(cfg_file=None, model_name=None, dev='cuda', also_load_savepoint=True, load_path=None, preloaded_options=None, strict_load=True): +def load_model_from_config(cfg_file=None, model_name=None, also_load_savepoint=True, load_path=None, + preloaded_options=None, strict_load=True, device=None): if preloaded_options is not None: opt = preloaded_options else: @@ -480,13 +481,14 @@ def load_model_from_config(cfg_file=None, model_name=None, dev='cuda', also_load model_cfg = opt['networks'][model_name] if 'which_model_G' in model_cfg.keys() and 'which_model' not in model_cfg.keys(): model_cfg['which_model'] = model_cfg['which_model_G'] - model = networks.create_model(opt, model_cfg).to(dev) + model = networks.create_model(opt, model_cfg).to(device) if also_load_savepoint and f'pretrain_model_{model_name}' in opt['path'].keys(): assert load_path is None load_path = opt['path'][f'pretrain_model_{model_name}'] if load_path is not None: print(f"Loading from {load_path}") - model.load_state_dict(torch.load(load_path), strict=strict_load) + sd = torch.load(load_path, map_location=device) + model.load_state_dict(sd, strict=strict_load) return model