-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlearner.py
More file actions
267 lines (245 loc) · 10.4 KB
/
Copy pathlearner.py
File metadata and controls
267 lines (245 loc) · 10.4 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
import numpy as np
import os
import torch
import json
import torch.nn as nn
from datetime import datetime
from tqdm import tqdm
from torch.utils.tensorboard.writer import SummaryWriter
from typing import Optional
from pathlib import Path
import torch.nn.functional as F
from utils import nested_map
class StrucMusDiffLearner:
def __init__(
self, output_dir, model_name, model, train_dl, val_dl, optimizer, params, param_scheduler=None
):
self.output_dir = output_dir
self.model_name=model_name
self.log_dir = f"{output_dir}/logs"
self.checkpoint_dir = f"{output_dir}/chkpts"
self.model = model
self.train_dl = train_dl
self.val_dl = val_dl
self.optimizer = optimizer
self.params = params
self.param_scheduler = param_scheduler # teacher-forcing stuff
self.step = 0
self.epoch = 0
self.grad_norm = 0.
self.summary_writer = None
self.is_from_scratch = True
self.device = "cuda" if torch.cuda.is_available() else "cpu"
self.autocast = torch.cuda.amp.autocast(enabled=params.fp16)
self.scaler = torch.cuda.amp.GradScaler(enabled=params.fp16)
self.best_cnt=0
self.best_val_loss = torch.tensor([1e10], device=self.device)
# restore if directory exists
# print("???? output_dir exit !!!!",self.output_dir)
if os.path.exists(self.output_dir):
#print(Path(self.output_dir).parent.name[4:],model_name)
if Path(self.output_dir).parent.name[4:]!=model_name:
print("Change save path !!!")
self.restore_from_checkpoint(fname="best")
output_dir = 'saved_models/ME_'+model_name+'/'
output_dir = f"{output_dir}/{datetime.now().strftime('%m-%d_%H%M%S')}"
self.output_dir = output_dir
self.log_dir = f"{output_dir}/logs"
self.checkpoint_dir = f"{output_dir}/chkpts"
os.makedirs(self.output_dir)
os.makedirs(self.log_dir)
os.makedirs(self.checkpoint_dir)
else:
print("Keep original path !!!")
self.is_from_scratch=False
self.restore_from_checkpoint(fname="best")
else:
os.makedirs(self.output_dir)
os.makedirs(self.log_dir)
os.makedirs(self.checkpoint_dir)
with open(f"{output_dir}/params.json", "w") as params_file:
json.dump(self.params, params_file)
# print(json.dumps(self.params, sort_keys=True, indent=4))
def _write_summary(self, losses: dict, type, is_inpaint):
"""type: train or val"""
summary_losses = losses
# summary_losses["grad_norm"] = self.grad_norm
writer = self.summary_writer or SummaryWriter(
self.log_dir, purge_step=self.step
)
writer.add_scalars('loss/loss', {type:summary_losses['loss']}, self.step)
# writer.add_scalars(type, summary_losses, self.step)
if is_inpaint:
writer.add_scalars('loss/noise_loss1',{type:summary_losses['noise_loss1']},self.step)
writer.add_scalars('loss/noise_loss2', {type: summary_losses['noise_loss2']}, self.step)
writer.add_scalars('loss/phrase_loss', {type: summary_losses['phrase_loss']}, self.step)
writer.add_scalars('loss/str_loss', {type: summary_losses['str_loss']}, self.step)
writer.flush()
self.summary_writer = writer
def state_dict(self):
model_state = self.model.state_dict()
return {
"step": self.step,
"epoch": self.epoch,
"model":
{
k: v.cpu() if isinstance(v, torch.Tensor) else v
for k, v in model_state.items()
},
"optimizer":
{
k: v.cpu() if isinstance(v, torch.Tensor) else v
for k, v in self.optimizer.state_dict().items()
},
"scaler": self.scaler.state_dict(),
}
def load_state_dict(self, state_dict):
if self.is_from_scratch:
self.step = 0#state_dict["step"]
self.epoch = 0#state_dict["epoch"]
else:
self.step = state_dict["step"]
self.epoch = state_dict["epoch"]
self.model.load_state_dict(state_dict["model"])
self.optimizer.load_state_dict(state_dict["optimizer"])
self.scaler.load_state_dict(state_dict["scaler"])
def restore_from_checkpoint(self, fname="weights"):
try:
fpath = f"{self.checkpoint_dir}/{fname}.pt"
checkpoint = torch.load(fpath)
self.load_state_dict(checkpoint)
print(f"Restored from checkpoint {fpath} --> {fname}-{self.epoch}.pt!")
return True
except FileNotFoundError:
print("No checkpoint found. Starting from scratch...")
return False
def _link_checkpoint(self, save_name, link_fpath):
if os.path.islink(link_fpath):
os.unlink(link_fpath)
os.symlink(save_name, link_fpath)
def write_epoch_info(self,loss):
with open(f"{self.checkpoint_dir}/info.txt", "a") as f:
if self.best_cnt == 0:
f.write(str(self.epoch)+" ")
f.write(str(self.best_val_loss))
f.write(datetime.now().strftime('%m-%d_%H%M%S'))
f.write(" best!\n")
else:
f.write(str(self.epoch) + " ")
f.write(str(loss))
f.write(datetime.now().strftime('%m-%d_%H%M%S')+"\n")
f.close()
def save_to_checkpoint(self, fname="weights", is_best=False):
save_fpath = f"{self.checkpoint_dir}/{fname}.pt"
save_best_fpath = f"{self.checkpoint_dir}/{fname}_best.pt"
save_best_fpath_only = f"{self.checkpoint_dir}/best.pt"
# link_best_fpath = f"{self.checkpoint_dir}/{fname}_best.pt"
# link_fpath = f"{self.checkpoint_dir}/{fname}.pt"
if is_best:
# self._link_checkpoint(save_name, link_best_fpath)
torch.save(self.state_dict(), save_best_fpath)
torch.save(self.state_dict(), save_best_fpath_only)
# self.write_epoch_info(0)
else:
torch.save(self.state_dict(), save_fpath)
# self._link_checkpoint(save_name, link_fpath)
def train(self, max_epoch=None,is_inpaint=False):
self.model.train()
while True:
if self.best_cnt==20:
break
if self.param_scheduler is not None:
self.param_scheduler.train()
if max_epoch is not None and self.epoch >= max_epoch:
return
for _step, batch in enumerate(
tqdm(self.train_dl, desc=f"Epoch {self.epoch}")
):
batch = nested_map(
batch, lambda x: x.to(self.device)
if isinstance(x, torch.Tensor) else x
)
losses = self.train_step(batch,is_inpaint)
# check NaN
for loss_value in list(losses.values()):
if isinstance(loss_value,
torch.Tensor) and torch.isnan(loss_value).any():
raise RuntimeError(
f"Detected NaN loss at step {self.step}, epoch {self.epoch}"
)
self.step += 1
if self.step % 100 == 0:
self._write_summary(losses, "train",is_inpaint)
if _step % 5000 == 4999:
break
# self.model.eval()
# self.valid()
# self.model.train()
self.epoch += 1
# valid
self.model.eval()
self.valid(self.epoch,is_inpaint)
self.model.train()
return self.output_dir
def valid(self,epoch,is_inpaint):
if self.param_scheduler is not None:
self.param_scheduler.eval()
losses = None
for batch in self.val_dl:
batch = nested_map(
batch, lambda x: x.to(self.device) if isinstance(x, torch.Tensor) else x
)
current_losses = self.val_step(batch,is_inpaint)
if losses is None:
losses = current_losses
else:
for k, v in current_losses.items():
losses[k] += v
assert losses is not None
for k, v in losses.items():
losses[k] /= len(self.val_dl)
self._write_summary(losses,"val",is_inpaint)
if self.best_val_loss >= losses["loss"]:
self.best_val_loss = losses["loss"]
self.best_cnt = 0
self.save_to_checkpoint(fname=str(epoch),is_best=True)
self.write_epoch_info(0)
else:
self.best_cnt += 1
self.save_to_checkpoint(fname=str(epoch),is_best=False)
self.write_epoch_info(losses["loss"])
if is_inpaint:
with open(f"{self.checkpoint_dir}/info.txt", "a") as f:
f.write(f"noise1: {round(losses['noise_loss1'],6)} "
f"noise2: {round(losses['noise_loss2'],6)} "
f"phrase: {round(losses['phrase_loss'],6)} "
f"str: {round(losses['str_loss'],6)}\n")
f.close()
def train_step(self, batch,is_inpaint):
# people say this is the better way to set zero grad
# instead of self.optimizer.zero_grad()
for param in self.model.parameters():
param.grad = None
# here forward the model
#with self.autocast:
loss_dict,_ = self.model.get_loss_dict(batch,is_inpaint)
loss = loss_dict["loss"]
self.optimizer.zero_grad()
loss.backward()
self.grad_norm = nn.utils.clip_grad.clip_grad_norm_(
self.model.parameters(), self.params.max_grad_norm or 1e9
)
self.optimizer.step()
#self.scaler.scale(loss).backward()
#self.scaler.unscale_(self.optimizer)
#self.grad_norm = nn.utils.clip_grad.clip_grad_norm_(
# self.model.parameters(), self.params.max_grad_norm or 1e9
# )
#self.scaler.step(self.optimizer)
#self.scaler.update()
return loss_dict
def val_step(self, batch,is_inpaint):
with torch.no_grad():
#with self.autocast:
loss_dict,_ = self.model.get_loss_dict(batch,is_inpaint)
return loss_dict