-
Notifications
You must be signed in to change notification settings - Fork 0
/
uncertainty.py
95 lines (58 loc) · 2.56 KB
/
uncertainty.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
# Copyright (c) OpenMMLab. All rights reserved.
import numpy as np
import torch
import torch.nn.functional as F
class Uncertainty:
def entropy(self, logits):
# Compute entropy given logits
# Logits to classification scores
scores = F.softmax(logits, dim=-1)
entropy = -torch.sum(scores * torch.log(scores), dim = 1)
return entropy
def max_class_entropy(self, logits):
# Compute entropy of the maximum scoring class for sigmoid-based detectors
# Logits to classification scores
scores, _ = logits.sigmoid().max(dim=-1)
entropy = -(scores * torch.log(scores) + (1 - scores) * torch.log(1 - scores))
return entropy
def avg_entropy(self, logits):
# Compute average entropy of scores for sigmoid-based detectors
# Logits to classification scores
scores = logits.sigmoid()
entropy = -(scores * torch.log(scores) + (1 - scores) * torch.log(1 - scores))
return entropy.mean(dim=-1)
def ds(self, logits):
# Compute dempster-shafer
# Logits to classification scores
K = logits.shape[1]
ds = K / (K + torch.sum(torch.exp(logits), dim = 1))
return ds
def gaussian_entropy(self, covariance, D=4, pi = 3.14159265359):
# Compute entropy of predicted gaussian bounding boxes
# Assume diagonal prediction
determinant = torch.prod(covariance, -1)
sp_entropy = (D / 2) * (1 + np.log(2 * pi)) + (1 / 2) * torch.log(determinant)
return sp_entropy
def determinant(self, covariance):
# Compute determinant of predicted gaussian bounding boxes
# Assume diagonal prediction
determinant = torch.prod(covariance, -1)
return determinant
def trace(self, covariance):
# Compute trace of predicted gaussian bounding boxes
# Assume diagonal prediction
trace = torch.sum(covariance, -1)
return trace
def get_uncertainties(self, uncertainties, logits=None, covariance=None):
result = {}
if "cls_type" in uncertainties and logits is not None:
result['cls'] = {}
for unc in uncertainties['cls_type']:
unc_func = getattr(self, unc)
result['cls'][unc] = unc_func(self, logits)
if "loc_type" in uncertainties and covariance is not None:
result['loc'] = {}
for unc in uncertainties['loc_type']:
unc_func = getattr(self, unc)
result['loc'][unc] = unc_func(self, covariance)
return result