forked from fatchord/WaveRNN
-
Notifications
You must be signed in to change notification settings - Fork 37
/
find_lr.py
293 lines (261 loc) · 8.53 KB
/
find_lr.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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import argparse
import math
import os
import pickle
import shutil
import sys
import traceback
import librosa
import matplotlib.pyplot as plt
import numpy as np
import torch
from torch import nn, optim
from torch.utils.data import DataLoader
from torch.utils.data.distributed import DistributedSampler
from dataset import MyDataset
from distribute import *
from models.wavernn import Model
from utils.audio import AudioProcessor
from utils.display import *
from utils.distribution import discretized_mix_logistic_loss, gaussian_loss
from utils.generic_utils import (check_update, count_parameters, load_config,
remove_experiment_folder, save_checkpoint,
save_best_model)
torch.backends.cudnn.enabled = True
torch.backends.cudnn.benchmark = False
torch.manual_seed(54321)
use_cuda = torch.cuda.is_available()
num_gpus = torch.cuda.device_count()
print(" > Using CUDA: ", use_cuda, flush=True)
print(" > Number of GPUs: ", num_gpus, flush=True)
def setup_loader(is_val=False):
global train_ids
dataset = MyDataset(
test_ids if is_val else train_ids,
DATA_PATH,
CONFIG.mel_len,
ap.hop_length,
CONFIG.mode,
CONFIG.pad,
ap,
is_val,
)
sampler = DistributedSampler(dataset) if num_gpus > 1 else None
loader = DataLoader(
dataset,
collate_fn=dataset.collate,
batch_size=CONFIG.batch_size,
num_workers=0,
# shuffle=True,
pin_memory=True,
sampler=sampler,
drop_last=True
)
return loader
def find_lr(model, optimizer, criterion, batch_size, args, init_lr=1e-7, end_lr=1., beta=0.98):
""" from https://sgugger.github.io/how-do-you-find-a-good-learning-rate.html """
global CONFIG
global train_ids
# create train loader
data_loader = setup_loader(False)
num_iter = len(data_loader) - 1
coeff = (end_lr / init_lr) ** (1 / num_iter)
lr = init_lr
for p in optimizer.param_groups:
p["lr"] = lr
best_loss = float('inf')
avg_loss = 0.0
losses = []
log_lrs = []
start = time.time()
# train loop
print(" > Training", flush=True)
model.train()
for i, (x, m, y) in enumerate(data_loader):
if use_cuda:
x, m, y = x.cuda(), m.cuda(), y.cuda()
optimizer.zero_grad()
y_hat = model(x, m)
# y_hat = y_hat.transpose(1, 2)
if type(model.mode) == int :
y_hat = y_hat.transpose(1, 2).unsqueeze(-1)
else:
y = y.float()
y = y.unsqueeze(-1)
loss = criterion(y_hat, y)
# compute smoothed loss
avg_loss = beta * avg_loss + (1-beta) * loss.item()
smoothed_loss = avg_loss / (1 - beta**(i + 1))
# stop if the loss is exploding
if i > 0 and smoothed_loss > 100 * best_loss:
break
# Record the best loss
if smoothed_loss < best_loss:
best_loss = smoothed_loss
# Store the values
losses.append(smoothed_loss)
log_lrs.append(math.log10(lr))
# Do optimizer step
loss.backward()
optimizer.step()
speed = (i + 1) / (time.time() - start)
if i % CONFIG.print_step == 0:
print(
" | > Epoch: {}/{} -- Batch: {}/{} -- Loss: {:.3f}"
" -- Speed: {:.2f} steps/sec -- lr: {}".format(
1, 1, i + 1, num_iter, avg_loss, speed, lr
), flush=True
)
# Update the lr for the next step
lr *= coeff
optimizer.param_groups[0]['lr'] = lr
# plot results
if args.rank == 0:
plt.plot(log_lrs, losses)
print(f"{VIS_PATH}/find_lr.png")
plt.savefig(f"{VIS_PATH}/find_lr.png")
plt.close
def main(args):
global train_ids
global test_ids
# read meta data
with open(f"{DATA_PATH}/dataset_ids.pkl", "rb") as f:
train_ids = pickle.load(f)
# pick validation set
test_ids = train_ids[-10:]
test_id = train_ids[4]
train_ids = train_ids[:-10]
# create the model
assert np.prod(CONFIG.upsample_factors) == ap.hop_length, ap.hop_length
model = Model(
rnn_dims=512,
fc_dims=512,
mode=CONFIG.mode,
mulaw=CONFIG.mulaw,
pad=CONFIG.pad,
upsample_factors=CONFIG.upsample_factors,
feat_dims=80,
compute_dims=128,
res_out_dims=128,
res_blocks=10,
hop_length=ap.hop_length,
sample_rate=ap.sample_rate,
).cuda()
num_parameters = count_parameters(model)
print(" > Number of model parameters: {}".format(num_parameters), flush=True)
optimizer = optim.Adam(model.parameters(), lr=CONFIG.lr)
# restore any checkpoint
if args.restore_path:
checkpoint = torch.load(args.restore_path)
try:
model.load_state_dict(checkpoint["model"])
# TODO: fix resetting restored optimizer lr
# optimizer.load_state_dict(checkpoint["optimizer"])
except:
model_dict = model.state_dict()
# Partial initialization: if there is a mismatch with new and old layer, it is skipped.
# 1. filter out unnecessary keys
pretrained_dict = {
k: v for k, v in checkpoint["model"].items() if k in model_dict
}
# 2. filter out different size layers
pretrained_dict = {
k: v
for k, v in pretrained_dict.items()
if v.numel() == model_dict[k].numel()
}
# 3. overwrite entries in the existing state dict
model_dict.update(pretrained_dict)
# 4. load the new state dict
model.load_state_dict(model_dict)
print(
" | > {} / {} layers are initialized".format(
len(pretrained_dict), len(model_dict)
)
)
# DISTRIBUTED
if num_gpus > 1:
model = apply_gradient_allreduce(model)
# define train functions
if CONFIG.mode == 'mold':
criterion = discretized_mix_logistic_loss
elif CONFIG.mode == 'gauss':
criterion = gaussian_loss
elif type(CONFIG.mode) is int:
criterion = torch.nn.CrossEntropyLoss()
model.train()
# HIT IT!!!
find_lr(
model,
optimizer,
criterion,
CONFIG.batch_size,
args,
init_lr=args.init_lr,
end_lr=args.end_lr,
beta=0.98
)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--config_path", type=str, help="path to config file for training."
)
parser.add_argument(
"--restore_path", type=str, default=0, help="path for a model to fine-tune."
)
parser.add_argument(
"--data_path", type=str, default="", help="data path to overwrite config.json."
)
parser.add_argument(
"--output_path", type=str, help="path for training outputs.", default=""
)
parser.add_argument(
"--init_lr", type=float, help="path for training outputs.", default=1e-7
)
parser.add_argument(
"--end_lr", type=float, help="path for training outputs.", default=1
)
# DISTRUBUTED
parser.add_argument(
"--rank",
type=int,
default=0,
help="DISTRIBUTED: process rank for distributed training.",
)
parser.add_argument(
"--group_id", type=str, default="", help="DISTRIBUTED: process group id."
)
args = parser.parse_args()
CONFIG = load_config(args.config_path)
if args.data_path != "":
CONFIG.data_path = args.data_path
DATA_PATH = CONFIG.data_path
# DISTRUBUTED
if num_gpus > 1:
init_distributed(
args.rank,
num_gpus,
args.group_id,
CONFIG.distributed["backend"],
CONFIG.distributed["url"],
)
global ap
ap = AudioProcessor(**CONFIG.audio)
mode = CONFIG.mode
# setup output paths and read configs
_ = os.path.dirname(os.path.realpath(__file__))
if args.data_path != "":
CONFIG.data_path = args.data_path
if args.output_path == "":
OUT_PATH = os.path.join(_, CONFIG.output_path)
else:
OUT_PATH = args.output_path
if args.group_id == "":
OUT_PATH = create_experiment_folder(OUT_PATH, CONFIG.model_name)
if args.rank == 0:
# set paths
VIS_PATH = f"{OUT_PATH}/lr_find/"
shutil.copyfile(args.config_path, os.path.join(OUT_PATH, "config.json"))
# create paths
os.makedirs(VIS_PATH, exist_ok=True)
main(args)