-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
131 lines (104 loc) · 3.82 KB
/
main.py
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
from pathlib import Path
import argparse
import wandb
import json
import warnings
warnings.filterwarnings("ignore")
from torch.utils.data import DataLoader, random_split
import torch
from nv.spectrogram import MelSpectrogram
from nv.collate_fn import LJSpeechCollator
from nv.datasets import LJSpeechDataset
from nv.trainer import *
from nv.models import *
from nv.utils import *
def main(config):
if config.use_wandb:
wandb.init(project=config.wandb_project_name)
fix_seed(config)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
if config.verbose:
print(f"The training process will be performed on {device}.")
print("Downloading and splitting the data.")
dataset = LJSpeechDataset(config.path_to_data)
train_size = int(config.train_ratio * len(dataset))
train_dataset, val_dataset = random_split(
dataset,
[train_size, len(dataset) - train_size],
generator=torch.Generator().manual_seed(config.seed)
)
train_dataloader = DataLoader(
train_dataset,
collate_fn=LJSpeechCollator(),
batch_size=config.batch_size,
num_workers=config.num_workers
)
val_dataloader = DataLoader(
val_dataset,
collate_fn=LJSpeechCollator(),
batch_size=config.batch_size,
num_workers=config.num_workers
)
melspectrogramer = MelSpectrogram(config, for_loss=False).to(device)
melspectrogramer_for_loss = MelSpectrogram(config, for_loss=True).to(device)
if config.verbose:
print("Initializing discriminator, generator, optimizers and lr_schedulers.")
generator = HiFiGenerator(config).to(device)
trainable_params_generator = filter(
lambda param: param.requires_grad, generator.parameters()
)
optimizer_generator = torch.optim.AdamW(
trainable_params_generator,
betas=(config.adam_beta_1, config.adam_beta_2),
weight_decay=config.weight_decay,
lr=config.learning_rate
)
scheduler_generator = torch.optim.lr_scheduler.ExponentialLR(
optimizer_generator,
gamma=config.gamma
)
discriminator = HiFiDiscriminator(config).to(device)
trainable_params_discriminator = filter(
lambda param: param.requires_grad, discriminator.parameters()
)
optimizer_discriminator = torch.optim.AdamW(
trainable_params_discriminator,
betas=(config.adam_beta_1, config.adam_beta_2),
weight_decay=config.weight_decay,
lr=config.learning_rate
)
scheduler_discriminator = torch.optim.lr_scheduler.ExponentialLR(
optimizer_discriminator,
gamma=config.gamma
)
if config.load_model:
if config.verbose:
print("Downloading the pretrained model.")
checkpoint = torch.load(config.checkpoint_path)
generator.load_state_dict(checkpoint["generator"])
optimizer_generator.load_state_dict(checkpoint["optimizer_generator"])
discriminator.load_state_dict(checkpoint["discriminator"])
optimizer_discriminator.load_state_dict(checkpoint["optimizer_discriminator"])
if config.use_wandb:
wandb.watch(generator)
wandb.watch(discriminator)
train(
config, train_dataloader, val_dataloader,
generator, optimizer_generator, scheduler_generator,
discriminator, optimizer_discriminator, scheduler_discriminator,
melspectrogramer, melspectrogramer_for_loss, device
)
if __name__ == "__main__":
argparser = argparse.ArgumentParser()
argparser.add_argument(
"-c",
"--config",
type=str,
required=True,
help="Config file path"
)
args = argparser.parse_args()
config_path = Path(args.config)
with config_path.open("r") as file:
config = AttrDict(json.load(file))
main(config)