forked from abhishekkrthakur/long-text-token-classification
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
418 lines (351 loc) · 14.4 KB
/
utils.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import copy
import os
import numpy as np
import pandas as pd
import torch
from joblib import Parallel, delayed
from tez import enums
from tez.callbacks import Callback
from tqdm import tqdm
target_id_map = {
"B-Lead": 0,
"I-Lead": 1,
"B-Position": 2,
"I-Position": 3,
"B-Evidence": 4,
"I-Evidence": 5,
"B-Claim": 6,
"I-Claim": 7,
"B-Concluding Statement": 8,
"I-Concluding Statement": 9,
"B-Counterclaim": 10,
"I-Counterclaim": 11,
"B-Rebuttal": 12,
"I-Rebuttal": 13,
"O": 14,
"PAD": -100,
}
id_target_map = {v: k for k, v in target_id_map.items()}
def _prepare_training_data_helper(args, tokenizer, df, train_ids):
training_samples = []
for idx in tqdm(train_ids):
filename = os.path.join(args.input, "train", idx + ".txt")
with open(filename, "r") as f:
text = f.read()
encoded_text = tokenizer.encode_plus(
text,
add_special_tokens=False,
return_offsets_mapping=True,
)
input_ids = encoded_text["input_ids"]
input_labels = copy.deepcopy(input_ids)
offset_mapping = encoded_text["offset_mapping"]
for k in range(len(input_labels)):
input_labels[k] = "O"
sample = {
"id": idx,
"input_ids": input_ids,
"text": text,
"offset_mapping": offset_mapping,
}
temp_df = df[df["id"] == idx]
for _, row in temp_df.iterrows():
text_labels = [0] * len(text)
discourse_start = int(row["discourse_start"])
discourse_end = int(row["discourse_end"])
prediction_label = row["discourse_type"]
text_labels[discourse_start:discourse_end] = [1] * (discourse_end - discourse_start)
target_idx = []
for map_idx, (offset1, offset2) in enumerate(encoded_text["offset_mapping"]):
if sum(text_labels[offset1:offset2]) > 0:
if len(text[offset1:offset2].split()) > 0:
target_idx.append(map_idx)
targets_start = target_idx[0]
targets_end = target_idx[-1]
pred_start = "B-" + prediction_label
pred_end = "I-" + prediction_label
input_labels[targets_start] = pred_start
input_labels[targets_start + 1 : targets_end + 1] = [pred_end] * (targets_end - targets_start)
sample["input_ids"] = input_ids
sample["input_labels"] = input_labels
training_samples.append(sample)
return training_samples
def prepare_training_data(df, tokenizer, args, num_jobs):
training_samples = []
train_ids = df["id"].unique()
train_ids_splits = np.array_split(train_ids, num_jobs)
results = Parallel(n_jobs=num_jobs, backend="multiprocessing")(
delayed(_prepare_training_data_helper)(args, tokenizer, df, idx) for idx in train_ids_splits
)
for result in results:
training_samples.extend(result)
return training_samples
def calc_overlap(row):
"""
Calculates the overlap between prediction and
ground truth and overlap percentages used for determining
true positives.
"""
set_pred = set(row.predictionstring_pred.split(" "))
set_gt = set(row.predictionstring_gt.split(" "))
# Length of each and intersection
len_gt = len(set_gt)
len_pred = len(set_pred)
inter = len(set_gt.intersection(set_pred))
overlap_1 = inter / len_gt
overlap_2 = inter / len_pred
return [overlap_1, overlap_2]
def score_feedback_comp_micro(pred_df, gt_df):
"""
A function that scores for the kaggle
Student Writing Competition
Uses the steps in the evaluation page here:
https://www.kaggle.com/c/feedback-prize-2021/overview/evaluation
This code is from Rob Mulla's Kaggle kernel.
"""
gt_df = gt_df[["id", "discourse_type", "predictionstring"]].reset_index(drop=True).copy()
pred_df = pred_df[["id", "class", "predictionstring"]].reset_index(drop=True).copy()
pred_df["pred_id"] = pred_df.index
gt_df["gt_id"] = gt_df.index
# Step 1. all ground truths and predictions for a given class are compared.
joined = pred_df.merge(
gt_df,
left_on=["id", "class"],
right_on=["id", "discourse_type"],
how="outer",
suffixes=("_pred", "_gt"),
)
joined["predictionstring_gt"] = joined["predictionstring_gt"].fillna(" ")
joined["predictionstring_pred"] = joined["predictionstring_pred"].fillna(" ")
joined["overlaps"] = joined.apply(calc_overlap, axis=1)
# 2. If the overlap between the ground truth and prediction is >= 0.5,
# and the overlap between the prediction and the ground truth >= 0.5,
# the prediction is a match and considered a true positive.
# If multiple matches exist, the match with the highest pair of overlaps is taken.
joined["overlap1"] = joined["overlaps"].apply(lambda x: eval(str(x))[0])
joined["overlap2"] = joined["overlaps"].apply(lambda x: eval(str(x))[1])
joined["potential_TP"] = (joined["overlap1"] >= 0.5) & (joined["overlap2"] >= 0.5)
joined["max_overlap"] = joined[["overlap1", "overlap2"]].max(axis=1)
tp_pred_ids = (
joined.query("potential_TP")
.sort_values("max_overlap", ascending=False)
.groupby(["id", "predictionstring_gt"])
.first()["pred_id"]
.values
)
# 3. Any unmatched ground truths are false negatives
# and any unmatched predictions are false positives.
fp_pred_ids = [p for p in joined["pred_id"].unique() if p not in tp_pred_ids]
matched_gt_ids = joined.query("potential_TP")["gt_id"].unique()
unmatched_gt_ids = [c for c in joined["gt_id"].unique() if c not in matched_gt_ids]
# Get numbers of each type
TP = len(tp_pred_ids)
FP = len(fp_pred_ids)
FN = len(unmatched_gt_ids)
# calc microf1
my_f1_score = TP / (TP + 0.5 * (FP + FN))
return my_f1_score
def score_feedback_comp(pred_df, gt_df, return_class_scores=False):
class_scores = {}
pred_df = pred_df[["id", "class", "predictionstring"]].reset_index(drop=True).copy()
for discourse_type, gt_subset in gt_df.groupby("discourse_type"):
pred_subset = pred_df.loc[pred_df["class"] == discourse_type].reset_index(drop=True).copy()
class_score = score_feedback_comp_micro(pred_subset, gt_subset)
class_scores[discourse_type] = class_score
f1 = np.mean([v for v in class_scores.values()])
if return_class_scores:
return f1, class_scores
return f1
class FeedbackDatasetValid:
def __init__(self, samples, max_len, tokenizer):
self.samples = samples
self.max_len = max_len
self.tokenizer = tokenizer
self.length = len(samples)
def __len__(self):
return self.length
def __getitem__(self, idx):
input_ids = self.samples[idx]["input_ids"]
input_ids = [self.tokenizer.cls_token_id] + input_ids
if len(input_ids) > self.max_len - 1:
input_ids = input_ids[: self.max_len - 1]
# add end token id to the input_ids
input_ids = input_ids + [self.tokenizer.sep_token_id]
attention_mask = [1] * len(input_ids)
return {
"ids": input_ids,
"mask": attention_mask,
}
class Collate:
def __init__(self, tokenizer):
self.tokenizer = tokenizer
def __call__(self, batch):
output = dict()
output["ids"] = [sample["ids"] for sample in batch]
output["mask"] = [sample["mask"] for sample in batch]
# calculate max token length of this batch
batch_max = max([len(ids) for ids in output["ids"]])
# add padding
if self.tokenizer.padding_side == "right":
output["ids"] = [s + (batch_max - len(s)) * [self.tokenizer.pad_token_id] for s in output["ids"]]
output["mask"] = [s + (batch_max - len(s)) * [0] for s in output["mask"]]
else:
output["ids"] = [(batch_max - len(s)) * [self.tokenizer.pad_token_id] + s for s in output["ids"]]
output["mask"] = [(batch_max - len(s)) * [0] + s for s in output["mask"]]
# convert to tensors
output["ids"] = torch.tensor(output["ids"], dtype=torch.long)
output["mask"] = torch.tensor(output["mask"], dtype=torch.long)
return output
class EarlyStopping(Callback):
def __init__(
self,
model_path,
valid_df,
valid_samples,
batch_size,
tokenizer,
patience=5,
mode="max",
delta=0.001,
save_weights_only=True,
):
self.patience = patience
self.counter = 0
self.mode = mode
self.best_score = None
self.early_stop = False
self.delta = delta
self.save_weights_only = save_weights_only
self.model_path = model_path
self.valid_samples = valid_samples
self.batch_size = batch_size
self.valid_df = valid_df
self.tokenizer = tokenizer
if self.mode == "min":
self.val_score = np.Inf
else:
self.val_score = -np.Inf
def on_epoch_end(self, model):
model.eval()
valid_dataset = FeedbackDatasetValid(self.valid_samples, 4096, self.tokenizer)
collate = Collate(self.tokenizer)
preds_iter = model.predict(
valid_dataset,
batch_size=self.batch_size,
n_jobs=-1,
collate_fn=collate,
)
final_preds = []
final_scores = []
for preds in preds_iter:
pred_class = np.argmax(preds, axis=2)
pred_scrs = np.max(preds, axis=2)
for pred, pred_scr in zip(pred_class, pred_scrs):
final_preds.append(pred.tolist())
final_scores.append(pred_scr.tolist())
for j in range(len(self.valid_samples)):
tt = [id_target_map[p] for p in final_preds[j][1:]]
tt_score = final_scores[j][1:]
self.valid_samples[j]["preds"] = tt
self.valid_samples[j]["pred_scores"] = tt_score
submission = []
min_thresh = {
"Lead": 9,
"Position": 5,
"Evidence": 14,
"Claim": 3,
"Concluding Statement": 11,
"Counterclaim": 6,
"Rebuttal": 4,
}
proba_thresh = {
"Lead": 0.7,
"Position": 0.55,
"Evidence": 0.65,
"Claim": 0.55,
"Concluding Statement": 0.7,
"Counterclaim": 0.5,
"Rebuttal": 0.55,
}
for _, sample in enumerate(self.valid_samples):
preds = sample["preds"]
offset_mapping = sample["offset_mapping"]
sample_id = sample["id"]
sample_text = sample["text"]
sample_pred_scores = sample["pred_scores"]
# pad preds to same length as offset_mapping
if len(preds) < len(offset_mapping):
preds = preds + ["O"] * (len(offset_mapping) - len(preds))
sample_pred_scores = sample_pred_scores + [0] * (len(offset_mapping) - len(sample_pred_scores))
idx = 0
phrase_preds = []
while idx < len(offset_mapping):
start, _ = offset_mapping[idx]
if preds[idx] != "O":
label = preds[idx][2:]
else:
label = "O"
phrase_scores = []
phrase_scores.append(sample_pred_scores[idx])
idx += 1
while idx < len(offset_mapping):
if label == "O":
matching_label = "O"
else:
matching_label = f"I-{label}"
if preds[idx] == matching_label:
_, end = offset_mapping[idx]
phrase_scores.append(sample_pred_scores[idx])
idx += 1
else:
break
if "end" in locals():
phrase = sample_text[start:end]
phrase_preds.append((phrase, start, end, label, phrase_scores))
temp_df = []
for phrase_idx, (phrase, start, end, label, phrase_scores) in enumerate(phrase_preds):
word_start = len(sample_text[:start].split())
word_end = word_start + len(sample_text[start:end].split())
word_end = min(word_end, len(sample_text.split()))
ps = " ".join([str(x) for x in range(word_start, word_end)])
if label != "O":
if sum(phrase_scores) / len(phrase_scores) >= proba_thresh[label]:
temp_df.append((sample_id, label, ps))
temp_df = pd.DataFrame(temp_df, columns=["id", "class", "predictionstring"])
submission.append(temp_df)
submission = pd.concat(submission).reset_index(drop=True)
submission["len"] = submission.predictionstring.apply(lambda x: len(x.split()))
def threshold(df):
df = df.copy()
for key, value in min_thresh.items():
index = df.loc[df["class"] == key].query(f"len<{value}").index
df.drop(index, inplace=True)
return df
submission = threshold(submission)
# drop len
submission = submission.drop(columns=["len"])
scr = score_feedback_comp(submission, self.valid_df, return_class_scores=True)
print(scr)
model.train()
epoch_score = scr[0]
if self.mode == "min":
score = -1.0 * epoch_score
else:
score = np.copy(epoch_score)
if self.best_score is None:
self.best_score = score
self.save_checkpoint(epoch_score, model)
elif score < self.best_score + self.delta:
self.counter += 1
print("EarlyStopping counter: {} out of {}".format(self.counter, self.patience))
if self.counter >= self.patience:
model.model_state = enums.ModelState.END
else:
self.best_score = score
self.save_checkpoint(epoch_score, model)
self.counter = 0
def save_checkpoint(self, epoch_score, model):
if epoch_score not in [-np.inf, np.inf, -np.nan, np.nan]:
print("Validation score improved ({} --> {}). Saving model!".format(self.val_score, epoch_score))
model.save(self.model_path, weights_only=self.save_weights_only)
self.val_score = epoch_score