-
Notifications
You must be signed in to change notification settings - Fork 17
/
compute_KPQA.py
166 lines (129 loc) · 5.13 KB
/
compute_KPQA.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
import numpy as np
import pickle
import json
import os
import kpqa_coco
from tqdm import tqdm
import pandas as pd
from transformers import AutoTokenizer
from collections import defaultdict
from kpqa_bertscore.bert_score.utils import get_idf_dict
from kpqa_bertscore.bert_score import score
from kpqa_bertscore.bert_score import BERTScorer
from copy import copy
import transformers
from transformers import (AutoTokenizer, BertForTokenClassification, AutoConfig)
import logging
transformers.tokenization_utils.logger.setLevel(logging.ERROR)
transformers.configuration_utils.logger.setLevel(logging.ERROR)
transformers.modeling_utils.logger.setLevel(logging.ERROR)
import string
import argparse
import kpw
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--use_kp', type=bool, default=True)
parser.add_argument('--is_q', type=int, default=1)
parser.add_argument('--data', type=str, default='sample.csv')
parser.add_argument('--model_path',type=str, default='ckpt')
parser.add_argument('--num_ref', type=int, default=1)
parser.add_argument('--out_file', type=str, default='results.csv')
args = parser.parse_args()
return args
if __name__ == "__main__":
args = parse_args()
## Configuration
stopwords = []
is_q = bool(args.is_q)
# Number of References for Each Dataset
Nr = args.num_ref
use_idf = True
use_stopwords = False
use_KP = True
if not use_stopwords:
stopwords=None
## Data Load
data = pd.read_csv(args.data)
refs = []
for i in range(Nr):
refs.append(data['reference'+str(i+1)])
ques = data['question']
hyps = data['answer']
all_refs = []
for x in refs:
all_refs += list(x)
## KPW Load
PATH = os.path.join(args.model_path)
config = AutoConfig.from_pretrained(os.path.join(PATH, 'config.json'))
tokenizer = AutoTokenizer.from_pretrained(PATH)
model = BertForTokenClassification.from_pretrained(PATH)
model = model.cuda()
probs = kpw.get_kpw(model, hyps, refs, ques, is_q=is_q)
## BERTScorer Setting
# model_type = 'bert-base-uncased' # shows slight worse performance
model_type = 'bert-large-uncased-whole-word-masking-finetuned-squad'
model_path = None
tokenizer = AutoTokenizer.from_pretrained(model_type)
n_layers = 1
scorer = BERTScorer(lang="en", model_type=model_type,
num_layers=n_layers, idf=use_idf,
idf_sents=all_refs, stopwords=stopwords,
rescale_with_baseline=False,
model_path=model_path)
scorer_o = BERTScorer(lang="en", model_type=model_type,
num_layers=n_layers, idf=True,
idf_sents=all_refs, stopwords=None,
rescale_with_baseline=False,
model_path=model_path)
## BERTScore Calculation
pscores = []
rscores = []
f1scores = []
f1scores_o = []
for j in tqdm(range(Nr)):
reference = list(refs[j])
hypothesis = list(hyps)
P, R, F1 = scorer.score(hypothesis, reference, w_hyps=probs[0], w_refs=probs[j+1])
P = P.numpy()
R = R.numpy()
F1 = F1.numpy()
P_, R_, F1_ = scorer_o.score(hypothesis, reference)
f1scores_o.append(F1_.numpy())
pscores.append(P)
rscores.append(R)
f1scores.append(F1)
f1_max_o = np.max(f1scores_o, axis=0)
p_max = np.max(pscores, axis=0)
r_max = np.max(rscores, axis=0)
f1_max = np.max(f1scores, axis=0)
## Exact Match Based Metrics
coco_evaluator_c = kpqa_coco.CocoEvaluator(average=False)
rouge_kpqa_evaluator = kpqa_coco.RougeEvaluator(num_parallel_calls=1, average=False)
r1s_k = []
rs_k = []
b1s_k = []
mode = 0
for j in tqdm(range(Nr)):
reference = list(refs[j])
hypothesis = list(hyps)
if(mode):
hypothesis_ = [rm_stop(x, stopwords) for x in hypothesis]
reference_ = [rm_stop(x, stopwords) for x in reference]
rouge_eval = rouge_kpqa_evaluator.run_evaluation(hypothesis_, reference_, probs[0], probs[j+1])
else:
rouge_eval = rouge_kpqa_evaluator.run_evaluation(hypothesis, reference, probs[0], probs[j+1])
coco_eval_c = coco_evaluator_c.run_evaluation(hypothesis, reference, probs[0], probs[j+1])
r1s_k.append([x for x in rouge_eval['rouge1'][0]])
rs_k.append([x for x in rouge_eval['rougeL'][0]])
b1s_k.append([x['Bleu_1'] for x in coco_eval_c])
rs_k_max = np.max(rs_k, axis = 0)
b1s_k_max = np.max(b1s_k, axis = 0)
r1s_k_max = np.max(r1s_k, axis = 0)
## Compute Metric
lst = [f1_max_o, b1s_k_max, rs_k_max, f1_max]
average_scores = [np.average(x) for x in lst]
df = pd.DataFrame(lst, index=['BERTScore','BLEU-1-KPQA', 'ROUGE-L-KPQA', 'BERTScore-KPQA']).T
df_avg = pd.DataFrame(average_scores, index=['BERTScore','BLEU-1-KPQA', 'ROUGE-L-KPQA', 'BERTScore-KPQA']).T
print("## Result")
print(df_avg)
df.to_csv(args.out_file, index=False)