-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconversions.py
More file actions
277 lines (255 loc) · 9.89 KB
/
conversions.py
File metadata and controls
277 lines (255 loc) · 9.89 KB
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
"""
Copyright (c) 2022 Xu Cao
Copyright (c) Meta Platforms, Inc. and affiliates.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Partly adapted from https://github.com/xucao-42/bilateral_normal_integration/.
"""
import numpy as np
import scipy
from bini.bilateral_normal_integration import (
move_bottom,
move_left,
move_right,
move_top,
)
from bini.misc import sigmoid
def build_gamma_equation_matrix_from_bini(
v_where_is_valid: np.ndarray,
u_where_is_valid: np.ndarray,
v_where_is_valid_and_valid_neighbor: np.ndarray,
u_where_is_valid_and_valid_neighbor: np.ndarray,
shift_where_is_valid_and_valid_neighbor: np.ndarray,
H: int,
W: int,
num_shifts: int,
channel_idx_to_du_dv: np.ndarray,
nz_u_bini: np.ndarray,
nz_v_bini: np.ndarray,
mask_bini: np.ndarray,
normal_image_ours_convention: np.ndarray,
):
assert num_shifts == 4
direction_to_shift = {"left": 0, "top": 1, "right": 2, "bottom": 3}
assert nz_u_bini.shape == nz_v_bini.shape == (len(v_where_is_valid),)
assert mask_bini.shape == (H, W)
assert normal_image_ours_convention.shape == (H, W, 3)
mask_own = np.zeros((H, W), dtype=bool)
mask_own[v_where_is_valid, u_where_is_valid] = True
assert np.all(mask_bini == mask_own)
nz_u_image = np.zeros((H, W))
nz_u_image[:] = np.nan
nz_v_image = nz_u_image.copy()
nz_u_image[v_where_is_valid, u_where_is_valid] = nz_u_bini
nz_v_image[v_where_is_valid, u_where_is_valid] = nz_v_bini
has_mask = {
"left": np.logical_and(move_right(mask_bini), mask_bini),
"right": np.logical_and(move_left(mask_bini), mask_bini),
"bottom": np.logical_and(move_top(mask_bini), mask_bini),
"top": np.logical_and(move_bottom(mask_bini), mask_bini),
}
neighbor_masks_own = np.zeros((H, W, num_shifts), dtype=bool)
neighbor_masks_own[
v_where_is_valid_and_valid_neighbor,
u_where_is_valid_and_valid_neighbor,
shift_where_is_valid_and_valid_neighbor,
] = True
for direction, shift in direction_to_shift.items():
assert np.all(neighbor_masks_own[..., shift] == has_mask[direction])
nz_bini = {}
nxy_bini = {}
nz_bini_image = np.zeros((H, W, num_shifts))
nz_bini_image[:] = np.nan
nxy_bini_image = nz_bini_image.copy()
for direction, shift in direction_to_shift.items():
if direction in ["left", "right"]:
curr_nz_uv = -nz_u_image if direction == "left" else nz_u_image
curr_nxy_uv = normal_image_ours_convention[..., 0]
elif direction in ["top", "bottom"]:
# NOTE: This is a necessary step to convert normals from our convention to
# the convention used by BiNI.
curr_nz_uv = -nz_v_image if direction == "bottom" else nz_v_image
curr_nxy_uv = -normal_image_ours_convention[..., 1]
nz_bini[direction] = curr_nz_uv[
v_where_is_valid_and_valid_neighbor[
shift_where_is_valid_and_valid_neighbor == shift
],
u_where_is_valid_and_valid_neighbor[
shift_where_is_valid_and_valid_neighbor == shift
],
]
nxy_bini[direction] = curr_nxy_uv[
v_where_is_valid_and_valid_neighbor[
shift_where_is_valid_and_valid_neighbor == shift
],
u_where_is_valid_and_valid_neighbor[
shift_where_is_valid_and_valid_neighbor == shift
],
]
nz_bini_image[
v_where_is_valid_and_valid_neighbor[
shift_where_is_valid_and_valid_neighbor == shift
],
u_where_is_valid_and_valid_neighbor[
shift_where_is_valid_and_valid_neighbor == shift
],
shift,
] = nz_bini[direction]
nxy_bini_image[
v_where_is_valid_and_valid_neighbor[
shift_where_is_valid_and_valid_neighbor == shift
],
u_where_is_valid_and_valid_neighbor[
shift_where_is_valid_and_valid_neighbor == shift
],
shift,
] = nxy_bini[direction]
num_is_valid_pixel = v_where_is_valid.shape[0]
num_is_valid_and_valid_neighbor = v_where_is_valid_and_valid_neighbor.shape[0]
num_equations = num_is_valid_and_valid_neighbor
assert H * W < 2**62 - 1
valid_pixel_to_valid_idx = -np.ones([H, W], dtype=np.int64)
valid_pixel_to_valid_idx[v_where_is_valid, u_where_is_valid] = np.arange(
num_is_valid_pixel
)
valid_v_u_shift_to_valid_idx = -np.ones([H, W, num_shifts], dtype=np.int64)
valid_v_u_shift_to_valid_idx[
v_where_is_valid_and_valid_neighbor,
u_where_is_valid_and_valid_neighbor,
shift_where_is_valid_and_valid_neighbor,
] = num_is_valid_pixel + np.arange(num_is_valid_and_valid_neighbor)
indices_z_a = valid_pixel_to_valid_idx[
v_where_is_valid_and_valid_neighbor, u_where_is_valid_and_valid_neighbor
]
indices_z_b = valid_pixel_to_valid_idx[
v_where_is_valid_and_valid_neighbor
+ channel_idx_to_du_dv[shift_where_is_valid_and_valid_neighbor, 1],
u_where_is_valid_and_valid_neighbor
+ channel_idx_to_du_dv[shift_where_is_valid_and_valid_neighbor, 0],
]
assert not (np.any(indices_z_a == -1) or np.any(indices_z_b == -1)), (
np.where(indices_z_a == -1),
np.where(indices_z_b == -1),
)
assert len(indices_z_a) == len(indices_z_b) == num_equations
assert len(indices_z_a) == len(indices_z_b)
eqn_indices_z_a = np.arange(len(indices_z_a))
eqn_indices_z_b = np.arange(len(indices_z_b))
col_indices_bini = np.concatenate([indices_z_a, indices_z_b])
row_indices_bini = np.concatenate(
[
# This accounts for the z_a and z_b terms.
eqn_indices_z_a,
eqn_indices_z_b,
]
)
all_values_bini = np.concatenate(
[
# This accounts for the z_a and z_b terms.
-nz_bini_image[
v_where_is_valid_and_valid_neighbor,
u_where_is_valid_and_valid_neighbor,
shift_where_is_valid_and_valid_neighbor,
],
nz_bini_image[
v_where_is_valid_and_valid_neighbor,
u_where_is_valid_and_valid_neighbor,
shift_where_is_valid_and_valid_neighbor,
],
]
)
A_bini = scipy.sparse.csr_matrix(
(all_values_bini, (row_indices_bini, col_indices_bini)),
shape=(num_equations, num_is_valid_pixel),
)
b_bini = -nxy_bini_image[
v_where_is_valid_and_valid_neighbor,
u_where_is_valid_and_valid_neighbor,
shift_where_is_valid_and_valid_neighbor,
]
return (
A_bini,
(indices_z_a, indices_z_b),
)
def compute_residual_and_wuv_image(
A_ours_log_with_gamma,
log_z,
H,
W,
num_shifts,
v_where_is_valid_and_valid_neighbor,
u_where_is_valid_and_valid_neighbor,
shift_where_is_valid_and_valid_neighbor,
sigmoid_k_value,
):
if sigmoid_k_value != 2:
print(
f"\033[91mNOTE: Setting sigmoid `k` to {sigmoid_k_value} rather than 2."
"\033[0m"
)
residual_ = A_ours_log_with_gamma @ log_z
residual_image = np.zeros((H, W, num_shifts))
residual_image[
v_where_is_valid_and_valid_neighbor,
u_where_is_valid_and_valid_neighbor,
shift_where_is_valid_and_valid_neighbor,
] = residual_
wuv_image = np.zeros((H, W, num_shifts))
wuv_image[:] = np.nan
for channel_idx in range(num_shifts // 2):
opposite_channel_idx = channel_idx + num_shifts // 2
if num_shifts == 8:
assert opposite_channel_idx in [4, 5, 6, 7]
elif num_shifts == 4:
assert opposite_channel_idx in [2, 3]
else:
raise ValueError(f"Invalid number of channels: {num_shifts}.")
curr_w_image = sigmoid(
residual_image[..., channel_idx] ** 2
- residual_image[..., opposite_channel_idx] ** 2,
k=sigmoid_k_value,
)
wuv_image[
v_where_is_valid_and_valid_neighbor[
shift_where_is_valid_and_valid_neighbor == channel_idx
],
u_where_is_valid_and_valid_neighbor[
shift_where_is_valid_and_valid_neighbor == channel_idx
],
channel_idx,
] = (
1
- curr_w_image[
v_where_is_valid_and_valid_neighbor[
shift_where_is_valid_and_valid_neighbor == channel_idx
],
u_where_is_valid_and_valid_neighbor[
shift_where_is_valid_and_valid_neighbor == channel_idx
],
]
)
wuv_image[
v_where_is_valid_and_valid_neighbor[
shift_where_is_valid_and_valid_neighbor == opposite_channel_idx
],
u_where_is_valid_and_valid_neighbor[
shift_where_is_valid_and_valid_neighbor == opposite_channel_idx
],
opposite_channel_idx,
] = curr_w_image[
v_where_is_valid_and_valid_neighbor[
shift_where_is_valid_and_valid_neighbor == opposite_channel_idx
],
u_where_is_valid_and_valid_neighbor[
shift_where_is_valid_and_valid_neighbor == opposite_channel_idx
],
]
return residual_image, wuv_image