TTS icon indicating copy to clipboard operation
TTS copied to clipboard

[Discussion] WaveGrad

Open george-roussos opened this issue 6 years ago • 76 comments

This is not an issue and is more of a discussion. I read the WaveGrad paper today (which may be found here) and listened to the samples here, which sound very good. There seems to be an open source implementation already here with great progress. Has anyone read the paper or used this implementation?

george-roussos avatar Sep 17 '20 07:09 george-roussos

in the repo it says 2 days of training is enough for convergence which is a huge time reduction, however, it is unclear how fast it is on a CPU.

I skim the paper quickly. Probability diffusion and Lavegin dynamics are topics that I am not really familiar but the main idea looks similar to normalizing flows with its iterations having a resemblance to the flow steps.

erogol avatar Sep 17 '20 10:09 erogol

I saw the same! 2 days then it seems that it should be even faster than the GANs and no finetuning needed if you want less iterations for faster inference. I don't think it can use a CPU though, because the repo mentions grid search on CPU is slow, so I would imagine even 6 iterations (so lowest quality) would take longer on CPU. I also tried to see if it works with spectrograms synthesized by mozilla TTS but didn't get far. But even the 6 iters sample sounds nice

george-roussos avatar Sep 17 '20 10:09 george-roussos

CPU is a blocker for me to consider it since MB-MelGAN is already faster than real-time on a CPU. But 2 days of training is really impressive for a vocoder. Now I wait who will write the paper using GAN with this model and report better results :)

erogol avatar Sep 17 '20 11:09 erogol

I agree. They also mention a 2080ti for inference, so I guess it is just too expensive.

george-roussos avatar Sep 17 '20 11:09 george-roussos

@freds0 and I supported the TTS audio configuration on this fork here . We adapted the upsample factors of the model to our hop_lenght. We are training the model on the train-clean-100 and 360 subsets of the LibriTTS dataset and we hope to have good results in the coming days. We intend to provide the checkpoint. On the other hand, we are also trying to train WaveGlow with the TTS audio configuration. So we would have 2 SOTA vocoders.

Edresson avatar Sep 19 '20 14:09 Edresson

Have you tried inference with 6 iterations and a number between 25 and 50? How does it sound? Is it fast?

george-roussos avatar Sep 21 '20 07:09 george-roussos

Have you tried inference with 6 iterations and a number between 25 and 50? How does it sound? Is it fast?

I tested an 11-second audio on Google Colab with 6 interactions: GPU: 0.41608357429504395 seconds CPU: 77.3947184085846 seconds

Here is a sample of the model trained for 1 day, in the TTS audio configuration

Edresson avatar Sep 21 '20 10:09 Edresson

It sounds nice for 6. And the other repo is also updated to show inference on CPU for 6 iterations.

Is it plugged in TTS the same way as WaveRNN?

george-roussos avatar Sep 21 '20 11:09 george-roussos

@george-roussos Yes, integration is very simple. I used this implementation and not the one that is more active because it provided a multispeaker model for transfer learning.

I synthesized some new samples with our best multispeaker model. These voices are voices generated with random vectors, that is, they are artificial voices. The other samples were from a model that was not very good.

wavegrad-english-sample-with-our-better-model.zip

Edresson avatar Sep 21 '20 12:09 Edresson

Do you have a notebook somewhere that you run it?

george-roussos avatar Sep 21 '20 13:09 george-roussos

You can download the current WaveGrad checkpoint at the link: https://drive.google.com/uc?id=1Te-9YaUirTGOa2syq2uQwBNU1vbDlUN0 (gdown https://drive.google.com/uc?id=1Te-9YaUirTGOa2syq2uQwBNU1vbDlUN0)

You must install WaveGrad with the following command: pip install git+https://github.com/freds0/wavegrad.git

Then, just use the wavegrad_predict function by passing the transposed Mel spectrogram (spec.T) and the device.

import os
import torch
import torchaudio
import time
from tqdm import tqdm

from argparse import ArgumentParser

from wavegrad.params import AttrDict, params as WAVEGRAD_PARAMS
from wavegrad.model import WaveGrad



# wavegrad 
WAVEGRAD_VOCODER_PATH = './'

if os.path.exists(f'{WAVEGRAD_VOCODER_PATH}/weights.pt'):
  checkpoint = torch.load(f'{WAVEGRAD_VOCODER_PATH}/weights.pt')
else:
  checkpoint = torch.load(WAVEGRAD_VOCODER_PATH)


WAVEGRAD_MODEL = WaveGrad(AttrDict(WAVEGRAD_PARAMS))
WAVEGRAD_MODEL.load_state_dict(checkpoint['model'])
WAVEGRAD_MODEL.eval()
print("WaveGrad load !")

WAVEGRAD_PARAMS['noise_schedule'] = [4.47739327e-06, 4.47739327e-05, 9.49513587e-04, 9.49513587e-03, 9.49513587e-02, 4.47739327e-01] # get this with noise_schedule search


