-
Notifications
You must be signed in to change notification settings - Fork 32
/
main.py
145 lines (118 loc) · 5.1 KB
/
main.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
__author__ = "Jie Lei"
import os
import torch
import torch.nn as nn
import torch.backends.cudnn as cudnn
from torch.utils.data import DataLoader
from tqdm import tqdm
from tensorboardX import SummaryWriter
from model.tvqa_abc import ABC
from tvqa_dataset import TVQADataset, pad_collate, preprocess_inputs
from config import BaseOptions
def train(opt, dset, model, criterion, optimizer, epoch, previous_best_acc):
dset.set_mode("train")
model.train()
train_loader = DataLoader(dset, batch_size=opt.bsz, shuffle=True, collate_fn=pad_collate)
train_loss = []
valid_acc_log = ["batch_idx\tacc"]
train_corrects = []
torch.set_grad_enabled(True)
for batch_idx, batch in tqdm(enumerate(train_loader)):
model_inputs, targets, _ = preprocess_inputs(batch, opt.max_sub_l, opt.max_vcpt_l, opt.max_vid_l,
device=opt.device)
outputs = model(*model_inputs)
loss = criterion(outputs, targets)
optimizer.zero_grad()
loss.backward()
optimizer.step()
# measure accuracy and record loss
train_loss.append(loss.item())
pred_ids = outputs.data.max(1)[1]
train_corrects += pred_ids.eq(targets.data).cpu().numpy().tolist()
if batch_idx % opt.log_freq == 0:
niter = epoch * len(train_loader) + batch_idx
train_acc = sum(train_corrects) / float(len(train_corrects))
train_loss = sum(train_loss) / float(len(train_corrects))
opt.writer.add_scalar("Train/Acc", train_acc, niter)
opt.writer.add_scalar("Train/Loss", train_loss, niter)
# Test
valid_acc, valid_loss = validate(opt, dset, model, mode="valid")
opt.writer.add_scalar("Valid/Loss", valid_loss, niter)
valid_log_str = "%02d\t%.4f" % (batch_idx, valid_acc)
valid_acc_log.append(valid_log_str)
if valid_acc > previous_best_acc:
previous_best_acc = valid_acc
torch.save(model.state_dict(), os.path.join(opt.results_dir, "best_valid.pth"))
print(" Train Epoch %d loss %.4f acc %.4f Val loss %.4f acc %.4f"
% (epoch, train_loss, train_acc, valid_loss, valid_acc))
# reset to train
torch.set_grad_enabled(True)
model.train()
dset.set_mode("train")
train_corrects = []
train_loss = []
if opt.debug:
break
# additional log
with open(os.path.join(opt.results_dir, "valid_acc.log"), "a") as f:
f.write("\n".join(valid_acc_log) + "\n")
return previous_best_acc
def validate(opt, dset, model, mode="valid"):
dset.set_mode(mode)
torch.set_grad_enabled(False)
model.eval()
valid_loader = DataLoader(dset, batch_size=opt.test_bsz, shuffle=False, collate_fn=pad_collate)
valid_qids = []
valid_loss = []
valid_corrects = []
for _, batch in enumerate(valid_loader):
model_inputs, targets, qids = preprocess_inputs(batch, opt.max_sub_l, opt.max_vcpt_l, opt.max_vid_l,
device=opt.device)
outputs = model(*model_inputs)
loss = criterion(outputs, targets)
# measure accuracy and record loss
valid_qids += [int(x) for x in qids]
valid_loss.append(loss.item())
pred_ids = outputs.data.max(1)[1]
valid_corrects += pred_ids.eq(targets.data).cpu().numpy().tolist()
if opt.debug:
break
valid_acc = sum(valid_corrects) / float(len(valid_corrects))
valid_loss = sum(valid_loss) / float(len(valid_corrects))
return valid_acc, valid_loss
if __name__ == "__main__":
torch.manual_seed(2018)
opt = BaseOptions().parse()
writer = SummaryWriter(opt.results_dir)
opt.writer = writer
dset = TVQADataset(opt)
opt.vocab_size = len(dset.word2idx)
model = ABC(opt)
if not opt.no_glove:
model.load_embedding(dset.vocab_embedding)
model.to(opt.device)
cudnn.benchmark = True
criterion = nn.CrossEntropyLoss(size_average=False).to(opt.device)
optimizer = torch.optim.Adam(filter(lambda p: p.requires_grad, model.parameters()),
lr=opt.lr, weight_decay=opt.wd)
best_acc = 0.
early_stopping_cnt = 0
early_stopping_flag = False
for epoch in range(opt.n_epoch):
if not early_stopping_flag:
# train for one epoch, valid per n batches, save the log and the best model
cur_acc = train(opt, dset, model, criterion, optimizer, epoch, best_acc)
# remember best acc
is_best = cur_acc > best_acc
best_acc = max(cur_acc, best_acc)
if not is_best:
early_stopping_cnt += 1
if early_stopping_cnt >= opt.max_es_cnt:
early_stopping_flag = True
else:
print("early stop with valid acc %.4f" % best_acc)
opt.writer.export_scalars_to_json(os.path.join(opt.results_dir, "all_scalars.json"))
opt.writer.close()
break # early stop break
if opt.debug:
break