-
Notifications
You must be signed in to change notification settings - Fork 1
/
evaluate.py
71 lines (51 loc) · 1.79 KB
/
evaluate.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
import os
import sys
import json
import random
from typing import Dict, Union, Any
from string import punctuation
sys.path.append("model")
sys.path.append("data")
sys.path.append("caption_evaluation_tools")
import numpy as np
import pandas as pd
from transformers.modeling_utils import PreTrainedModel
from transformers.tokenization_utils import PreTrainedTokenizer
from data.clotho_captioning_dataset import ClothoCaptioningDataset
from caption_evaluation_tools.eval_metrics import evaluate_metrics_from_lists
inference_csv = sys.argv[1]
test_split = sys.argv[2]
strip_punct_table = str.maketrans("", "", punctuation)
def read_inference_csv(csv_path):
df = pd.read_csv(csv_path)
filenames = []
captions = []
for i, row in df.iterrows():
filenames.append(row["file_name"])
captions.append(row["caption_predicted"])
return filenames, captions
if __name__ == "__main__":
test_dset = ClothoCaptioningDataset(
f"clotho/{test_split}",
"facebook/bart-base",
f"clotho/clotho_captions_{test_split}.csv",
)
gen_files, gen_captions = read_inference_csv(inference_csv)
assert len(gen_files) == len(test_dset)
# print(gen_captions[:10])
# exit()
true_captions = []
for i in range(len(test_dset)):
assert test_dset.idx_to_sample[i] == gen_files[i]
samp_name = gen_files[i]
true_captions.append(
[
s.strip().lower().translate(strip_punct_table)
for s in test_dset.captions[samp_name]
]
)
eval_res = evaluate_metrics_from_lists(gen_captions, true_captions)
print("[evaluation results]", eval_res[0])
open(
os.path.join(os.path.dirname(inference_csv), "inference_metrics.json"), "w"
).write(json.dumps(eval_res[0], indent=2))