-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
285 lines (222 loc) · 7.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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import os
import subprocess
import sys
import time
from inspect import isclass
import numpy as np
class TimerBlock:
def __init__(self, title):
print(("{}".format(title)))
def __enter__(self):
self.start = time.clock()
return self
def __exit__(self, exc_type, exc_value, traceback):
self.end = time.clock()
self.interval = self.end - self.start
if exc_type is not None:
self.log("Operation failed\n")
else:
self.log("Operation finished\n")
def log(self, string):
duration = time.clock() - self.start
units = 's'
if duration > 60:
duration = duration / 60.
units = 'm'
print(" [{:.3f}{}] {}".format(duration, units, string), flush=True)
def module_to_dict(module, exclude=None):
if exclude is None:
exclude = []
return dict([(x, getattr(module, x)) for x in dir(module)
if isclass(getattr(module, x)) and x not in exclude and getattr(module, x) not in exclude])
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
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 copy_arguments(main_dict, main_filepath='', save_dir='./'):
pycmd = 'python3 ' + main_filepath + ' \\\n'
_main_dict = main_dict.copy()
_main_dict['--name'] = _main_dict['--name'] + '_replicate'
for k in _main_dict.keys():
if 'batchNorm' in k:
pycmd += ' ' + k + ' ' + str(_main_dict[k]) + ' \\\n'
elif type(_main_dict[k]) == bool and _main_dict[k]:
pycmd += ' ' + k + ' \\\n'
elif type(_main_dict[k]) == list:
pycmd += ' ' + k + ' ' + \
' '.join([str(f) for f in _main_dict[k]]) + ' \\\n'
elif type(_main_dict[k]) != bool:
pycmd += ' ' + k + ' ' + str(_main_dict[k]) + ' \\\n'
pycmd = '#!/bin/bash\n' + pycmd[:-2]
job_script = os.path.join(save_dir, 'job.sh')
file = open(job_script, 'w')
file.write(pycmd)
file.close()
return
def block_print():
sys.stdout = open(os.devnull, 'w')
def get_pred_flag(height, width):
pred_flag = np.ones((height, width, 3), dtype=np.uint8)
pred_values = np.zeros((height, width, 3), dtype=np.uint8)
hstart = int((192. / 1200) * height)
wstart = int((224. / 1920) * width)
h_step = int((24. / 1200) * height)
w_step = int((32. / 1920) * width)
pred_flag[hstart:hstart + h_step, -wstart + 0 * w_step:-wstart + 1 * w_step, :] = np.asarray([0, 0, 0])
pred_flag[hstart:hstart + h_step, -wstart + 1 * w_step:-wstart + 2 * w_step, :] = np.asarray([0, 0, 0])
pred_flag[hstart:hstart + h_step, -wstart + 2 * w_step:-wstart + 3 * w_step, :] = np.asarray([0, 0, 0])
pred_values[hstart:hstart + h_step, -wstart + 0 * w_step:-wstart + 1 * w_step, :] = np.asarray([0, 0, 255])
pred_values[hstart:hstart + h_step, -wstart + 1 * w_step:-wstart + 2 * w_step, :] = np.asarray([0, 255, 0])
pred_values[hstart:hstart + h_step, -wstart + 2 * w_step:-wstart + 3 * w_step, :] = np.asarray([255, 0, 0])
return pred_flag, pred_values
def create_pipe(pipe_filename, width, height, frame_rate=60, quite=True):
# default extension and tonemapper
pix_fmt = 'rgb24'
out_fmt = 'yuv420p'
codec = 'h264'
command = [
'ffmpeg',
'-threads',
'2', # number of threads to start
'-y', # (optional) overwrite output file if it exists
'-f',
'rawvideo', # input format
'-vcodec',
'rawvideo', # input codec
'-s',
str(width) + 'x' + str(height), # size of one frame
'-pix_fmt',
pix_fmt, # input pixel format
'-r',
str(frame_rate), # frames per second
'-i',
'-', # The imput comes from a pipe
'-an', # Tells FFMPEG not to expect any audio
'-codec:v',
codec, # output codec
'-crf',
'18',
# compression quality for h264 (maybe h265 too?) - http://slhck.info/video/2017/02/24/crf-guide.html
# '-compression_level', '10', # compression level for libjpeg if doing lossy depth
'-strict',
'-2', # experimental 16 bit support nessesary for gray16le
'-pix_fmt',
out_fmt, # output pixel format
'-s',
str(width) + 'x' + str(height), # output size
pipe_filename
]
cmd = ' '.join(command)
if not quite:
print('openning a pip ....\n' + cmd + '\n')
# open the pipe, and ignore stdout and stderr output
dev_null = open(os.devnull, 'wb')
return subprocess.Popen(command, stdin=subprocess.PIPE, stdout=dev_null, stderr=dev_null, close_fds=True)
def make_color_wheel():
"""
Generate color wheel according Middlebury color code
:return: Color wheel
"""
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] = 255
colorwheel[0:ry, 1] = np.transpose(np.floor(255 * np.arange(0, ry) / ry))
col += ry
# yg
colorwheel[col:col + yg, 0] = 255 - np.transpose(np.floor(255 * np.arange(0, yg) / yg))
colorwheel[col:col + yg, 1] = 255
col += yg
# gc
colorwheel[col:col + gc, 1] = 255
colorwheel[col:col + gc, 2] = np.transpose(np.floor(255 * np.arange(0, gc) / gc))
col += gc
# cb
colorwheel[col:col + cb, 1] = 255 - np.transpose(np.floor(255 * np.arange(0, cb) / cb))
colorwheel[col:col + cb, 2] = 255
col += cb
# bm
colorwheel[col:col + bm, 2] = 255
colorwheel[col:col + bm, 0] = np.transpose(np.floor(255 * np.arange(0, bm) / bm))
col += +bm
# mr
colorwheel[col:col + mr, 2] = 255 - np.transpose(np.floor(255 * np.arange(0, mr) / mr))
colorwheel[col:col + mr, 0] = 255
return colorwheel
def compute_color(u, v):
"""
compute optical flow color map
:param u: optical flow horizontal map
:param v: optical flow vertical map
:return: optical flow in color code
"""
[h, w] = u.shape
img = np.zeros([h, w, 3])
nan_idx = np.isnan(u) | np.isnan(v)
u[nan_idx] = 0
v[nan_idx] = 0
colorwheel = make_color_wheel()
ncols = np.size(colorwheel, 0)
rad = np.sqrt(u**2 + v**2)
a = np.arctan2(-v, -u) / np.pi
fk = (a + 1) / 2 * (ncols - 1) + 1
k0 = np.floor(fk).astype(int)
k1 = k0 + 1
k1[k1 == ncols + 1] = 1
f = fk - k0
for i in range(0, np.size(colorwheel, 1)):
tmp = colorwheel[:, i]
col0 = tmp[k0 - 1] / 255
col1 = tmp[k1 - 1] / 255
col = (1 - f) * col0 + f * col1
idx = rad <= 1
col[idx] = 1 - rad[idx] * (1 - col[idx])
notidx = np.logical_not(idx)
col[notidx] *= 0.75
img[:, :, i] = np.uint8(np.floor(255 * col * (1 - nan_idx)))
return img
def flow_to_image(flow):
"""
Convert flow into middlebury color code image
:param flow: optical flow map
:return: optical flow image in middlebury color
"""
u = flow[:, :, 0]
v = flow[:, :, 1]
maxu = -999.
maxv = -999.
minu = 999.
minv = 999.
unknown_flow_threshold = 1e7
idx_unknow = (abs(u) > unknown_flow_threshold) | (abs(v) > unknown_flow_threshold)
u[idx_unknow] = 0
v[idx_unknow] = 0
maxu = max(maxu, np.max(u))
minu = min(minu, np.min(u))
maxv = max(maxv, np.max(v))
minv = min(minv, np.min(v))
rad = np.sqrt(u**2 + v**2)
maxrad = max(-1, np.max(rad))
u = u / (maxrad + np.finfo(float).eps)
v = v / (maxrad + np.finfo(float).eps)
img = compute_color(u, v)
idx = np.repeat(idx_unknow[:, :, np.newaxis], 3, axis=2)
img[idx] = 0
return np.uint8(img)