-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathevaluate_gnmt.py
260 lines (211 loc) · 11 KB
/
evaluate_gnmt.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
"""
Google Neural Machine Translation
=================================
@article{wu2016google,
title={Google's neural machine translation system:
Bridging the gap between human and machine translation},
author={Wu, Yonghui and Schuster, Mike and Chen, Zhifeng and Le, Quoc V and
Norouzi, Mohammad and Macherey, Wolfgang and Krikun, Maxim and Cao, Yuan and Gao, Qin and
Macherey, Klaus and others},
journal={arXiv preprint arXiv:1609.08144},
year={2016}
}
"""
from absl import app, flags
from absl.flags import FLAGS
import random
import os
import sys
import numpy as np
import mxnet as mx
from mxnet import gluon
import gluonnlp as nlp
from gluonnlp.model.translation import NMTModel
from models.captioning.gnmt import get_gnmt_encoder_decoder
from utils.translation import BeamSearchTranslator
from utils.captioning import get_dataloaders, write_sentences, read_sentences
from dataset import TennisSet
from models.vision.definitions import FrameModel
from utils.layers import TimeDistributed
from gluoncv.model_zoo import get_model
from mxnet.gluon.data.vision import transforms
from nlgeval import NLGEval
np.random.seed(100)
random.seed(100)
mx.random.seed(10000)
flags.DEFINE_string('model_id', '0000',
'model identification string')
flags.DEFINE_integer('num_hidden', 128,
'Dimension of the states')
flags.DEFINE_integer('emb_size', 100,
'Dimension of the embedding vectors')
flags.DEFINE_float('dropout', 0.2,
'dropout applied to layers (0 = no dropout)')
flags.DEFINE_integer('num_layers', 2,
'Number of layers in the encoder and decoder')
flags.DEFINE_integer('num_bi_layers', 1,
'Number of bidirectional layers in the encoder and decoder')
flags.DEFINE_string('cell_type', 'gru',
'gru or lstm')
flags.DEFINE_integer('batch_size', 128,
'Batch size for detection: higher faster, but more memory intensive.')
flags.DEFINE_integer('beam_size', 4,
'Beam size.')
flags.DEFINE_float('lp_alpha', 1.0,
'Alpha used in calculating the length penalty')
flags.DEFINE_integer('lp_k', 5,
'K used in calculating the length penalty')
flags.DEFINE_integer('tgt_max_len', 50,
'Maximum length of the target sentence')
flags.DEFINE_integer('num_gpus', 1,
'Number of GPUs to use')
flags.DEFINE_string('backbone', 'DenseNet121',
'Backbone CNN name')
flags.DEFINE_string('backbone_from_id', None,
'Load a backbone model from a model_id, used for Temporal Pooling with fine-tuned CNN')
flags.DEFINE_bool('freeze_backbone', False,
'Freeze the backbone model')
flags.DEFINE_integer('data_shape', 512,
'The width and height for the input image to be cropped to.')
flags.DEFINE_integer('every', 1,
'Use only every this many frames: [train, val, test] splits')
flags.DEFINE_string('feats_model', None,
'load CNN features as npy files from this model')
flags.DEFINE_string('emb_file', 'embeddings-ex.txt',
'the word embedding file generated by train_embeddings.py')
def main(_argv):
if FLAGS.num_gpus > 0: # only supports 1 GPU
ctx = mx.gpu()
else:
ctx = mx.cpu()
key_flags = FLAGS.get_key_flags_for_module(sys.argv[0])
print('\n'.join(f.serialize() for f in key_flags))
# are we using features or do we include the CNN?
if FLAGS.feats_model is None:
backbone_net = get_model(FLAGS.backbone, pretrained=True, ctx=ctx).features
cnn_model = FrameModel(backbone_net, 11) # hardcoded the number of classes
if FLAGS.backbone_from_id:
if os.path.exists(os.path.join('models', 'vision', 'experiments', FLAGS.backbone_from_id)):
files = os.listdir(os.path.join('models', 'vision', 'experiments', FLAGS.backbone_from_id))
files = [f for f in files if f[-7:] == '.params']
if len(files) > 0:
files = sorted(files, reverse=True) # put latest model first
model_name = files[0]
cnn_model.load_parameters(os.path.join('models', 'vision', 'experiments', FLAGS.backbone_from_id, model_name), ctx=ctx)
print('Loaded backbone params: {}'.format(os.path.join('models', 'vision', 'experiments', FLAGS.backbone_from_id, model_name)))
else:
raise FileNotFoundError('{}'.format(os.path.join('models', 'vision', 'experiments', FLAGS.backbone_from_id)))
if FLAGS.freeze_backbone:
for param in cnn_model.collect_params().values():
param.grad_req = 'null'
cnn_model = TimeDistributed(cnn_model.backbone)
src_embed = cnn_model
transform_test = transforms.Compose([
transforms.Resize(FLAGS.data_shape + 32),
transforms.CenterCrop(FLAGS.data_shape),
transforms.ToTensor(),
transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))
])
else:
from mxnet.gluon import nn # need to do this to force no use of Embedding on src
src_embed = nn.HybridSequential(prefix='src_embed_')
with src_embed.name_scope():
src_embed.add(nn.Dropout(rate=0.0))
transform_train = None
transform_test = None
# setup the data
data_train = TennisSet(split='train', transform=transform_train, captions=True, max_cap_len=FLAGS.tgt_max_len,
every=FLAGS.every, feats_model=FLAGS.feats_model)
data_val = TennisSet(split='val', transform=transform_test, captions=True, vocab=data_train.vocab,
every=FLAGS.every, inference=True, feats_model=FLAGS.feats_model)
data_test = TennisSet(split='test', transform=transform_test, captions=True, vocab=data_train.vocab,
every=FLAGS.every, inference=True, feats_model=FLAGS.feats_model)
test_tgt_sentences = data_test.get_captions(split=True)
write_sentences(test_tgt_sentences, os.path.join('models', 'captioning', 'experiments', FLAGS.model_id, 'test_gt.txt'))
# load embeddings for tgt_embed
if FLAGS.emb_file:
word_embs = nlp.embedding.TokenEmbedding.from_file(file_path=os.path.join('data', FLAGS.emb_file))
data_test.vocab.set_embedding(word_embs)
input_dim, output_dim = data_test.vocab.embedding.idx_to_vec.shape
tgt_embed = gluon.nn.Embedding(input_dim, output_dim)
tgt_embed.initialize(ctx=ctx)
tgt_embed.weight.set_data(data_test.vocab.embedding.idx_to_vec)
else:
tgt_embed = None
# setup the model
encoder, decoder = get_gnmt_encoder_decoder(cell_type=FLAGS.cell_type,
hidden_size=FLAGS.num_hidden,
dropout=FLAGS.dropout,
num_layers=FLAGS.num_layers,
num_bi_layers=FLAGS.num_bi_layers)
model = NMTModel(src_vocab=None, tgt_vocab=data_test.vocab, encoder=encoder, decoder=decoder,
embed_size=FLAGS.emb_size, prefix='gnmt_', src_embed=src_embed, tgt_embed=tgt_embed)
model.initialize(init=mx.init.Uniform(0.1), ctx=ctx)
static_alloc = True
model.hybridize(static_alloc=static_alloc)
print(model)
if os.path.exists(os.path.join('models', 'captioning', 'experiments', FLAGS.model_id)):
files = os.listdir(os.path.join('models', 'captioning', 'experiments', FLAGS.model_id))
files = [f for f in files if f[-7:] == '.params']
if len(files) > 0:
files = sorted(files, reverse=True) # put latest model first
model_name = files[0]
if model_name == 'valid_best.params':
model_name = files[1]
model.load_parameters(os.path.join('models', 'captioning', 'experiments', FLAGS.model_id, model_name), ctx=ctx)
print('Loaded model params: {}'.format(os.path.join('models', 'captioning', 'experiments', FLAGS.model_id, model_name)))
# setup the beam search
translator = BeamSearchTranslator(model=model, beam_size=FLAGS.beam_size,
scorer=nlp.model.BeamSearchScorer(alpha=FLAGS.lp_alpha, K=FLAGS.lp_k),
max_length=FLAGS.tgt_max_len + 100)
print('Use beam_size={}, alpha={}, K={}'.format(FLAGS.beam_size, FLAGS.lp_alpha, FLAGS.lp_k))
# run the training
train_data_loader, val_data_loader, test_data_loader = get_dataloaders(data_train, data_val, data_test)
# load and evaluate the best model
if os.path.exists(os.path.join('models', 'captioning', 'experiments', FLAGS.model_id, 'valid_best.params')):
model.load_parameters(os.path.join('models', 'captioning', 'experiments', FLAGS.model_id, 'valid_best.params'))
preds_path = os.path.join('models', 'captioning', 'experiments', FLAGS.model_id, 'best_test_out.txt')
if not os.path.exists(preds_path):
_, test_translation_out = evaluate(test_data_loader, model, translator, data_train, ctx)
else:
test_translation_out = read_sentences(preds_path)
str_ = ''
nlgeval = NLGEval()
metrics_dict = nlgeval.compute_metrics([[' '.join(sent) for sent in test_tgt_sentences]],
[' '.join(sent) for sent in test_translation_out])
for k, v in metrics_dict.items():
str_ += ', test ' + k + '={:.4f}'.format(float(v))
print(str_)
write_sentences(test_translation_out, preds_path)
def evaluate(data_loader, model, translator, data_train, ctx):
"""Evaluate
"""
translation_out = []
all_inst_ids = []
for batch_id, (src_seq, tgt_seq, src_valid_length, tgt_valid_length, inst_ids) in enumerate(data_loader):
# Put on ctxs
src_seq = src_seq.as_in_context(ctx)
tgt_seq = tgt_seq.as_in_context(ctx)
src_valid_length = src_valid_length.as_in_context(ctx)
tgt_valid_length = tgt_valid_length.as_in_context(ctx)
# Calculating Loss
out, _ = model(src_seq, tgt_seq[:, :-1], src_valid_length, tgt_valid_length - 1)
all_inst_ids.extend(inst_ids.asnumpy().astype(np.int32).tolist())
# Translate
samples, _, sample_valid_length =\
translator.translate(src_seq=src_seq, src_valid_length=src_valid_length)
max_score_sample = samples[:, 0, :].asnumpy()
sample_valid_length = sample_valid_length[:, 0].asnumpy()
for i in range(max_score_sample.shape[0]):
translation_out.append(
[data_train.vocab.idx_to_token[ele] for ele in
max_score_sample[i][1:(sample_valid_length[i] - 1)]])
real_translation_out = [None for _ in range(len(all_inst_ids))]
for ind, sentence in zip(all_inst_ids, translation_out):
real_translation_out[ind] = sentence
return real_translation_out
if __name__ == '__main__':
try:
app.run(main)
except SystemExit:
pass