-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.py
74 lines (61 loc) · 1.86 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
from __future__ import absolute_import, division, print_function
import torch
import matplotlib.pyplot as plt
_DEPTH_COLORMAP = plt.get_cmap('plasma', 256) # for plotting
import torch.nn.functional as F
def readlines(filename):
"""Read all the lines in a text file and return as a list
"""
with open(filename, 'r') as f:
lines = f.read().splitlines()
return lines
def normalize_image(x):
"""Rescale image pixels to span range [0, 1]
"""
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):
"""Convert time in seconds to time in hours, minutes and seconds
e.g. 10239 -> (2, 50, 39)
"""
t = int(t)
s = t % 60
t //= 60
m = t % 60
t //= 60
return t, m, s
def sec_to_hm_str(t):
"""Convert time in seconds to a nice string
e.g. 10239 -> '02h50m39s'
"""
h, m, s = sec_to_hm(t)
return "{:02d}h{:02d}m{:02d}s".format(h, m, s)
def colormap(inputs, normalize=True, torch_transpose=True):
if isinstance(inputs, torch.Tensor):
inputs = inputs.detach().cpu().numpy()
# vis = inputs.detach().cpu().numpy()
vis = inputs
if normalize:
ma = float(vis.max())
mi = float(vis.min())
d = ma - mi if ma != mi else 1e5
vis = (vis - mi) / d
if vis.ndim == 4:
vis = vis.transpose([0, 2, 3, 1])
vis = _DEPTH_COLORMAP(vis)
vis = vis[:, :, :, 0, :3]
if torch_transpose:
vis = vis.transpose(0, 3, 1, 2)
elif vis.ndim == 3:
vis = _DEPTH_COLORMAP(vis)
vis = vis[:, :, :, :3]
if torch_transpose:
vis = vis.transpose(0, 3, 1, 2)
elif vis.ndim == 2:
vis = _DEPTH_COLORMAP(vis)
vis = vis[..., :3]
if torch_transpose:
vis = vis.transpose(2, 0, 1)
return vis