forked from simonrouard/CRASH
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlearner.py
More file actions
292 lines (242 loc) · 10.8 KB
/
learner.py
File metadata and controls
292 lines (242 loc) · 10.8 KB
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
import numpy as np
import os
import re
import torch
import torch.nn as nn
from torch.nn.parallel import DistributedDataParallel
from torch.utils.tensorboard import SummaryWriter
from tqdm import tqdm
from glob import glob
from dataset import from_path as dataset_from_path
from model import UNet
from getters import get_sde
def _nested_map(struct, map_fn):
if isinstance(struct, tuple):
return tuple(_nested_map(x, map_fn) for x in struct)
if isinstance(struct, list):
return [_nested_map(x, map_fn) for x in struct]
if isinstance(struct, dict):
return {k: _nested_map(v, map_fn) for k, v in struct.items()}
return map_fn(struct)
class Learner:
def __init__(
self, model_dir, model, train_set, test_set, optimizer, params
):
os.makedirs(model_dir, exist_ok=True)
self.model_dir = model_dir
self.model = model
self.ema_weights = [param.clone().detach()
for param in self.model.parameters()]
self.sde = get_sde(params['sde_type'], params['sde_kwargs'])
self.ema_rate = params['ema_rate']
self.train_set = train_set
self.test_set = test_set
self.optimizer = optimizer
self.params = params
self.scheduler = torch.optim.lr_scheduler.StepLR(
self.optimizer, step_size=params['scheduler_step_size'], gamma=params['scheduler_gamma']
)
self.step = 0
self.is_master = True
self.loss_fn = nn.MSELoss()
self.v_loss = nn.MSELoss(reduction="none")
self.summary_writer = None
self.n_bins = params['n_bins']
self.num_elems_in_bins_train = np.zeros(self.n_bins)
self.sum_loss_in_bins_train = np.zeros(self.n_bins)
self.num_elems_in_bins_test = np.zeros(self.n_bins)
self.sum_loss_in_bins_test = np.zeros(self.n_bins)
self.cum_grad_norms = 0
def state_dict(self):
if hasattr(self.model, "module") and isinstance(self.model.module, nn.Module):
model_state = self.model.module.state_dict()
else:
model_state = self.model.state_dict()
return {
"step": self.step,
"model": {
k: v.cpu() if isinstance(v, torch.Tensor) else v
for k, v in model_state.items()
},
'ema_weights': self.ema_weights,
}
def load_state_dict(self, state_dict):
if hasattr(self.model, "module") and isinstance(self.model.module, nn.Module):
self.model.module.load_state_dict(state_dict["model"])
else:
self.model.load_state_dict(state_dict["model"])
self.step = state_dict["step"]
self.ema_weights = state_dict['ema_weights']
def save_to_checkpoint(self, filename="weights"):
save_basename = f"{filename}-{self.step}.pt"
save_name = f"{self.model_dir}/{save_basename}"
torch.save(self.state_dict(), save_name)
def restore_from_checkpoint(self, checkpoint_id=None):
try:
if checkpoint_id is None:
# find latest checkpoint_id
list_weights = glob(f'{self.model_dir}/weights-*')
id_regex = re.compile('weights-(\d*)')
list_ids = [int(id_regex.search(weight_path).groups()[0])
for weight_path in list_weights]
checkpoint_id = max(list_ids)
checkpoint = torch.load(
f"{self.model_dir}/weights-{checkpoint_id}.pt")
self.load_state_dict(checkpoint)
return True
except (FileNotFoundError, ValueError):
return False
def train(self):
device = next(self.model.parameters()).device
while True:
for features in (
tqdm(self.train_set,
desc=f"Epoch {self.step // len(self.train_set)}")
if self.is_master
else self.train_set
):
features = _nested_map(
features,
lambda x: x.to(device) if isinstance(
x, torch.Tensor) else x,
)
loss = self.train_step(features)
if torch.isnan(loss).any():
raise RuntimeError(
f"Detected NaN loss at step {self.step}.")
if self.is_master:
if self.step % 250 == 249:
self._write_summary(self.step)
if self.step % self.params['num_steps_to_test'] == self.params['num_steps_to_test'] - 1:
self.test_set_evaluation()
self._write_test_summary(self.step)
if self.step % (self.params['num_epochs_to_save'] * len(self.train_set)) == 10:
self.save_to_checkpoint()
self.step += 1
def train_step(self, features):
for param in self.model.parameters():
param.grad = None
audio = features["audio"]
N, T = audio.shape
t = torch.rand(N, 1, device=audio.device)
t = (self.sde.t_max - self.sde.t_min) * t + self.sde_t.t_min
noise = torch.randn_like(audio)
noisy_audio = self.sde.perturb(audio, t, noise)
sigma = self.sde.sigma(t)
predicted = self.model(noisy_audio, sigma)
loss = self.loss_fn(noise, predicted)
loss.backward()
self.grad_norm = nn.utils.clip_grad_norm_(self.model.parameters(), 1.0)
self.optimizer.step()
self.scheduler.step()
if self.is_master:
self.update_ema_weights()
t_detach = t.clone().detach().cpu().numpy()
t_detach = np.reshape(t_detach, -1)
vectorial_loss = self.v_loss(noise, predicted).detach()
vectorial_loss = torch.mean(vectorial_loss, 1).cpu().numpy()
vectorial_loss = np.reshape(vectorial_loss, -1)
self.update_conditioned_loss(vectorial_loss, t_detach, True)
self.cum_grad_norms += self.grad_norm
return loss
def _write_summary(self, step):
loss_in_bins_train = np.divide(
self.sum_loss_in_bins_train, self.num_elems_in_bins_train
)
dic_loss_train = {}
for k in range(self.n_bins):
dic_loss_train["loss_bin_" + str(k)] = loss_in_bins_train[k]
sum_loss_n_steps = np.sum(self.sum_loss_in_bins_train)
mean_grad_norms = self.cum_grad_norms / self.num_elems_in_bins_train.sum() * \
self.params['batch_size']
writer = self.summary_writer or SummaryWriter(
self.model_dir, purge_step=step)
writer.add_scalar('train/sum_loss_on_n_steps',
sum_loss_n_steps, step)
writer.add_scalar("train/mean_grad_norm", mean_grad_norms, step)
writer.add_scalars("train/conditioned_loss", dic_loss_train, step)
writer.flush()
self.summary_writer = writer
self.num_elems_in_bins_train = np.zeros(self.n_bins)
self.sum_loss_in_bins_train = np.zeros(self.n_bins)
self.cum_grad_norms = 0
def _write_test_summary(self, step):
# Same thing for test set
loss_in_bins_test = np.divide(
self.sum_loss_in_bins_test, self.num_elems_in_bins_test
)
dic_loss_test = {}
for k in range(self.n_bins):
dic_loss_test["loss_bin_" + str(k)] = loss_in_bins_test[k]
writer = self.summary_writer or SummaryWriter(
self.model_dir, purge_step=step)
writer.add_scalars("test/conditioned_loss", dic_loss_test, step)
writer.flush()
self.summary_writer = writer
self.num_elems_in_bins_test = np.zeros(self.n_bins)
self.sum_loss_in_bins_test = np.zeros(self.n_bins)
def update_conditioned_loss(self, vectorial_loss, continuous_array, isTrain):
continuous_array = np.trunc(self.n_bins * continuous_array)
continuous_array = continuous_array.astype(int)
if isTrain:
for k in range(len(continuous_array)):
self.num_elems_in_bins_train[continuous_array[k]] += 1
self.sum_loss_in_bins_train[continuous_array[k]
] += vectorial_loss[k]
else:
for k in range(len(continuous_array)):
self.num_elems_in_bins_test[continuous_array[k]] += 1
self.sum_loss_in_bins_test[continuous_array[k]
] += vectorial_loss[k]
def update_ema_weights(self):
for ema_param, param in zip(self.ema_weights, self.model.parameters()):
if param.requires_grad:
ema_param -= (1 - self.ema_rate) * (ema_param - param.detach())
def test_set_evaluation(self):
with torch.no_grad():
self.model.eval()
for features in self.test_set:
audio = features["audio"].cuda()
N, T = audio.shape
t = torch.rand(N, 1, device=audio.device)
t = (self.sde.t_max - self.sde.t_min) * t + self.sde_t.t_min
noise = torch.randn_like(audio)
noisy_audio = self.sde.perturb(audio, t, noise)
sigma = self.sde.sigma(t)
predicted = self.model(noisy_audio, sigma)
vectorial_loss = self.v_loss(noise, predicted).detach()
vectorial_loss = torch.mean(vectorial_loss, 1).cpu().numpy()
vectorial_loss = np.reshape(vectorial_loss, -1)
t = t.cpu().numpy()
t = np.reshape(t, -1)
self.update_conditioned_loss(
vectorial_loss, t, False)
def _train_impl(replica_id, model, train_set, test_set, params):
torch.backends.cudnn.benchmark = True
opt = torch.optim.Adam(model.parameters(), lr=params['lr'])
learner = Learner(
params['model_dir'], model, train_set, test_set, opt, params
)
learner.is_master = replica_id == 0
learner.restore_from_checkpoint(params['checkpoint_id'])
learner.train()
def train(params):
model = UNet().cuda()
train_set = dataset_from_path(params['train_dirs'], params)
test_set = dataset_from_path(params['test_dirs'], params)
_train_impl(0, model, train_set, test_set, params)
def train_distributed(replica_id, replica_count, port, params):
os.environ["MASTER_ADDR"] = "localhost"
os.environ["MASTER_PORT"] = str(port)
torch.distributed.init_process_group(
"nccl", rank=replica_id, world_size=replica_count
)
device = torch.device("cuda", replica_id)
torch.cuda.set_device(device)
model = UNet().to(device)
train_set = dataset_from_path(
params['train_dirs'], params)
test_set = dataset_from_path(
params['test_dirs'], params)
model = DistributedDataParallel(model, device_ids=[replica_id])
_train_impl(replica_id, model, train_set, test_set, params)