def wavegrad_predict(spectrogram, device=torch.device('cuda')):
  start = time.time()
  # Lazy load model.
  model = WAVEGRAD_MODEL.to(device)
  model.params.override(WAVEGRAD_PARAMS)
  with torch.no_grad():
    beta = np.array(model.params.noise_schedule)
    alpha = 1 - beta
    alpha_cum = np.cumprod(alpha)

    # Expand rank 2 tensors by adding a batch dimension.
    if len(spectrogram.shape) == 2:
      spectrogram = spectrogram.unsqueeze(0)
    spectrogram = spectrogram.to(device)

    audio = torch.randn(spectrogram.shape[0], model.params.hop_length * spectrogram.shape[-1], device=device)
    noise_scale = torch.from_numpy(alpha_cum**0.5).float().unsqueeze(1).to(device)

    for n in tqdm(range(len(alpha) - 1, -1, -1)):
      c1 = 1 / alpha[n]**0.5
      c2 = (1 - alpha[n]) / (1 - alpha_cum[n])**0.5
      audio = c1 * (audio - c2 * model(audio, spectrogram, noise_scale[n]).squeeze(1))
      if n > 0:
        noise = torch.randn_like(audio)
        sigma = ((1.0 - alpha_cum[n-1]) / (1.0 - alpha_cum[n]) * beta[n])**0.5
        audio += sigma * noise
      audio = torch.clamp(audio, -1.0, 1.0)
  print('Wavegrad Time', time.time()-start)
  return audio.cpu().numpy(), model.params.sample_rate

Edresson avatar Sep 21 '20 13:09 Edresson

Thanks, I will try it! But do you only extract the spectrogram from TTS?

torch.FloatTensor(mel_postnet_spec.T) np.save("spectrogram.npy", spectrogram)

george-roussos avatar Sep 21 '20 13:09 george-roussos

That's right.

The model is compatible with the following configuration:

 // AUDIO PARAMETERS
    "audio":{
        // Audio processing parameters
        "num_mels": 80,         // size of the mel spec frame. 
        "fft_size": 1024,       // number of stft frequency levels. Size of the linear spectogram frame.
        "sample_rate": 22050,   // DATASET-RELATED: wav sample-rate. If different than the original data, it is resampled.
        "win_length": 1024,     // stft window length in ms.
        "hop_length": 256,      // stft window hop-lengh in ms.
        "frame_length_ms": null,  // stft window length in ms.If null, 'win_length' is used.
        "frame_shift_ms": null,   // stft window hop-lengh in ms. If null, 'hop_length' is used.
        "preemphasis": 0.98,    // pre-emphasis to reduce spec noise and make it more structured. If 0.0, no -pre-emphasis.
        "min_level_db": -100,   // normalization range
        "ref_level_db": 20,     // reference level db, theoretically 20db is the sound of air.
        "power": 1.5,           // value to sharpen wav signals after GL algorithm.
        "griffin_lim_iters": 60,// #griffin-lim iterations. 30-60 is a good range. Larger the value, slower the generation.
        "stft_pad_mode": "reflect",
        // Normalization parameters
        "signal_norm": true,    // normalize the spec values in range [0, 1]
        "symmetric_norm": true, // move normalization to range [-1, 1]
        "max_norm": 4.0,          // scale normalization to range [-max_norm, max_norm] or [0, max_norm]
        "clip_norm": true,      // clip normalized values into the range.
        "mel_fmin": 0.0,         // minimum freq level for mel-spec. ~50 for male and ~95 for female voices. Tune for dataset!!
        "mel_fmax": 8000.0,        // maximum freq level for mel-spec. Tune for dataset!!
        "spec_gain": 20.0, 
        "do_trim_silence": false,  // enable trimming of slience of audio as you load it. LJspeech (false), TWEB (false), Nancy (true)
        "trim_db": 60          // threshold for timming silence. Set this according to your dataset.
    },

Edresson avatar Sep 21 '20 13:09 Edresson

Coolio, it worked yay! Although it gives me strong vocal fry in low frequencies, so I wonder if it is my TTS or WaveGrad. Oh well!

george-roussos avatar Sep 21 '20 14:09 george-roussos

thanks @Edresson

erogol avatar Sep 22 '20 02:09 erogol

Coolio, it worked yay! Although it gives me strong vocal fry in low frequencies, so I wonder if it is my TTS or WaveGrad. Oh well!

You can see if the noise comes from your TTS or the vocoder by computing the mel spectogram with the audio processor and the passing it trough the vocoder.

WeberJulian avatar Sep 22 '20 14:09 WeberJulian

Coolio, it worked yay! Although it gives me strong vocal fry in low frequencies, so I wonder if it is my TTS or WaveGrad. Oh well!

You can see if the noise comes from your TTS or the vocoder by computing the mel spectogram with the audio processor and the passing it trough the vocoder.

This is what I am doing right now. I extract the spectrogram from TTS with synthesize.py and then I pass it through WaveGrad. Are you on about something different?

george-roussos avatar Sep 22 '20 14:09 george-roussos

No compute the mel directly from the wav sample without using the TTS model. That way you're only testing the vocoder independently from the TTS model.

WeberJulian avatar Sep 22 '20 14:09 WeberJulian

Oh you mean a ground truth sample? Good idea! I will try it. Thanks :)

george-roussos avatar Sep 22 '20 14:09 george-roussos

WaveGrad is still training, I will keep it for some more time. After I update the checkpoint here.

Edresson avatar Sep 22 '20 19:09 Edresson