-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.py
108 lines (84 loc) · 4.5 KB
/
metrics.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import numpy as np
#from pysaliency import roc
from pysaliency.roc import general_roc
import torch
#from torch import std_mean
def log_likelihood(log_density, fixation_mask, weights=None):
# if weights is None:
# weights = torch.ones(log_density.shape[0])
weights = len(weights) * weights.view(-1, 1, 1) / weights.sum()
dense_mask = fixation_mask.to_dense()
fixation_count = dense_mask.sum(dim=(-1, -2), keepdim=True)
ll = torch.mean(
weights.type('torch.cuda.FloatTensor') * torch.sum(log_density.type('torch.cuda.FloatTensor') * dense_mask.type(
'torch.cuda.FloatTensor'), dim=(-1, -2), keepdim=True) / fixation_count.type('torch.cuda.FloatTensor')
)
return (ll + np.log(log_density.shape[-1] * log_density.shape[-2])) / np.log(2)
def log_likelihood_std(log_density, fixation_mask, weights=None):
# if weights is None:
# weights = torch.ones(log_density.shape[0])
weights = len(weights) * weights.view(-1, 1, 1) / weights.sum()
dense_mask = fixation_mask.to_dense()
fixation_count = dense_mask.sum(dim=(-1, -2), keepdim=True)
ll = torch.std(
weights.type('torch.cuda.FloatTensor') * torch.sum(log_density.type('torch.cuda.FloatTensor') * dense_mask.type(
'torch.cuda.FloatTensor'), dim=(-1, -2), keepdim=True) / fixation_count.type('torch.cuda.FloatTensor')
)
return (ll + np.log(log_density.shape[-1] * log_density.shape[-2])) / np.log(2)
def nss(log_density, fixation_mask, weights=None):
weights = len(weights) * weights.view(-1, 1, 1) / weights.sum()
dense_mask = fixation_mask.to_dense()
fixation_count = dense_mask.sum(dim=(-1, -2), keepdim=True)
density = torch.exp(log_density)
mean = torch.mean(density, dim=(-1, -2), keepdim=True)
std = torch.std(density, dim=(-1, -2), keepdim=True)
saliency_map = (density - mean) / std
nss = torch.mean(
weights.type('torch.cuda.FloatTensor') * torch.sum(saliency_map.type('torch.cuda.FloatTensor') * dense_mask.type(
'torch.cuda.FloatTensor'), dim=(-1, -2), keepdim=True) / fixation_count.type('torch.cuda.FloatTensor')
)
return nss
def nss_std(log_density, fixation_mask, weights=None):
weights = len(weights) * weights.view(-1, 1, 1) / weights.sum()
dense_mask = fixation_mask.to_dense()
fixation_count = dense_mask.sum(dim=(-1, -2), keepdim=True)
density = torch.exp(log_density)
mean = torch.mean(density, dim=(-1, -2), keepdim=True)
std = torch.std(density, dim=(-1, -2), keepdim=True)
saliency_map = (density - mean) / std
nss = torch.std(
weights.type('torch.cuda.FloatTensor') * torch.sum(saliency_map.type('torch.cuda.FloatTensor') * dense_mask.type(
'torch.cuda.FloatTensor'), dim=(-1, -2), keepdim=True) / fixation_count.type('torch.cuda.FloatTensor')
)
return nss
def auc(log_density, fixation_mask, weights=None):
weights = len(weights) * weights / weights.sum()
# TODO: This doesn't account for multiple fixations in the same location!
def image_auc(log_density, fixation_mask):
dense_mask = fixation_mask # .to_dense()
positives = torch.masked_select(log_density, dense_mask.type(
torch.cuda.ByteTensor)).detach().cpu().numpy()
negatives = log_density.flatten().detach().cpu().numpy()
auc, _, _ = general_roc(positives.astype(
'double'), negatives.astype('double'))
print('AUC:', auc)
return torch.tensor(auc) # .type('torch.cuda.FloatTensor')
fix_n = fixation_mask.to_dense()
return torch.mean(weights.cpu() * torch.tensor([
image_auc(log_density[i], fix_n[i]) for i in range(log_density.shape[0])
]).type('torch.DoubleTensor'))
def auc_std(log_density, fixation_mask, weights=None):
weights = len(weights) * weights / weights.sum()
# TODO: This doesn't account for multiple fixations in the same location!
def image_auc(log_density, fixation_mask):
dense_mask = fixation_mask # .to_dense()
positives = torch.masked_select(log_density, dense_mask.type(
torch.cuda.ByteTensor)).detach().cpu().numpy()
negatives = log_density.flatten().detach().cpu().numpy()
auc, _, _ = general_roc(positives.astype(
'double'), negatives.astype('double'))
return torch.tensor(auc) # .type('torch.cuda.FloatTensor')
fix_n = fixation_mask.to_dense()
return torch.std(weights.cpu() * torch.tensor([
image_auc(log_density[i], fix_n[i]) for i in range(log_density.shape[0])
]).type('torch.DoubleTensor'))