forked from faizaan09/visual-qa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_baseline.py
284 lines (232 loc) · 8.99 KB
/
main_baseline.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
from __future__ import print_function
import os
import torch
import random
import argparse
import json
import spacy
import torch.nn as nn
import torch.optim as optim
from torch.autograd import Variable
from datetime import datetime
import model
from torchtext.data import TabularDataset, Field, Iterator
import pickle as pkl
from tensorboardX import SummaryWriter
spacy_en = spacy.load('en')
def tokenizer(text): # create a tokenizer function
return [tok.text for tok in spacy_en.tokenizer(text)]
def repackage_hidden(hidden):
"""Wraps hidden states in new Variables, to detach them from their history."""
if not type(hidden) == tuple:
return Variable(hidden)
else:
return tuple(repackage_hidden(variable) for variable in hidden)
def main(params):
try:
output_dir = os.path.join(
params['outf'], datetime.strftime(datetime.now(), "%Y%m%d_%H%M"))
os.makedirs(output_dir)
except OSError:
pass
writer = SummaryWriter(output_dir)
if torch.cuda.is_available() and not params['cuda']:
print(
"WARNING: You have a CUDA device, so you should probably run with --cuda"
)
TEXT = Field(
sequential=True,
use_vocab=True,
tokenize=tokenizer,
lower=True,
batch_first=True)
LABEL = Field(
sequential=False, use_vocab=False, is_target=True, batch_first=True)
IMG_IND = Field(sequential=False, use_vocab=False, batch_first=True)
fields = {
'ans': ('ans', LABEL),
'img_ind': ('img_ind', IMG_IND),
'question': ('question', TEXT)
}
train, val = TabularDataset.splits(
path=params['dataroot'],
train=params['input_train'],
validation=params['input_test'],
format='csv',
skip_header=False,
fields=fields)
print("Train data")
print(train[0].__dict__.keys())
print(train[0].ans, train[0].img_ind, train[0].question)
print("Validation data")
print(val[0].__dict__.keys())
print(val[0].ans, val[0].img_ind, val[0].question)
print("Building Vocabulary ..")
TEXT.build_vocab(train, vectors='glove.6B.100d')
vocab = TEXT.vocab
print("Creating Embedding from vocab vectors ..")
params['vocab'] = vocab
vqa_model = model.Model(params)
print(vqa_model)
if params['use_checkpoint']:
checkpoint = torch.load(params['mcq_model'])
vqa_model.load_state_dict(checkpoint['model_state_dict'])
vqa_model.hidden = checkpoint['lstm_hidden']
criterion = torch.nn.CrossEntropyLoss()
if params['cuda']:
vqa_model.cuda()
criterion.cuda()
optimizer = torch.optim.Adam(vqa_model.parameters(), lr=params['lr'])
train_iter, val_iter = Iterator.splits((train, val),
batch_sizes=(params['batch_size'],
params['batch_size']),
sort_within_batch=False,
sort=False)
for epoch in range(1, params['niter'] + 1):
total_val_loss = 0
total_val_matches = 0
total_train_loss = 0
total_train_matches = 0
for i, row in enumerate(train_iter):
vqa_model.train()
# Starting each batch, we detach the hidden state from how it was previously produced.
# If we didn't, the model would try backpropagating all the way to start of the dataset.
if len(row) < params['batch_size']:
continue
vqa_model.hidden = repackage_hidden(vqa_model.hidden)
vqa_model.zero_grad()
ans, img_ind, question = row.ans, row.img_ind, row.question
batch_size = ans.size(0)
if params['cuda']:
ans = ans.cuda()
img_ind = img_ind.cuda()
question = question.cuda()
vqa_model.hidden = tuple([v.cuda() for v in vqa_model.hidden])
ans_var = Variable(ans)
img_ind_var = Variable(img_ind)
question_var = Variable(question)
pred_ans = vqa_model(img_ind_var, question_var)
train_loss = criterion(pred_ans, ans_var)
pred_ind = pred_ans.max(dim=1)[1]
train_acc = (pred_ind == ans_var).sum()
total_train_loss += train_loss.item()
total_train_matches += train_acc.item()
train_loss.backward()
optimizer.step()
if i % 1000 == 0:
print('[%d/%d][%d/%d] train_loss: %.4f' %
(epoch, params['niter'], i + 1, len(train_iter),
train_loss))
vqa_model.eval()
for row in val_iter:
if len(row) < params['batch_size']:
continue
vqa_model.hidden = repackage_hidden(vqa_model.hidden)
vqa_model.zero_grad()
ans, img_ind, question = row.ans, row.img_ind, row.question
batch_size = ans.size(0)
if params['cuda']:
ans = ans.cuda()
img_ind = img_ind.cuda()
question = question.cuda()
vqa_model.hidden = tuple([v.cuda() for v in vqa_model.hidden])
ans_var = Variable(ans)
img_ind_var = Variable(img_ind)
question_var = Variable(question)
pred_ans = vqa_model(img_ind_var, question_var)
val_loss = criterion(pred_ans, ans_var)
pred_ind = pred_ans.max(dim=1)[1]
val_acc = (pred_ind == ans_var).sum()
total_val_loss += val_loss.item()
total_val_matches += val_acc.item()
print(
'[%d/%d] train_loss: %.4f val_loss: %.4f train_acc: %.4f val_acc: %.4f'
% (epoch, params['niter'], total_train_loss / len(train_iter),
total_val_loss / len(val_iter), total_train_matches * 100 /
len(train_iter) / params['batch_size'],
total_val_matches * 100 / len(val_iter) / params['batch_size']))
writer.add_scalars(
'data', {
'train_loss':
train_loss,
'train_acc':
total_train_matches * 100 / len(train_iter) /
params['batch_size'],
'val_loss':
total_val_loss / len(val_iter),
'val_acc':
total_val_matches * 100 / len(val_iter) / params['batch_size']
}, epoch)
torch.save({
'lstm_hidden': vqa_model.hidden,
'model_state_dict': vqa_model.state_dict()
}, '%s/baseline_%d.pth' % (output_dir, epoch))
writer.close()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
# input json
parser.add_argument(
'--input_train', default='vqa_base_train.csv', help='input json file')
parser.add_argument(
'--input_test', default='vqa_base_test.csv', help='input json file')
parser.add_argument(
'--mapping_file',
default='image_index.pkl',
help='This files contains the img_id to path mapping and vice versa')
parser.add_argument(
'--image_embeddings',
default='./data/img_embedding.pkl',
help='output pkl file with img features')
parser.add_argument(
'--mcq_model',
default='output/checkpoint/baseline_15.pth',
help='saved baseline model path')
parser.add_argument(
'--dataroot', default='./data/', help='path to dataset')
parser.add_argument(
'--workers',
type=int,
help='number of data loading workers',
default=2)
parser.add_argument(
'--batch_size', type=int, default=32, help='input batch size')
parser.add_argument(
'--txt_emb_size',
type=int,
default=100,
help='the size of the text embedding vector')
parser.add_argument(
'--img_feature_size',
type=int,
default=2048,
help='the size of the image feature vector')
parser.add_argument(
'--niter', type=int, default=15, help='number of epochs to train for')
parser.add_argument(
'--lr',
type=float,
default=0.0002,
help='learning rate, default=0.0002')
parser.add_argument(
'--beta1', type=float, default=0.5, help='beta1 for adam. default=0.5')
parser.add_argument(
'--cuda', action='store_true', help='enables cuda', default=True)
parser.add_argument(
'--use_checkpoint',
action='store_true',
help='loads saved model from "mcq_model"',
default=True)
parser.add_argument(
'--outf',
default='./output/',
help='folder to output images and model checkpoints')
parser.add_argument('--manualSeed', type=int, help='manual seed')
parser.add_argument(
'--eval',
action='store_true',
help="choose whether to train the model or show demo")
args = parser.parse_args()
params = vars(args)
print('parsed input parameters:')
print(json.dumps(params, indent=2))
main(params)