forked from eldar/pose-tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval_mscoco.py
64 lines (45 loc) · 1.65 KB
/
eval_mscoco.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
import os
import sys
import argparse
import json
from config import load_config
dir_path = os.path.dirname(os.path.realpath(__file__))
sys.path.append(dir_path + '/lib/coco/PythonAPI')
from pycocotools.coco import COCO as COCO
from pycocotools.cocoeval import COCOeval
def apply_threhsold(inFile, threshold):
outFile = inFile[:-5] + '-' + str(threshold) + '.json'
with open(inFile) as data_file:
data = json.load(data_file)
for person_id in range(len(data)):
keypoints = data[person_id]["keypoints"]
keypoints = [int(keypoints[i] > threshold) if i % 3 == 2 else int(keypoints[i]) for i in range(len(keypoints))]
data[person_id]["keypoints"] = keypoints
with open(outFile, 'w') as outfile:
json.dump(data, outfile)
return outFile
def eval_init(cfg):
dataset = cfg.dataset
dataset_phase = cfg.dataset_phase
dataset_ann = cfg.dataset_ann
threshold = 0
# initialize cocoGT api
annFile = '%s/annotations/%s_%s.json' % (dataset, dataset_ann, dataset_phase)
cocoGT = COCO(annFile)
# initialize cocoPred api
inFile = "predictions_with_segm.json"
predFile = apply_threhsold(inFile, threshold)
cocoPred = cocoGT.loadRes(predFile)
return cocoGT, cocoPred
def eval_mscoco_with_segm(cocoGT, cocoPred):
# running evaluation
cocoEval = COCOeval(cocoGT, cocoPred, "keypoints")
cocoEval.evaluate()
cocoEval.accumulate()
cocoEval.summarize()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
args, unparsed = parser.parse_known_args()
cfg = load_config()
cocoGT, cocoPred = eval_init(cfg)
eval_mscoco_with_segm(cocoGT, cocoPred)