-
Notifications
You must be signed in to change notification settings - Fork 5
/
train.py
268 lines (225 loc) · 11.7 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
import torch
import torch.nn as nn
import time
from data_handling import get_clotho_loader, get_test_data_loader
from model import TransformerModel # , RNNModel, RNNModelSmall
import itertools
import numpy as np
import os
import sys
import logging
import csv
from util import get_file_list, get_padding, print_hparams, greedy_decode, \
calculate_bleu, calculate_spider, LabelSmoothingLoss, beam_search, align_word_embedding, gen_str
from hparams import hparams
from torch.utils.tensorboard import SummaryWriter
import argparse
hp = hparams()
parser = argparse.ArgumentParser(description='hparams for model')
device = torch.device(hp.device)
np.random.seed(hp.seed)
torch.manual_seed(hp.seed)
def train():
model.train()
total_loss_text = 0.
start_time = time.time()
batch = 0
for src, tgt, tgt_len, ref in training_data:
src = src.to(device)
tgt = tgt.to(device)
tgt_pad_mask = get_padding(tgt, tgt_len)
tgt_in = tgt[:, :-1]
tgt_pad_mask = tgt_pad_mask[:, :-1]
tgt_y = tgt[:, 1:]
optimizer.zero_grad()
output = model(src, tgt_in, target_padding_mask=tgt_pad_mask)
loss_text = criterion(output.contiguous().view(-1, hp.ntoken), tgt_y.transpose(0, 1).contiguous().view(-1))
loss = loss_text
loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), hp.clip_grad)
optimizer.step()
total_loss_text += loss_text.item()
writer.add_scalar('Loss/train-text', loss_text.item(), (epoch - 1) * len(training_data) + batch)
batch += 1
if batch % hp.log_interval == 0 and batch > 0:
mean_text_loss = total_loss_text / hp.log_interval
elapsed = time.time() - start_time
current_lr = [param_group['lr'] for param_group in optimizer.param_groups][0]
logging.info('| epoch {:3d} | {:5d}/{:5d} batches | lr {:02.2e} | ms/batch {:5.2f} | '
'loss-text {:5.4f}'.format(
epoch, batch, len(training_data), current_lr,
elapsed * 1000 / hp.log_interval, mean_text_loss))
total_loss_text = 0
start_time = time.time()
def eval_all(evaluation_data, max_len=30, eos_ind=9, word_dict_pickle_path=None):
model.eval()
with torch.no_grad():
output_sentence_all = []
ref_all = []
for src, tgt, _, ref in evaluation_data:
src = src.to(device)
output = greedy_decode(model, src, max_len=max_len)
output_sentence_ind_batch = []
for i in range(output.size()[0]):
output_sentence_ind = []
for j in range(1, output.size(1)):
sym = output[i, j]
if sym == eos_ind: break
output_sentence_ind.append(sym.item())
output_sentence_ind_batch.append(output_sentence_ind)
output_sentence_all.extend(output_sentence_ind_batch)
ref_all.extend(ref)
score, output_str, ref_str = calculate_spider(output_sentence_all, ref_all, word_dict_pickle_path)
loss_mean = score
writer.add_scalar(f'Loss/eval_greddy', loss_mean, epoch)
msg = f'eval_greddy SPIDEr: {loss_mean:2.4f}'
logging.info(msg)
def eval_with_beam(evaluation_data, max_len=30, eos_ind=9, word_dict_pickle_path=None, beam_size=3):
model.eval()
with torch.no_grad():
output_sentence_all = []
ref_all = []
for src, tgt, _, ref in evaluation_data:
src = src.to(device)
output = beam_search(model, src, max_len, start_symbol_ind=0, beam_size=beam_size)
output_sentence_ind_batch = []
for single_sample in output:
output_sentence_ind = []
for sym in single_sample:
if sym == eos_ind: break
output_sentence_ind.append(sym.item())
output_sentence_ind_batch.append(output_sentence_ind)
output_sentence_all.extend(output_sentence_ind_batch)
ref_all.extend(ref)
score, output_str, ref_str = calculate_spider(output_sentence_all, ref_all, word_dict_pickle_path)
loss_mean = score
writer.add_scalar(f'Loss/eval_beam', loss_mean, epoch)
msg = f'eval_beam_{beam_size} SPIDEr: {loss_mean:2.4f}'
logging.info(msg)
def test_with_beam(test_data, max_len=30, eos_ind=9, beam_size=3):
model.eval()
with torch.no_grad():
with open("test_out.csv", "w") as f:
writer = csv.writer(f)
writer.writerow(['file_name', 'caption_predicted'])
for src, filename in test_data:
src = src.to(device)
output = beam_search(model, src, max_len, start_symbol_ind=0, beam_size=beam_size)
output_sentence_ind_batch = []
for single_sample in output:
output_sentence_ind = []
for sym in single_sample:
if sym == eos_ind: break
output_sentence_ind.append(sym.item())
output_sentence_ind_batch.append(output_sentence_ind)
out_str = gen_str(output_sentence_ind_batch, hp.word_dict_pickle_path)
for caption, fn in zip(out_str, filename):
writer.writerow(['{}.wav'.format(fn), caption])
if __name__ == '__main__':
parser.add_argument('--device', type=str, default=hp.device)
parser.add_argument('--nlayers', type=int, default=hp.nlayers)
parser.add_argument('--nhead', type=int, default=hp.nhead)
parser.add_argument('--nhid', type=int, default=hp.nhid)
parser.add_argument('--training_epochs', type=int, default=hp.training_epochs)
parser.add_argument('--lr', type=float, default=hp.lr)
parser.add_argument('--scheduler_decay', type=float, default=hp.scheduler_decay)
parser.add_argument('--load_pretrain_cnn', action='store_true')
parser.add_argument('--freeze_cnn', action='store_true')
parser.add_argument('--load_pretrain_emb', action='store_true')
parser.add_argument('--load_pretrain_model', action='store_true')
parser.add_argument('--spec_augmentation', action='store_true')
parser.add_argument('--label_smoothing', action='store_true')
parser.add_argument('--name', type=str, default=hp.name)
parser.add_argument('--pretrain_emb_path', type=str, default=hp.pretrain_emb_path)
parser.add_argument('--pretrain_cnn_path', type=str, default=hp.pretrain_cnn_path)
parser.add_argument('--pretrain_model_path', type=str, default=hp.pretrain_model_path)
args = parser.parse_args()
for k, v in vars(args).items():
setattr(hp, k, v)
args = parser.parse_args()
pretrain_emb = align_word_embedding(hp.word_dict_pickle_path, hp.pretrain_emb_path, hp.ntoken,
hp.nhid) if hp.load_pretrain_emb else None
pretrain_cnn = torch.load(hp.pretrain_cnn_path) if hp.load_pretrain_cnn else None
model = TransformerModel(hp.ntoken, hp.ninp, hp.nhead, hp.nhid, hp.nlayers, hp.batch_size, dropout=0.2,
pretrain_cnn=pretrain_cnn, pretrain_emb=pretrain_emb, freeze_cnn=hp.freeze_cnn).to(device)
if hp.load_pretrain_model:
model.load_state_dict(torch.load(hp.pretrain_model_path))
optimizer = torch.optim.Adam(filter(lambda p: p.requires_grad, model.parameters()), lr=hp.lr, weight_decay=1e-6)
scheduler = torch.optim.lr_scheduler.ExponentialLR(optimizer, hp.scheduler_decay)
if hp.label_smoothing:
criterion = LabelSmoothingLoss(hp.ntoken, smoothing=0.1)
else:
criterion = nn.CrossEntropyLoss(ignore_index=hp.ntoken - 1)
now_time = str(time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime(time.time())))
log_dir = 'models/{name}'.format(name=hp.name)
writer = SummaryWriter(log_dir=log_dir)
log_path = os.path.join(log_dir, 'train.log')
logging.basicConfig(level=logging.DEBUG,
format=
'%(asctime)s - %(levelname)s: %(message)s',
handlers=[
logging.FileHandler(log_path),
logging.StreamHandler(sys.stdout)]
)
data_dir = hp.data_dir
eval_data_dir = hp.eval_data_dir
train_data_dir = hp.train_data_dir
word_dict_pickle_path = hp.word_dict_pickle_path
word_freq_pickle_path = hp.word_freq_pickle_path
test_data_dir = hp.test_data_dir
training_data = get_clotho_loader(data_dir=data_dir, split='development',
input_field_name='features',
output_field_name='words_ind',
load_into_memory=False,
batch_size=hp.batch_size,
nb_t_steps_pad='max',
num_workers=4, return_reference=True, augment=hp.spec_augmentation)
evaluation_beam = get_clotho_loader(data_dir=data_dir, split='evaluation',
input_field_name='features',
output_field_name='words_ind',
load_into_memory=False,
batch_size=32,
nb_t_steps_pad='max',
shuffle=False,
return_reference=True)
test_data = get_test_data_loader(data_dir=test_data_dir,
batch_size=hp.batch_size * 2,
nb_t_steps_pad='max',
shuffle=False,
drop_last=False,
input_pad_at='start',
num_workers=8)
logging.info(str(model))
logging.info(str(print_hparams(hp)))
logging.info('Data loaded!')
logging.info('Data size: ' + str(len(training_data)))
logging.info('Total Model parameters: ' + str(sum(p.numel() for p in model.parameters() if p.requires_grad)))
epoch = 1
if hp.mode == 'train':
while epoch < hp.training_epochs + 1:
epoch_start_time = time.time()
train()
torch.save(model.state_dict(), '{log_dir}/{num_epoch}.pt'.format(log_dir=log_dir, num_epoch=epoch))
scheduler.step(epoch)
eval_all(evaluation_beam, word_dict_pickle_path=word_dict_pickle_path)
eval_with_beam(evaluation_beam, max_len=30, eos_ind=9, word_dict_pickle_path=word_dict_pickle_path,
beam_size=2)
eval_with_beam(evaluation_beam, max_len=30, eos_ind=9, word_dict_pickle_path=word_dict_pickle_path,
beam_size=3)
eval_with_beam(evaluation_beam, max_len=30, eos_ind=9, word_dict_pickle_path=word_dict_pickle_path,
beam_size=4)
epoch += 1
if hp.mode == 'eval':
# Evaluation model score
model.load_state_dict(torch.load("./models/best.pt"))
eval_all(evaluation_beam, word_dict_pickle_path=word_dict_pickle_path)
eval_with_beam(evaluation_beam, max_len=30, eos_ind=9, word_dict_pickle_path=word_dict_pickle_path,
beam_size=2)
eval_with_beam(evaluation_beam, max_len=30, eos_ind=9, word_dict_pickle_path=word_dict_pickle_path,
beam_size=3)
eval_with_beam(evaluation_beam, max_len=30, eos_ind=9, word_dict_pickle_path=word_dict_pickle_path,
beam_size=4)
elif hp.mode == 'test':
# Generate caption(in test_out.csv)
model.load_state_dict(torch.load("./models/best.pt"))
test_with_beam(test_data, beam_size=3)