Skip to content

Commit

Permalink
Fix bug when pattern is only one.
Browse files Browse the repository at this point in the history
  • Loading branch information
CodePointer committed Jul 29, 2022
1 parent 9fb2107 commit 3d61cdd
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 30 deletions.
7 changes: 4 additions & 3 deletions exps/exp_xyz2density.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ def init_dataset(self):

self.pat_dataset = MultiPatDataset(
scene_folder=self.train_dir,
pat_idx_set=[0, 1, 2, 3, 4, 5, 6, 7],
# pat_idx_set=[0, 1, 2, 3, 4, 5, 6, 7],
pat_idx_set=[11],
sample_num=self.sample_num,
calib_para=config['Calibration'],
device=self.device
Expand Down Expand Up @@ -277,7 +278,7 @@ def callback_img_visual(self, data, net_out, tag, step):
# encoding='utf-8')

img_input = self.pat_dataset.img_set.unsqueeze(0).detach().cpu()
img_input_re = torch.nn.functional.interpolate(img_input, scale_factor=1 / resolution_level).squeeze()
img_input_re = torch.nn.functional.interpolate(img_input, scale_factor=1 / resolution_level).squeeze(dim=0)
for c in range(channel):
img_gt = img_input_re[c]
img_viz = pvf.img_visual(img_gt)
Expand All @@ -291,7 +292,7 @@ def callback_img_visual(self, data, net_out, tag, step):
depth_viz = pvf.disp_visual(depth_mat, range_val=self.bound[:, 2].to(depth_mat.device))
self.loss_writer.add_image(f'{tag}/depth_map', depth_viz, step)

# plb.imviz(depth_mat, 'depth', 10, normalize=[300, 900])
# plb.imviz(depth_mat, 'depth', 0, normalize=[300, 900])

# Mesh
# mesh_bound = np.stack([np.min(pts_set, axis=0), np.max(pts_set, axis=0)])
Expand Down
2 changes: 1 addition & 1 deletion networks/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def forward(self, xyz_set, mask_flag=False):
mask[mask < 1.0] = 0.0
sample_mat *= mask

return sample_mat.squeeze().permute(1, 0) # [N, C]
return sample_mat.squeeze(axis=3)[0].permute(1, 0) # [N, C]

# hei_s, wid_s = src_mat.shape[-2:]
#
Expand Down
3 changes: 1 addition & 2 deletions networks/neus.py
Original file line number Diff line number Diff line change
Expand Up @@ -766,8 +766,7 @@ def render_density(self, rays_d):

# Section midpoints
pts = rays_d[:, None, :] * mid_z_vals[..., :, None] # n_rays, n_samples, 3
pts = pts.reshape(-1, 3)
pts_n = self.pts_normalize(pts)
pts_n = self.pts_normalize(pts.reshape(-1, 3))

density = self.sdf_network(pts_n).reshape(z_vals.shape)
sampled_color = self.color_network(pts_n).reshape(batch_size, n_samples, -1)
Expand Down
21 changes: 21 additions & 0 deletions params/xyz2density_eval.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
config = ./params/xyz2density_eval.ini

argset = xyz2density
train_dir = C:/SLDataSet/20220617s
out_dir = C:/SLDataSet/20220617s-out
;test_dir =
model_dir = C:/SLDataSet/20220617s-out/xyz2density-success/model
exp_type = eval
run_tag =
debug_mode = True

num_workers = 0
epoch_start = 32001
epoch_end = 32002
; remove_history = True
lr = 1e-3
lr_step = 0
report_stone = 32
img_stone = 32
model_stone = 32
save_stone = 8
2 changes: 1 addition & 1 deletion params/xyz2density_train.ini
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ out_dir = C:/SLDataSet/20220617s-out
model_dir =
exp_type = train
run_tag =
;debug_mode = True
debug_mode = True

num_workers = 0
epoch_start = 0
Expand Down
56 changes: 48 additions & 8 deletions tools/generate_pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,59 @@ def draw_gray(gray_num, hei, wid):
return pat


def generate_phase(interval, hei, wid):
intensity_base = 127.50
intensity_max = 127.50

shift = np.array([0.0, (2 / 3) * np.pi, (4 / 3) * np.pi], dtype=np.float32).reshape(1, 3)
theta = (np.arange(0, interval) / interval * (2 * np.pi)).reshape(-1, 1)
phase_set = intensity_base + intensity_max * np.cos(theta + shift)

step = wid // interval
phase_set_part = phase_set.reshape(1, interval, 3)
pats = np.tile(phase_set_part, [hei, step, 1])
return pats.astype(np.uint8)


def decode_phase(pats, interval):
pats = pats.astype(np.float32)
ntr = np.sqrt(3) * (pats[:, :, 1] - pats[:, :, 2])
dtr = 2 * pats[:, :, 0] - (pats[:, :, 1] + pats[:, :, 2])
# tan_val = ntr / dtr
theta = - np.arctan2(ntr, dtr)
theta[theta < 0.0] += 2 * np.pi
pixel_val = theta / (2 * np.pi) * interval
return pixel_val


def main():
target_folder = Path('C:/SLDataSet/20220617s/scene000/pat')
target_folder = Path('C:/SLDataSet/20220617s/pat')

digit_num = 8
# digit_num = 8
hei = 800
wid = 1280
#
# gray_code = generate_gray(digit_num)
# for i in range(digit_num):
# pat = draw_gray(gray_code[:, i], hei, wid)
# cv2.imshow('pat', pat)
# cv2.waitKey(10)
# cv2.imwrite(str(target_folder / f'pat_{i}.png'), pat)

interval = 40
start_idx = 8
pats = generate_phase(interval, hei, wid)

pixel_val = decode_phase(pats, interval)
pix_show = (pixel_val * 6).astype(np.uint8)
cv2.imshow('pixel', pix_show)
cv2.waitKey(0)

gray_code = generate_gray(digit_num)
for i in range(digit_num):
pat = draw_gray(gray_code[:, i], hei, wid)
cv2.imshow('pat', pat)
cv2.waitKey(10)
cv2.imwrite(str(target_folder / f'pat_{i}.png'), pat)
# for i in range(3):
# pat = pats[:, :, i]
# cv2.imshow('pat', pat)
# cv2.waitKey(10)
# cv2.imwrite(str(target_folder / f'pat_{i + start_idx}.png'), pat)

# pass

Expand Down
30 changes: 15 additions & 15 deletions tools/render_imgs.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,33 +92,33 @@ def load_render(folder): # TODO: 标定参数还是有很多小问题。先跳
config = ConfigParser()
config.read(str(folder / 'config.ini'), encoding='utf-8')

ext_tran = plb.str2array(config['Calibration']['ext_tran'], np.float32)
target_pt = np.array([0.0, 100.0, 800.0], dtype=np.float32)
look = target_pt - ext_tran
up = np.array([0.0, -1.0, 0.0], dtype=np.float32)
z_vec = look / np.linalg.norm(look)
right = np.cross(look, up)
x_vec = right / np.linalg.norm(right)
y_vec = np.cross(z_vec, x_vec)
rot_mat = np.stack([x_vec, y_vec, z_vec], axis=1)
rot_str = ','.join([str(x) for x in rot_mat.reshape(-1)])
config['Calibration']['ext_rot'] = rot_str
# ext_tran = plb.str2array(config['Calibration']['ext_tran'], np.float32)
# target_pt = np.array([0.0, 100.0, 800.0], dtype=np.float32)
# look = target_pt - ext_tran
# up = np.array([0.0, -1.0, 0.0], dtype=np.float32)
# z_vec = look / np.linalg.norm(look)
# right = np.cross(look, up)
# x_vec = right / np.linalg.norm(right)
# y_vec = np.cross(z_vec, x_vec)
# rot_mat = np.stack([x_vec, y_vec, z_vec], axis=1)
# rot_str = ','.join([str(x) for x in rot_mat.reshape(-1)])
# config['Calibration']['ext_rot'] = rot_str

depth2uv = CoordCompute(config['Calibration'])

# load img, depth
scene_list = plb.subfolders(folder)
# scene_list = plb.subfolders(folder)

scene_folder = scene_list[0]
scene_folder = folder
depth = plb.imload(scene_folder / 'depth' / f'depth_0.png', scale=10.0).squeeze(0)
uu, vv = depth2uv(depth)
warp_layer = WarpLayer2D()
pat_num = len(list((scene_folder / 'pat').glob('*.png')))
for pat_idx in range(pat_num):
pat = plb.imload(scene_folder / 'pat' / f'pat_{pat_idx}.png')
img = warp_layer(uu, vv, pat, mask_flag=True)
plb.imviz(img, 'img', 0)
plb.imsave(scene_folder / 'img' / f'img_{pat_idx}.png', img)
plb.imviz(img[0], 'img', 0)
plb.imsave(scene_folder / 'img' / f'img_{pat_idx}.png', img[0])


def main():
Expand Down

0 comments on commit 3d61cdd

Please sign in to comment.