-
Notifications
You must be signed in to change notification settings - Fork 13
/
train_butd.py
240 lines (197 loc) · 9.26 KB
/
train_butd.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
import argparse
import sys
import pdb
import torch.backends.cudnn as cudnn
import torch.optim
import torch.utils.data
import torchvision.transforms as transforms
from torch import nn
from torch.nn.utils.rnn import pack_padded_sequence
import opts
from dataset import *
from models.butd import BUTD
from utils import *
from ficeval import *
# Model parameters
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # sets device for model and PyTorch tensors
cudnn.benchmark = True # set to true only if inputs to model are fixed size; otherwise lot of computational overhead
def main(opt):
"""
Training and validation.
"""
start_epoch = opt.start_epoch
epochs_since_improvement = opt.epochs_since_improvement
best_cider = 0
word_map_file = opt.word_map_file
with open(word_map_file, 'r') as f:
word_map = json.load(f)
# Initialize / load checkpoint
if len(opt.checkpoint) == 0:
model = BUTD(attention_dim=opt.attention_dim, embed_dim=opt.emb_dim, decoder_dim=opt.decoder_dim,
vocab_size=len(word_map), dropout=opt.dropout)
optimizer = torch.optim.Adam(params=filter(lambda p: p.requires_grad, model.parameters()), lr=opt.learning_rate)
else:
checkpoint = torch.load(opt.checkpoint)
start_epoch = checkpoint['epoch'] + 1
epochs_since_improvement = checkpoint['epochs_since_improvement']
best_cider = checkpoint['cider']
model = checkpoint['model']
optimizer = checkpoint['optimizer']
# Move to GPU, if available
model = model.to(device)
# using multiple GPUs, if available
if torch.cuda.device_count() > 1:
print("Using ", torch.cuda.device_count(), " GPUs!")
model = nn.DataParallel(model)
# Loss function
criterion = nn.CrossEntropyLoss().to(device)
# Custom dataloaders
preprocess = transforms.Compose([
transforms.ToPILImage(),
transforms.Resize([224, 224]),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])
train_loader = torch.utils.data.DataLoader(
FICDataset(opt.data_folder, 'TRAIN', transform=transforms.Compose([preprocess])),
batch_size=opt.batch_size, shuffle=True, num_workers=opt.workers, pin_memory=True)
val_loader = torch.utils.data.DataLoader(
FICDataset(opt.data_folder, 'VAL', transform=transforms.Compose([preprocess])),
batch_size=opt.batch_size, shuffle=False, num_workers=opt.workers, pin_memory=True)
# Epochs
for epoch in range(start_epoch, opt.epochs):
# Decay learning rate if there is no improvement for 5 consecutive epochs, and terminate training after 20
if opt.epochs_since_improvement == 20:
break
if epochs_since_improvement > 0 and epochs_since_improvement % 5 == 0:
adjust_learning_rate(optimizer, 0.8)
# One epoch's training
train(opt=opt, train_loader=train_loader, model=model, criterion=criterion, optimizer=optimizer, epoch=epoch)
# One epoch's validation
eval_result = validate(opt=opt, val_loader=val_loader, model=model, criterion=criterion, vocab=word_map)
recent_cider = eval_result['CIDEr']
# Check if there was an improvement
is_best = recent_cider > best_cider
best_cider = max(recent_cider, best_cider)
if not is_best:
epochs_since_improvement += 1
print("\nEpochs since last improvement: %d\n" % (epochs_since_improvement,))
else:
epochs_since_improvement = 0
# Save checkpoint
to_save = 'vanilla'
model_folder = os.path.join(opt.model_folder, to_save)
if not os.path.exists(opt.model_folder):
os.mkdir(opt.model_folder)
if not os.path.exists(model_folder):
os.mkdir(model_folder)
save_checkpoint(model_folder, epoch, epochs_since_improvement, model, optimizer, best_cider, is_best)
def train(opt, train_loader, model, criterion, optimizer, epoch):
"""
Performs one epoch's training.
:param train_loader: DataLoader for training data
:param model: model
:param criterion: loss layer
:param optimizer: optimizer to update weights (if fine-tuning)
:param epoch: epoch number
"""
model.train() # train mode (dropout and batchnorm is used)
# Batches
loss_total = [] # total loss
for i, (imgs, caps, caplens) in enumerate(train_loader):
imgs = imgs.to(device)
caps = caps.to(device)
caplens = caplens.to(device)
# Forward prop.
scores, caps_sorted, decode_lengths, sort_ind = model(imgs, caps, caplens)
# Since we decoded starting with <start>, the targets are all words after <start>, up to <end>
targets = caps_sorted[:, 1:] # torch.Size([32, 52])
# Remove timesteps that we didn't decode at, or are pads
# pack_padded_sequence is an easy trick to do this
scores = pack_padded_sequence(scores, decode_lengths, batch_first=True)
targets = pack_padded_sequence(targets, decode_lengths, batch_first=True)
# Calculate loss
loss = criterion(scores.data, targets.data)
loss_total.append(loss)
# Back prop.
optimizer.zero_grad()
loss.backward()
# Clip gradients
if opt.grad_clip is not None:
clip_gradient(optimizer, opt.grad_clip)
# Update weights
optimizer.step()
# Print status
if i % opt.print_freq == 0 and i != 0:
print('Epoch: [{}][{}/{}]\t Loss Total {:.4f}'.format(epoch, i, len(train_loader), loss))
def validate(opt, val_loader, model, criterion, vocab):
"""
Performs one epoch's validation.
:param val_loader: DataLoader for validation data.
:param model: model
:param criterion: loss layer
:return: BLEU-4 score
"""
model.eval() # eval mode (no dropout or batchnorm)
references = [] # references (true captions) for calculating BLEU-4 score
hypotheses = [] # hypotheses (predictions)
id_2_word = {x: y for y, x in vocab.items()}
loss_total = [] # total loss
with torch.no_grad():
# Batches
for i, (imgs, caps, caplens) in enumerate(val_loader):
# Move to device, if available
imgs = imgs.to(device)
caps = caps.to(device)
caplens = caplens.to(device)
# Forward prop.
scores, caps_sorted, decode_lengths, sort_ind = model(imgs, caps, caplens)
# Since we decoded starting with <start>, the targets are all words after <start>, up to <end>
targets = caps_sorted[:, 1:]
# Remove timesteps that we didn't decode at, or are pads
# pack_padded_sequence is an easy trick to do this
scores_copy = scores.clone()
scores = pack_padded_sequence(scores, decode_lengths, batch_first=True)
targets = pack_padded_sequence(targets, decode_lengths, batch_first=True)
# Calculate loss
loss = criterion(scores.data, targets.data)
loss_total.append(loss)
# Keep track of metrics
if i % opt.print_freq == 0 and i != 0:
print('Validation: [{}/{}]\t Loss Total {:.4f}'.format(i, len(val_loader), loss))
# Store references (true captions), and hypothesis (prediction) for each image
# If for n images, we have n hypotheses, and references a, b, c... for each image, we need -
# references = [[ref1a, ref1b, ref1c], [ref2a, ref2b], ...], hypotheses = [hyp1, hyp2, ...]
# References
for j in range(caps_sorted.shape[0]):
img_cap = caps_sorted[j].tolist() # [[26056, 10, 25, 2414, 5672, 71, 5, 0, 0, 0, 0]]
img_caption = [id_2_word[w] for w in img_cap if w not in {vocab['<start>'], vocab['<pad>'],
vocab['<end>'], vocab['<unk>']}]
sent = ' '.join(img_caption)
# remove <start> and pads
references.append({'caption': sent})
# Hypotheses
_, preds = torch.max(scores_copy, dim=2) # [100, 33, 26058], [100, 33]
preds = preds.tolist()
for j, p in enumerate(preds):
pre_cap = p[:decode_lengths[j]]
pre_caption = [id_2_word[w] for w in pre_cap if w not in {vocab['<start>'], vocab['<pad>'],
vocab['<end>'], vocab['<unk>']}]
sent = ' '.join(pre_caption)
hypotheses.append({'caption': sent})
# remove pads decode_lengths: [33, 32, 32, 31, 31, 31, 29, 29, 28,]
assert len(references) == len(hypotheses)
scorer = FICScorer()
ids = [str(k) for k in range(len(hypotheses))]
hypo = {}
refe = {}
for k in range(len(hypotheses)):
hypo[str(k)] = [hypotheses[k]]
refe[str(k)] = [references[k]]
final_scores = scorer.score(refe, hypo, ids)
cache_path = os.path.join('eval_result/', 'cache_' + 'butd' + '.json')
json.dump(hypo, open(cache_path, 'w')) # serialize to temporary json file. Sigh, COCO API...
return final_scores
if __name__ == '__main__':
opt = opts.parse_opt()
main(opt)