-
Notifications
You must be signed in to change notification settings - Fork 6
/
solver.py
403 lines (331 loc) · 15.9 KB
/
solver.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import timeit
import numpy as np
import sklearn
from skimage import morphology
from sklearn.cluster import MiniBatchKMeans
from params import IntrinsicParameters
from decomposition import IntrinsicDecomposition
from energy import IntrinsicEnergy
from optimization import minimize_l1, minimize_l2
from krahenbuhl2013.krahenbuhl2013 import DenseCRF
# from pydensecrf.densecrf import DenseCRF2D as DenseCRF
# from numba import vectorize
class IntrinsicSolver(object):
def __init__(self, input, params):
""" Create a new solver with given input and parameters. Nothing
happens until you call ``solve``. """
if isinstance(params, dict):
params = IntrinsicParameters.from_dict(params)
self.params = params
self.input = input
self.energy = IntrinsicEnergy(self.input, params)
def solve(self):
""" Perform all steps. """
if self.params.logging:
t0 = timeit.default_timer()
print("solve...")
# Initialize
self.decomposition = IntrinsicDecomposition(self.params, self.input)
self.decomposition_history = []
self.initialize_intensities()
for i in range(self.params.n_iters):
if self.params.logging:
print(("\nrun: starting iteration %s/%s" % (i+1, self.params.n_iters)))
self.decomposition.iter_num = i
# STAGE 1
self.decomposition.stage_num = 1
self.stage1_optimize_r()
self.remove_unused_intensities()
self.decomposition_history.append(self.decomposition.copy())
if self.decomposition.intensities.shape[0] <= 1:
if self.params.logging:
print("Warning: only 1 reflectance -- exit early")
break
# STAGE 2
self.decomposition.stage_num = 2
if self.params.split_clusters and i == self.params.n_iters - 1:
self.split_label_clusters()
self.stage2_smooth_s()
self.decomposition_history.append(self.decomposition.copy())
# prepare final solution
r, s = self.decomposition.get_r_s()
if self.params.logging:
t1 = timeit.default_timer()
print(("solve (%s s)" % (t1 - t0)))
return r, s, self.decomposition
def prev_decomposition(self):
""" Return the previous decomposition (used to compute the blurred
shading target). """
if self.decomposition_history:
return self.decomposition_history[-1]
else:
return None
def initialize_intensities(self):
""" Initialization: k-means of the input image """
if self.params.logging:
t0 = timeit.default_timer()
print(("initialization: k-means clustering with %s centers..." %
self.params.kmeans_n_clusters))
image_irg = self.input.image_irg
mask_nz = self.input.mask_nz
if self.params.fixed_seed:
# fix the seed when computing things like gradients across
# hyperparameters
random_state = np.random.RandomState(seed=59173)
else:
random_state = None
samples = image_irg[mask_nz[0], mask_nz[1], :]
if samples.shape[0] > self.params.kmeans_max_samples:
print(("image is large: subsampling %s/%s random pixels" %
(self.params.kmeans_max_samples, samples.shape[0])))
samples = sklearn.utils \
.shuffle(samples)[:self.params.kmeans_max_samples, :]
samples[:, 0] *= self.params.kmeans_intensity_scale
kmeans = MiniBatchKMeans(
n_clusters=self.params.kmeans_n_clusters,
compute_labels=True, random_state=random_state)
kmeans.fit(samples)
assert self.params.kmeans_intensity_scale > 0
self.decomposition.intensities = (
kmeans.cluster_centers_[:, 0] /
self.params.kmeans_intensity_scale
)
self.decomposition.chromaticities = (
kmeans.cluster_centers_[:, 1:3]
)
if self.params.logging:
t1 = timeit.default_timer()
print(("clustering done (%s s). intensities:\n%s" %
(t1 - t0, self.decomposition.intensities)))
def stage1_optimize_r(self):
""" Stage 1: dense CRF optimization """
if self.params.logging:
t0 = timeit.default_timer()
print("stage1_optimize_r: compute costs...")
nlabels = self.decomposition.intensities.shape[0]
npixels = self.input.mask_nnz
# use a Python wrapper around the code from [Krahenbuhl et al 2013]
densecrf = DenseCRF(npixels, nlabels)
# unary costs
unary_costs = self.energy.compute_unary_costs(
decomposition=self.decomposition,
prev_decomposition=self.prev_decomposition(),
)
densecrf.set_unary_energy(unary_costs)
# pairwise costs
if self.params.pairwise_weight:
pairwise_costs = self.energy.compute_pairwise_costs(
decomposition=self.decomposition,
)
densecrf.add_pairwise_energy(
pairwise_costs=(self.params.pairwise_weight * pairwise_costs).astype(np.float32),
features=self.energy.get_features().copy(),
)
if self.params.logging:
print(("stage1_optimize_r: optimizing dense crf (%s iters)..." %
self.params.n_crf_iters))
t0crf = timeit.default_timer()
# maximum aposteriori labeling ("x" variable in the paper)
self.decomposition.labels_nz = densecrf.map(self.params.n_crf_iters)
if self.params.logging:
t1crf = timeit.default_timer()
print(("stage1_optimize_r: dense crf done (%s s)" % (t1crf - t0crf)))
if self.params.logging:
t1 = timeit.default_timer()
print(("stage1_optimize_r: done (%s s)" % (t1 - t0)))
def stage2_smooth_s(self):
""" Stage 2: L1 shading smoothness """
if self.params.logging:
t0 = timeit.default_timer()
print('stage2_smooth_s: constructing linear system...')
if self.params.stage2_maintain_median_intensity:
median_intensity = np.median(self.decomposition.intensities)
log_intensities = np.log(self.decomposition.intensities)
# the 'A' matrix (in Ax = b) is in CSR sparse format
A_data, A_rows, A_cols, A_shape, b = \
self.construct_shading_smoothness_system(log_intensities)
if len(b) < 1:
if self.params.logging:
print(('Warning: empty linear system (%s, nlabels=%s)' % (
self.basename, self.cur_intensities.shape[0])))
return
if self.params.logging:
print('solving linear system...')
# solve for the change to the variables, so that we can slightly
# regularize the variables to be near zero (i.e. near the previous
# value).
if self.params.stage2_norm == "L1":
minimize = minimize_l1
elif self.params.stage2_norm == "L2":
minimize = minimize_l2
else:
raise ValueError("Invalid stage2_norm: %s" % self.params.stage2_norm)
delta_intensities = minimize(
A_data, A_rows, A_cols, A_shape, b,
damp=1e-8, logging=self.params.logging,
)
intensities = np.exp(log_intensities + delta_intensities)
if self.params.stage2_maintain_median_intensity:
# Since there's a scale ambiguity and stage1 includes a term that
# depends on absolute shading, keep the median intensity constant.
# This is a pretty small adjustment.
intensities *= median_intensity / np.median(intensities)
self.decomposition.intensities = intensities
if self.params.logging:
t1 = timeit.default_timer()
print(('stage2_smooth_s: done (%s s)' % (t1 - t0)))
def construct_shading_smoothness_system(self, log_intensities):
""" Create a sparse matrix (CSR format) to minimize discontinuities in
the shading channel (by adjusting ``decomposition.intensities``).
:return: A_data, A_rows, A_cols, A_shape, b
"""
rows, cols = self.input.shape[0:2]
mask = self.input.mask
labels = self.decomposition.get_labels()
if self.params.stage2_chromaticity:
# labels represent RGB colors (but we are still only adjusting
# intensity)
log_image_rgb = self.input.log_image_rgb
log_reflectances_rgb = np.log(np.clip(self.decomposition.get_reflectances_rgb(), 1e-5, np.inf))
else:
# labels represent intensities
log_image_gray = self.input.log_image_gray
A_rows = []
A_cols = []
A_data = []
b = []
# Note that in the paper, params.shading_smooth_k = 1, i.e. only the
# immediate pixel neighbors are smoothed. This code is slightly more
# general in that it allows to smooth pixels k units away if you set
# shading_smooth_k > 1, weighted by 1/(k*k).
for k in range(1, self.params.shading_smooth_k + 1):
weight = 1.0 / (k * k)
for i in range(rows - k):
for j in range(cols - k):
if not mask[i, j]:
continue
if mask[i + k, j]:
l0 = labels[i, j]
l1 = labels[i + k, j]
if l0 != l1:
if self.params.stage2_chromaticity:
# RGB interpretation
for c in range(3):
A_rows.append(len(b))
A_cols.append(l0)
A_data.append(weight)
A_rows.append(len(b))
A_cols.append(l1)
A_data.append(-weight)
bval = (log_image_rgb[i, j, c] -
log_image_rgb[i + k, j, c] +
log_reflectances_rgb[l1, c] -
log_reflectances_rgb[l0, c])
b.append(weight * bval)
else:
# intensity interpretation
A_rows.append(len(b))
A_cols.append(l0)
A_data.append(weight)
A_rows.append(len(b))
A_cols.append(l1)
A_data.append(-weight)
bval = (log_image_gray[i, j] -
log_image_gray[i + k, j] +
log_intensities[l1] -
log_intensities[l0])
b.append(weight * bval)
if mask[i, j + k]:
l0 = labels[i, j]
l1 = labels[i, j + k]
if l0 != l1:
if self.params.stage2_chromaticity:
# RGB interpretation
for c in range(3):
A_rows.append(len(b))
A_cols.append(l0)
A_data.append(weight)
A_rows.append(len(b))
A_cols.append(l1)
A_data.append(-weight)
bval = (log_image_rgb[i, j, c] -
log_image_rgb[i, j + k, c] +
log_reflectances_rgb[l1, c] -
log_reflectances_rgb[l0, c])
b.append(weight * bval)
else:
# intensity interpretation
A_rows.append(len(b))
A_cols.append(l0)
A_data.append(weight)
A_rows.append(len(b))
A_cols.append(l1)
A_data.append(-weight)
bval = (log_image_gray[i, j] -
log_image_gray[i, j + k] +
log_intensities[l1] -
log_intensities[l0])
b.append(weight * bval)
A_shape = (len(b), log_intensities.shape[0])
return (
np.array(A_data),
np.array(A_rows),
np.array(A_cols),
A_shape,
np.array(b, dtype=np.float32)
)
def remove_unused_intensities(self):
""" Remove any intensities that are not currently assigned to a pixel,
and then re-number all labels so they are contiguous again. """
if self.params.logging:
prev_r_s = self.decomposition.get_r_s()
labels_nz = self.decomposition.labels_nz
intensities = self.decomposition.intensities
chromaticities = self.decomposition.chromaticities
nlabels = intensities.shape[0]
new_to_old = np.nonzero(np.bincount(
labels_nz, minlength=nlabels))[0]
old_to_new = np.empty(nlabels, dtype=np.int32)
old_to_new.fill(-1)
for new, old in enumerate(new_to_old):
old_to_new[old] = new
self.decomposition.labels_nz = old_to_new[labels_nz]
self.decomposition.intensities = intensities[new_to_old]
self.decomposition.chromaticities = chromaticities[new_to_old]
if self.params.logging:
print(('remove_unused_intensities: %s/%s labels kept' % (
len(self.decomposition.intensities), len(intensities))))
if self.params.logging:
np.testing.assert_equal(self.decomposition.get_r_s(), prev_r_s)
assert (self.decomposition.chromaticities.shape[0] ==
self.decomposition.intensities.shape[0])
def split_label_clusters(self, neighbors=4):
""" Expand the set of labels by looking at each connected component in
the labels. Assign each component a new label number, and copy its old
intensity value to its new label. This typically expands the number of
labels from ~30 to ~3000, so you should only really do it on the last
iteration. """
if self.params.logging:
prev_r_s = self.decomposition.get_r_s()
rows, cols = self.input.shape[0:2]
labels = self.decomposition.get_labels()
intensities = self.decomposition.intensities
chromaticities = self.decomposition.chromaticities
# split labels
new_labels = morphology.label(labels, neighbors=neighbors)
# map labels
self.decomposition.labels_nz = new_labels[self.input.mask_nz]
# map intensities
_, indices = np.unique(new_labels.ravel(), return_index=True)
new_to_old = labels.ravel()[indices]
new_to_old = new_to_old[new_to_old != -1]
self.decomposition.intensities = intensities[new_to_old]
self.decomposition.chromaticities = chromaticities[new_to_old]
if self.params.logging:
print(('split_label_clusters: %s --> %s' % (
intensities.shape[0], self.decomposition.intensities.shape[0])))
self.remove_unused_intensities()
if self.params.logging:
np.testing.assert_equal(self.decomposition.get_r_s(), prev_r_s)
assert (self.decomposition.chromaticities.shape[0] ==
self.decomposition.intensities.shape[0])