-
Notifications
You must be signed in to change notification settings - Fork 40
/
util.py
349 lines (282 loc) · 9.66 KB
/
util.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import os
import numpy as np
from numpy.random import uniform
import torch
from torch.utils.data import Dataset
import cv2
def process_path(directory, create=False):
directory = os.path.expanduser(directory)
directory = os.path.normpath(directory)
directory = os.path.abspath(directory)
if create:
try:
os.makedirs(directory)
except:
pass
return directory
def split_path(directory):
directory = process_path(directory)
name, ext = os.path.splitext(os.path.basename(directory))
return os.path.dirname(directory), name, ext
# From torchnet
# https://github.com/pytorch/tnt/blob/master/torchnet/transform.py
def compose(transforms):
'Composes list of transforms (each accept and return one item)'
assert isinstance(transforms, list)
for transform in transforms:
assert callable(transform), 'list of functions expected'
def composition(obj):
'Composite function'
for transform in transforms:
obj = transform(obj)
return obj
return composition
def map_range(x, low=0, high=1):
return np.interp(x, [x.min(), x.max()], [low, high]).astype(x.dtype)
def str2bool(x):
if x is None or x.lower() in ['no', 'false', 'f', '0']:
return False
else:
return True
def cv2torch(np_img):
rgb = np_img[:, :, (2, 1, 0)]
return torch.from_numpy(rgb.swapaxes(1, 2).swapaxes(0, 1))
def torch2cv(t_img):
return t_img.numpy().swapaxes(0, 2).swapaxes(0, 1)[:, :, (2, 1, 0)]
def resize(x, size):
return cv2.resize(x, size)
class Exposure(object):
def __init__(self, stops=0.0, gamma=1.0):
self.stops = stops
self.gamma = gamma
def process(self, img):
return np.clip(img * (2 ** self.stops), 0, 1) ** self.gamma
class PercentileExposure(object):
def __init__(self, gamma=2.0, low_perc=10, high_perc=90, randomize=False):
if randomize:
gamma = uniform(1.8, 2.2)
low_perc = uniform(0, 15)
high_perc = uniform(85, 100)
self.gamma = gamma
self.low_perc = low_perc
self.high_perc = high_perc
def __call__(self, x):
low, high = np.percentile(x, (self.low_perc, self.high_perc))
return map_range(np.clip(x, low, high)) ** (1 / self.gamma)
class BaseTMO(object):
def __call__(self, img):
return self.op.process(img)
class Reinhard(BaseTMO):
def __init__(
self,
intensity=-1.0,
light_adapt=0.8,
color_adapt=0.0,
gamma=2.0,
randomize=False,
):
if randomize:
gamma = uniform(1.8, 2.2)
intensity = uniform(-1.0, 1.0)
light_adapt = uniform(0.8, 1.0)
color_adapt = uniform(0.0, 0.2)
self.op = cv2.createTonemapReinhard(
gamma=gamma,
intensity=intensity,
light_adapt=light_adapt,
color_adapt=color_adapt,
)
class Mantiuk(BaseTMO):
def __init__(self, saturation=1.0, scale=0.75, gamma=2.0, randomize=False):
if randomize:
gamma = uniform(1.8, 2.2)
scale = uniform(0.65, 0.85)
self.op = cv2.createTonemapMantiuk(
saturation=saturation, scale=scale, gamma=gamma
)
class Drago(BaseTMO):
def __init__(self, saturation=1.0, bias=0.85, gamma=2.0, randomize=False):
if randomize:
gamma = uniform(1.8, 2.2)
bias = uniform(0.7, 0.9)
self.op = cv2.createTonemapDrago(
saturation=saturation, bias=bias, gamma=gamma
)
class Durand(BaseTMO):
def __init__(
self,
contrast=3,
saturation=1.0,
sigma_space=8,
sigma_color=0.4,
gamma=2.0,
randomize=False,
):
if randomize:
gamma = uniform(1.8, 2.2)
contrast = uniform(3.5)
self.op = cv2.createTonemapDurand(
contrast=contrast,
saturation=saturation,
sigma_space=sigma_space,
sigma_color=sigma_color,
gamma=gamma,
)
TMO_DICT = {
'exposure': Exposure,
'reinhard': Reinhard,
'mantiuk': Mantiuk,
'drago': Drago,
'durand': Durand,
}
def tone_map(img, tmo_name, **kwargs):
return TMO_DICT[tmo_name](**kwargs)(img)
TRAIN_TMO_DICT = {
'exposure': PercentileExposure,
'reinhard': Reinhard,
'mantiuk': Mantiuk,
'drago': Drago,
'durand': Durand,
}
def random_tone_map(x):
tmos = list(TRAIN_TMO_DICT.keys())
choice = np.random.randint(0, len(tmos))
tmo = TRAIN_TMO_DICT[tmos[choice]](randomize=True)
return map_range(tmo(x))
def create_tmo_param_from_args(opt):
if opt.tmo == 'exposure':
return {k: opt.get(k) for k in ['gamma', 'stops']}
else: # TODO: Implement for others
return {}
def clamped_gaussian(mean, std, min_value, max_value):
if max_value <= min_value:
return mean
factor = 0.99
while True:
ret = np.random.normal(mean, std)
if ret > min_value and ret < max_value:
break
else:
std = std * factor
ret = np.random.normal(mean, std)
return ret
def exponential_size(val):
return val * (np.exp(-np.random.uniform())) / (np.exp(0) + 1)
# Accepts hwc-bgr image
def index_gauss(
img,
precision=None,
crop_size=None,
random_size=True,
ratio=None,
seed=None,
):
"""Returns indices (Numpy slice) of an image crop sampled spatially using a gaussian distribution.
Args:
img (Array): Image as a Numpy array (OpenCV view, hwc-BGR).
precision (list or tuple, optional): Floats representing the precision
of the Gaussians (default [1, 4])
crop_size (list or tuple, optional): Ints representing the crop size
(default [img_width/4, img_height/4]).
random_size (bool, optional): If true, randomizes the crop size with
a minimum of crop_size. It uses an exponential distribution such
that smaller crops are more likely (default True).
ratio (float, optional): Keep a constant crop ratio width/height (default None).
seed (float, optional): Set a seed for np.random.seed() (default None)
Note:
- If `ratio` is None then the resulting ratio can be anything.
- If `random_size` is False and `ratio` is not None, the largest dimension
dictated by the ratio is adjusted accordingly:
- `crop_size` is (w=100, h=10) and `ratio` = 9 ==> (w=90, h=10)
- `crop_size` is (w=100, h=10) and `ratio` = 0.2 ==> (w=100, h=20)
"""
np.random.seed(seed)
dims = {'w': img.shape[1], 'h': img.shape[0]}
if precision is None:
precision = {'w': 1, 'h': 4}
else:
precision = {'w': precision[0], 'h': precision[1]}
if crop_size is None:
crop_size = {key: int(dims[key] / 4) for key in dims}
else:
crop_size = {'w': crop_size[0], 'h': crop_size[1]}
if ratio is not None:
ratio = max(ratio, 1e-4)
if ratio > 1:
if random_size:
crop_size['h'] = int(
max(crop_size['h'], exponential_size(dims['h']))
)
crop_size['w'] = int(np.round(crop_size['h'] * ratio))
else:
if random_size:
crop_size['w'] = int(
max(crop_size['w'], exponential_size(dims['w']))
)
crop_size['h'] = int(np.round(crop_size['w'] / ratio))
else:
if random_size:
crop_size = {
key: int(max(val, exponential_size(dims[key])))
for key, val in crop_size.items()
}
centers = {
key: int(
clamped_gaussian(
dim / 2,
crop_size[key] / precision[key],
min(int(crop_size[key] / 2), dim),
max(int(dim - crop_size[key] / 2), 0),
)
)
for key, dim in dims.items()
}
starts = {
key: max(center - int(crop_size[key] / 2), 0)
for key, center in centers.items()
}
ends = {key: start + crop_size[key] for key, start in starts.items()}
return np.s_[starts['h'] : ends['h'], starts['w'] : ends['w'], :]
def slice_gauss(
img,
precision=None,
crop_size=None,
random_size=True,
ratio=None,
seed=None,
):
"""Returns a cropped sample from an image array using :func:`index_gauss`"""
return img[index_gauss(img, precision, crop_size, random_size, ratio)]
class DirectoryDataset(Dataset):
def __init__(
self,
data_root_path='hdr_data',
data_extensions=['.hdr', '.exr'],
load_fn=None,
preprocess=None,
):
super(DirectoryDataset, self).__init__()
data_root_path = process_path(data_root_path)
self.file_list = []
for root, _, fnames in sorted(os.walk(data_root_path)):
for fname in fnames:
if any(
fname.lower().endswith(extension)
for extension in data_extensions
):
self.file_list.append(os.path.join(root, fname))
if len(self.file_list) == 0:
msg = 'Could not find any files with extensions:\n[{0}]\nin\n{1}'
raise RuntimeError(
msg.format(', '.join(data_extensions), data_root_path)
)
self.preprocess = preprocess
def __getitem__(self, index):
dpoint = cv2.imread(
self.file_list[index], flags=cv2.IMREAD_ANYDEPTH + cv2.IMREAD_COLOR
)
if self.preprocess is not None:
dpoint = self.preprocess(dpoint)
return dpoint
def __len__(self):
return len(self.file_list)