forked from gabbiurlaro/aml22-ego
-
Notifications
You must be signed in to change notification settings - Fork 2
/
train_multimodal.py
254 lines (212 loc) · 12.2 KB
/
train_multimodal.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
from datetime import datetime
from statistics import mean
from utils.logger import logger
import torch.nn.parallel
import torch.optim
import torch
from utils.loaders import EpicKitchensDataset
from utils.args import args
from utils.utils import pformat_dict
import utils
import numpy as np
import os
import models as model_list
import tasks
import wandb
# global variables among training functions
training_iterations = 0
modalities = None
np.random.seed(13696641)
torch.manual_seed(13696641)
def init_operations():
"""
parse all the arguments, generate the logger, check gpus to be used and wandb
"""
print(args)
logger.info("Running with parameters: " + pformat_dict(args, indent=1))
# this is needed for multi-GPUs systems where you just want to use a predefined set of GPUs
if args.gpus is not None:
logger.debug('Using only these GPUs: {}'.format(args.gpus))
os.environ['CUDA_VISIBLE_DEVICES'] = str(args.gpus)
# wanbd logging configuration
if args.wandb_name is not None:
wandb.init(group=args.wandb_name, dir=args.wandb_dir)
wandb.run.name = args.name + "_" + args.shift.split("-")[0] + "_" + args.shift.split("-")[-1]
def main():
global training_iterations, modalities
init_operations()
modalities = args.modality
# recover valid paths, domains, classes
# this will output the domain conversion (D1 -> 8, et cetera) and the label list
num_classes, valid_labels, source_domain, target_domain = utils.utils.get_domains_and_labels_actionnet(args)
# device where everything is run
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# these dictionaries are for more multi-modal training/testing, each key is a modality used
models = {}
logger.info("Instantiating models per modality")
for m in modalities:
logger.info('{} Net\tModality: {}'.format(args.models[m].model, m))
# notice that here, the first parameter passed is the input dimension
# In our case it represents the feature dimensionality which is equivalent to 1024 for I3D
print(modalities)
print(args.models)
models[m] = getattr(model_list, args.models[m].model)()
# the models are wrapped into the ActionRecognition task which manages all the training steps
action_classifier = tasks.ActionRecognition("action-classifier", models, args.batch_size,
args.total_batch, args.models_dir, num_classes,
args.train.num_clips, args.models, args=args)
action_classifier.load_on_gpu(device)
print(args.action)
if args.action == "train":
# resume_from argument is adopted in case of restoring from a checkpoint
#if args.resume_from is not None:
# action_classifier.load_last_model(args.resume_from)
# define number of iterations I'll do with the actual batch: we do not reason with epochs but with iterations
# i.e. number of batches passed
# notice, here it is multiplied by tot_batch/batch_size since gradient accumulation technique is adopted
training_iterations = args.train.num_iter * (args.total_batch // args.batch_size)
# all dataloaders are generated here
train_loader = torch.utils.data.DataLoader(EpicKitchensDataset(args.dataset.shift.split("-")[0], modalities,
'train', args.dataset, None, None, None,
None, load_feat=True),
batch_size=args.batch_size, shuffle=True,
num_workers=args.dataset.workers, pin_memory=True, drop_last=True)
val_loader = torch.utils.data.DataLoader(EpicKitchensDataset(args.dataset.shift.split("-")[-1], modalities,
'val', args.dataset, None, None, None,
None, load_feat=True),
batch_size=args.batch_size, shuffle=False,
num_workers=args.dataset.workers, pin_memory=True, drop_last=False)
train(action_classifier, train_loader, val_loader, device, num_classes)
elif args.action == "validate":
if args.resume_from is not None:
action_classifier.load_last_model(args.resume_from)
val_loader = torch.utils.data.DataLoader(EpicKitchensDataset(args.dataset.shift.split("-")[-1], modalities,
'val', args.dataset, None, None, None,
None, load_feat=True),
batch_size=args.batch_size, shuffle=False,
num_workers=args.dataset.workers, pin_memory=True, drop_last=False)
validate(action_classifier, val_loader, device, action_classifier.current_iter, num_classes)
def train(action_classifier, train_loader, val_loader, device, num_classes):
"""
function to train the model on the test set
action_classifier: Task containing the model to be trained
train_loader: dataloader containing the training data
val_loader: dataloader containing the validation data
device: device on which you want to test
num_classes: int, number of classes in the classification problem
"""
global training_iterations, modalities
data_loader_source = iter(train_loader)
action_classifier.train(True)
action_classifier.zero_grad()
iteration = action_classifier.current_iter * (args.total_batch // args.batch_size)
# the batch size should be total_batch but batch accumulation is done with batch size = batch_size.
# real_iter is the number of iterations if the batch size was really total_batch
for i in range(iteration, training_iterations):
# iteration w.r.t. the paper (w.r.t the bs to simulate).... i is the iteration with the actual bs( < tot_bs)
real_iter = (i + 1) / (args.total_batch // args.batch_size)
if real_iter == args.train.lr_steps:
# learning rate decay at iteration = lr_steps
action_classifier.reduce_learning_rate()
# gradient_accumulation_step is a bool used to understand if we accumulated at least total_batch
# samples' gradient
gradient_accumulation_step = real_iter.is_integer()
"""
Retrieve the data from the loaders
"""
start_t = datetime.now()
# the following code is necessary as we do not reason in epochs so as soon as the dataloader is finished we need
# to redefine the iterator
try:
source_data, source_label = next(data_loader_source)
except StopIteration:
data_loader_source = iter(train_loader)
source_data, source_label = next(data_loader_source)
end_t = datetime.now()
#logger.info(f"Iteration {i}/{training_iterations} batch retrieved! Elapsed time = "
# f"{(end_t - start_t).total_seconds() // 60} m {(end_t - start_t).total_seconds() % 60} s")
''' Action recognition'''
source_label = source_label.to(device)
data = {}
#for clip in range(args.train.num_clips):
# in case of multi-clip training one clip per time is processed
for m in modalities:
data[m] = source_data[m].to(device)
#we feed the whole sequence
logits, _ = action_classifier.forward(data)
action_classifier.compute_loss(logits, source_label, loss_weight=1)
action_classifier.backward(retain_graph=False)
action_classifier.compute_accuracy(logits, source_label)
# update weights and zero gradients if total_batch samples are passed
if gradient_accumulation_step:
logger.info("[%d/%d]\tlast Verb loss: %.4f\tMean verb loss: %.4f\tAcc@1: %.2f%%\tAccMean@1: %.2f%%" %
(real_iter, args.train.num_iter, action_classifier.loss.val, action_classifier.loss.avg,
action_classifier.accuracy.val[1], action_classifier.accuracy.avg[1]))
action_classifier.check_grad()
action_classifier.step()
action_classifier.zero_grad()
# every eval_freq "real iteration" (iterations on total_batch) the validation is done, notice we validate and
# save the last 9 models
if gradient_accumulation_step and real_iter % args.train.eval_freq == 0:
val_metrics = validate(action_classifier, val_loader, device, int(real_iter), num_classes)
if val_metrics['top1'] <= action_classifier.best_iter_score:
logger.info("New best accuracy {:.2f}%"
.format(action_classifier.best_iter_score))
else:
logger.info("New best accuracy {:.2f}%".format(val_metrics['top1']))
action_classifier.best_iter = real_iter
action_classifier.best_iter_score = val_metrics['top1']
action_classifier.save_model(real_iter, val_metrics['top1'], prefix=None)
action_classifier.train(True)
def validate(model, val_loader, device, it, num_classes):
"""
function to validate the model on the test set
model: Task containing the model to be tested
val_loader: dataloader containing the validation data
device: device on which you want to test
it: int, iteration among the training num_iter at which the model is tested
num_classes: int, number of classes in the classification problem
"""
global modalities
model.reset_acc()
model.train(False)
logits = {}
# Iterate over the models
with torch.no_grad():
for i_val, (data, label) in enumerate(val_loader):
label = label.to(device)
for m in modalities:
batch = data[m].shape[0]
logits[m] = torch.zeros((args.test.num_clips, batch, num_classes)).to(device)
clip = {}
#for i_c in range(args.test.num_clips):
for m in modalities:
clip[m] = data[m].to(device) #We use the whole sequence
output, _ = model(clip)
for m in modalities:
logits[m] = output[m]
#Since we use all the sequence, we don't need to average
# for m in modalities:
# logits[m] = torch.mean(logits[m], dim=0)
model.compute_accuracy(logits, label)
#if (i_val + 1) % (len(val_loader) // 5) == 0: #since we have only one batch in validation, we don't use this.
logger.info("[{}/{}] top1= {:.3f}% top5 = {:.3f}%".format(i_val + 1, len(val_loader),
model.accuracy.avg[1], model.accuracy.avg[5]))
class_accuracies = [(x / y) * 100 if y > 0.0 else 0.0 for x, y in zip(model.accuracy.correct, model.accuracy.total)]
logger.info('Final accuracy: top1 = %.2f%%\ttop5 = %.2f%%' % (model.accuracy.avg[1],
model.accuracy.avg[5]))
for i_class, class_acc in enumerate(class_accuracies):
logger.info('Class %d = [%d/%d] = %.2f%%' % (i_class,
int(model.accuracy.correct[i_class]),
int(model.accuracy.total[i_class]),
class_acc))
logger.info('Accuracy by averaging class accuracies (same weight for each class): {}%'
.format(np.array(class_accuracies).mean(axis=0)))
test_results = {'top1': model.accuracy.avg[1], 'top5': model.accuracy.avg[5],
'class_accuracies': np.array(class_accuracies)}
with open(os.path.join(args.log_dir, f'val_precision_{args.dataset.shift.split("-")[0]}-'
f'{args.dataset.shift.split("-")[-1]}.txt'), 'a+') as f:
f.write("[%d/%d]\tAcc@top1: %.2f%%\n" % (it, args.train.num_iter, test_results['top1']))
return test_results
if __name__ == '__main__':
main()