-
Notifications
You must be signed in to change notification settings - Fork 65
/
metric_performance.py
57 lines (45 loc) · 1.47 KB
/
metric_performance.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
from pycocoevalcap.eval import calculate_metrics
import numpy as np
import json
import argparse
def create_dataset(array):
dataset = {'annotations': []}
for i, caption in enumerate(array):
dataset['annotations'].append({
'image_id': i,
'caption': caption
})
return dataset
def load_json(json_file):
with open(json_file, 'r') as f:
data = json.load(f)
return data
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--result_path', type=str,
default='./results/clean_test.json')
args = parser.parse_args()
test = load_json(args.result_path)
datasetGTS = {'annotations': []}
datasetRES = {'annotations': []}
for i, image_id in enumerate(test):
array = []
for each in test[image_id]['Pred Sent']:
array.append(test[image_id]['Pred Sent'][each])
pred_sent = '. '.join(array)
array = []
for each in test[image_id]['Real Sent']:
sent = test[image_id]['Real Sent'][each]
if len(sent) != 0:
array.append(sent)
real_sent = '. '.join(array)
datasetGTS['annotations'].append({
'image_id': i,
'caption': real_sent
})
datasetRES['annotations'].append({
'image_id': i,
'caption': pred_sent
})
rng = range(len(test))
print calculate_metrics(rng, datasetGTS, datasetRES)