-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
421 lines (324 loc) · 11.7 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
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
from PIL import Image
import torch.nn.functional as F
import geometry
import os
import numpy as np
import torch
import collections
import h5py
import glob
import matplotlib
import util
import torchvision
import skimage
import cv2
def load_rgb_hdf5(instance_ds, key):
rgb_ds = instance_ds["rgb"]
img_arr = np.array(rgb_ds[key][:])
img = resize_img(Image.fromarray(img_arr))
img = skimage.img_as_float32(img)
# Normalization
img -= 0.5
img *= 2.0
return img
def load_pose_hdf5(instance_ds, key):
pose_ds = instance_ds["pose"]
extrinsics = np.array(pose_ds[key][:])
return extrinsics.astype(np.float32).squeeze()
def parse_intrinsics_hdf5(raw_data, trgt_sidelength=None, invert_y=False):
i_arr = np.array(raw_data)
f, cx, cy = i_arr[:3]
height, width = i_arr[3:]
if trgt_sidelength is not None:
cx = cx / width * trgt_sidelength
cy = cy / height * trgt_sidelength
f = trgt_sidelength / height * f
fx = f
if invert_y:
fy = -f
else:
fy = f
full_intrinsic = np.array(
[[fx, 0.0, cx, 0.0], [0.0, fy, cy, 0], [0.0, 0, 1, 0], [0, 0, 0, 1]]
)
return full_intrinsic
def getMedianImageChannels(im):
b, g, r = cv2.split(im)
# Remove zeros
b = b[b != 0]
g = g[g != 0]
r = r[r != 0]
# median values
b_median = np.median(b)
r_median = np.median(r)
g_median = np.median(g)
return r_median, g_median, b_median
def resize_img(img):
image_size = img.size
width = image_size[0]
height = image_size[1]
if width != height:
bigside = width if width > height else height
r, g, b = [int(out) for out in getMedianImageChannels(np.array(img))]
background = Image.new("RGB", (bigside, bigside), (r, g, b))
offset = (
int(round(((bigside - width) / 2), 0)),
int(round(((bigside - height) / 2), 0)),
)
background.paste(img, offset)
return background
else:
return img
def gradient(y, x, grad_outputs=None, create_graph=True):
if grad_outputs is None:
grad_outputs = torch.ones_like(y)
grad = torch.autograd.grad(
y, [x], grad_outputs=grad_outputs, create_graph=create_graph
)[0]
return grad
def convert_image(img):
img = img.squeeze(0)
img = detach_all(lin2img(img, mode="np"))
img = img.squeeze(0)
img += 1.0
img /= 2.0
img *= 255.0
img = np.clip(img, 0.0, 255.0).astype(np.uint8)
return img
def flatten_first_two(tensor):
b, s, *rest = tensor.shape
return tensor.view(b * s, *rest)
def cond_mkdir(path):
if not os.path.exists(path):
os.makedirs(path)
def pick(list, item_idcs):
if not list:
return list
return [list[i] for i in item_idcs]
def add_batch_dim_to_dict(ob):
if isinstance(ob, collections.Mapping):
return {k: add_batch_dim_to_dict(v) for k, v in ob.items()}
elif isinstance(ob, tuple):
return tuple(add_batch_dim_to_dict(k) for k in ob)
elif isinstance(ob, list):
return [add_batch_dim_to_dict(k) for k in ob]
else:
try:
return ob[None, ...]
except:
return ob
def detach_all(tensor):
return tensor.detach().cpu().numpy()
def lin2img(tensor, image_resolution=None, mode="torch"):
if len(tensor.shape) == 3:
batch_size, num_samples, channels = tensor.shape
elif len(tensor.shape) == 2:
num_samples, channels = tensor.shape
if image_resolution is None:
width = np.sqrt(num_samples).astype(int)
height = width
else:
height = image_resolution[0]
width = image_resolution[1]
if len(tensor.shape) == 3:
if mode == "torch":
tensor = tensor.permute(0, 2, 1).view(batch_size, channels, height, width)
elif mode == "np":
tensor = tensor.view(batch_size, height, width, channels)
elif len(tensor.shape) == 2:
if mode == "torch":
tensor = tensor.permute(1, 0).view(channels, height, width)
elif mode == "np":
tensor = tensor.view(height, width, channels)
return tensor
def light_field_depth_map(plucker_coords, cam2world, light_field_fn):
x = geometry.get_ray_origin(cam2world)
D = 1
x_prim = x + D * plucker_coords[..., :3]
d_prim = torch.normal(
torch.zeros_like(plucker_coords[..., :3]),
torch.ones_like(plucker_coords[..., :3]),
).to(plucker_coords.device)
d_prim = F.normalize(d_prim, dim=-1)
dcdsts = []
for _ in range(5):
st = (
((torch.rand_like(plucker_coords[..., :2]) - 0.5) * 1e-2)
.requires_grad_(True)
.to(plucker_coords.device)
)
a = x + st[..., :1] * d_prim
b = x_prim + st[..., 1:] * d_prim
v_dir = b - a
v_mom = torch.cross(a, b, dim=-1)
v_norm = torch.cat((v_dir, v_mom), dim=-1) / v_dir.norm(dim=-1, keepdim=True)
with torch.enable_grad():
c = light_field_fn(v_norm)
dcdst = gradient(c, st, create_graph=False)
dcdsts.append(dcdst)
del dcdst
del c
dcdsts = torch.stack(dcdsts, dim=0)
dcdt = dcdsts[0, ..., 1:]
dcds = dcdsts[0, ..., :1]
all_depth_estimates = D * dcdsts[..., 1:] / (dcdsts.sum(dim=-1, keepdim=True))
all_depth_estimates[torch.abs(dcdsts.sum(dim=-1)) < 5] = 0
all_depth_estimates[all_depth_estimates < 0] = 0.0
depth_var = torch.std(all_depth_estimates, dim=0, keepdim=True)
d = D * dcdt / (dcds + dcdt)
d[torch.abs(dcds + dcdt) < 5] = 0.0
d[d < 0] = 0.0
d[depth_var[0, ..., 0] > 0.01] = 0.0
return {"depth": d, "points": x + d * plucker_coords[..., :3]}
def dict_to_gpu(ob):
if isinstance(ob, collections.Mapping):
return {k: dict_to_gpu(v) for k, v in ob.items()}
elif isinstance(ob, tuple):
return tuple(dict_to_gpu(k) for k in ob)
elif isinstance(ob, list):
return [dict_to_gpu(k) for k in ob]
else:
try:
return ob.cuda()
except:
return ob
def assemble_model_input(context, query, gpu=True):
context["mask"] = torch.Tensor([1.0])
query["mask"] = torch.Tensor([1.0])
context = add_batch_dim_to_dict(context)
context = add_batch_dim_to_dict(context)
query = add_batch_dim_to_dict(query)
query = add_batch_dim_to_dict(query)
model_input = {"context": context, "query": query, "post_input": query}
if gpu:
model_input = dict_to_gpu(model_input)
return model_input
def glob_imgs(path):
imgs = []
for ext in ["*.png", "*.jpg", "*.JPEG", "*.JPG"]:
imgs.extend(glob(os.path.join(path, ext)))
return imgs
def visualize_data(filepath, instance_num):
file = h5py.File(
filepath,
"r",
)
instance = file["instance_" + str(instance_num)]
color_keys = sorted(list(instance["rgb"].keys()))
pose_keys = sorted(list(instance["pose"].keys()))
img = np.array(load_rgb_hdf5(instance, color_keys[0]))
img = skimage.img_as_ubyte(img)
img = Image.fromarray(img)
img.show()
print(load_pose_hdf5(instance, pose_keys[0]))
print(parse_intrinsics_hdf5(instance["intrinsics.txt"]))
# Testing with a custom example I made
# You might notice that the lighting is a bit darker here -- this is
# because of the normalization step in the load_rgb_hdf5 function
def test_example():
visualize_data("image_data/cyberpunk_mercenary_with_a_tech_armor.hdf5", 1)
def image_loss(model_out, gt, mask=None):
gt_rgb = gt["rgb"]
return torch.nn.MSELoss()(gt_rgb, model_out["rgb"]) * 200
class LFLoss:
def __init__(self, l2_weight=1, reg_weight=1e2):
self.l2_weight = l2_weight
self.reg_weight = reg_weight
def __call__(self, model_out, gt):
loss_dict = {}
loss_dict["img_loss"] = image_loss(model_out, gt)
loss_dict["reg"] = (model_out["z"] ** 2).mean() * self.reg_weight
return loss_dict
def img_summaries(
model_input,
ground_truth,
model_output,
writer,
iter,
prefix="",
img_shape=None,
):
matplotlib.use("Agg")
predictions = model_output["rgb"]
trgt_imgs = ground_truth["rgb"]
indices = model_input["query"]["instance_idx"]
predictions = flatten_first_two(predictions)
trgt_imgs = flatten_first_two(trgt_imgs)
with torch.no_grad():
if "context" in model_input and model_input["context"]:
context_images = (
model_input["context"]["rgb"]
* model_input["context"]["mask"][..., None]
)
context_images = lin2img(
flatten_first_two(context_images), image_resolution=img_shape
)
writer.add_image(
prefix + "context_images",
torchvision.utils.make_grid(
context_images, scale_each=False, normalize=True
)
.cpu()
.numpy(),
iter,
)
output_vs_gt = torch.cat((predictions, trgt_imgs), dim=0)
output_vs_gt = lin2img(output_vs_gt, image_resolution=img_shape)
writer.add_image(
prefix + "output_vs_gt",
torchvision.utils.make_grid(output_vs_gt, scale_each=False, normalize=True)
.cpu()
.detach()
.numpy(),
iter,
)
writer.add_scalar(prefix + "out_min", predictions.min(), iter)
writer.add_scalar(prefix + "out_max", predictions.max(), iter)
writer.add_scalar(prefix + "trgt_min", trgt_imgs.min(), iter)
writer.add_scalar(prefix + "trgt_max", trgt_imgs.max(), iter)
writer.add_scalar(prefix + "idx_min", indices.min(), iter)
writer.add_scalar(prefix + "idx_max", indices.max(), iter)
def get_psnr(p, trgt):
p = lin2img(p.squeeze(), mode="np")
trgt = lin2img(trgt.squeeze(), mode="np")
p = detach_all(p)
trgt = detach_all(trgt)
p = (p / 2.0) + 0.5
p = np.clip(p, a_min=0.0, a_max=1.0)
trgt = (trgt / 2.0) + 0.5
ssim = skimage.metrics.structural_similarity(
p, trgt, multichannel=True, data_range=1, win_size=7, channel_axis=2
)
psnr = skimage.metrics.structural_similarity(
p, trgt, data_range=1, win_size=7, channel_axis=2
)
return psnr, ssim
def test_results(log_dir, model, dataset, save_first_n, gpu_avail):
psnrs = []
with torch.no_grad():
for i in range(len(dataset)):
print(f"Object {i:04d}")
dummy_query = dataset[i][0]
instance_name = dummy_query["instance_name"]
if i < save_first_n:
instance_dir = log_dir + f"{instance_name}"
os.makedirs(instance_dir, exist_ok=True)
for j, query in enumerate(dataset[i]):
model_input = assemble_model_input(query, query, gpu_avail)
model_output = model(model_input)
# Obtaining the generated image and the ground truth image
out_dict = {}
out_dict["rgb"] = model_output["rgb"]
out_dict["gt_rgb"] = model_input["query"]["rgb"]
psnr, ssim = get_psnr(out_dict["rgb"], out_dict["gt_rgb"])
psnrs.append((psnr, ssim))
# Saving the images in the logging folder
if i < save_first_n:
img = convert_image(out_dict["gt_rgb"])
cv2.imwrite(str(instance_dir + f"{j:06d}_gt.png"), img)
img = convert_image(out_dict["rgb"])
cv2.imwrite(str(instance_dir + f"{j:06d}.png"), img)
print("Mean PSNRs", np.mean(np.array(psnrs), axis=0))
with open(os.path.join(log_dir, "results.txt"), "w") as out_file:
mean = np.mean(psnrs, axis=0)
out_file.write(f"{mean[0]} PSRN {mean[1]} SSIM")