-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
164 lines (129 loc) · 4.9 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
'''
Reference: https://github.com/SeokjuLee/Insta-DM/blob/master/utils.py
'''
from __future__ import division
import shutil
import numpy as np
import torch
from path import Path
import datetime
from collections import OrderedDict
from matplotlib import cm
from matplotlib.colors import ListedColormap, LinearSegmentedColormap
def viz_flow(u, v, logscale=True, scaledown=6, output=False):
"""
topleft is zero, u is horiz, v is vertical
red is 3 o'clock, yellow is 6, light blue is 9, blue/purple is 12
"""
colorwheel = makecolorwheel()
ncols = colorwheel.shape[0]
radius = np.sqrt(u**2 + v**2)
if output:
print("Maximum flow magnitude: %04f" % np.max(radius))
if logscale:
radius = np.log(radius + 1)
if output:
print("Maximum flow magnitude (after log): %0.4f" % np.max(radius))
radius = radius / scaledown
if output:
print("Maximum flow magnitude (after scaledown): %0.4f" % np.max(radius))
rot = np.arctan2(-v, -u) / np.pi
fk = (rot+1)/2 * (ncols-1) # -1~1 maped to 0~ncols
k0 = fk.astype(np.uint8) # 0, 1, 2, ..., ncols
k1 = k0+1
k1[k1 == ncols] = 0
f = fk - k0
ncolors = colorwheel.shape[1]
img = np.zeros(u.shape+(ncolors,))
for i in range(ncolors):
tmp = colorwheel[:,i]
col0 = tmp[k0]
col1 = tmp[k1]
col = (1-f)*col0 + f*col1
idx = radius <= 1
# increase saturation with radius
col[idx] = 1 - radius[idx]*(1-col[idx])
# out of range
col[~idx] *= 0.75
img[:,:,i] = np.floor(255*col).astype(np.uint8)
return img.astype(np.uint8)
def makecolorwheel():
# Create a colorwheel for visualization
RY = 15
YG = 6
GC = 4
CB = 11
BM = 13
MR = 6
ncols = RY + YG + GC + CB + BM + MR
colorwheel = np.zeros((ncols,3))
col = 0
# RY
colorwheel[0:RY,0] = 1
colorwheel[0:RY,1] = np.arange(0,1,1./RY)
col += RY
# YG
colorwheel[col:col+YG,0] = np.arange(1,0,-1./YG)
colorwheel[col:col+YG,1] = 1
col += YG
# GC
colorwheel[col:col+GC,1] = 1
colorwheel[col:col+GC,2] = np.arange(0,1,1./GC)
col += GC
# CB
colorwheel[col:col+CB,1] = np.arange(1,0,-1./CB)
colorwheel[col:col+CB,2] = 1
col += CB
# BM
colorwheel[col:col+BM,2] = 1
colorwheel[col:col+BM,0] = np.arange(0,1,1./BM)
col += BM
# MR
colorwheel[col:col+MR,2] = np.arange(1,0,-1./MR)
colorwheel[col:col+MR,0] = 1
return colorwheel
def high_res_colormap(low_res_cmap, resolution=1000, max_value=1):
# Construct the list colormap, with interpolated values for higer resolution
# For a linear segmented colormap, you can just specify the number of point in
# cm.get_cmap(name, lutsize) with the parameter lutsize
x = np.linspace(0,1,low_res_cmap.N)
low_res = low_res_cmap(x)
new_x = np.linspace(0,max_value,resolution)
high_res = np.stack([np.interp(new_x, x, low_res[:,i]) for i in range(low_res.shape[1])], axis=1)
return ListedColormap(high_res)
def opencv_rainbow(resolution=1000):
# Construct the opencv equivalent of Rainbow
opencv_rainbow_data = (
(0.000, (1.00, 0.00, 0.00)),
(0.400, (1.00, 1.00, 0.00)),
(0.600, (0.00, 1.00, 0.00)),
(0.800, (0.00, 0.00, 1.00)),
(1.000, (0.60, 0.00, 1.00))
)
return LinearSegmentedColormap.from_list('opencv_rainbow', opencv_rainbow_data, resolution)
COLORMAPS = {'rainbow': opencv_rainbow(),
'magma': high_res_colormap(cm.get_cmap('magma')),
'bone': cm.get_cmap('bone', 10000)}
def tensor2array(tensor, max_value=None, colormap='rainbow'):
tensor = tensor.detach().cpu()
if max_value is None:
max_value = tensor.max().item()
if tensor.ndimension() == 2 or tensor.size(0) == 1:
norm_array = tensor.squeeze().numpy()/max_value
array = COLORMAPS[colormap](norm_array).astype(np.float32)
array = array.transpose(2, 0, 1)
elif tensor.ndimension() == 3:
assert(tensor.size(0) == 3)
array = 0.5 + tensor.numpy()*0.5
return array
def save_checkpoint(epoch, save_freq, save_path, dispnet_state, ego_pose_state, obj_pose_state, initial_ego_pose_state, is_best, filename='checkpoint.pth.tar'):
file_prefixes = ['dispnet', 'ego_pose', 'obj_pose', 'initial_ego_pose']
states = [dispnet_state, ego_pose_state, obj_pose_state, initial_ego_pose_state]
for (prefix, state) in zip(file_prefixes, states):
torch.save(state, save_path/'{}_{}'.format(prefix,filename))
if epoch % save_freq == 0:
for (prefix, state) in zip(file_prefixes, states):
torch.save(state, save_path/'{}_{}_{}'.format(prefix, epoch, filename))
if is_best:
for prefix in file_prefixes:
shutil.copyfile(save_path/'{}_{}'.format(prefix,filename), save_path/'{}_model_best.pth.tar'.format(prefix))