-
Notifications
You must be signed in to change notification settings - Fork 14
/
train.py
279 lines (203 loc) · 7.45 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
import numpy as np
import pandas as pd
import importlib
import sys
import random
from tqdm import tqdm
import gc
import argparse
import torch
from torch import optim
from torch.cuda.amp import GradScaler, autocast
from collections import defaultdict
import cv2
from copy import copy
import os
from transformers import get_cosine_schedule_with_warmup
from torch.utils.data import SequentialSampler, DataLoader
cv2.setNumThreads(0)
sys.path.append("configs")
sys.path.append("models")
sys.path.append("data")
sys.path.append("losses")
sys.path.append("utils")
def worker_init_fn(worker_id):
np.random.seed(np.random.get_state()[1][0] + worker_id)
def get_train_dataloader(train_ds, cfg):
train_dataloader = DataLoader(
train_ds,
sampler=None,
shuffle=True,
batch_size=cfg.batch_size,
num_workers=cfg.num_workers,
pin_memory=False,
collate_fn=cfg.tr_collate_fn,
drop_last=cfg.drop_last,
worker_init_fn=worker_init_fn,
)
print(f"train: dataset {len(train_ds)}, dataloader {len(train_dataloader)}")
return train_dataloader
def get_val_dataloader(val_ds, cfg):
sampler = SequentialSampler(val_ds)
if cfg.batch_size_val is not None:
batch_size = cfg.batch_size_val
else:
batch_size = cfg.batch_size
val_dataloader = DataLoader(
val_ds,
sampler=sampler,
batch_size=batch_size,
num_workers=cfg.num_workers,
pin_memory=False,
collate_fn=cfg.val_collate_fn,
worker_init_fn=worker_init_fn,
)
print(f"valid: dataset {len(val_ds)}, dataloader {len(val_dataloader)}")
return val_dataloader
def get_scheduler(cfg, optimizer, total_steps):
scheduler = get_cosine_schedule_with_warmup(
optimizer,
num_warmup_steps=cfg.warmup * (total_steps // cfg.batch_size),
num_training_steps=cfg.epochs * (total_steps // cfg.batch_size),
)
return scheduler
def set_seed(seed=1234):
random.seed(seed)
os.environ["PYTHONHASHSEED"] = str(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.backends.cudnn.deterministic = False
torch.backends.cudnn.benchmark = True
def get_model(cfg, train_dataset):
Net = importlib.import_module(cfg.model).Net
return Net(cfg)
def create_checkpoint(model, optimizer, epoch, scheduler=None, scaler=None):
checkpoint = {
"model": model.state_dict(),
"optimizer": optimizer.state_dict(),
"epoch": epoch,
}
if scheduler is not None:
checkpoint["scheduler"] = scheduler.state_dict()
if scaler is not None:
checkpoint["scaler"] = scaler.state_dict()
return checkpoint
parser = argparse.ArgumentParser(description="")
parser.add_argument("-C", "--config", help="config filename")
parser.add_argument("-s", "--seed", type=int, default=-1, help="seed")
parser_args, _ = parser.parse_known_args(sys.argv)
cfg = copy(importlib.import_module(parser_args.config).cfg)
if parser_args.seed > -1:
cfg.seed = parser_args.seed
os.makedirs(str(cfg.output_dir + "/"), exist_ok=True)
cfg.CustomDataset = importlib.import_module(cfg.dataset).CustomDataset
cfg.tr_collate_fn = importlib.import_module(cfg.dataset).tr_collate_fn
cfg.val_collate_fn = importlib.import_module(cfg.dataset).val_collate_fn
batch_to_device = importlib.import_module(cfg.dataset).batch_to_device
def run_eval(model, val_dataloader, cfg, pre="val"):
model.eval()
torch.set_grad_enabled(False)
val_data = defaultdict(list)
for data in tqdm(val_dataloader):
batch = batch_to_device(data, device)
if cfg.mixed_precision:
with autocast():
output = model(batch)
else:
output = model(batch)
for key, val in output.items():
val_data[key] += [output[key]]
for key, val in output.items():
value = val_data[key]
if len(value[0].shape) == 0:
val_data[key] = torch.stack(value)
else:
val_data[key] = torch.cat(value, dim=0)
if cfg.save_val_data:
torch.save(val_data, f"{cfg.output_dir}/{pre}_data_seed{cfg.seed}.pth")
if "loss" in val_data:
val_losses = val_data["loss"].cpu().numpy()
val_loss = np.mean(val_losses)
print(f"Mean {pre}_loss", np.mean(val_losses))
else:
val_loss = 0.0
print("EVAL FINISHED")
return val_loss
if __name__ == "__main__":
if cfg.seed < 0:
cfg.seed = np.random.randint(1_000_000)
print("seed", cfg.seed)
device = "cuda:%d" % cfg.gpu
cfg.device = device
set_seed(cfg.seed)
train_df = pd.read_csv(cfg.train_df)
val_df = pd.read_csv(cfg.val_df)
train_dataset = cfg.CustomDataset(train_df, cfg, aug=cfg.train_aug, mode="train")
val_dataset = cfg.CustomDataset(val_df, cfg, aug=cfg.val_aug, mode="val")
train_dataloader = get_train_dataloader(train_dataset, cfg)
val_dataloader = get_val_dataloader(val_dataset, cfg)
model = get_model(cfg, train_dataset)
model.to(device)
total_steps = len(train_dataset)
params = model.parameters()
optimizer = optim.Adam(params, lr=cfg.lr, weight_decay=0)
scheduler = get_scheduler(cfg, optimizer, total_steps)
if cfg.mixed_precision:
scaler = GradScaler()
else:
scaler = None
cfg.curr_step = 0
i = 0
best_val_loss = np.inf
optimizer.zero_grad()
for epoch in range(cfg.epochs):
set_seed(cfg.seed + epoch)
cfg.curr_epoch = epoch
print("EPOCH:", epoch)
progress_bar = tqdm(range(len(train_dataloader)))
tr_it = iter(train_dataloader)
losses = []
gc.collect()
if cfg.train:
# ==== TRAIN LOOP
for itr in progress_bar:
i += 1
cfg.curr_step += cfg.batch_size
data = next(tr_it)
model.train()
torch.set_grad_enabled(True)
batch = batch_to_device(data, device)
if cfg.mixed_precision:
with autocast():
output_dict = model(batch)
else:
output_dict = model(batch)
loss = output_dict["loss"]
losses.append(loss.item())
if cfg.mixed_precision:
scaler.scale(loss).backward()
scaler.step(optimizer)
scaler.update()
optimizer.zero_grad()
else:
loss.backward()
optimizer.step()
optimizer.zero_grad()
if scheduler is not None:
scheduler.step()
if cfg.curr_step % cfg.batch_size == 0:
progress_bar.set_description(f"loss: {np.mean(losses[-10:]):.4f}")
if cfg.val:
if (epoch + 1) % cfg.eval_epochs == 0 or (epoch + 1) == cfg.epochs:
val_loss = run_eval(model, val_dataloader, cfg)
else:
val_score = 0
if cfg.epochs > 0:
checkpoint = create_checkpoint(
model, optimizer, epoch, scheduler=scheduler, scaler=scaler
)
torch.save(checkpoint, f"{cfg.output_dir}/checkpoint_last_seed{cfg.seed}.pth")
if cfg.epochs > 0:
checkpoint = create_checkpoint(model, optimizer, epoch, scheduler=scheduler, scaler=scaler)
torch.save(checkpoint, f"{cfg.output_dir}/checkpoint_last_seed{cfg.seed}.pth")