File size: 8,225 Bytes
6bc94ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import time
from pathlib import Path
from os.path import exists

import numpy as np
import torch
import torch.nn.functional as F
from torch import no_grad, optim
from torch.utils.data import DataLoader

import vocoder.hparams as hp
from vocoder.display import stream, simple_table
from vocoder.distribution import discretized_mix_logistic_loss
from vocoder.gen_wavernn import gen_devset
from vocoder.models.fatchord_version import WaveRNN
from vocoder.vocoder_dataset import VocoderDataset, collate_vocoder
from vocoder.utils import ValueWindow
from utils.profiler import Profiler


def train(run_id: str, syn_dir: Path, voc_dir: Path, models_dir: Path, ground_truth: bool, save_every: int,
          backup_every: int, force_restart: bool, use_tb: bool):
    if use_tb:
        print("Use Tensorboard")
        import tensorflow as tf
        import datetime
        # Hide GPU from visible devices
        log_dir = f"log/vc/vocoder/tensorboard/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
        train_summary_writer = tf.summary.create_file_writer(log_dir)
    # Check to make sure the hop length is correctly factorised
    train_syn_dir = syn_dir.joinpath("train")
    train_voc_dir = voc_dir.joinpath("train")    
    dev_syn_dir = syn_dir.joinpath("dev")
    dev_voc_dir = voc_dir.joinpath("dev")
    assert np.cumprod(hp.voc_upsample_factors)[-1] == hp.hop_length

    # Instantiate the model
    print("Initializing the model...")
    model = WaveRNN(
        rnn_dims=hp.voc_rnn_dims,
        fc_dims=hp.voc_fc_dims,
        bits=hp.bits,
        pad=hp.voc_pad,
        upsample_factors=hp.voc_upsample_factors,
        feat_dims=hp.num_mels,
        compute_dims=hp.voc_compute_dims,
        res_out_dims=hp.voc_res_out_dims,
        res_blocks=hp.voc_res_blocks,
        hop_length=hp.hop_length,
        sample_rate=hp.sample_rate,
        mode=hp.voc_mode
    )

    if torch.cuda.is_available():
        model = model.cuda()

    # Initialize the optimizer
    optimizer = optim.Adam(model.parameters())
    for p in optimizer.param_groups:
        p["lr"] = hp.voc_lr
    loss_func = F.cross_entropy if model.mode == "RAW" else discretized_mix_logistic_loss
    train_loss_window = ValueWindow(100)

    # Load the weights
    model_dir = models_dir / run_id
    model_dir.mkdir(exist_ok=True)
    weights_fpath = model_dir / "vocoder.pt"
    # train_loss_file_path = "vocoder_loss/vocoder_train_loss.npy"
    # dev_loss_file_path = "vocoder_loss/vocoder_dev_loss.npy"

    # if not exists("vocoder_loss"):
    #     import os
    #     os.mkdir("vocoder_loss")
    if force_restart or not weights_fpath.exists():
        print("\nStarting the training of WaveRNN from scratch\n")
        model.save(weights_fpath, optimizer)
        # losses = []
        # dev_losses = []
    else:
        print("\nLoading weights at %s" % weights_fpath)
        model.load(weights_fpath, optimizer)
        print("WaveRNN weights loaded from step %d" % model.step)
        # losses = list(np.load(train_loss_file_path)) if exists(train_loss_file_path) else []
        # dev_losses = list(np.load(dev_loss_file_path)) if exists(dev_loss_file_path) else []

    # Initialize the dataset
    train_metadata_fpath = train_syn_dir.joinpath("train.txt") if ground_truth else \
        train_voc_dir.joinpath("synthesized.txt")
    train_mel_dir = train_syn_dir.joinpath("mels") if ground_truth else train_voc_dir.joinpath("mels_gta")
    train_wav_dir = train_syn_dir.joinpath("audio")
    train_dataset = VocoderDataset(train_metadata_fpath, train_mel_dir, train_wav_dir)
    
    dev_metadata_fpath = dev_syn_dir.joinpath("dev.txt") if ground_truth else \
        dev_voc_dir.joinpath("synthesized.txt")
    dev_mel_dir = dev_syn_dir.joinpath("mels") if ground_truth else dev_voc_dir.joinpath("mels_gta")
    dev_wav_dir = dev_syn_dir.joinpath("audio")
    dev_dataset = VocoderDataset(dev_metadata_fpath, dev_mel_dir, dev_wav_dir)
    train_dataloader = DataLoader(train_dataset, hp.voc_batch_size, shuffle=True, num_workers=8, collate_fn=collate_vocoder, pin_memory=True)
    dev_dataloader = DataLoader(dev_dataset, hp.voc_batch_size, shuffle=True, num_workers=8, collate_fn=collate_vocoder, pin_memory=True)
    dev_dataloader_ = DataLoader(dev_dataset, 1, shuffle=True)

    # Begin the training
    simple_table([('Batch size', hp.voc_batch_size),
                  ('LR', hp.voc_lr),
                  ('Sequence Len', hp.voc_seq_len)])
    # best_loss_file_path = "vocoder_loss/best_loss.npy"
    # best_loss = np.load(best_loss_file_path)[0] if exists(best_loss_file_path) else 1000

    # profiler = Profiler(summarize_every=10, disabled=False)
    for epoch in range(1, 3500):
        start = time.time()

        for i, (x, y, m) in enumerate(train_dataloader, 1):
            model.train()
            # profiler.tick("Blocking, waiting for batch (threaded)")
            if torch.cuda.is_available():
                x, m, y = x.cuda(), m.cuda(), y.cuda()
            # profiler.tick("Data to cuda")

            # Forward pass
            y_hat = model(x, m)
            if model.mode == 'RAW':
                y_hat = y_hat.transpose(1, 2).unsqueeze(-1)
            elif model.mode == 'MOL':
                y = y.float()
            y = y.unsqueeze(-1)
            # profiler.tick("Forward pass")

            # Backward pass
            loss = loss_func(y_hat, y)
            # profiler.tick("Loss")
            optimizer.zero_grad()
            loss.backward()
            # profiler.tick("Backward pass")
            optimizer.step()
            # profiler.tick("Parameter update")

            speed = i / (time.time() - start)
            train_loss_window.append(loss.item())

            step = model.get_step()
            k = step // 1000

            msg = f"| Epoch: {epoch} ({i}/{len(train_dataloader)}) | " \
                f"Train Loss: {train_loss_window.average:.4f} | " \
                f"{speed:.4f}steps/s | Step: {k}k | "
            stream(msg)

            if use_tb:
                with train_summary_writer.as_default():
                    tf.summary.scalar('train_loss', train_loss_window.average, step=step)

            torch.cuda.empty_cache()
            if backup_every != 0 and step % backup_every == 0 :
                model.checkpoint(model_dir, optimizer)

            if save_every != 0 and step % save_every == 0 :
                dev_loss = validate(dev_dataloader, model, loss_func)
                msg = f"| Epoch: {epoch} ({i}/{len(train_dataloader)}) | " \
                    f"Train Loss: {train_loss_window.average:.4f} | Dev Loss: {dev_loss:.4f} | " \
                    f"{speed:.4f}steps/s | Step: {k}k | "
                stream(msg)

                if use_tb:
                    with train_summary_writer.as_default():
                        tf.summary.scalar('val_loss', dev_loss, step=step)
                # losses.append(train_loss_window.average)
                # np.save(train_loss_file_path, np.array(losses, dtype=float))
                # dev_losses.append(dev_loss)
                # np.save(dev_loss_file_path, np.array(dev_losses, dtype=float))
                # if dev_loss < best_loss :
                    # best_loss = dev_loss
                    # np.save(best_loss_file_path, np.array([best_loss]))
                model.save(weights_fpath, optimizer)

            # profiler.tick("Extra saving")

        # gen_devset(model, dev_dataloader_, hp.voc_gen_at_checkpoint, hp.voc_gen_batched,
        #         hp.voc_target, hp.voc_overlap, model_dir)
        print("")

def validate(dataloader, model, loss_func):
    model.eval()
    losses = []
    with no_grad():
        for i, (x, y, m) in enumerate(dataloader, 1):
            if torch.cuda.is_available():
                x, m, y = x.cuda(), m.cuda(), y.cuda()
                y_hat = model(x, m)
                if model.mode == 'RAW':
                    y_hat = y_hat.transpose(1, 2).unsqueeze(-1)
                elif model.mode == 'MOL':
                    y = y.float()
                y = y.unsqueeze(-1)
                loss = loss_func(y_hat, y).item()
                losses.append(loss)
    torch.cuda.empty_cache()
    return sum(losses) / len(losses)