-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
89 lines (80 loc) · 2.63 KB
/
utils.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
import numpy as np
import torch
import os
import logging
def create_log_dir(path='./log'):
path += '/'
if not os.path.exists(path):
os.makedirs(path)
logger = logging.getLogger(path)
logger.setLevel(logging.DEBUG)
fh = logging.FileHandler(path + 'log.txt')
fh.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
logger.addHandler(fh)
logger.addHandler(ch)
return logger
class ConfMatrix(object):
def __init__(self, num_classes):
self.num_classes = num_classes
self.mat = None
def update(self, pred, target):
n = self.num_classes
if self.mat is None:
self.mat = torch.zeros((n, n), dtype=torch.int64, device=pred.device)
with torch.no_grad():
k = (target >= 0) & (target < n)
inds = n * target[k].to(torch.int64) + pred[k]
self.mat += torch.bincount(inds, minlength=n ** 2).reshape(n, n)
def get_metrics(self):
h = self.mat.float()
acc = torch.diag(h).sum() / h.sum()
iu = torch.diag(h) / (h.sum(1) + h.sum(0) - torch.diag(h))
return torch.mean(iu).cpu().numpy(), acc.cpu().numpy()
def depth_error(x_pred, x_output):
device = x_pred.device
binary_mask = (torch.sum(x_output, dim=1) != 0).unsqueeze(1).to(device)
x_pred_true = x_pred.masked_select(binary_mask)
x_output_true = x_output.masked_select(binary_mask)
abs_err = torch.abs(x_pred_true - x_output_true)
rel_err = torch.abs(x_pred_true - x_output_true) / x_output_true
return (
torch.sum(abs_err) / torch.nonzero(binary_mask, as_tuple=False).size(0)
).item(), (
torch.sum(rel_err) / torch.nonzero(binary_mask, as_tuple=False).size(0)
).item()
def normal_error(x_pred, x_output):
binary_mask = torch.sum(x_output, dim=1) != 0
error = (
torch.acos(
torch.clamp(
torch.sum(x_pred * x_output, 1).masked_select(binary_mask), -1, 1
)
)
.detach()
.cpu()
.numpy()
)
error = np.degrees(error)
return (
np.mean(error),
np.median(error),
np.mean(error < 11.25),
np.mean(error < 22.5),
np.mean(error < 30),
)
# for calculating \Delta_m
def delta_fn_cityscapes(a):
delta_stats = [
"mean iou",
"pix acc",
"abs err",
"rel err",
]
BASE = np.array(
[0.7401, 0.9316, 0.0125, 27.77]
) # base results from CAGrad (single task / independent)
SIGN = np.array([1, 1, 0, 0])
KK = np.ones(4) * -1
return (KK ** SIGN * (a - BASE) / BASE).mean() * 100.0 # * 100 for percentage