-
Notifications
You must be signed in to change notification settings - Fork 1
/
eval_cam.py
495 lines (397 loc) · 17.1 KB
/
eval_cam.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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
from operator import concat
import os
import pickle
import numpy as np
import cv2
import torch
import torch.utils.data
import json
from core.utils.image_util import load_image
from core.utils.body_util import \
body_pose_to_body_RTs, \
get_canonical_global_tfms, \
approx_gaussian_bone_volumes
from core.utils.file_util import list_files, split_path
from core.utils.camera_util import \
apply_global_tfm_to_camera, \
get_rays_from_KRT, \
get_rays, \
rays_intersect_3d_bbox
from configs import cfg
import random
class Dataset(torch.utils.data.Dataset):
def __init__(
self,
dataset_path,
keyfilter=None,
maxframes=-1,
bgcolor=None,
ray_shoot_mode='image',
skip=1,
now_subject=None,
**_):
self.dataset_path = dataset_path+'_eval'
self.now_subject=str(now_subject)
print('[Dataset Path]', self.dataset_path )
self.image_dir = os.path.join(self.dataset_path , 'images')
self.canonical_joints, self.canonical_bbox = \
self.load_canonical_joints_all()
if 'motion_weights_priors' in keyfilter:
self.motion_weights_priors = {}
for subject in self.canonical_joints.keys():
self.motion_weights_priors[subject] = \
approx_gaussian_bone_volumes(
self.canonical_joints[subject],
self.canonical_bbox[subject]['min_xyz'],
self.canonical_bbox[subject]['max_xyz'],
grid_size=cfg.mweight_volume.volume_size).astype('float32')
self.cameras = self.load_train_cameras_all()
self.mesh_infos = self.load_train_mesh_infos_all()
framelist = self.load_train_frames()
self.framelist = framelist[::skip]
if maxframes > 0:
self.framelist = self.framelist[:maxframes]
print(f' -- Total Frames: {self.get_total_frames()}')
self.keyfilter = keyfilter
self.bgcolor = bgcolor
self.ray_shoot_mode = ray_shoot_mode
def load_sim_all(self,name):
all_name_list = os.listdir(self.dataset_path)
concat_dict = {}
for all_name in all_name_list:
if name in all_name:
path = os.path.join(self.dataset_path, all_name)
with open(path, 'rb') as f:
concat_dict.update ( pickle.load(f))
return concat_dict
def load_canonical_joints_all(self):
name = 'canonical_joints'
all_name_list = os.listdir(self.dataset_path)
canonical_joints = {}
canonical_bbox = {}
for all_name in all_name_list:
if name in all_name:
subject = self.now_subject
path = os.path.join(self.dataset_path, all_name)
with open(path, 'rb') as f:
cl_joint_data = pickle.load(f)
canonical_joints[subject] = cl_joint_data['joints_'+subject].astype('float32')
canonical_bbox[subject] = self.skeleton_to_bbox(canonical_joints[subject])
return canonical_joints , canonical_bbox
def load_train_cameras_all(self):
name = 'cameras'
all_name_list = os.listdir(self.dataset_path)
cameras = {}
for all_name in all_name_list:
if name in all_name:
path = os.path.join(self.dataset_path, all_name)
with open(path, 'rb') as f:
cameras_one = pickle.load(f)
cameras.update(cameras_one)
return cameras
@staticmethod
def skeleton_to_bbox(skeleton):
min_xyz = np.min(skeleton, axis=0) - cfg.bbox_offset
max_xyz = np.max(skeleton, axis=0) + cfg.bbox_offset
return {
'min_xyz': min_xyz,
'max_xyz': max_xyz
}
def load_train_mesh_infos_all(self):
name = 'mesh_infos'
all_name_list = os.listdir(self.dataset_path)
mesh_infos = {}
for all_name in all_name_list:
if name in all_name:
path = os.path.join(self.dataset_path, all_name)
with open(path, 'rb') as f:
mesh_infos_one = pickle.load(f)
mesh_infos.update(mesh_infos_one)
for frame_name in mesh_infos.keys():
bbox = self.skeleton_to_bbox(mesh_infos[frame_name]['joints'])
mesh_infos[frame_name]['bbox'] = bbox
return mesh_infos
def load_train_mesh_infos(self):
mesh_infos = None
with open(os.path.join(self.dataset_path, 'mesh_infos.pkl'), 'rb') as f:
mesh_infos = pickle.load(f)
for frame_name in mesh_infos.keys():
bbox = self.skeleton_to_bbox(mesh_infos[frame_name]['joints'])
mesh_infos[frame_name]['bbox'] = bbox
return mesh_infos
def load_train_frames(self):
img_paths = list_files(os.path.join(self.dataset_path, 'images'),
exts=['.png'])
return [split_path(ipath)[1] for ipath in img_paths]
def query_dst_skeleton(self, frame_name):
return {
'poses': self.mesh_infos[frame_name]['poses'].astype('float32'),
'dst_tpose_joints': \
self.mesh_infos[frame_name]['tpose_joints'].astype('float32'),
'bbox': self.mesh_infos[frame_name]['bbox'].copy(),
'Rh': self.mesh_infos[frame_name]['Rh'].astype('float32'),
'Th': self.mesh_infos[frame_name]['Th'].astype('float32')
}
@staticmethod
def select_rays(select_inds, rays_o, rays_d, ray_img, near, far):
rays_o = rays_o[select_inds]
rays_d = rays_d[select_inds]
ray_img = ray_img[select_inds]
near = near[select_inds]
far = far[select_inds]
return rays_o, rays_d, ray_img, near, far
def get_patch_ray_indices(
self,
N_patch,
ray_mask,
subject_mask,
bbox_mask,
patch_size,
H, W):
assert subject_mask.dtype == np.bool
assert bbox_mask.dtype == np.bool
bbox_exclude_subject_mask = np.bitwise_and(
bbox_mask,
np.bitwise_not(subject_mask)
)
list_ray_indices = []
list_mask = []
list_xy_min = []
list_xy_max = []
total_rays = 0
patch_div_indices = [total_rays]
for _ in range(N_patch):
# let p = cfg.patch.sample_subject_ratio
# prob p: we sample on subject area
# prob (1-p): we sample on non-subject area but still in bbox
if random.random() < cfg.patch.sample_subject_ratio:
candidate_mask = subject_mask
else:
candidate_mask = bbox_exclude_subject_mask
ray_indices, mask, xy_min, xy_max = \
self._get_patch_ray_indices(ray_mask, candidate_mask,
patch_size, H, W)
assert len(ray_indices.shape) == 1
total_rays += len(ray_indices)
list_ray_indices.append(ray_indices)
list_mask.append(mask)
list_xy_min.append(xy_min)
list_xy_max.append(xy_max)
patch_div_indices.append(total_rays)
select_inds = np.concatenate(list_ray_indices, axis=0)
patch_info = {
'mask': np.stack(list_mask, axis=0),
'xy_min': np.stack(list_xy_min, axis=0),
'xy_max': np.stack(list_xy_max, axis=0)
}
patch_div_indices = np.array(patch_div_indices)
return select_inds, patch_info, patch_div_indices
def _get_patch_ray_indices(
self,
ray_mask,
candidate_mask,
patch_size,
H, W):
assert len(ray_mask.shape) == 1
assert ray_mask.dtype == np.bool
assert candidate_mask.dtype == np.bool
valid_ys, valid_xs = np.where(candidate_mask)
if valid_ys.shape[0]==0:
print('shape',candidate_mask.shape,valid_ys.shape)
# determine patch center
if valid_ys.shape[0]!=0:
select_idx = random.randint(0,valid_ys.shape[0]-1)
center_x = valid_xs[select_idx]
center_y = valid_ys[select_idx]
else:
center_x = random.randint(0,candidate_mask.shape[0]-1)
center_y = random.randint(0,candidate_mask.shape[0]-1)
# determine patch boundary
half_patch_size = patch_size // 2
x_min = np.clip(a=center_x-half_patch_size,
a_min=0,
a_max=W-patch_size)
x_max = x_min + patch_size
y_min = np.clip(a=center_y-half_patch_size,
a_min=0,
a_max=H-patch_size)
y_max = y_min + patch_size
sel_ray_mask = np.zeros_like(candidate_mask)
sel_ray_mask[y_min:y_max, x_min:x_max] = True
#####################################################
## Below we determine the selected ray indices
## and patch valid mask
sel_ray_mask = sel_ray_mask.reshape(-1)
inter_mask = np.bitwise_and(sel_ray_mask, ray_mask)
select_masked_inds = np.where(inter_mask)
masked_indices = np.cumsum(ray_mask) - 1
select_inds = masked_indices[select_masked_inds]
inter_mask = inter_mask.reshape(H, W)
return select_inds, \
inter_mask[y_min:y_max, x_min:x_max], \
np.array([x_min, y_min]), np.array([x_max, y_max])
def load_image(self, frame_name, bg_color):
imagepath = os.path.join(self.image_dir, '{}.png'.format(frame_name))
orig_img = np.array(load_image(imagepath))
maskpath = os.path.join(self.dataset_path,
'masks',
'{}.png'.format(frame_name))
alpha_mask = np.array(load_image(maskpath))
# undistort image
if frame_name in self.cameras and 'distortions' in self.cameras[frame_name]:
K = self.cameras[frame_name]['intrinsics']
D = self.cameras[frame_name]['distortions']
orig_img = cv2.undistort(orig_img, K, D)
alpha_mask = cv2.undistort(alpha_mask, K, D)
alpha_mask = alpha_mask / 255.
img = alpha_mask * orig_img + (1.0 - alpha_mask) * bg_color[None, None, :]
if cfg.resize_img_scale != 1.:
img = cv2.resize(img, None,
fx=cfg.resize_img_scale,
fy=cfg.resize_img_scale,
interpolation=cv2.INTER_LANCZOS4)
alpha_mask = cv2.resize(alpha_mask, None,
fx=cfg.resize_img_scale,
fy=cfg.resize_img_scale,
interpolation=cv2.INTER_LINEAR)
return img, alpha_mask
def get_total_frames(self):
return len(self.framelist)
def sample_patch_rays(self, img, H, W,
subject_mask, bbox_mask, ray_mask,
rays_o, rays_d, ray_img, near, far):
select_inds, patch_info, patch_div_indices = \
self.get_patch_ray_indices(
N_patch=cfg.patch.N_patches,
ray_mask=ray_mask,
subject_mask=subject_mask,
bbox_mask=bbox_mask,
patch_size=cfg.patch.size,
H=H, W=W)
rays_o, rays_d, ray_img, near, far = self.select_rays(
select_inds, rays_o, rays_d, ray_img, near, far)
targets = []
for i in range(cfg.patch.N_patches):
x_min, y_min = patch_info['xy_min'][i]
x_max, y_max = patch_info['xy_max'][i]
targets.append(img[y_min:y_max, x_min:x_max])
target_patches = np.stack(targets, axis=0) # (N_patches, P, P, 3)
patch_masks = patch_info['mask'] # boolean array (N_patches, P, P)
return rays_o, rays_d, ray_img, near, far, \
target_patches, patch_masks, patch_div_indices
def __len__(self):
return self.get_total_frames()
def __getitem__(self, idx):
re_set = 0
frame_name = self.framelist[idx]
frame_name_split_list = frame_name.split('_')
subject = self.now_subject
time_int = int(frame_name_split_list[2])
results = {
'frame_name': frame_name,
'time_id':time_int/1000.0,
'subject_id':re_set,
}
if self.bgcolor is None:
bgcolor = (random.rand(3) * 255.).astype('float32')
else:
bgcolor = np.array(self.bgcolor, dtype='float32')
img, alpha = self.load_image(frame_name, bgcolor)
img = (img / 255.).astype('float32')
H, W = img.shape[0:2]
dst_skel_info = self.query_dst_skeleton(frame_name)
dst_bbox = dst_skel_info['bbox']
dst_poses = dst_skel_info['poses']
dst_tpose_joints = dst_skel_info['dst_tpose_joints']
assert frame_name in self.cameras
K = self.cameras[frame_name]['intrinsics'][:3, :3].copy()
K[:2] *= cfg.resize_img_scale
E = self.cameras[frame_name]['extrinsics']
E = apply_global_tfm_to_camera(
E=E,
Rh=dst_skel_info['Rh'],
Th=dst_skel_info['Th'])
R = E[:3, :3]
T = E[:3, 3]
smpl_Rh = dst_skel_info['Rh']
smpl_R = cv2.Rodrigues(smpl_Rh)[0].astype(np.float32)
smpl_Th = dst_skel_info['Th'].astype(np.float32)
# rays_o, rays_d = get_rays_from_KRT(H, W, K, R, T)
rays_o, rays_d = get_rays(H, W, K, R, T)
ray_img = img.reshape(-1, 3)
rays_o = rays_o.reshape(-1, 3) # (H, W, 3) --> (N_rays, 3)
rays_d = rays_d.reshape(-1, 3)
# (selected N_samples, ), (selected N_samples, ), (N_samples, )
near, far, ray_mask = rays_intersect_3d_bbox(dst_bbox, rays_o, rays_d)
rays_o = rays_o[ray_mask]
rays_d = rays_d[ray_mask]
ray_img = ray_img[ray_mask]
near = near[:, None].astype('float32')
far = far[:, None].astype('float32')
if self.ray_shoot_mode == 'image':
pass
elif self.ray_shoot_mode == 'patch':
rays_o, rays_d, ray_img, near, far, \
target_patches, patch_masks, patch_div_indices = \
self.sample_patch_rays(img=img, H=H, W=W,
subject_mask=alpha[:, :, 0] > 0.,
bbox_mask=ray_mask.reshape(H, W),
ray_mask=ray_mask,
rays_o=rays_o,
rays_d=rays_d,
ray_img=ray_img,
near=near,
far=far)
else:
assert False, f"Ivalid Ray Shoot Mode: {self.ray_shoot_mode}"
batch_rays = np.stack([rays_o, rays_d], axis=0)
if 'rays' in self.keyfilter:
results.update({
'smpl_R':smpl_R,
'smpl_Th':smpl_Th,
'img_width': W,
'img_height': H,
'ray_mask': ray_mask,
'rays': batch_rays,
'near': near,
'far': far,
'bgcolor': bgcolor})
if self.ray_shoot_mode == 'patch':
results.update({
'patch_div_indices': patch_div_indices,
'patch_masks': patch_masks,
'target_patches': target_patches})
if 'target_rgbs' in self.keyfilter:
results['target_rgbs'] = ray_img
if 'motion_bases' in self.keyfilter:
dst_Rs, dst_Ts = body_pose_to_body_RTs(
dst_poses, dst_tpose_joints
)
cnl_gtfms = get_canonical_global_tfms(
self.canonical_joints[subject])
results.update({
'dst_Rs': dst_Rs,
'dst_Ts': dst_Ts,
'cnl_gtfms': cnl_gtfms
})
if 'motion_weights_priors' in self.keyfilter:
results['motion_weights_priors'] = self.motion_weights_priors[subject].copy()
# get the bounding box of canonical volume
if 'cnl_bbox' in self.keyfilter:
min_xyz = self.canonical_bbox[subject]['min_xyz'].astype('float32')
max_xyz = self.canonical_bbox[subject]['max_xyz'].astype('float32')
# print('bound',min_xyz,max_xyz)
results.update({
'cnl_bbox_min_xyz': min_xyz,
'cnl_bbox_max_xyz': max_xyz,
'cnl_bbox_scale_xyz': 2.0 / (max_xyz - min_xyz)
})
assert np.all(results['cnl_bbox_scale_xyz'] >= 0)
if 'dst_posevec_69' in self.keyfilter:
# 1. ignore global orientation
# 2. add a small value to avoid all zeros
dst_posevec_69 = dst_poses[3:] + 1e-2
results.update({
'dst_posevec': dst_posevec_69,
})
return results