-
Notifications
You must be signed in to change notification settings - Fork 2
/
metric.py
106 lines (88 loc) · 3.2 KB
/
metric.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
import torch
import pandas as pd
from tqdm import tqdm
import time
import numpy as np
from datasets import load_dataset, load_metric, Dataset
from transformers import (
AutoTokenizer,
MarianMTModel,
AutoTokenizer,
AutoModelForSeq2SeqLM,
T5Tokenizer,
)
from transformers import Seq2SeqTrainer, Seq2SeqTrainingArguments, DataCollatorForSeq2Seq
import multiprocessing
from easydict import EasyDict
import yaml
# Read config.yaml file
with open("config.yaml") as infile:
SAVED_CFG = yaml.load(infile, Loader=yaml.FullLoader)
CFG = EasyDict(SAVED_CFG["CFG"])
device = "cuda:0" if torch.cuda.is_available() else "cpu"
training_args = Seq2SeqTrainingArguments
model_name = CFG.inference_model_name
valid_dataset = load_dataset(CFG.dset_name, split="valid", use_auth_token=True)
print(valid_dataset)
tokenizer = AutoTokenizer.from_pretrained(model_name, use_auth_token=True)
model = AutoModelForSeq2SeqLM.from_pretrained(model_name, use_auth_token=True)
model.to(device)
start = 0
batch_size = 150 # P100:batch_size 250 / A100:batch_size 700
length = len(valid_dataset)
cnt = length // batch_size + 1
df = pd.DataFrame(columns={"src", "gen", "label"})
csv_start = 0
save_start = csv_start
save_count = 0
for i in tqdm(range(start, cnt)):
save_count += 1
if i == cnt - 1:
end = len(valid_dataset)
else:
end = csv_start + batch_size
src_sentences = valid_dataset["ko"][csv_start:end]
label = valid_dataset["en"][csv_start:end]
encoding = tokenizer(
src_sentences, padding=True, return_tensors="pt", max_length=CFG.max_token_length
).to(device)
# https://huggingface.co/docs/transformers/internal/generation_utils
with torch.no_grad():
translated = model.generate(
**encoding,
max_length=CFG.max_token_length,
num_beams=CFG.num_beams,
repetition_penalty=CFG.repetition_penalty,
no_repeat_ngram_size=CFG.no_repeat_ngram_size,
num_return_sequences=CFG.num_return_sequences,
)
del encoding
# https://github.com/huggingface/transformers/issues/10704
generated_texts = tokenizer.batch_decode(translated, skip_special_tokens=True)
del translated
print(generated_texts[0:2])
df1 = pd.DataFrame({"src": src_sentences, "gen": generated_texts, "label": label})
df = df.append(df1, ignore_index=True)
if save_count == 30:
save_count = 0
df.to_csv(f"./results/tmp_translated-{save_start}-{end}-sentences.csv", index=False)
csv_start = end
# load sacrebleu
# https://huggingface.co/spaces/evaluate-metric/sacrebleu | https://github.com/mjpost/sacreBLEU
metric = load_metric("sacrebleu")
preds = df["gen"]
labels = np.expand_dims(df["label"], axis=1)
score = metric.compute(predictions=preds, references=labels) # takes 3 minutes for 550K pairs
print(score)
"""
# Result of Korean to English Translation
{
"score": 45.14821527744787,
"counts": [10287887, 6969037, 5035938, 3719578],
"totals": [14100267, 13546767, 12993267, 12439767],
"precisions": [72.96235596106088, 51.44428187182964, 38.75805830819916, 29.90070473184908],
"bp": 0.9886003016662179,
"sys_len": 14100267,
"ref_len": 14261929,
}
"""