forked from ucfnlp/multidoc_summarization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecode.py
356 lines (301 loc) · 16.5 KB
/
decode.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# Copyright 2016 The TensorFlow Authors. All Rights Reserved.
# Modifications Copyright 2017 Abigail See
# Modifications made 2018 by Logan Lebanoff
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""This file contains code to run beam search decoding, including running ROUGE evaluation and producing JSON datafiles for the in-browser attention visualizer, which can be found here https://github.com/abisee/attn_vis"""
import glob
import os
import time
import cPickle
import tensorflow as tf
import beam_search
import data
import json
import pyrouge
import util
from sumy.nlp.tokenizers import Tokenizer
from tqdm import tqdm
from absl import flags
from absl import logging
import logging as log
import importance_features
FLAGS = flags.FLAGS
SECS_UNTIL_NEW_CKPT = 60 # max number of seconds before loading new checkpoint
threshold = 0.5
prob_to_keep = 0.33
class BeamSearchDecoder(object):
"""Beam search decoder."""
def __init__(self, model, batcher, vocab):
"""Initialize decoder.
Args:
model: a Seq2SeqAttentionModel object.
batcher: a Batcher object.
vocab: Vocabulary object
"""
self._model = model
self._model.build_graph()
self._batcher = batcher
self._vocab = vocab
self._saver = tf.train.Saver() # we use this to load checkpoints for decoding
self._sess = tf.Session(config=util.get_config())
# Load an initial checkpoint to use for decoding
ckpt_path = util.load_ckpt(self._saver, self._sess)
if FLAGS.single_pass:
# Make a descriptive decode directory name
ckpt_name = "ckpt-" + ckpt_path.split('-')[-1] # this is something of the form "ckpt-123456"
self._decode_dir = os.path.join(FLAGS.log_root, get_decode_dir_name(ckpt_name))
# if os.path.exists(self._decode_dir):
# raise Exception("single_pass decode directory %s should not already exist" % self._decode_dir)
else: # Generic decode dir name
self._decode_dir = os.path.join(FLAGS.log_root, "decode")
# Make the decode dir if necessary
if not os.path.exists(self._decode_dir): os.makedirs(self._decode_dir)
if FLAGS.single_pass:
# Make the dirs to contain output written in the correct format for pyrouge
self._rouge_ref_dir = os.path.join(self._decode_dir, "reference")
if not os.path.exists(self._rouge_ref_dir): os.mkdir(self._rouge_ref_dir)
self._rouge_dec_dir = os.path.join(self._decode_dir, "decoded")
if not os.path.exists(self._rouge_dec_dir): os.mkdir(self._rouge_dec_dir)
def decode(self):
"""Decode examples until data is exhausted (if FLAGS.single_pass) and return, or decode indefinitely, loading latest checkpoint at regular intervals"""
t0 = time.time()
counter = 0
while True:
batch = self._batcher.next_batch() # 1 example repeated across batch
if batch is None: # finished decoding dataset in single_pass mode
assert FLAGS.single_pass, "Dataset exhausted, but we are not in single_pass mode"
logging.info("Decoder has finished reading dataset for single_pass.")
logging.info("Output has been saved in %s and %s.", self._rouge_ref_dir, self._rouge_dec_dir)
if len(os.listdir(self._rouge_ref_dir)) != 0:
logging.info("Now starting ROUGE eval...")
results_dict = rouge_eval(self._rouge_ref_dir, self._rouge_dec_dir)
rouge_log(results_dict, self._decode_dir)
return
original_article = batch.original_articles[0] # string
original_abstract = batch.original_abstracts[0] # string
all_original_abstract_sents = batch.all_original_abstracts_sents[0]
article_withunks = data.show_art_oovs(original_article, self._vocab) # string
abstract_withunks = data.show_abs_oovs(original_abstract, self._vocab, (batch.art_oovs[0] if FLAGS.pointer_gen else None)) # string
# Run beam search to get best Hypothesis
best_hyp = beam_search.run_beam_search(self._sess, self._model, self._vocab, batch, counter, self._batcher._hps)
# Extract the output ids from the hypothesis and convert back to words
output_ids = [int(t) for t in best_hyp.tokens[1:]]
decoded_words = data.outputids2words(output_ids, self._vocab, (batch.art_oovs[0] if FLAGS.pointer_gen else None))
# Remove the [STOP] token from decoded_words, if necessary
try:
fst_stop_idx = decoded_words.index(data.STOP_DECODING) # index of the (first) [STOP] symbol
decoded_words = decoded_words[:fst_stop_idx]
except ValueError:
decoded_words = decoded_words
decoded_output = ' '.join(decoded_words) # single string
if FLAGS.single_pass:
self.write_for_rouge(all_original_abstract_sents, decoded_words, counter) # write ref summary and decoded summary to file, to eval with pyrouge later
# self.write_for_attnvis(article_withunks, abstract_withunks, decoded_words, best_hyp.attn_dists, best_hyp.p_gens) # write info to .json file for visualization tool
counter += 1 # this is how many examples we've decoded
else:
print_results(article_withunks, abstract_withunks, decoded_output) # log output to screen
self.write_for_attnvis(article_withunks, abstract_withunks, decoded_words, best_hyp.attn_dists, best_hyp.p_gens) # write info to .json file for visualization tool
# Check if SECS_UNTIL_NEW_CKPT has elapsed; if so return so we can load a new checkpoint
t1 = time.time()
if t1-t0 > SECS_UNTIL_NEW_CKPT:
logging.info('We\'ve been decoding with same checkpoint for %i seconds. Time to load new checkpoint', t1-t0)
_ = util.load_ckpt(self._saver, self._sess)
t0 = time.time()
def write_for_rouge(self, all_reference_sents, decoded_words, ex_index):
"""Write output to file in correct format for eval with pyrouge. This is called in single_pass mode.
Args:
all_reference_sents: list of list of strings
decoded_words: list of strings
ex_index: int, the index with which to label the files
"""
# First, divide decoded output into sentences
decoded_sents = []
while len(decoded_words) > 0:
try:
fst_period_idx = decoded_words.index(".")
except ValueError: # there is text remaining that doesn't end in "."
fst_period_idx = len(decoded_words)
sent = decoded_words[:fst_period_idx+1] # sentence up to and including the period
decoded_words = decoded_words[fst_period_idx+1:] # everything else
decoded_sents.append(' '.join(sent))
# pyrouge calls a perl script that puts the data into HTML files.
# Therefore we need to make our output HTML safe.
decoded_sents = [make_html_safe(w) for w in decoded_sents]
all_reference_sents = [[make_html_safe(w) for w in abstract] for abstract in all_reference_sents]
# Write to file
decoded_file = os.path.join(self._rouge_dec_dir, "%06d_decoded.txt" % ex_index)
for abs_idx, abs in enumerate(all_reference_sents):
ref_file = os.path.join(self._rouge_ref_dir, "%06d_reference.%s.txt" % (
ex_index, chr(ord('A') + abs_idx)))
with open(ref_file, "w") as f:
for idx,sent in enumerate(abs):
f.write(sent+"\n")
# f.write(sent) if idx==len(abs)-1 else f.write(sent+"\n")
with open(decoded_file, "w") as f:
for idx,sent in enumerate(decoded_sents):
f.write(sent+"\n")
# f.write(sent) if idx==len(decoded_sents)-1 else f.write(sent+"\n")
logging.info("Wrote example %i to file" % ex_index)
def write_for_attnvis(self, article, abstract, decoded_words, attn_dists, p_gens, ex_index=None):
"""Write some data to json file, which can be read into the in-browser attention visualizer tool:
https://github.com/abisee/attn_vis
Args:
article: The original article string.
abstract: The human (correct) abstract string.
attn_dists: List of arrays; the attention distributions.
decoded_words: List of strings; the words of the generated summary.
p_gens: List of scalars; the p_gen values. If not running in pointer-generator mode, list of None.
"""
article_lst = article.split() # list of words
decoded_lst = decoded_words # list of decoded words
to_write = {
'article_lst': [make_html_safe(t) for t in article_lst],
'decoded_lst': [make_html_safe(t) for t in decoded_lst],
'abstract_str': make_html_safe(abstract),
'attn_dists': attn_dists
}
if FLAGS.pointer_gen:
to_write['p_gens'] = p_gens
if ex_index is None:
output_fname = os.path.join(self._decode_dir, 'attn_vis', 'attn_vis_data.json')
else:
output_fname = os.path.join(self._decode_dir, 'attn_vis', 'attn_vis_data%06d.json' % ex_index)
with open(output_fname, 'w') as output_file:
json.dump(to_write, output_file)
logging.info('Wrote visualization data to %s', output_fname)
def calc_importance_features(self, data_path, hps, model_save_path, docs_desired):
"""Calculate sentence-level features and save as a dataset"""
data_path_filter_name = os.path.basename(data_path)
if 'train' in data_path_filter_name:
data_split = 'train'
elif 'val' in data_path_filter_name:
data_split = 'val'
elif 'test' in data_path_filter_name:
data_split = 'test'
else:
data_split = 'feats'
if 'cnn-dailymail' in data_path:
inst_per_file = 1000
else:
inst_per_file = 1
filelist = glob.glob(data_path)
num_documents_desired = docs_desired
pbar = tqdm(initial=0, total=num_documents_desired)
instances = []
sentences = []
counter = 0
doc_counter = 0
file_counter = 0
while True:
batch = self._batcher.next_batch() # 1 example repeated across batch
if doc_counter >= num_documents_desired:
save_path = os.path.join(model_save_path, data_split + '_%06d'%file_counter)
with open(save_path, 'wb') as f:
cPickle.dump(instances, f)
print('Saved features at %s' % save_path)
return
if batch is None: # finished decoding dataset in single_pass mode
raise Exception('We havent reached the num docs desired (%d), instead we reached (%d)' % (num_documents_desired, doc_counter))
batch_enc_states, _ = self._model.run_encoder(self._sess, batch)
for batch_idx, enc_states in enumerate(batch_enc_states):
art_oovs = batch.art_oovs[batch_idx]
all_original_abstracts_sents = batch.all_original_abstracts_sents[batch_idx]
tokenizer = Tokenizer('english')
# List of lists of words
enc_sentences, enc_tokens = batch.tokenized_sents[batch_idx], batch.word_ids_sents[batch_idx]
enc_sent_indices = importance_features.get_sent_indices(enc_sentences, batch.doc_indices[batch_idx])
enc_sentences_str = [' '.join(sent) for sent in enc_sentences]
sent_representations_separate = importance_features.get_separate_enc_states(self._model, self._sess, enc_sentences, self._vocab, hps)
sent_indices = enc_sent_indices
sent_reps = importance_features.get_importance_features_for_article(
enc_states, enc_sentences, sent_indices, tokenizer, sent_representations_separate)
y, y_hat = importance_features.get_ROUGE_Ls(art_oovs, all_original_abstracts_sents, self._vocab, enc_tokens)
binary_y = importance_features.get_best_ROUGE_L_for_each_abs_sent(art_oovs, all_original_abstracts_sents, self._vocab, enc_tokens)
for rep_idx, rep in enumerate(sent_reps):
rep.y = y[rep_idx]
rep.binary_y = binary_y[rep_idx]
for rep_idx, rep in enumerate(sent_reps):
# Keep all sentences with importance above threshold. All others will be kept with a probability of prob_to_keep
if FLAGS.importance_fn == 'svr':
instances.append(rep)
sentences.append(sentences)
counter += 1 # this is how many examples we've decoded
doc_counter += len(batch_enc_states)
pbar.update(len(batch_enc_states))
def print_results(article, abstract, decoded_output):
"""Prints the article, the reference summmary and the decoded summary to screen"""
print ""
logging.info('ARTICLE: %s', article)
logging.info('REFERENCE SUMMARY: %s', abstract)
logging.info('GENERATED SUMMARY: %s', decoded_output)
print ""
def make_html_safe(s):
"""Replace any angled brackets in string s to avoid interfering with HTML attention visualizer."""
s.replace("<", "<")
s.replace(">", ">")
return s
def rouge_eval(ref_dir, dec_dir):
"""Evaluate the files in ref_dir and dec_dir with pyrouge, returning results_dict"""
r = pyrouge.Rouge155()
# r.model_filename_pattern = '#ID#_reference.txt'
r.model_filename_pattern = '#ID#_reference.[A-Z].txt'
r.system_filename_pattern = '(\d+)_decoded.txt'
r.model_dir = ref_dir
r.system_dir = dec_dir
log.getLogger('global').setLevel(log.WARNING) # silence pyrouge logging
rouge_args = ['-e', r._data_dir,
'-c',
'95',
'-2', '4', # This is the only one we changed (changed the max skip from -1 to 4)
'-U',
'-r', '1000',
'-n', '4',
'-w', '1.2',
'-a',
'-l', '100']
rouge_args = ' '.join(rouge_args)
rouge_results = r.convert_and_evaluate(rouge_args=rouge_args)
return r.output_to_dict(rouge_results)
def rouge_log(results_dict, dir_to_write):
"""Log ROUGE results to screen and write to file.
Args:
results_dict: the dictionary returned by pyrouge
dir_to_write: the directory where we will write the results to"""
log_str = ""
for x in ["1","2","l","s4","su4"]:
log_str += "\nROUGE-%s:\n" % x
for y in ["f_score", "recall", "precision"]:
key = "rouge_%s_%s" % (x,y)
key_cb = key + "_cb"
key_ce = key + "_ce"
val = results_dict[key]
val_cb = results_dict[key_cb]
val_ce = results_dict[key_ce]
log_str += "%s: %.4f with confidence interval (%.4f, %.4f)\n" % (key, val, val_cb, val_ce)
logging.info(log_str) # log to screen
results_file = os.path.join(dir_to_write, "ROUGE_results.txt")
logging.info("Writing final ROUGE results to %s...", results_file)
with open(results_file, "w") as f:
f.write(log_str)
def get_decode_dir_name(ckpt_name):
"""Make a descriptive name for the decode dir, including the name of the checkpoint we use to decode. This is called in single_pass mode."""
if "train" in FLAGS.data_path: dataset = "train"
elif "val" in FLAGS.data_path: dataset = "val"
elif "test" in FLAGS.data_path: dataset = "test"
else: raise ValueError("FLAGS.data_path %s should contain one of train, val or test" % (FLAGS.data_path))
dirname = "decode_%s_%imaxenc_%ibeam_%imindec_%imaxdec" % (dataset, FLAGS.max_enc_steps, FLAGS.beam_size, FLAGS.min_dec_steps, FLAGS.max_dec_steps)
if ckpt_name is not None:
dirname += "_%s" % ckpt_name
return dirname