-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain.py
430 lines (381 loc) · 14.1 KB
/
train.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#!/usr/bin/env python
# coding=UTF-8
import argparse
import os
from typing import Callable, Dict, Iterable, Optional
import mlflow
import torch
import torch.cuda.amp as amp
import torch.nn as nn
import torch.nn.functional as F
from pyutils.config import configs
from pyutils.general import AverageMeter
from pyutils.general import logger as lg
from pyutils.torch_train import (
BestKModelSaver,
count_parameters,
get_learning_rate,
load_model,
set_torch_deterministic,
)
from pyutils.typing import Criterion, DataLoader, Optimizer, Scheduler
from core import builder
from core.datasets.mixup import MixupAll
from core.utils import get_parameter_group, register_hidden_hooks
def train(
model: nn.Module,
train_loader: DataLoader,
optimizer: Optimizer,
scheduler: Scheduler,
epoch: int,
criterion: Criterion,
aux_criterions: Dict,
mixup_fn: Callable = None,
device: torch.device = torch.device("cuda:0"),
grad_scaler: Optional[Callable] = None,
teacher: Optional[nn.Module] = None,
) -> None:
model.train()
step = epoch * len(train_loader)
class_meter = AverageMeter("ce")
aux_meters = {name: AverageMeter(name) for name in aux_criterions}
data_counter = 0
correct = 0
total_data = len(train_loader.dataset)
for batch_idx, (data, target) in enumerate(train_loader):
data = data.to(device, non_blocking=True)
data_counter += data.shape[0]
target = target.to(device, non_blocking=True)
if mixup_fn is not None:
data, target = mixup_fn(data, target)
with amp.autocast(enabled=grad_scaler._enabled):
output = model(data)
class_loss = criterion(output, target)
class_meter.update(class_loss.item())
loss = class_loss
for name, config in aux_criterions.items():
aux_criterion, weight = config
aux_loss = 0
if name in {"kd", "dkd"} and teacher is not None:
with torch.no_grad():
teacher_scores = teacher(data).data.detach()
aux_loss = weight * aux_criterion(output, teacher_scores, target)
elif name == "mse_distill" and teacher is not None:
with torch.no_grad():
teacher(data).data.detach()
teacher_hiddens = [
m._recorded_hidden
for m in teacher.modules()
if hasattr(m, "_recorded_hidden")
]
student_hiddens = [
m._recorded_hidden
for m in model.modules()
if hasattr(m, "_recorded_hidden")
]
aux_loss = weight * sum(
F.mse_loss(h1, h2)
for h1, h2 in zip(teacher_hiddens, student_hiddens)
)
loss = loss + aux_loss
aux_meters[name].update(aux_loss)
pred = output.data.max(1)[1]
correct += pred.eq(target.data).sum().item()
optimizer.zero_grad()
grad_scaler.scale(loss).backward()
grad_scaler.unscale_(optimizer)
if configs.run.grad_clip:
torch.nn.utils.clip_grad_value_(
[p for p in model.parameters() if p.requires_grad],
float(configs.run.max_grad_value),
)
grad_scaler.step(optimizer)
grad_scaler.update()
step += 1
if batch_idx % int(configs.run.log_interval) == 0:
log = "Train Epoch: {} [{:7d}/{:7d} ({:3.0f}%)] Loss: {:.4e} class Loss: {:.4e}".format(
epoch,
data_counter,
total_data,
100.0 * data_counter / total_data,
loss.data.item(),
class_loss.data.item(),
)
for name, aux_meter in aux_meters.items():
log += f" {name}: {aux_meter.val:.4e}"
lg.info(log)
mlflow.log_metrics({"train_loss": loss.item()}, step=step)
scheduler.step()
avg_class_loss = class_meter.avg
accuracy = 100.0 * correct / total_data
lg.info(
f"Train class Loss: {avg_class_loss:.4e}, Accuracy: {correct}/{total_data} ({accuracy:.2f}%)"
)
mlflow.log_metrics(
{
"train_class": avg_class_loss,
"train_acc": accuracy,
"lr": get_learning_rate(optimizer),
},
step=epoch,
)
def validate(
model: nn.Module,
validation_loader: DataLoader,
epoch: int,
criterion: Criterion,
loss_vector: Iterable,
accuracy_vector: Iterable,
device: torch.device,
mixup_fn: Callable = None,
fp16: bool = False,
) -> None:
model.eval()
val_loss = 0
correct = 0
class_meter = AverageMeter("ce")
with amp.autocast(enabled=fp16):
with torch.no_grad():
for i, (data, target) in enumerate(validation_loader):
data = data.to(device, non_blocking=True)
target = target.to(device, non_blocking=True)
if mixup_fn is not None:
data, target = mixup_fn(data, target, random_state=i, vflip=False)
output = model(data)
val_loss = criterion(output, target)
class_meter.update(val_loss.item())
pred = output.data.max(1)[1]
correct += pred.eq(target.data).sum().item()
loss_vector.append(class_meter.avg)
accuracy = 100.0 * correct / len(validation_loader.dataset)
accuracy_vector.append(accuracy)
lg.info(
f"\nValidation set: Average loss: {class_meter.avg:.4e}, Accuracy: {correct}/{len(validation_loader.dataset)} ({accuracy:.2f}%)\n"
)
mlflow.log_metrics({"val_loss": class_meter.avg, "val_acc": accuracy}, step=epoch)
def test(
model: nn.Module,
test_loader: DataLoader,
epoch: int,
criterion: Criterion,
loss_vector: Iterable,
accuracy_vector: Iterable,
device: torch.device,
mixup_fn: Callable = None,
fp16: bool = False,
) -> None:
model.eval()
val_loss = 0
correct = 0
class_meter = AverageMeter("ce")
with amp.autocast(enabled=fp16):
with torch.no_grad():
for i, (data, target) in enumerate(test_loader):
data = data.to(device, non_blocking=True)
target = target.to(device, non_blocking=True)
if mixup_fn is not None:
data, target = mixup_fn(
data, target, random_state=i + 10000, vflip=False
)
output = model(data)
val_loss = criterion(output, target)
class_meter.update(val_loss.item())
pred = output.data.max(1)[1]
correct += pred.eq(target.data).sum().item()
loss_vector.append(class_meter.avg)
accuracy = 100.0 * correct / len(test_loader.dataset)
accuracy_vector.append(accuracy)
lg.info(
f"\nTest set: Average loss: {class_meter.avg:.4e}, Accuracy: {correct}/{len(test_loader.dataset)} ({accuracy:.2f}%)\n"
)
mlflow.log_metrics({"test_loss": class_meter.avg, "test_acc": accuracy}, step=epoch)
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("config", metavar="FILE", help="config file")
args, opts = parser.parse_known_args()
configs.load(args.config, recursive=True)
configs.update(opts)
lg.info(configs)
if torch.cuda.is_available() and int(configs.run.use_cuda):
torch.cuda.set_device(configs.run.gpu_id)
device = torch.device("cuda:" + str(configs.run.gpu_id))
torch.backends.cudnn.benchmark = True
else:
device = torch.device("cpu")
torch.backends.cudnn.benchmark = False
if bool(configs.run.deterministic):
set_torch_deterministic()
train_loader, validation_loader, test_loader = builder.make_dataloader(
splits=["train", "valid", "test"]
)
if (
configs.run.do_distill
and configs.teacher is not None
and os.path.exists(configs.teacher.checkpoint)
):
teacher = builder.make_model(device, model_cfg=configs.teacher)
load_model(teacher, path=configs.teacher.checkpoint)
teacher.eval()
lg.info(f"Load teacher model from {configs.teacher.checkpoint}")
else:
teacher = None
model = builder.make_model(
device,
model_cfg=configs.model,
random_state=(
int(configs.run.random_state) if int(configs.run.deterministic) else None
),
)
lg.info(model)
optimizer = builder.make_optimizer(
get_parameter_group(model, weight_decay=float(configs.optimizer.weight_decay)),
name=configs.optimizer.name,
configs=configs.optimizer,
)
scheduler = builder.make_scheduler(optimizer)
criterion = builder.make_criterion(configs.criterion.name, configs.criterion).to(
device
)
aux_criterions = dict()
if configs.aux_criterion is not None:
for name, config in configs.aux_criterion.items():
if float(config.weight) > 0:
try:
fn = builder.make_criterion(name, cfg=config)
except NotImplementedError:
fn = name
aux_criterions[name] = [fn, float(config.weight)]
print(aux_criterions)
if "mse_distill" in aux_criterions and teacher is not None:
## register hooks for teacher and student
register_hidden_hooks(teacher)
register_hidden_hooks(model)
print(len([m for m in teacher.modules() if hasattr(m, "_recorded_hidden")]))
print(len([m for m in teacher.modules() if hasattr(m, "_recorded_hidden")]))
print("Register hidden state hooks for teacher and students")
mixup_config = configs.dataset.augment
mixup_fn = MixupAll(**mixup_config) if mixup_config is not None else None
test_mixup_fn = (
MixupAll(**configs.dataset.test_augment) if mixup_config is not None else None
)
saver = BestKModelSaver(
k=int(configs.checkpoint.save_best_model_k),
descend=True,
truncate=2,
metric_name="acc",
format="{:.2f}",
)
grad_scaler = amp.GradScaler(enabled=getattr(configs.run, "fp16", False))
lg.info(f"Number of parameters: {count_parameters(model)}")
model_name = f"{configs.model.name}"
checkpoint = f"./checkpoint/{configs.checkpoint.checkpoint_dir}/{model_name}_{configs.checkpoint.model_comment}.pt"
lg.info(f"Current checkpoint: {checkpoint}")
mlflow.set_experiment(configs.run.experiment)
experiment = mlflow.get_experiment_by_name(configs.run.experiment)
mlflow.start_run(run_name=model_name)
mlflow.log_params(
{
"exp_name": configs.run.experiment,
"exp_id": experiment.experiment_id,
"run_id": mlflow.active_run().info.run_id,
"init_lr": configs.optimizer.lr,
"checkpoint": checkpoint,
"restore_checkpoint": configs.checkpoint.restore_checkpoint,
"pid": os.getpid(),
}
)
lossv, accv = [0], [0]
epoch = 0
try:
lg.info(
f"Experiment {configs.run.experiment} ({experiment.experiment_id}) starts. Run ID: ({mlflow.active_run().info.run_id}). PID: ({os.getpid()}). PPID: ({os.getppid()}). Host: ({os.uname()[1]})"
)
if (
int(configs.checkpoint.resume)
and len(configs.checkpoint.restore_checkpoint) > 0
):
load_model(
model,
configs.checkpoint.restore_checkpoint,
ignore_size_mismatch=int(configs.checkpoint.no_linear),
)
lg.info("Validate resumed model...")
test(
model,
validation_loader,
0,
criterion,
lossv,
accv,
device,
fp16=grad_scaler._enabled,
)
if teacher is not None:
test(
teacher,
validation_loader,
0,
criterion,
[],
[],
device,
fp16=grad_scaler._enabled,
)
lg.info("Map teacher to student...")
if hasattr(model, "load_from_teacher"):
with amp.autocast(grad_scaler._enabled):
model.load_from_teacher(teacher)
## compile models
if getattr(configs.run, "compile", False):
model = torch.compile(model)
if teacher is not None:
teacher = torch.compile(teacher)
for epoch in range(1, int(configs.run.n_epochs) + 1):
train(
model,
train_loader,
optimizer,
scheduler,
epoch,
criterion,
aux_criterions,
mixup_fn,
device,
grad_scaler=grad_scaler,
teacher=teacher,
)
if validation_loader is not None:
validate(
model,
validation_loader,
epoch,
criterion,
lossv,
accv,
device,
mixup_fn=test_mixup_fn,
fp16=grad_scaler._enabled,
)
test(
model,
test_loader,
epoch,
criterion,
lossv if validation_loader is None else [],
accv if validation_loader is None else [],
device,
mixup_fn=test_mixup_fn,
fp16=grad_scaler._enabled,
)
saver.save_model(
getattr(model, "_orig_mod", model), # remove compiled wrapper
accv[-1],
epoch=epoch,
path=checkpoint,
save_model=False,
print_msg=True,
)
except KeyboardInterrupt:
lg.warning("Ctrl-C Stopped")
if __name__ == "__main__":
main()