-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluation.py
228 lines (193 loc) · 11.8 KB
/
evaluation.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# 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.
# ==============================================================================
import argparse
import datetime
import os
import time
import json
import torch
from rouge import Rouge
from Hypergraph import Hypergraph
from Tester_hyper import SLTester
from module.dataloader_withedge import ExampleSet, graph_collate_fn
from module.embedding import Word_Embedding
from module.vocabulary import Vocab
from tools import utils
from tools.logger import *
def load_test_model(model, model_name, eval_dir, save_root):
""" choose which model will be loaded for evaluation """
if model_name.startswith('eval'):
bestmodel_load_path = os.path.join(eval_dir, model_name[4:])
elif model_name.startswith('train'):
train_dir = os.path.join(save_root, "train")
bestmodel_load_path = os.path.join(train_dir, model_name[5:])
elif model_name == "earlystop":
train_dir = os.path.join(save_root, "train")
bestmodel_load_path = os.path.join(train_dir, 'earlystop')
else:
logger.error("None of such model! Must be one of evalbestmodel/trainbestmodel/earlystop")
raise ValueError("None of such model! Must be one of evalbestmodel/trainbestmodel/earlystop")
if not os.path.exists(bestmodel_load_path):
logger.error("[ERROR] Restoring %s for testing...The path %s does not exist!", model_name, bestmodel_load_path)
return None
logger.info("[INFO] Restoring %s for testing...The path is %s", model_name, bestmodel_load_path)
model.load_state_dict(torch.load(bestmodel_load_path))
return model
def run_test(model, dataset, loader, model_name, hps):
test_dir = os.path.join(hps.save_root, "test") # make a subdir of the root dir for eval data
eval_dir = os.path.join(hps.save_root, "eval")
if not os.path.exists(test_dir) : os.makedirs(test_dir)
if not os.path.exists(eval_dir) :
logger.exception("[Error] eval_dir %s doesn't exist. Run in train mode to create it.", eval_dir)
raise Exception("[Error] eval_dir %s doesn't exist. Run in train mode to create it." % (eval_dir))
resfile = None
if hps.save_label:
log_dir = os.path.join(test_dir, hps.cache_dir.split("/")[-1])
resfile = open(log_dir, "w")
logger.info("[INFO] Write the Evaluation into %s", log_dir)
model = load_test_model(model, model_name, eval_dir, hps.save_root)
model.eval()
iter_start_time=time.time()
with torch.no_grad():
logger.info("[Model] Sequence Labeling!")
tester = SLTester(model, hps.m, limited=hps.limited, test_dir=test_dir)
for i, (G, index, hyperedges, actual_node_list) in enumerate(loader):
if hps.cuda:
G.to(torch.device("cuda"))
# bert_feature = bert_feature.to(torch.device("cuda"))
hyperedges = hyperedges.to(torch.device("cuda"))
actual_node_list = actual_node_list.to(torch.device("cuda"))
tester.evaluation(G, index, dataset, hyperedges, actual_node_list, blocking=hps.blocking)
running_avg_loss = tester.running_avg_loss
if hps.save_label:
# save label and do not calculate rouge
json.dump(tester.extractLabel, resfile)
tester.SaveDecodeFile()
logger.info(' | end of test | time: {:5.2f}s | '.format((time.time() - iter_start_time)))
return
logger.info("The number of pairs is %d", tester.rougePairNum)
if not tester.rougePairNum:
logger.error("During testing, no hyps is selected!")
sys.exit(1)
if hps.use_pyrouge:
if isinstance(tester.refer[0], list):
logger.info("Multi Reference summaries!")
scores_all = utils.pyrouge_score_all_multi(tester.hyps, tester.refer)
else:
scores_all = utils.pyrouge_score_all(tester.hyps, tester.refer)
else:
rouge = Rouge()
scores_all = rouge.get_scores(tester.hyps, tester.refer, avg=True)
res = "Rouge1:\n\tp:%.6f, r:%.6f, f:%.6f\n" % (scores_all['rouge-1']['p'], scores_all['rouge-1']['r'], scores_all['rouge-1']['f']) \
+ "Rouge2:\n\tp:%.6f, r:%.6f, f:%.6f\n" % (scores_all['rouge-2']['p'], scores_all['rouge-2']['r'], scores_all['rouge-2']['f']) \
+ "Rougel:\n\tp:%.6f, r:%.6f, f:%.6f\n" % (scores_all['rouge-l']['p'], scores_all['rouge-l']['r'], scores_all['rouge-l']['f'])
logger.info(res)
tester.getMetric()
tester.SaveDecodeFile()
logger.info('[INFO] End of test | time: {:5.2f}s | test loss {:5.4f} | '.format((time.time() - iter_start_time),float(running_avg_loss)))
def main():
parser = argparse.ArgumentParser(description='Hypergraph Model')
# Where to find data
parser.add_argument('--data_dir', type=str, default='dataset/arxiv', help='The dataset directory.')
parser.add_argument('--cache_dir', type=str, default='cache/arxiv', help='The processed dataset directory')
parser.add_argument('--embedding_path', type=str, default='glove.42B.300d.txt', help='Path expression to external word embedding.')
# Important settings
parser.add_argument('--model', type=str, default="Hypergraph", help="model structure")
parser.add_argument('--test_model', type=str, default='multi', help='choose different model to test [multi/evalbestmodel/trainbestmodel/earlystop]')
parser.add_argument('--use_pyrouge', action='store_true', default=True, help='use_pyrouge')
# Where to save output
parser.add_argument('--save_root', type=str, default='models_arxiv/', help='Root directory for all model.')
parser.add_argument('--log_root', type=str, default='log_arxiv/', help='Root directory for all logging.')
# Hyperparameters
parser.add_argument('--gpu', type=str, default='0', help='GPU ID to use')
parser.add_argument('--cuda', action='store_true', default=True, help='use cuda')
parser.add_argument('--vocab_size', type=int, default=50000, help='Size of vocabulary.')
parser.add_argument('--batch_size', type=int, default=100, help='Mini batch size [default: 32]')
parser.add_argument('--n_iter', type=int, default=1, help='iteration hop [default: 1]')
parser.add_argument('--word_embedding', action='store_true', default=True, help='whether to use Word embedding')
parser.add_argument('--word_emb_dim', type=int, default=300, help='Word embedding size [default: 300]')
parser.add_argument('--embed_train', action='store_true', default=False, help='whether to train Word embedding [default: False]')
parser.add_argument('--feat_embed_size', type=int, default=50, help='feature embedding size [default: 50]')
parser.add_argument('--n_layers', type=int, default=1, help='Number of GAT layers [default: 1]')
parser.add_argument('--lstm_hidden_state', type=int, default=128, help='size of lstm hidden state')
parser.add_argument('--lstm_layers', type=int, default=2, help='lstm layers')
parser.add_argument('--bidirectional', action='store_true', default=True, help='use bidirectional LSTM')
parser.add_argument('--n_feature_size', type=int, default=128, help='size of node feature')
parser.add_argument('--hidden_size', type=int, default=64, help='hidden size [default: 64]')
parser.add_argument('--gcn_hidden_size', type=int, default=128, help='hidden size [default: 64]')
parser.add_argument('--ffn_inner_hidden_size', type=int, default=512, help='PositionwiseFeedForward inner hidden size [default: 512]')
parser.add_argument('--n_head', type=int, default=8, help='multihead attention number [default: 8]')
parser.add_argument('--recurrent_dropout_prob', type=float, default=0.1, help='recurrent dropout prob [default: 0.1]')
parser.add_argument('--atten_dropout_prob', type=float, default=0.1,help='attention dropout prob [default: 0.1]')
parser.add_argument('--ffn_dropout_prob', type=float, default=0.1, help='PositionwiseFeedForward dropout prob [default: 0.1]')
parser.add_argument('--use_orthnormal_init', action='store_true', default=True, help='use orthnormal init for lstm [default: true]')
parser.add_argument('--sent_max_len', type=int, default=100, help='max length of sentences (max source text sentence tokens)')
parser.add_argument('--doc_max_timesteps', type=int, default=250, help='max length of documents (max timesteps of documents)')
parser.add_argument('--save_label', action='store_true', default=False, help='require multihead attention')
parser.add_argument('--limited', action='store_true', default=False, help='limited hypo length')
parser.add_argument('--blocking', action='store_true', default=False, help='ngram blocking')
parser.add_argument('-m', type=int, default=5, help='decode summary length')
args = parser.parse_args()
os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu
torch.set_printoptions(threshold=50000)
# File paths
DATA_FILE = os.path.join(args.data_dir, "test.label.jsonl")
VOCAL_FILE = os.path.join(args.cache_dir, "vocab")
FILTER_WORD = os.path.join(args.cache_dir, "filter_word.txt")
HEDGE_FILE = os.path.join(args.data_dir, 'test.hedges.jsonl')
LOG_PATH = args.log_root
# train_log setting
if not os.path.exists(LOG_PATH):
logger.exception("[Error] Logdir %s doesn't exist. Run in train mode to create it.", LOG_PATH)
raise Exception("[Error] Logdir %s doesn't exist. Run in train mode to create it." % (LOG_PATH))
nowTime=datetime.datetime.now().strftime('%Y%m%d_%H%M%S')
log_path = os.path.join(LOG_PATH, "test_" + nowTime)
file_handler = logging.FileHandler(log_path)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
logger.info("Pytorch %s", torch.__version__)
logger.info("[INFO] Create Vocab, vocab path is %s", VOCAL_FILE)
vocab = Vocab(VOCAL_FILE, args.vocab_size)
embed = torch.nn.Embedding(vocab.size(), args.word_emb_dim)
if args.word_embedding:
embed_loader = Word_Embedding(args.embedding_path, vocab)
vectors = embed_loader.load_my_vecs(args.word_emb_dim)
pretrained_weight = embed_loader.add_unknown_words_by_avg(vectors, args.word_emb_dim)
embed.weight.data.copy_(torch.Tensor(pretrained_weight))
embed.weight.requires_grad = args.embed_train
hps = args
logger.info(hps)
test_w2s_path = os.path.join(args.cache_dir, "test.w2s.tfidf.jsonl")
if hps.model == "Hypergraph":
model = Hypergraph(hps, embed)
logger.info("[MODEL] Hypergraph ")
dataset = ExampleSet(DATA_FILE, vocab, hps.doc_max_timesteps, hps.sent_max_len, FILTER_WORD, test_w2s_path, HEDGE_FILE)
loader = torch.utils.data.DataLoader(dataset, batch_size=hps.batch_size, shuffle=True, num_workers=12, collate_fn=graph_collate_fn)
else:
logger.error("[ERROR] Invalid Model Type!")
raise NotImplementedError("Model Type has not been implemented")
if args.cuda:
model.to(torch.device("cuda"))
logger.info("[INFO] Use cuda")
logger.info("[INFO] Decoding...")
if hps.test_model == "multi":
for i in range(3):
model_name = "evalbestmodel_%d" % i
run_test(model, dataset, loader, model_name, hps)
else:
run_test(model, dataset, loader, hps.test_model, hps)
if __name__ == '__main__':
main()