-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.py
315 lines (260 loc) · 9.98 KB
/
helper.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
import torch
from torch import Tensor, nn
from tqdm import tqdm
import time
import os
from torch.cuda.amp import autocast
from contextlib import nullcontext
from typing import Optional, Tuple
import lta.utils as utils
from lta.utils import Target, Prediction
from lta.config import Config
from lta.criterion import Criterion_Ego4D
from lta.datasets.utils import dump_json
from ego4d_test_set import generate_outputs
__all__ = [
"get_dtype",
"train_one_epoch",
"evaluate",
"build_optimizer",
"build_lrscheduler",
"create_ckpt_path",
"save_model",
"load_model",
"CKPT_PATH",
"CKPT_BEST_FNAME",
]
CKPT_PATH = 'checkpoints'
CKPT_BEST_FNAME = 'checkpoint_best.pth'
logger = utils.get_logger(__name__)
def get_dtype(str_type):
dtype_dict = {
"float32": torch.float32,
"float16": torch.float16,
"bfloat16": torch.bfloat16,
}
return dtype_dict[str_type]
def get_batch_size(data: dict):
for key, val in data.items():
if isinstance(val, Tensor):
return val.size(0)
if isinstance(val, list):
return len(val)
def train_one_epoch(
model,
criterion: Criterion_Ego4D,
data_loader,
optimizer,
scheduler,
metric_tracker,
grad_clip,
device,
dtype=torch.float32,
loss_scaler=None,
mixup: utils.MixUp = None,
disable_pregress=False,
):
model.train()
# Conditionally use autocast or the dummy context manager
context = autocast(dtype=dtype) if dtype != torch.float32 else nullcontext()
for data in tqdm(data_loader, desc="Training", disable=disable_pregress):
data = {
key: val.to(device=device)
if val is not None and not isinstance(val, list)
else val
for key, val in data.items()
}
batch_size = get_batch_size(data)
past_feats = data.get("past_feats", None)
future_feats = data.get("future_feats", None)
future_verb = data.get("future_verb", None)
future_noun = data.get("future_noun", None)
past_verbs = data.get("past_verb", None)
past_nouns = data.get("past_noun", None)
target = Target(
past_feats=past_feats,
future_feats=future_feats,
past_actions=data.get("past_act", None),
past_verbs=past_verbs,
past_nouns=past_nouns,
future_actions=data.get("future_act", None),
future_verbs=future_verb,
future_nouns=future_noun,
vid_name=data.get("vid_name", None),
work_indices=data.get("work_indices", None),
num_frames=data.get("num_frames", None),
)
# Mixup in place
if mixup is not None:
mixup(past_feats, future_feats, target)
target.mixup_enabled = True
with context:
pred: Prediction = model(past_feats)
loss, loss_dict = criterion(pred, target)
optimizer.zero_grad()
loss.backward()
# Clip the gradients if required
if grad_clip is not None:
torch.nn.utils.clip_grad_norm_(model.parameters(), grad_clip)
optimizer.step()
metric_tracker.update(loss_dict, batch_size, is_training=True)
# gather the stats from all processes
metric_tracker.synchronize_between_processes(is_training=True)
if scheduler is not None:
scheduler.step()
@torch.inference_mode()
def evaluate(
model,
criterion: Optional[Criterion_Ego4D],
data_loader,
metric_tracker: Optional,
device,
dtype=torch.float32,
disable_pregress=False,
test_enable=False,
dataset_name="Ego4D",
max_len: Optional[int] = None,
):
model.eval()
# Conditionally use autocast or the dummy context manager
context = autocast(dtype=dtype) if dtype != torch.float32 else nullcontext()
out = {}
for data in tqdm(data_loader, desc="Evaluation", disable=disable_pregress):
data = {
key: val.to(device=device)
if val is not None and not isinstance(val, list)
else val
for key, val in data.items()
}
batch_size = get_batch_size(data)
past_feats = data.get("past_feats", None)
future_feats = data.get("future_feats", None)
future_verb = data.get("future_verb", None)
past_verbs = data.get("past_verb", None)
past_nouns = data.get("past_noun", None)
target = Target(
past_feats=past_feats,
future_feats=future_feats,
past_actions=data.get("past_act", None),
past_verbs=past_verbs,
past_nouns=past_nouns,
future_actions=data.get("future_act", None),
future_verbs=data.get("future_verb", None),
future_nouns=data.get("future_noun", None),
vid_name=data.get("vid_name", None),
work_indices=data.get("work_indices", None),
num_frames=data.get("num_frames", None),
)
with context:
pred: Prediction = model(past_feats, future_feats)
if criterion is not None:
loss, loss_dict = criterion(pred, target, is_training=False)
# We generate outputs for Ego4d
if test_enable and dataset_name in ["Ego4D"]:
out_cur = generate_outputs(
pred.future_verbs,
pred.future_nouns,
data_loader.dataset.verb_noun_cooccurrences,
data["clip_uid_action_idx"],
)
out.update(out_cur)
if criterion is not None:
metric_tracker.update(loss_dict, batch_size, is_training=False)
if out:
os.makedirs("output", exist_ok=True)
dump_json(out, "output/output.json")
if criterion is not None:
# gather the stats from all processes
metric_tracker.synchronize_between_processes(is_training=False)
def create_ckpt_path(cfg: Config):
time_cur = time.strftime('%Y%m%d-%H:%M:%S')
experiment_name = f'{cfg.MODEL.MODEL_CLASS}-{cfg.MODEL.N_LAYER}-{cfg.MODEL.D_MODEL}_' \
f'bs{cfg.TRAIN.BATCH_SIZE}_lr{cfg.TRAIN.LR}_' \
f'wd{cfg.TRAIN.WEIGHT_DECAY}_' \
f'{time_cur}'
if cfg.NOTE is not None:
experiment_name = f'{cfg.NOTE}_{experiment_name}'
ckpt_path = os.path.join(CKPT_PATH, experiment_name)
ckpt_save_path = os.path.join(ckpt_path, CKPT_BEST_FNAME)
return experiment_name, ckpt_path, ckpt_save_path
def build_optimizer(model, cfg: Config):
optimizer_name = cfg.TRAIN.OPTIMIZER
params = model.parameters()
if optimizer_name == 'adamw':
optimizer = torch.optim.AdamW(
params, lr=cfg.TRAIN.LR, weight_decay=cfg.TRAIN.WEIGHT_DECAY)
elif optimizer_name == 'sgd':
optimizer = torch.optim.SGD(
params, lr=cfg.TRAIN.LR, weight_decay=cfg.TRAIN.WEIGHT_DECAY,
nesterov=True, momentum=0.9)
else:
raise NotImplementedError(f'Optimizer {optimizer_name} not supported!')
return optimizer
def build_lrscheduler(optimizer, cfg: Config):
scheduler_name = cfg.TRAIN.SCHEDULER
if scheduler_name is None or scheduler_name in ["NONE", "none", "None"]:
scheduler = None
elif scheduler_name == 'cosine':
scheduler = utils.WarmUpCosineAnnealingLR(
optimizer,
T_max=cfg.TRAIN.EPOCHS,
warmup_epochs=cfg.TRAIN.WARMUP_STEPS,
eta_min=cfg.TRAIN.MIN_LR,
)
else:
raise NotImplementedError(
f'LRScheduler {scheduler_name} not supported!')
return scheduler
def save_model(
model, optimizer, scheduler,
metric_cur: float, metric_best: float, epoch: int,
metric_descending=False, fpath: Optional[str] = None,
save_each_after_epoch=0,
) -> Tuple[bool, float]:
save = False
if metric_descending:
if metric_cur < metric_best:
metric_best = metric_cur
save = True
else:
if metric_cur > metric_best:
metric_best = metric_cur
save = True
if fpath is None:
return save, metric_best
if save:
store_checkpoint(model, optimizer, scheduler, epoch, fpath)
if epoch > save_each_after_epoch:
path = fpath.replace(CKPT_BEST_FNAME, f"checkpoint_epoch_{epoch:03}")
store_checkpoint(model, optimizer, scheduler, epoch, path)
return save, metric_best
def store_checkpoint(model, optimizer, scheduler,
epoch: int, fpath: Optional[str] = None) -> None:
model_without_ddp = model
if isinstance(model, nn.parallel.DistributedDataParallel) or isinstance(
model, nn.parallel.DataParallel):
model_without_ddp = model.module
checkpoint = {
'model': model_without_ddp.state_dict(),
'optimizer': optimizer.state_dict(),
'lr_scheduler':
scheduler.state_dict()
if scheduler is not None
else None,
'epoch': epoch,
}
ckpt_path = "/".join(fpath.split("/")[:-1])
os.makedirs(ckpt_path, exist_ok=True)
logger.info(f'Storing ckpt at epoch {epoch} to {fpath}')
if utils.is_master_proc():
torch.save(checkpoint, fpath)
def load_model(model, ckpt_path: str) -> None:
checkpoint = torch.load(ckpt_path, map_location="cpu")
pretrained_dict = checkpoint['model']
missing_keys, unexp_keys = model.load_state_dict(
pretrained_dict, strict=False)
if not missing_keys and not unexp_keys:
logger.info(f'Loaded model from {ckpt_path}')
return
logger.warning('Could not init from %s: %s', ckpt_path, missing_keys)
logger.warning('Unused keys in %s: %s', ckpt_path, unexp_keys)