-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_net.py
285 lines (223 loc) · 9.35 KB
/
train_net.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
from mmsf.utils.gpu import to_gpu
import torch
import numpy as np
from loguru import logger
from fvcore.common.config import CfgNode
from fvcore.nn.precise_bn import get_bn_modules, update_bn_stats
from tqdm import tqdm
import wandb
from mmsf.model.mmsf import MultimodalSlowFast
from mmsf.utils import metrics
from mmsf.utils.meters import EPICTrainMeter, EPICValMeter
import mmsf.utils.optimizer as optim
import mmsf.model.asf.checkpoint as asf_checkpoint
import mmsf.model.vsf.checkpoint as vsf_checkpoint
import mmsf.utils.checkpoint as cu
import mmsf.utils.distributed as du
import mmsf.datasets.loader as loader
import mmsf.utils.misc as misc
import mmsf.model.losses as losses
from eval_net import eval_epoch
from mmsf.datasets.mm_epickitchens import MultimodalEpicKitchens
def train_epoch(
train_loader: torch.utils.data.DataLoader,
model: torch.nn.Module,
optimizer: torch.optim.Optimizer,
train_meter: None,
cur_epoch: int,
cfg: CfgNode,
) -> None:
# Enable train mode.
model.train()
if cfg.BN.FREEZE:
model.module.freeze_fn("bn_statistics") if cfg.NUM_GPUS > 1 else model.freeze_fn("bn_statistics")
train_meter.iter_tic()
data_size = len(train_loader)
for cur_iter, batch in enumerate(
tqdm(
train_loader,
total=len(train_loader),
desc="Training",
unit="batch",
),
):
specs, frames, labels, _, meta = batch
if cfg.NUM_GPUS > 0:
specs = to_gpu(specs)
frames = to_gpu(frames)
labels = to_gpu(labels)
# Update the learning rate.
lr = optim.get_epoch_lr(cur_epoch + float(cur_iter) / data_size, cfg)
optim.set_lr(optimizer, lr)
# Perform the forward pass.
preds = model((specs, frames))
verb_preds, noun_preds = preds
# Explicitly declare reduction to mean.
loss_fun = losses.get_loss_func(cfg.MODEL.LOSS_FUNC)(reduction="mean")
# Compute the loss.
loss_verb = loss_fun(verb_preds, labels["verb"])
loss_noun = loss_fun(noun_preds, labels["noun"])
loss = 0.5 * (loss_verb + loss_noun)
# check Nan Loss.
misc.check_nan_losses(loss)
# Perform the backward pass.
optimizer.zero_grad()
loss.backward()
# Update the parameters.
optimizer.step()
# Compute the verb accuracies.
verb_top1_acc, verb_top5_acc = metrics.topk_accuracies(verb_preds, labels["verb"], (1, 5))
# Gather all the predictions across all the devices.
if cfg.NUM_GPUS > 1:
loss_verb, verb_top1_acc, verb_top5_acc = du.all_reduce([loss_verb, verb_top1_acc, verb_top5_acc])
# Copy the stats from GPU to CPU (sync point).
loss_verb, verb_top1_acc, verb_top5_acc = (
loss_verb.item(),
verb_top1_acc.item(),
verb_top5_acc.item(),
)
# Compute the noun accuracies.
noun_top1_acc, noun_top5_acc = metrics.topk_accuracies(noun_preds, labels["noun"], (1, 5))
# Gather all the predictions across all the devices.
if cfg.NUM_GPUS > 1:
loss_noun, noun_top1_acc, noun_top5_acc = du.all_reduce([loss_noun, noun_top1_acc, noun_top5_acc])
# Copy the stats from GPU to CPU (sync point).
loss_noun, noun_top1_acc, noun_top5_acc = (
loss_noun.item(),
noun_top1_acc.item(),
noun_top5_acc.item(),
)
# Compute the action accuracies.
action_top1_acc, action_top5_acc = metrics.multitask_topk_accuracies(
(verb_preds, noun_preds), (labels["verb"], labels["noun"]), (1, 5)
)
# Gather all the predictions across all the devices.
if cfg.NUM_GPUS > 1:
loss, action_top1_acc, action_top5_acc = du.all_reduce([loss, action_top1_acc, action_top5_acc])
# Copy the stats from GPU to CPU (sync point).
loss, action_top1_acc, action_top5_acc = (
loss.item(),
action_top1_acc.item(),
action_top5_acc.item(),
)
train_meter.iter_toc()
# Update and log stats.
train_meter.update_stats(
(verb_top1_acc, noun_top1_acc, action_top1_acc),
(verb_top5_acc, noun_top5_acc, action_top5_acc),
(loss_verb, loss_noun, loss),
lr,
specs[0].size(0) * max(cfg.NUM_GPUS, 1),
)
train_meter.log_iter_stats(cur_epoch, cur_iter)
train_meter.iter_tic()
# Log epoch stats.
train_meter.log_epoch_stats(cur_epoch)
train_meter.reset()
def train_model(cfg: CfgNode) -> None:
logger.warning("Training model.")
np.random.seed(cfg.RNG_SEED)
torch.manual_seed(cfg.RNG_SEED)
logger.info(f"Training with config:\n{cfg}")
model = MultimodalSlowFast(cfg=cfg)
if cfg.NUM_GPUS > 0:
cur_device = torch.cuda.current_device()
model = model.cuda(device=cur_device)
# Freeze the weights of the audio_model and video_model
if cfg.MODEL.FREEZE_MOD_SPECIFIC_WEIGHTS:
freeze_mod_specific_weights(model)
misc.log_model_info(model)
if cfg.BN.FREEZE:
model.audio_model.freeze_fn("bn_parameters")
model.video_model.freeze_fn("bn_parameters")
optimizer = optim.construct_optimizer(model, cfg=cfg)
# TODO: Add the case when the training resumes from a checkpoint
load_weights(cfg=cfg, model=model)
start_epoch = 0
train_loader = loader.construct_loader(cfg=cfg, split="train", dataset_class=MultimodalEpicKitchens)
val_loader = loader.construct_loader(cfg=cfg, split="val", dataset_class=MultimodalEpicKitchens)
logger.info(f"Train Loader: {len(train_loader):,} batches of size {cfg.TRAIN.BATCH_SIZE}")
logger.info(f"Val Loader: {len(val_loader):,} batches of size {cfg.TRAIN.BATCH_SIZE}")
if cfg.WANDB.ENABLE:
project_name = "MMMid-SlowFast"
if not cfg.MODEL.FREEZE_MOD_SPECIFIC_WEIGHTS:
project_name += "-full"
project_name += f"-lr={cfg.SOLVER.BASE_LR}"
project_name += f"-{cfg.SOLVER.OPTIMIZING_METHOD}"
wandb.init(project=project_name, config=cfg)
wandb.watch(model)
# TODO: Create meters
train_meter = EPICTrainMeter(len(train_loader), cfg)
val_meter = EPICValMeter(len(val_loader), cfg)
logger.info("Start training.")
# Print the number of trainable parameters
logger.warning(f"Number of trainable parameters: {sum(p.numel() for p in model.parameters() if p.requires_grad):,}")
for cur_epoch in range(start_epoch, cfg.SOLVER.MAX_EPOCH):
logger.success(f"Starting epoch {cur_epoch}...")
loader.shuffle_dataset(loader=train_loader, cur_epoch=cur_epoch)
train_epoch(
train_loader=train_loader,
model=model,
optimizer=optimizer,
train_meter=train_meter,
cur_epoch=cur_epoch,
cfg=cfg,
)
# Compute precise BN stats.
if cfg.BN.USE_PRECISE_STATS and len(get_bn_modules(model)) > 0:
calculate_and_update_precise_bn(train_loader, model, cfg)
# Save a checkpoint.
if cu.is_checkpoint_epoch(cfg, cur_epoch):
cu.save_checkpoint(cfg.OUTPUT_DIR, model, optimizer, cur_epoch, cfg)
# Evaluate the model on validation set.
if misc.is_eval_epoch(cfg, cur_epoch):
is_best_epoch = eval_epoch(val_loader, model, val_meter, cur_epoch, cfg)
if is_best_epoch:
cu.save_checkpoint(cfg.OUTPUT_DIR, model, optimizer, cur_epoch, cfg, is_best_epoch=is_best_epoch)
def load_weights(
cfg: CfgNode,
model: torch.nn.Module,
) -> None:
if cfg.TRAIN.CHECKPOINT_FILE_PATH_ASF:
asf_checkpoint.load_train_checkpoint(
cfg=cfg,
model=model.audio_model,
optimizer=None,
)
if cfg.TRAIN.CHECKPOINT_FILE_PATH_VSF:
_ = vsf_checkpoint.load_checkpoint(
path_to_checkpoint=cfg.TRAIN.CHECKPOINT_FILE_PATH_VSF,
data_parallel=cfg.NUM_GPUS > 1,
model=model.video_model,
optimizer=None,
inflation=cfg.TRAIN.CHECKPOINT_INFLATE_VSF,
convert_from_caffe2=False,
)
def calculate_and_update_precise_bn(loader, model, cfg):
"""
Update the stats in bn layers by calculate the precise stats.
Args:
loader (loader): data loader to provide training data.
model (model): model to update the bn stats.
num_iters (int): number of iterations to compute and update the bn stats.
"""
def _gen_loader():
for batch in loader:
specs, frames, labels, _, meta = batch
if cfg.NUM_GPUS > 0:
specs = to_gpu(specs)
frames = to_gpu(frames)
labels = to_gpu(labels)
yield (specs, frames)
# Update the bn stats.
update_bn_stats(model, _gen_loader(), cfg.BN.NUM_BATCHES_PRECISE)
def freeze_mod_specific_weights(model: torch.nn.Module) -> None:
logger.info("Freezing audio_model and video_model weights.")
assert hasattr(model, "audio_model") and hasattr(
model, "video_model"
), f"Model {model.__class__.__name__} does not have audio_model or video_model attributes."
# Freeze audio_model and video_model parameters
for param in model.audio_model.parameters():
param.requires_grad = False
for param in model.video_model.parameters():
param.requires_grad = False