-
Notifications
You must be signed in to change notification settings - Fork 11
/
train.py
364 lines (278 loc) · 10.9 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
import logging
import math
import os
import pickle
import random
from pprint import pprint
from typing import Dict
import numpy as np
import pandas as pd
# import ipdb
import torch
import torch.nn.functional as F
from pyhocon import ConfigFactory
from termcolor import colored
from torch import nn, optim
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
from tqdm import tqdm
import utils
from arguments import args
from dataset import get_dataloader
from model import get_model
from utils import MULTIPLE_CHOICE_TASKS, accuracy, count_correct
# from torchpie.config import config
# from torchpie.environment import args, experiment_path
# from torchpie.logging import logger
# from torchpie.meters import AverageMeter
# from torchpie.parallel import FakeObj
# from torchpie.utils.checkpoint import save_checkpoint
from utils.checkpoint import save_checkpoint
from utils.config import config
from utils.dictionary import CharDictionary, Dictionary
from utils.io import load_pickle
from utils.logging import set_default_logger
from utils.meters import AverageMeter
logger = logging.getLogger(__name__)
def train(model: nn.Module, loader: DataLoader, criterion: nn.Module, optimzier: optim.Optimizer, epoch: int):
loader_length = len(loader)
losses = AverageMeter('Loss')
if TASK in utils.MULTIPLE_CHOICE_TASKS or TASK in ['frameqa', 'youtube2text']:
result = AverageMeter('Acc')
else:
result = AverageMeter('MSE')
model.train()
# for i, data in enumerate(loader):
for i, data in enumerate(tqdm(loader)):
data = utils.batch_to_gpu(data)
(
question, question_length, question_chars,
a1, a1_length, a1_chars,
a2, a2_length, a2_chars,
a3, a3_length, a3_chars,
a4, a4_length, a4_chars,
a5, a5_length, a5_chars,
features, c3d_features, bbox_features, bbox,
answer
) = data
if config.get_bool('abc.is_multiple_choice'):
answer = torch.zeros_like(answer)
out = model(
question, question_length, question_chars,
a1, a1_length, a1_chars,
a2, a2_length, a2_chars,
a3, a3_length, a3_chars,
a4, a4_length, a4_chars,
a5, a5_length, a5_chars,
features, c3d_features, bbox_features, bbox
)
loss: torch.Tensor = criterion(out, answer)
optimzier.zero_grad()
loss.backward()
optimzier.step()
compute_score(losses, result, out, answer, loss)
# logger.info(
# f'Train Epoch [{epoch}][{i}/{loader_length}]\t'
# f'{result}%\t{losses}'
# )
if args.debug:
break
writer.add_scalar(f'Train/{losses.name}', losses.avg, epoch)
writer.add_scalar(f'Train/{result.name}', result.avg, epoch)
@torch.no_grad()
def test(model: nn.Module, loader: DataLoader, criterion: nn.Module, epoch: int) -> float:
loader_length = len(loader)
losses = AverageMeter('Loss')
if TASK in utils.MULTIPLE_CHOICE_TASKS or TASK in ['frameqa', 'youtube2text']:
result = AverageMeter('Acc')
else:
result = AverageMeter('MSE')
type_meters = dict()
if TASK == 'youtube2text':
youtube2text_meters: Dict[int, AverageMeter] = dict()
for qtype_id, qtype in youtube2text_qtype_dict.items():
youtube2text_meters[qtype_id] = AverageMeter(qtype, fmt=':.3f')
youtube2text_meters['other'] = AverageMeter('other', fmt=':.3f')
model.eval()
final_out = []
for i, data in enumerate(tqdm(loader)):
data = utils.batch_to_gpu(data)
(
question, question_length, question_chars,
a1, a1_length, a1_chars,
a2, a2_length, a2_chars,
a3, a3_length, a3_chars,
a4, a4_length, a4_chars,
a5, a5_length, a5_chars,
features, c3d_features, bbox_features, bbox,
answer
) = data
if config.get_bool('abc.is_multiple_choice'):
answer = torch.zeros_like(answer)
out = model(
question, question_length, question_chars,
a1, a1_length, a1_chars,
a2, a2_length, a2_chars,
a3, a3_length, a3_chars,
a4, a4_length, a4_chars,
a5, a5_length, a5_chars,
features, c3d_features, bbox_features, bbox
)
loss: torch.Tensor = criterion(out, answer)
compute_score(losses, result, out, answer, loss)
if TASK == 'youtube2text':
corrects = out.argmax(dim=1).eq(answer)
qtype_ids = question[:, 0]
all_corrects = corrects.sum()
all_questions = len(question)
for qtype_id in youtube2text_qtype_dict.keys():
qtype_meter = youtube2text_meters[qtype_id]
current_qtype = qtype_ids.eq(qtype_id)
num_questions = current_qtype.sum()
if num_questions > 0:
currect_qtype_corrects = (
corrects & current_qtype).sum()
qtype_meter.update(
currect_qtype_corrects.float() / num_questions,
num_questions
)
all_corrects -= currect_qtype_corrects
all_questions -= num_questions
if all_questions > 0:
youtube2text_meters['other'].update(
all_corrects.float() / all_questions, all_questions)
if args.debug:
break
writer.add_scalar(f'Test/{losses.name}', losses.avg, epoch)
writer.add_scalar(f'Test/{result.name}', result.avg, epoch)
if TASK == 'youtube2text':
avg_per_class = 0
for meter in youtube2text_meters.values():
logger.info(f'Test Epoch [{epoch}] {meter}, n={meter.count}')
avg_per_class += meter.avg
avg_per_class /= 3
logger.info(f'Test Epoch [{epoch}], Avg. Per-class: {avg_per_class}')
for meter in youtube2text_meters.values():
type_meters[meter.name] = meter.avg.item()
return result.avg, type_meters
@torch.no_grad()
def compute_score(losses: AverageMeter, result: AverageMeter, out: torch.Tensor, answer: torch.Tensor,
loss: torch.Tensor):
batch_size = answer.shape[0]
if TASK in utils.MULTIPLE_CHOICE_TASKS or TASK in ['frameqa', 'youtube2text']:
acc = accuracy(out, answer)
result.update(acc.item(), batch_size)
elif TASK == 'count':
out = out * 10. + 1.
mse = F.mse_loss(out.round().clamp(1., 10.), answer.clamp(1., 10.))
result.update(mse.item(), batch_size)
if TASK in MULTIPLE_CHOICE_TASKS or config.get_bool('abc.is_multiple_choice'):
losses.update(loss.item() / batch_size, batch_size)
else:
losses.update(loss.item(), batch_size)
def main():
best_result = math.inf if TASK == 'count' else 0.0
best_type_meters = dict()
train_loader, test_loader = get_dataloader(config, logger)
num_classes = 1
if TASK == 'frameqa':
answer_dict = utils.load_answer_dict()
num_classes = len(answer_dict)
if TASK == 'youtube2text':
if config.get_bool('abc.is_multiple_choice'):
num_classes = 1
else:
num_classes = 1000
logger.info(f'Num classes: {num_classes}')
vocab_size = utils.get_vocab_size(config, TASK, level='word')
char_vocab_size = utils.get_vocab_size(config, TASK, level='char')
model = get_model(vocab_size, char_vocab_size, num_classes)
model = model.cuda()
if TASK in MULTIPLE_CHOICE_TASKS:
criterion = nn.CrossEntropyLoss(reduction='sum')
elif TASK == 'count':
inner_criterion = nn.MSELoss()
def criterion(input, target):
target = (target - 1.) / 10.
return inner_criterion(input, target)
# criterion = nn.SmoothL1Loss()
elif TASK in ['frameqa']:
criterion = nn.CrossEntropyLoss()
elif TASK == 'youtube2text':
if config.get_bool('abc.is_multiple_choice'):
criterion = nn.CrossEntropyLoss(reduction='sum')
else:
criterion = nn.CrossEntropyLoss()
optimizer_type = config.get_string('optimizer')
if optimizer_type == 'adam':
optimizer = optim.Adam(
model.parameters(), lr=config.get_float('adam.lr'))
else:
raise Exception(f'Unknow optimizer: {optimizer_type}')
start_epoch = 1
end_epoch = config.get_int('num_epochs')
for epoch in range(start_epoch, end_epoch + 1):
logger.info(f'Epoch [{epoch}/{end_epoch}] start')
train(model, train_loader, criterion, optimizer, epoch)
current_result, current_type_meters = test(
model, test_loader, criterion, epoch)
logger.info(f'Epoch [{epoch}/{end_epoch}] end')
if args.debug:
break
is_best = False
if TASK == 'count':
if current_result < best_result:
is_best = True
best_result = current_result
else:
if current_result > best_result:
is_best = True
best_result = current_result
best_type_meters = current_type_meters
logger.info(
colored("Current best result: {:.2f}, Exp path: {}".format(best_result, args.experiment_path), "red"))
logger.info(best_type_meters)
save_checkpoint({
'arch': config.get_string('arch'),
'task': TASK,
'state_dict': model.state_dict(),
'epoch': epoch + 1,
'best_result': best_result,
'optimizer': optimizer.state_dict(),
'best_type_meters': best_type_meters,
}, is_best=is_best, folder=args.experiment_path)
if TASK == 'count':
logger.info(f'Best MSE: {best_result}')
else:
logger.info(f'Best Acc: {best_result}')
def fix_seed(config):
seed = config.get_int('seed')
logger.info(f'Set seed={seed}')
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
if __name__ == "__main__":
set_default_logger(args.experiment_path, debug=args.debug)
# config = ConfigFactory.parse_file(args.config)
fix_seed(config)
pprint(config)
TASK = config.get_string('task')
best_meters = dict()
if TASK == 'youtube2text':
youtube2text_dictionary = Dictionary.load_from_file(
os.path.join(
config.get_string('cache_path'), 'youtube2text_dictionary.pkl'
)
)
youtube2text_qtype_dict = dict()
for qtype in ['what', 'who']:
qtype_id = youtube2text_dictionary.word2idx[qtype]
youtube2text_qtype_dict[qtype_id] = qtype
if args.experiment_path is not None:
writer = SummaryWriter(log_dir=args.experiment_path)
else:
# writer: SummaryWriter = FakeObj()
raise Exception('No exp path for tensorboard')
main()
writer.close()