-
Notifications
You must be signed in to change notification settings - Fork 11
/
train.py
242 lines (196 loc) · 10.1 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
import argparse
import datetime
import gc
import math
import os
import numpy as np
import torch
from torch import nn, optim
from torch.autograd import Variable
from torch.optim import lr_scheduler
from torch.utils.data import DataLoader
from dataloader import VisDialDataset
from encoders import Encoder, LateFusionEncoder
from decoders import Decoder
parser = argparse.ArgumentParser()
VisDialDataset.add_cmdline_args(parser)
LateFusionEncoder.add_cmdline_args(parser)
parser.add_argument_group('Input modalites arguments')
parser.add_argument('-input_type', default='question_dialog_video_audio', choices=['question_only',
'question_dialog',
'question_audio',
'question_image',
'question_video',
'question_caption_image',
'question_dialog_video',
'question_dialog_image',
'question_video_audio',
'question_dialog_video_audio'], help='Specify the inputs')
parser.add_argument_group('Encoder Decoder choice arguments')
parser.add_argument('-encoder', default='lf-ques-im-hist', choices=['lf-ques-im-hist'], help='Encoder to use for training')
parser.add_argument('-concat_history', default=True, help='True for lf encoding')
parser.add_argument('-decoder', default='disc', choices=['disc'], help='Decoder to use for training')
parser.add_argument_group('Optimization related arguments')
parser.add_argument('-num_epochs', default=20, type=int, help='Epochs')
parser.add_argument('-batch_size', default=12, type=int, help='Batch size')
parser.add_argument('-lr', default=1e-3, type=float, help='Learning rate')
parser.add_argument('-lr_decay_rate', default=0.9997592083, type=float, help='Decay for lr')
parser.add_argument('-min_lr', default=5e-5, type=float, help='Minimum learning rate')
parser.add_argument('-weight_init', default='xavier', choices=['xavier', 'kaiming'], help='Weight initialization strategy')
parser.add_argument('-weight_decay', default=0.00075, help='Weight decay for l2 regularization')
parser.add_argument('-overfit', action='store_true', help='Overfit on 5 examples, meant for debugging')
parser.add_argument('-gpuid', default=0, type=int, help='GPU id to use')
parser.add_argument_group('Checkpointing related arguments')
parser.add_argument('-load_path', default='', help='Checkpoint to load path from')
parser.add_argument('-save_path', default='checkpoints/', help='Path to save checkpoints')
parser.add_argument('-save_step', default=2, type=int, help='Save checkpoint after every save_step epochs')
# ----------------------------------------------------------------------------
# input arguments and options
# ----------------------------------------------------------------------------
args = parser.parse_args()
start_time = datetime.datetime.strftime(datetime.datetime.utcnow(), '%d-%b-%Y-%H:%M:%S')
if args.save_path == 'checkpoints/':
args.save_path += start_time
# seed for reproducibility
torch.manual_seed(1234)
# set device and default tensor type
if args.gpuid >= 0:
torch.cuda.manual_seed_all(1234)
torch.cuda.set_device(args.gpuid)
# transfer all options to model
model_args = args
# ----------------------------------------------------------------------------
# read saved model and args
# ----------------------------------------------------------------------------
if args.load_path != '':
components = torch.load(args.load_path)
model_args = components['model_args']
model_args.gpuid = args.gpuid
model_args.batch_size = args.batch_size
# this is required by dataloader
args.img_norm = model_args.img_norm
for arg in vars(args):
print('{:<20}: {}'.format(arg, getattr(args, arg)))
# ----------------------------------------------------------------------------
# loading dataset wrapping with a dataloader
# ----------------------------------------------------------------------------
dataset = VisDialDataset(args, ['train'])
dataloader = DataLoader(dataset,
batch_size=args.batch_size,
shuffle=True,
collate_fn=dataset.collate_fn)
dataset_val = VisDialDataset(args, ['val'])
dataloader_val = DataLoader(dataset_val,
batch_size=args.batch_size,
shuffle=False,
collate_fn=dataset.collate_fn)
# ----------------------------------------------------------------------------
# setting model args
# ----------------------------------------------------------------------------
# transfer some useful args from dataloader to model
for key in {'num_data_points', 'vocab_size', 'max_ques_count',
'max_ques_len', 'max_ans_len'}:
setattr(model_args, key, getattr(dataset, key))
# iterations per epoch
setattr(args, 'iter_per_epoch', math.ceil(dataset.num_data_points['train'] / args.batch_size))
print("{} iter per epoch.".format(args.iter_per_epoch))
# ----------------------------------------------------------------------------
# setup the model
# ----------------------------------------------------------------------------
encoder = Encoder(model_args)
decoder = Decoder(model_args, encoder)
optimizer = optim.Adam(list(encoder.parameters()) + list(decoder.parameters()), lr=args.lr, weight_decay=args.weight_decay)
criterion = nn.CrossEntropyLoss()
scheduler = lr_scheduler.StepLR(optimizer, step_size=1, gamma=args.lr_decay_rate)
if args.load_path != '':
encoder.load_state_dict(components['encoder'])
decoder.load_state_dict(components['decoder'])
print("Loaded model from {}".format(args.load_path))
print("Encoder: {}".format(args.encoder))
print("Decoder: {}".format(args.decoder))
if args.gpuid >= 0:
encoder = encoder.cuda()
decoder = decoder.cuda()
criterion = criterion.cuda()
# ----------------------------------------------------------------------------
# training
# ----------------------------------------------------------------------------
encoder.train()
decoder.train()
os.makedirs(args.save_path, exist_ok=True)
running_loss = 0.0
train_begin = datetime.datetime.utcnow()
print("Training start time: {}".format(datetime.datetime.strftime(train_begin, '%d-%b-%Y-%H:%M:%S')))
log_loss = []
for epoch in range(1, model_args.num_epochs + 1):
for i, batch in enumerate(dataloader):
optimizer.zero_grad()
for key in batch:
if not isinstance(batch[key], list):
batch[key] = Variable(batch[key])
if args.gpuid >= 0:
batch[key] = batch[key].cuda()
# --------------------------------------------------------------------
# forward-backward pass and optimizer step
# --------------------------------------------------------------------
enc_out = encoder(batch)
dec_out = decoder(enc_out, batch)
cur_loss = criterion(dec_out, batch['ans_ind'].view(-1))
cur_loss.backward()
optimizer.step()
gc.collect()
# --------------------------------------------------------------------
# update running loss and decay learning rates
# --------------------------------------------------------------------
train_loss = cur_loss.data[0]
if running_loss > 0.0:
running_loss = 0.95 * running_loss + 0.05 * cur_loss.data[0]
else:
running_loss = cur_loss.data[0]
if optimizer.param_groups[0]['lr'] > args.min_lr:
scheduler.step()
# --------------------------------------------------------------------
# print after every few iterations
# --------------------------------------------------------------------
if i % 100 == 0:
validation_losses = []
for _, val_batch in enumerate(dataloader_val):
for key in val_batch:
if not isinstance(val_batch[key], list):
val_batch[key] = Variable(val_batch[key])
if args.gpuid >= 0:
val_batch[key] = val_batch[key].cuda()
enc_out = encoder(val_batch)
dec_out = decoder(enc_out, val_batch)
cur_loss = criterion(dec_out, val_batch['ans_ind'].view(-1))
validation_losses.append(cur_loss.data[0])
validation_loss = np.mean(validation_losses)
iteration = (epoch - 1) * args.iter_per_epoch + i
log_loss.append((epoch,
iteration,
running_loss,
train_loss,
validation_loss,
optimizer.param_groups[0]['lr']))
# print current time, running average, learning rate, iteration, epoch
print("[{}][Epoch: {:3d}][Iter: {:6d}][Loss: {:6f}][val loss: {:6f}][lr: {:7f}]".format(
datetime.datetime.utcnow() - train_begin, epoch,
iteration, running_loss, validation_loss,
optimizer.param_groups[0]['lr']))
# ------------------------------------------------------------------------
# save checkpoints and final model
# ------------------------------------------------------------------------
if epoch % args.save_step == 0:
torch.save({
'encoder': encoder.state_dict(),
'decoder': decoder.state_dict(),
'optimizer': optimizer.state_dict(),
'model_args': encoder.args
}, os.path.join(args.save_path, 'model_epoch_{}.pth'.format(epoch)))
torch.save({
'encoder': encoder.state_dict(),
'decoder': decoder.state_dict(),
'optimizer': optimizer.state_dict(),
'model_args': encoder.args
}, os.path.join(args.save_path, 'model_final.pth'))
np.save(os.path.join(args.save_path, 'log_loss'), log_loss)