evaluate_chartqa.py 1.36 KB
Newer Older
xingjinliang's avatar
xingjinliang committed
1
2
3
4
5
6
7
8
9
10
11
import argparse
import json

from evaluate_mmmu import get_input_output_paths
from evaluate_vqav2 import compute_vqa_accuracy


def merge_input_files(input_path):
    """Merge input files to a format compatible with the evaluator."""
    input_file_paths, output_file_path = get_input_output_paths(input_path, task="ChartQA")

xingjinliang's avatar
xingjinliang committed
12
    results = dict()
xingjinliang's avatar
xingjinliang committed
13
14
15
16
17

    for input_file_path in input_file_paths:
        with open(input_file_path, "r") as input_file:
            for line in input_file:
                res = json.loads(line)
xingjinliang's avatar
xingjinliang committed
18
                sample_id = res["sample_id"]
xingjinliang's avatar
xingjinliang committed
19

xingjinliang's avatar
xingjinliang committed
20
21
22
23
24
25
26
27
                # Ignore possible duplicates.
                if sample_id in results:
                    continue

                res["question_id"] = sample_id
                results[sample_id] = res

    results = list(results.values())
xingjinliang's avatar
xingjinliang committed
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

    with open(output_file_path, "w") as output_file:
        json.dump(results, output_file)

    return output_file_path


def chartqa_eval(input_path):
    """Run ChartQA evaluation."""
    result_file_path = merge_input_files(input_path)
    return compute_vqa_accuracy(result_file_path, task="ChartQA")


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('--input-path', type=str, help="Path to input file(s)")
    args = parser.parse_args()

    avg_acc = chartqa_eval(args.input_path)

    print(f"ChartQA accuracy: {avg_acc:.2f}")