def infer(flowtron_path, waveglow_path, output_dir, text, speaker_id, n_frames, sigma, gate_threshold, seed):
    torch.manual_seed(seed)
    torch.cuda.manual_seed(seed)
    waveglow = torch.load(waveglow_path)['model'].cuda().eval()
    waveglow.cuda().half()
    for k in waveglow.convinv:
        k.float()
    waveglow.eval()
    model = Flowtron(**model_config).cuda()
    state_dict = torch.load(flowtron_path, map_location='cpu')['state_dict']
    model.load_state_dict(state_dict)
    model.eval()
    print("Loaded checkpoint '{}')".format(flowtron_path))
    ignore_keys = ['training_files', 'validation_files']
    trainset = Data(data_config['training_files'], **dict(((k, v) for (k, v) in data_config.items() if (k not in ignore_keys))))
    speaker_vecs = trainset.get_speaker_id(speaker_id).cuda()
    text = trainset.get_text(text).cuda()
    speaker_vecs = speaker_vecs[None]
    text = text[None]
    with torch.no_grad():
        residual = (torch.cuda.FloatTensor(1, 80, n_frames).normal_() * sigma)
        (mels, attentions) = model.infer(residual, speaker_vecs, text, gate_threshold=gate_threshold)
    for k in range(len(attentions)):
        attention = torch.cat(attentions[k]).cpu().numpy()
        (fig, axes) = plt.subplots(1, 2, figsize=(16, 4))
        axes[0].imshow(mels[0].cpu().numpy(), origin='bottom', aspect='auto')
        axes[1].imshow(attention[:, 0].transpose(), origin='bottom', aspect='auto')
        fig.savefig(os.path.join(output_dir, 'sid{}_sigma{}_attnlayer{}.png'.format(speaker_id, sigma, k)))
        plt.close('all')
    with torch.no_grad():
        audio = waveglow.infer(mels.half(), sigma=0.8).float()
    audio = audio.cpu().numpy()[0]
    audio = (audio / np.abs(audio).max())
    print(audio.shape)
    write(os.path.join(output_dir, 'sid{}_sigma{}.wav'.format(speaker_id, sigma)), data_config['sampling_rate'], audio)
def infer(flowtron_path, waveglow_path, output_dir, text, speaker_id, n_frames, sigma, gate_threshold, seed):
    torch.manual_seed(seed)
    torch.cuda.manual_seed(seed)
    waveglow = torch.load(waveglow_path)['model'].cuda().eval()
    waveglow.cuda().half()
    for k in waveglow.convinv:
        k.float()
    waveglow.eval()
    model = Flowtron(**model_config).cuda()
    state_dict = torch.load(flowtron_path, map_location='cpu')['state_dict']
    model.load_state_dict(state_dict)
    model.eval()
    print("Loaded checkpoint '{}')".format(flowtron_path))
    ignore_keys = ['training_files', 'validation_files']
    trainset = Data(data_config['training_files'], **dict(((k, v) for (k, v) in data_config.items() if (k not in ignore_keys))))
    speaker_vecs = trainset.get_speaker_id(speaker_id).cuda()
    text = trainset.get_text(text).cuda()
    speaker_vecs = speaker_vecs[None]
    text = text[None]
    with torch.no_grad():
        residual = (torch.cuda.FloatTensor(1, 80, n_frames).normal_() * sigma)
        (mels, attentions) = model.infer(residual, speaker_vecs, text, gate_threshold=gate_threshold)
    for k in range(len(attentions)):
        attention = torch.cat(attentions[k]).cpu().numpy()
        (fig, axes) = plt.subplots(1, 2, figsize=(16, 4))
        axes[0].imshow(mels[0].cpu().numpy(), origin='bottom', aspect='auto')
        axes[1].imshow(attention[:, 0].transpose(), origin='bottom', aspect='auto')
        fig.savefig(os.path.join(output_dir, 'sid{}_sigma{}_attnlayer{}.png'.format(speaker_id, sigma, k)))
        plt.close('all')
    with torch.no_grad():
        audio = waveglow.infer(mels.half(), sigma=0.8).float()
    audio = audio.cpu().numpy()[0]
    audio = (audio / np.abs(audio).max())
    print(audio.shape)
    write(os.path.join(output_dir, 'sid{}_sigma{}.wav'.format(speaker_id, sigma)), data_config['sampling_rate'], audio)