-
Notifications
You must be signed in to change notification settings - Fork 35
/
run.py
499 lines (414 loc) · 17.9 KB
/
run.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
# Copyright 2018 AimBrain Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import division
from __future__ import print_function
from __future__ import absolute_import
import os
import json
import argparse
from tqdm import tqdm
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
from torch.utils.data.sampler import RandomSampler
from torch.optim.lr_scheduler import MultiStepLR
from sparse_graph_model import Model
from torch_dataset import *
from utils import *
def eval_model(args):
"""
Computes the VQA accuracy over the validation set
using a pre-trained model
"""
# Check that the model path is accurate
if args.model_path and os.path.isfile(args.model_path):
print('Resuming from checkpoint %s' % (args.model_path))
else:
raise SystemExit('Need to provide model path.')
# Set random seed
torch.manual_seed(1000)
if torch.cuda.is_available():
torch.cuda.manual_seed(1000)
else:
raise SystemExit('No CUDA available, script requires cuda')
# Load the validation set
print('Loading data')
dataset = VQA_Dataset(args.data_dir, args.emb, train=False)
loader = DataLoader(dataset, batch_size=args.bsize,
shuffle=False, num_workers=5,
collate_fn=collate_fn)
# Print data and model parameters
print('Parameters:\n\t'
'vocab size: %d\n\tembedding dim: %d\n\tfeature dim: %d'
'\n\thidden dim: %d\n\toutput dim: %d' % (dataset.q_words, args.emb,
dataset.feat_dim,
args.hid,
dataset.n_answers))
# Define the model
model = Model(vocab_size=dataset.q_words,
emb_dim=args.emb,
feat_dim=dataset.feat_dim,
hid_dim=args.hid,
out_dim=dataset.n_answers,
dropout=args.dropout,
pretrained_wemb=dataset.pretrained_wemb,
neighbourhood_size=args.neighbourhood_size)
# move to CUDA
model = model.cuda()
# Restore pre-trained model
ckpt = torch.load(args.model_path)
model.load_state_dict(ckpt['state_dict'])
model.train(False)
# Compute accuracy
result = []
correct = 0
for step, next_batch in tqdm(enumerate(loader)):
# move batch to cuda
q_batch, _, vote_batch, i_batch, k_batch, qlen_batch = \
batch_to_cuda(next_batch, volatile=True)
# get predictions
output, _ = model(q_batch, i_batch, k_batch, qlen_batch)
qid_batch = next_batch[3]
_, oix = output.data.max(1)
# record predictions
for i, qid in enumerate(qid_batch):
result.append({
'question_id': int(qid.numpy()),
'answer': dataset.a_itow[oix[i]]
})
# compute batch accuracy
correct += total_vqa_score(output, vote_batch)
# compute and print average accuracy
acc = correct/dataset.n_questions*100
print("accuracy: {} %".format(acc))
# save predictions
json.dump(result, open('result.json', 'w'))
print('Validation done')
def train(args):
"""
Train a VQA model using the training set
"""
# set random seed
torch.manual_seed(1000)
if torch.cuda.is_available():
torch.cuda.manual_seed(1000)
else:
raise SystemExit('No CUDA available, script requires cuda')
# Load the VQA training set
print('Loading data')
dataset = VQA_Dataset(args.data_dir, args.emb)
loader = DataLoader(dataset, batch_size=args.bsize,
shuffle=True, num_workers=5, collate_fn=collate_fn)
# Load the VQA validation set
dataset_test = VQA_Dataset(args.data_dir, args.emb, train=False)
test_sampler = RandomSampler(dataset_test)
loader_test = iter(DataLoader(dataset_test,
batch_size=args.bsize,
sampler=test_sampler,
shuffle=False,
num_workers=4,
collate_fn=collate_fn))
n_batches = len(dataset)//args.bsize
# Print data and model parameters
print('Parameters:\n\t'
'vocab size: %d\n\tembedding dim: %d\n\tfeature dim: %d'
'\n\thidden dim: %d\n\toutput dim: %d' % (dataset.q_words, args.emb,
dataset.feat_dim,
args.hid,
dataset.n_answers))
print('Initializing model')
model = Model(vocab_size=dataset.q_words,
emb_dim=args.emb,
feat_dim=dataset.feat_dim,
hid_dim=args.hid,
out_dim=dataset.n_answers,
dropout=args.dropout,
neighbourhood_size=args.neighbourhood_size,
pretrained_wemb=dataset.pretrained_wemb)
criterion = nn.MultiLabelSoftMarginLoss()
# Move it to GPU
model = model.cuda()
criterion = criterion.cuda()
# Define the optimiser
optimizer = torch.optim.Adam(model.parameters(), lr=args.lr)
# Continue training from saved model
start_ep = 0
if args.model_path and os.path.isfile(args.model_path):
print('Resuming from checkpoint %s' % (args.model_path))
ckpt = torch.load(args.model_path)
start_ep = ckpt['epoch']
model.load_state_dict(ckpt['state_dict'])
optimizer.load_state_dict(ckpt['optimizer'])
# Update the learning rate
for param_group in optimizer.param_groups:
param_group['lr'] = args.lr
# Learning rate scheduler
scheduler = MultiStepLR(optimizer, milestones=[30], gamma=0.5)
scheduler.last_epoch = start_ep - 1
# Train iterations
print('Start training.')
for ep in range(start_ep, start_ep+args.ep):
scheduler.step()
ep_loss = 0.0
ep_correct = 0.0
ave_loss = 0.0
ave_correct = 0.0
losses = []
for step, next_batch in tqdm(enumerate(loader)):
model.train()
# Move batch to cuda
q_batch, a_batch, vote_batch, i_batch, k_batch, qlen_batch = \
batch_to_cuda(next_batch)
# forward pass
output, adjacency_matrix = model(
q_batch, i_batch, k_batch, qlen_batch)
loss = criterion(output, a_batch)
# Compute batch accuracy based on vqa evaluation
correct = total_vqa_score(output, vote_batch)
ep_correct += correct
ep_loss += loss.data[0]
ave_correct += correct
ave_loss += loss.data[0]
losses.append(loss.cpu().data[0])
# This is a 40 step average
if step % 40 == 0 and step != 0:
print(' Epoch %02d(%03d/%03d), ave loss: %.7f, ave accuracy: %.2f%%' %
(ep+1, step, n_batches, ave_loss/40,
ave_correct * 100 / (args.bsize*40)))
ave_correct = 0
ave_loss = 0
# Compute gradient and do optimisation step
optimizer.zero_grad()
loss.backward()
optimizer.step()
# save model and compute validation accuracy every 400 steps
if step % 400 == 0:
epoch_loss = ep_loss / n_batches
epoch_acc = ep_correct * 100 / (n_batches * args.bsize)
save(model, optimizer, ep, epoch_loss, epoch_acc,
dir=args.save_dir, name=args.name+'_'+str(ep+1))
# compute validation accuracy over a small subset of the validation set
test_correct = 0
model.train(False)
for i in range(10):
test_batch = next(loader_test)
q_batch, a_batch, vote_batch, i_batch, k_batch, qlen_batch = \
batch_to_cuda(test_batch, volatile=True)
output, _ = model(q_batch, i_batch, k_batch, qlen_batch)
test_correct += total_vqa_score(output, vote_batch)
model.train(True)
acc = test_correct/(10*args.bsize)*100
print("Validation accuracy: {:.2f} %".format(acc))
# save model and compute accuracy for epoch
epoch_loss = ep_loss / n_batches
epoch_acc = ep_correct * 100 / (n_batches * args.bsize)
save(model, optimizer, ep, epoch_loss, epoch_acc,
dir=args.save_dir, name=args.name+'_'+str(ep+1))
print('Epoch %02d done, average loss: %.3f, average accuracy: %.2f%%' % (
ep+1, epoch_loss, epoch_acc))
def test(args):
"""
Creates a result.json for predictions on
the test set
"""
# Check that the model path is accurate
if args.model_path and os.path.isfile(args.model_path):
print('Resuming from checkpoint %s' % (args.model_path))
else:
raise SystemExit('Need to provide model path.')
torch.manual_seed(1000)
if torch.cuda.is_available():
torch.cuda.manual_seed(1000)
else:
raise SystemExit('No CUDA available, script requires CUDA')
print('Loading data')
dataset = VQA_Dataset_Test(args.data_dir, args.emb, train=False)
loader = DataLoader(dataset, batch_size=args.bsize,
shuffle=False, num_workers=5,
collate_fn=collate_fn)
# Print data and model parameters
print('Parameters:\n\t'
'vocab size: %d\n\tembedding dim: %d\n\tfeature dim: %d'
'\n\thidden dim: %d\n\toutput dim: %d' % (dataset.q_words, args.emb,
dataset.feat_dim,
args.hid,
dataset.n_answers))
# Define model
model = Model(vocab_size=dataset.q_words,
emb_dim=args.emb,
feat_dim=dataset.feat_dim,
hid_dim=args.hid,
out_dim=dataset.n_answers,
dropout=args.dropout,
pretrained_wemb=dataset.pretrained_wemb,
neighbourhood_size=args.neighbourhood_size)
# move to CUDA
model = model.cuda()
# Restore pre-trained model
ckpt = torch.load(args.model_path)
model.load_state_dict(ckpt['state_dict'])
model.train(False)
result = []
for step, next_batch in tqdm(enumerate(loader)):
# Batch preparation
q_batch, _, _, i_batch, k_batch, qlen_batch = \
batch_to_cuda(next_batch, volatile=True)
# get predictions
output, _ = model(q_batch, i_batch, k_batch, qlen_batch)
qid_batch = next_batch[3]
_, oix = output.data.max(1)
# record predictions
for i, qid in enumerate(qid_batch):
result.append({
'question_id': int(qid.numpy()),
'answer': dataset.a_itow[oix[i]]
})
json.dump(result, open('result.json', 'w'))
print('Testing done')
def trainval(args):
"""
Train a VQA model using the training + validation set
"""
# set random seed
torch.manual_seed(1000)
if torch.cuda.is_available():
torch.cuda.manual_seed(1000)
else:
raise SystemExit('No CUDA available, script requires CUDA.')
# load train+val sets for training
print ('Loading data')
dataset = VQA_Dataset_Test(args.data_dir, args.emb)
loader = DataLoader(dataset, batch_size=args.bsize,
shuffle=True, num_workers=5,
collate_fn=collate_fn)
n_batches = len(dataset)//args.bsize
# Print data and model parameters
print ('Parameters:\n\tvocab size: %d\n\tembedding dim: %d\n\tfeature dim: %d\
\n\thidden dim: %d\n\toutput dim: %d' % (dataset.q_words, args.emb, dataset.feat_dim,
args.hid, dataset.n_answers))
print ('Initializing model')
model = Model(vocab_size=dataset.q_words,
emb_dim=args.emb,
feat_dim=dataset.feat_dim,
hid_dim=args.hid,
out_dim=dataset.n_answers,
dropout=args.dropout,
neighbourhood_size=args.neighbourhood_size,
pretrained_wemb=dataset.pretrained_wemb)
criterion = nn.MultiLabelSoftMarginLoss()
# Move it to GPU
model = model.cuda()
criterion = criterion.cuda()
# Define the optimizer
optimizer = torch.optim.Adam(model.parameters(), lr=args.lr)
# Continue training from saved model
start_ep = 0
if args.model_path and os.path.isfile(args.model_path):
print ('Resuming from checkpoint %s' % (args.model_path))
ckpt = torch.load(args.model_path)
start_ep = ckpt['epoch']
model.load_state_dict(ckpt['state_dict'])
optimizer.load_state_dict(ckpt['optimizer'])
# ensure you can load with new lr
for param_group in optimizer.param_groups:
param_group['lr'] = args.lr
# learner rate scheduler
scheduler = MultiStepLR(optimizer, milestones=[30], gamma=0.5)
scheduler.last_epoch = start_ep - 1
# Training script
print ('Start training.')
for ep in range(start_ep, start_ep+args.ep):
scheduler.step()
ep_loss = 0.0
ep_correct = 0.0
ave_loss = 0.0
ave_correct = 0.0
losses = []
for step, next_batch in tqdm(enumerate(loader)):
model.train()
# batch to gpu
q_batch, a_batch, vote_batch, i_batch, k_batch, qlen_batch = \
batch_to_cuda(next_batch)
# Do model forward
output, adjacency_matrix = model(
q_batch, i_batch, k_batch, qlen_batch)
loss = criterion(output, a_batch)
# compute accuracy based on vqa evaluation
correct = total_vqa_score(output, vote_batch)
ep_correct += correct
ep_loss += loss.data[0]
ave_correct += correct
ave_loss += loss.data[0]
losses.append(loss.cpu().data[0])
# This is a 40 step average
if step % 40 == 0 and step != 0:
print(' Epoch %02d(%03d/%03d), ave loss: %.7f, ave accuracy: %.2f%%' %
(ep+1, step, n_batches, ave_loss/40,
ave_correct * 100 / (args.bsize*40)))
ave_correct = 0
ave_loss = 0
ave_correct = ave_loss = ave_sparsity = 0
# compute gradient and do optim step
optimizer.zero_grad()
loss.backward()
optimizer.step()
# save model and compute accuracy for epoch
epoch_loss = ep_loss / n_batches
epoch_acc = ep_correct * 100 / (n_batches * args.bsize)
save(model, optimizer, ep, epoch_loss, epoch_acc,
dir=args.save_dir, name=args.name+'_'+str(ep+1))
print('Epoch %02d done, average loss: %.3f, average accuracy: %.2f%%' % (
ep+1, epoch_loss, epoch_acc))
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Conditional Graph Convolutions for VQA')
parser.add_argument('--train', action='store_true',
help='set this to training mode.')
parser.add_argument('--trainval', action='store_true',
help='set this to train+val mode.')
parser.add_argument('--eval', action='store_true',
help='set this to evaluation mode.')
parser.add_argument('--test', action='store_true',
help='set this to test mode.')
parser.add_argument('--lr', metavar='', type=float,
default=1e-4, help='initial learning rate')
parser.add_argument('--ep', metavar='', type=int,
default=40, help='number of epochs.')
parser.add_argument('--bsize', metavar='', type=int,
default=64, help='batch size.')
parser.add_argument('--hid', metavar='', type=int,
default=1024, help='hidden dimension')
parser.add_argument('--emb', metavar='', type=int, default=300,
help='question embedding dimension')
parser.add_argument('--neighbourhood_size', metavar='', type=int, default=16,
help='number of graph neighbours to consider')
parser.add_argument('--data_dir', metavar='', type=str, default='./data',
help='path to data directory')
parser.add_argument('--save_dir', metavar='', type=str, default='./save')
parser.add_argument('--name', metavar='', type=str,
default='model', help='model name')
parser.add_argument('--dropout', metavar='', type=float, default=0.5,
help='probability of dropping out FC nodes during training')
parser.add_argument('--model_path', metavar='', type=str,
help='trained model path.')
args, unparsed = parser.parse_known_args()
if len(unparsed) != 0:
raise SystemExit('Unknown argument: {}'.format(unparsed))
if args.train:
train(args)
if args.trainval:
trainval(args)
if args.eval:
eval_model(args)
if args.test:
test(args)
if not args.train and not args.eval and not args.trainval and not args.test:
parser.print_help()