-
Notifications
You must be signed in to change notification settings - Fork 1
/
train.py
294 lines (249 loc) · 11.5 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
"""
Train a model on the Hateful Memes Dataset
"""
import numpy as np
import random
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import torch.optim.lr_scheduler as sched
import torch.utils.data as data
import util
from args import get_train_args
from models import Baseline_model, VisualBert_Model, VisualBert_Model_Fairface
from util import HatefulMemes, HatefulMemesRawImages, HatefulMemesRawImagesAdditionalFeat
from collections import OrderedDict
from sklearn import metrics
from tensorboardX import SummaryWriter
from tqdm import tqdm
from ujson import load as json_load
from json import dumps
import os
def main(args):
os.environ["TOKENIZERS_PARALLELISM"] = "false"
# set up logger and devices
args.save_dir = util.get_save_dir(args.save_dir, args.name, training = True)
log = util.get_logger(args.save_dir, args.name)
tbx = SummaryWriter(args.save_dir)
device, args.gpu_ids = util.get_available_devices()
# dump the args info
log.info(f'Args: {dumps(vars(args), indent=4, sort_keys=True)}')
args.batch_size *= max(1, len(args.gpu_ids))
# set seed
log.info(f'Seed: {args.seed}')
random.seed(args.seed)
np.random.seed(args.seed)
torch.manual_seed(args.seed)
torch.cuda.manual_seed_all(args.seed)
# Get Model
log.info("Making model....")
if(args.model_type == "baseline"):
model = Baseline_model(hidden_size=args.hidden_size)
elif(args.model_type == "visualbert"):
model = VisualBert_Model(args.batch_size, args.hidden_size, device)
elif(args.model_type == "visualbert_fairface"):
model = VisualBert_Model_Fairface(args.batch_size, args.hidden_size, device)
else:
raise Exception("Model provided not valid")
model = nn.DataParallel(model, args.gpu_ids)
# load the step if restarting
if args.load_path:
log.info(f'Loading checkpoint from {args.load_path}...')
model, step = util.load_model(model, args.load_path, args.gpu_ids)
else:
step = 0
# send model to dev and start training
model = model.to(device)
model.train()
ema = util.EMA(model, args.ema_decay)
# Get checkpoint saver
saver = util.CheckpointSaver(args.save_dir,
max_checkpoints = args.max_checkpoints,
metric_name = args.metric_name,
maximize_metric = args.maximize_metric,
log = log)
optimizer = optim.Adam(model.parameters(),
lr = args.lr,
betas = (0.9, 0.999),
eps = 1e-7,
weight_decay = args.l2_wd)
criterion = nn.BCEWithLogitsLoss()
scheduler = sched.LambdaLR(optimizer, lambda s: 1.) # Constant LR
# load in data
log.info("Building dataset....")
if(args.model_type == "baseline"):
train_dataset = HatefulMemes(args.train_eval_file,
args.img_folder_rel_path,
args.text_model_path,
balance = True)
###############
################## might need to modify collate fn to allow for padding of text data
###############
train_loader = data.DataLoader(train_dataset,
batch_size = args.batch_size,
shuffle = True,
num_workers = args.num_workers)
dev_dataset = HatefulMemes(args.dev_eval_file,
args.img_folder_rel_path,
args.text_model_path)
dev_loader = data.DataLoader(dev_dataset,
batch_size = args.batch_size,
shuffle = True,
num_workers = args.num_workers)
elif(args.model_type == "visualbert"):
train_dataset = HatefulMemesRawImages(args.train_eval_file,
args.img_folder_rel_path,
args.text_model_path,
balance = True)
###############
################## might need to modify collate fn to allow for padding of text data
###############
train_loader = data.DataLoader(train_dataset,
batch_size = args.batch_size,
shuffle = True,
num_workers = args.num_workers)
dev_dataset = HatefulMemesRawImages(args.dev_eval_file,
args.img_folder_rel_path,
args.text_model_path)
dev_loader = data.DataLoader(dev_dataset,
batch_size = args.batch_size,
shuffle = True,
num_workers = args.num_workers)
elif(args.model_type == "visualbert_fairface"):
train_dataset = HatefulMemesRawImagesAdditionalFeat(args.train_eval_file,
args.img_folder_rel_path,
args.text_model_path,
balance = True)
###############
################## might need to modify collate fn to allow for padding of text data
###############
train_loader = data.DataLoader(train_dataset,
batch_size = args.batch_size,
shuffle = True,
num_workers = args.num_workers)
dev_dataset = HatefulMemesRawImagesAdditionalFeat(args.dev_eval_file,
args.img_folder_rel_path,
args.text_model_path)
dev_loader = data.DataLoader(dev_dataset,
batch_size = args.batch_size,
shuffle = True,
num_workers = args.num_workers)
else:
raise Exception("Model provided not valid")
# Start training
log.info("Training...")
steps_till_eval = args.eval_steps
epoch = step // len(train_dataset)
while epoch != args.num_epochs:
epoch += 1
log.info(f'Starting epoch {epoch}....')
with torch.enable_grad(), \
tqdm(total=len(train_loader.dataset)) as progress_bar:
for img_id, image, text, label, add_feat in train_loader:
# forward pass here
image = image.to(device)
# text = text.to(device)
batch_size = args.batch_size
optimizer.zero_grad()
if(args.model_type == "baseline"):
score = model(image, text, device)
elif(args.model_type == "visualbert"):
score = model(image, text, device)
elif(args.model_type == "visualbert_fairface"):
score = model(image, text, add_feat, device)
else:
raise Exception("Model Type Invalid")
# calc loss
label = label.float().to(device)
loss = criterion(score, label.unsqueeze(1))
loss_val = loss.item()
# backward pass here
loss.backward()
nn.utils.clip_grad_norm_(model.parameters(), args.max_grad_norm)
optimizer.step()
scheduler.step(step//batch_size)
ema(model, step//batch_size)
# log info
step += batch_size
progress_bar.update(batch_size)
progress_bar.set_postfix(epoch = epoch,
loss = loss_val)
tbx.add_scalar("train/loss", loss_val, step)
tbx.add_scalar("train/LR", optimizer.param_groups[0]['lr'],
step)
steps_till_eval -= batch_size
if steps_till_eval <= 0:
steps_till_eval = args.eval_steps
# Eval and save checkpoint
log.info(f'Evaluating at step {step}...')
ema.assign(model)
results, pred_dict = evaluate(args,
model,
dev_loader,
device)
saver.save(step, model, results[args.metric_name], device)
ema.resume(model)
results_str = ", ".join(f'{k}: {v:05.2f}' for k, v in results.items())
log.info(f'Dev {results_str}')
# tensorboard
log.info("Visualizing in TensorBoard")
for k, v in results.items():
tbx.add_scalar(f'dev/{k}', v, step)
def evaluate(args, model, data_loader, device):
criterion = nn.BCEWithLogitsLoss()
nll_meter = util.AverageMeter()
model.eval()
pred_dict = {} # id, prob and prediction
full_score = []
full_labels = []
acc = 0
num_corrects, num_samples = 0, 0
with torch.no_grad(), \
tqdm(total=len(data_loader.dataset)) as progress_bar:
for img_id, image, text, label, add_feat in data_loader:
# forward pass here
image = image.to(device)
# text = text.to(device)
batch_size = args.batch_size
if(args.model_type == "baseline"):
score = model(image, text, device)
elif(args.model_type == "visualbert"):
score = model(image, text, device)
elif(args.model_type == "visualbert_fairface"):
score = model(image, text, add_feat, device)
else:
raise Exception("Model Type Invalid")
# calc loss
label = label.float().to(device)
preds, num_correct, acc = util.binary_acc(score, label.unsqueeze(1))
print(preds)
print(acc)
loss = criterion(score, label.unsqueeze(1))
nll_meter.update(loss.item(), batch_size)
# get acc and auroc
num_corrects += num_correct
num_samples += preds.size(0)
pred_dict_update = util.make_update_dict(
img_id,
preds,
score,
label
)
full_score.extend(torch.sigmoid(score).tolist())
full_labels.extend(label)
# update
pred_dict.update(pred_dict_update)
acc = float(num_corrects) / num_samples
# ROC
y_score = np.asarray(full_score)
y = np.asarray(full_labels).astype(int)
auc = metrics.roc_auc_score(y, y_score)
model.train()
results_list = [("NLL", nll_meter.avg),
("Acc", acc),
("AUROC", auc)]
results = OrderedDict(results_list)
return results, pred_dict
if __name__ == '__main__':
main(get_train_args())