-
Notifications
You must be signed in to change notification settings - Fork 19
/
cc.py
executable file
·88 lines (72 loc) · 2.7 KB
/
cc.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
#!/usr/bin/env python
#
# File Name : cc.py
#
# Description : Computes CC metric #
# Author : Ming Jiang
import numpy as np
import scipy.ndimage
def calc_score(gtsAnn, resAnn):
"""
Computer CC score. A simple implementation
:param gtsAnn : ground-truth fixation map
:param resAnn : predicted saliency map
:return score: int : score
"""
fixationMap = gtsAnn - np.mean(gtsAnn)
if np.max(fixationMap) > 0:
fixationMap = fixationMap / np.std(fixationMap)
salMap = resAnn - np.mean(resAnn)
if np.max(salMap) > 0:
salMap = salMap / np.std(salMap)
return np.corrcoef(salMap.reshape(-1), fixationMap.reshape(-1))[0][1]
class CC():
'''
Class for computing CC score for saliency maps
'''
def __init__(self,saliconRes):
self.saliconRes = saliconRes
self.imgs = self.saliconRes.imgs
def calc_score(self, gtsAnn, resAnn):
"""
Computer CC score. A simple implementation
:param gtsAnn : ground-truth fixation map
:param resAnn : predicted saliency map
:return score: int : score
"""
fixationMap = gtsAnn - np.mean(gtsAnn)
if np.max(fixationMap) > 0:
fixationMap = fixationMap / np.std(fixationMap)
salMap = resAnn - np.mean(resAnn)
if np.max(salMap) > 0:
salMap = salMap / np.std(salMap)
return np.corrcoef(salMap.reshape(-1), fixationMap.reshape(-1))[0][1]
def compute_score(self, gts, res):
"""
Computes CC score for a given set of predictions and fixations
:param gts : dict : fixation points with "image name" key and list of points as values
:param res : dict : salmap predictions with "image name" key and ndarray as values
:returns: average_score: float (mean CC score computed by averaging scores for all the images)
"""
assert(gts.keys() == res.keys())
imgIds = res.keys()
score = []
for id in imgIds:
img = self.imgs[id]
fixations = gts[id]
gtsAnn = {}
gtsAnn['image_id'] = id
gtsAnn['fixations'] = fixations
fixationmap = self.saliconRes.buildFixMap([gtsAnn])
height,width = (img['height'],img['width'])
salMap = self.saliconRes.decodeImage(res[id])
mapheight,mapwidth = np.shape(salMap)
salMap = scipy.ndimage.zoom(salMap, (float(height)/mapheight, float(width)/mapwidth), order=3)
score.append(self.calc_score(fixationmap,salMap))
average_score = np.mean(np.array(score))
return average_score, np.array(score)
def method(self):
return "CC"
if __name__=="__main__":
nss = CC()
#more tests here