-
Notifications
You must be signed in to change notification settings - Fork 21
/
utils.py
79 lines (66 loc) · 2.08 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
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
import torch
import cv2
import matplotlib as mpl
import matplotlib.cm as cm
import numpy as np
def nanmean(v, *args, inplace=False, **kwargs):
if not inplace:
v = v.clone()
is_nan = torch.isnan(v)
v[is_nan] = 0
return v.sum(*args, **kwargs) / (~is_nan).float().sum(*args, **kwargs)
class AverageMeter(object):
def __init__(self, name, fmt=':f'):
self.name = name
self.fmt = fmt
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def __str__(self):
fmtstr = '{name} :{avg' + self.fmt + '}\n'
return fmtstr.format(**self.__dict__)
def write_turbo_depth_metric(path, toplot, vmin=0.001, vmax=10.0):
v_min = vmin
v_max = vmax
normalizer = mpl.colors.Normalize(vmin=v_min, vmax=v_max)
mapper = cm.ScalarMappable(norm=normalizer, cmap='turbo')
colormapped_im = (mapper.to_rgba(toplot)[:,:,:3]*255).astype(np.uint8)
cv2.imwrite(path, colormapped_im[:,:,[2,1,0]])
def output_to_depth(level, min_depth, max_depth):
"""Convert network's sigmoid output into depth prediction"""
min_out = 1 / max_depth
max_out = 1 / min_depth
scaled_out = min_out + (max_out - min_out) * level
depth = 1.312 / scaled_out
return depth
def readlines(filename):
with open(filename, 'r') as f:
lines = f.read().splitlines()
return lines
def normalize_image(x):
ma = float(x.max().cpu().data)
mi = float(x.min().cpu().data)
d = ma - mi if ma != mi else 1e5
return (x - mi) / d
def sec_to_hm(t):
t = int(t)
s = t % 60
t //= 60
m = t % 60
t //= 60
return t, m, s
def sec_to_hm_str(t):
h, m, s = sec_to_hm(t)
return "{:02d}h{:02d}m{:02d}s".format(h, m, s)