update
This commit is contained in:
parent
1a0e3c8042
commit
7c7f071f95
@ -4,10 +4,10 @@ import PytorchBoot.namespace as namespace
|
|||||||
import PytorchBoot.stereotype as stereotype
|
import PytorchBoot.stereotype as stereotype
|
||||||
from PytorchBoot.config import ConfigManager
|
from PytorchBoot.config import ConfigManager
|
||||||
from PytorchBoot.utils.log_util import Log
|
from PytorchBoot.utils.log_util import Log
|
||||||
|
|
||||||
import torch
|
import torch
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
import time
|
||||||
|
|
||||||
sys.path.append(r"/data/hofee/project/nbv_rec/nbv_reconstruction")
|
sys.path.append(r"/data/hofee/project/nbv_rec/nbv_reconstruction")
|
||||||
|
|
||||||
@ -51,7 +51,7 @@ class NBVReconstructionDataset(BaseDataset):
|
|||||||
scene_name_list.append(scene_name)
|
scene_name_list.append(scene_name)
|
||||||
return scene_name_list
|
return scene_name_list
|
||||||
|
|
||||||
def get_datalist(self, bias=False):
|
def get_datalist(self):
|
||||||
datalist = []
|
datalist = []
|
||||||
for scene_name in self.scene_name_list:
|
for scene_name in self.scene_name_list:
|
||||||
seq_num = DataLoadUtil.get_label_num(self.root_dir, scene_name)
|
seq_num = DataLoadUtil.get_label_num(self.root_dir, scene_name)
|
||||||
@ -80,18 +80,16 @@ class NBVReconstructionDataset(BaseDataset):
|
|||||||
for data_pair in label_data["data_pairs"]:
|
for data_pair in label_data["data_pairs"]:
|
||||||
scanned_views = data_pair[0]
|
scanned_views = data_pair[0]
|
||||||
next_best_view = data_pair[1]
|
next_best_view = data_pair[1]
|
||||||
accept_probability = scanned_views[-1][1]
|
datalist.append(
|
||||||
if accept_probability > np.random.rand():
|
{
|
||||||
datalist.append(
|
"scanned_views": scanned_views,
|
||||||
{
|
"next_best_view": next_best_view,
|
||||||
"scanned_views": scanned_views,
|
"seq_max_coverage_rate": max_coverage_rate,
|
||||||
"next_best_view": next_best_view,
|
"scene_name": scene_name,
|
||||||
"seq_max_coverage_rate": max_coverage_rate,
|
"label_idx": seq_idx,
|
||||||
"scene_name": scene_name,
|
"scene_max_coverage_rate": scene_max_coverage_rate,
|
||||||
"label_idx": seq_idx,
|
}
|
||||||
"scene_max_coverage_rate": scene_max_coverage_rate,
|
)
|
||||||
}
|
|
||||||
)
|
|
||||||
return datalist
|
return datalist
|
||||||
|
|
||||||
def preprocess_cache(self):
|
def preprocess_cache(self):
|
||||||
@ -117,8 +115,13 @@ class NBVReconstructionDataset(BaseDataset):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
Log.error(f"Save cache failed: {e}")
|
Log.error(f"Save cache failed: {e}")
|
||||||
|
|
||||||
def voxel_downsample_with_mask(self, pts, voxel_size):
|
def voxel_downsample_with_mapping(self, point_cloud, voxel_size=0.003):
|
||||||
pass
|
voxel_indices = np.floor(point_cloud / voxel_size).astype(np.int32)
|
||||||
|
unique_voxels, inverse, counts = np.unique(voxel_indices, axis=0, return_inverse=True, return_counts=True)
|
||||||
|
idx_sort = np.argsort(inverse)
|
||||||
|
idx_unique = idx_sort[np.cumsum(counts)-counts]
|
||||||
|
downsampled_points = point_cloud[idx_unique]
|
||||||
|
return downsampled_points, inverse
|
||||||
|
|
||||||
|
|
||||||
def __getitem__(self, index):
|
def __getitem__(self, index):
|
||||||
@ -132,6 +135,9 @@ class NBVReconstructionDataset(BaseDataset):
|
|||||||
scanned_coverages_rate,
|
scanned_coverages_rate,
|
||||||
scanned_n_to_world_pose,
|
scanned_n_to_world_pose,
|
||||||
) = ([], [], [])
|
) = ([], [], [])
|
||||||
|
start_time = time.time()
|
||||||
|
start_indices = [0]
|
||||||
|
total_points = 0
|
||||||
for view in scanned_views:
|
for view in scanned_views:
|
||||||
frame_idx = view[0]
|
frame_idx = view[0]
|
||||||
coverage_rate = view[1]
|
coverage_rate = view[1]
|
||||||
@ -153,8 +159,12 @@ class NBVReconstructionDataset(BaseDataset):
|
|||||||
n_to_world_trans = n_to_world_pose[:3, 3]
|
n_to_world_trans = n_to_world_pose[:3, 3]
|
||||||
n_to_world_9d = np.concatenate([n_to_world_6d, n_to_world_trans], axis=0)
|
n_to_world_9d = np.concatenate([n_to_world_6d, n_to_world_trans], axis=0)
|
||||||
scanned_n_to_world_pose.append(n_to_world_9d)
|
scanned_n_to_world_pose.append(n_to_world_9d)
|
||||||
|
total_points += len(downsampled_target_point_cloud)
|
||||||
|
start_indices.append(total_points)
|
||||||
|
|
||||||
|
|
||||||
|
end_time = time.time()
|
||||||
|
#Log.info(f"load data time: {end_time - start_time}")
|
||||||
nbv_idx, nbv_coverage_rate = nbv[0], nbv[1]
|
nbv_idx, nbv_coverage_rate = nbv[0], nbv[1]
|
||||||
nbv_path = DataLoadUtil.get_path(self.root_dir, scene_name, nbv_idx)
|
nbv_path = DataLoadUtil.get_path(self.root_dir, scene_name, nbv_idx)
|
||||||
cam_info = DataLoadUtil.load_cam_info(nbv_path)
|
cam_info = DataLoadUtil.load_cam_info(nbv_path)
|
||||||
@ -167,14 +177,27 @@ class NBVReconstructionDataset(BaseDataset):
|
|||||||
best_to_world_9d = np.concatenate(
|
best_to_world_9d = np.concatenate(
|
||||||
[best_to_world_6d, best_to_world_trans], axis=0
|
[best_to_world_6d, best_to_world_trans], axis=0
|
||||||
)
|
)
|
||||||
|
|
||||||
combined_scanned_views_pts = np.concatenate(scanned_views_pts, axis=0)
|
|
||||||
voxel_downsampled_combined_scanned_pts_np = PtsUtil.voxel_downsample_point_cloud(combined_scanned_views_pts, 0.002)
|
|
||||||
random_downsampled_combined_scanned_pts_np = PtsUtil.random_downsample_point_cloud(voxel_downsampled_combined_scanned_pts_np, self.pts_num)
|
|
||||||
|
|
||||||
|
combined_scanned_views_pts = np.concatenate(scanned_views_pts, axis=0)
|
||||||
|
voxel_downsampled_combined_scanned_pts_np, inverse = self.voxel_downsample_with_mapping(combined_scanned_views_pts, 0.003)
|
||||||
|
random_downsampled_combined_scanned_pts_np, random_downsample_idx = PtsUtil.random_downsample_point_cloud(voxel_downsampled_combined_scanned_pts_np, self.pts_num, require_idx=True)
|
||||||
|
|
||||||
|
all_idx_unique = np.arange(len(voxel_downsampled_combined_scanned_pts_np))
|
||||||
|
all_random_downsample_idx = all_idx_unique[random_downsample_idx]
|
||||||
|
scanned_pts_mask = []
|
||||||
|
for idx, start_idx in enumerate(start_indices):
|
||||||
|
if idx == len(start_indices) - 1:
|
||||||
|
break
|
||||||
|
end_idx = start_indices[idx+1]
|
||||||
|
view_inverse = inverse[start_idx:end_idx]
|
||||||
|
view_unique_downsampled_idx = np.unique(view_inverse)
|
||||||
|
view_unique_downsampled_idx_set = set(view_unique_downsampled_idx)
|
||||||
|
mask = np.array([idx in view_unique_downsampled_idx_set for idx in all_random_downsample_idx])
|
||||||
|
scanned_pts_mask.append(mask)
|
||||||
data_item = {
|
data_item = {
|
||||||
"scanned_pts": np.asarray(scanned_views_pts, dtype=np.float32), # Ndarray(S x Nv x 3)
|
"scanned_pts": np.asarray(scanned_views_pts, dtype=np.float32), # Ndarray(S x Nv x 3)
|
||||||
"combined_scanned_pts": np.asarray(random_downsampled_combined_scanned_pts_np, dtype=np.float32), # Ndarray(N x 3)
|
"combined_scanned_pts": np.asarray(random_downsampled_combined_scanned_pts_np, dtype=np.float32), # Ndarray(N x 3)
|
||||||
|
"scanned_pts_mask": np.asarray(scanned_pts_mask, dtype=np.bool), # Ndarray(N)
|
||||||
"scanned_coverage_rate": scanned_coverages_rate, # List(S): Float, range(0, 1)
|
"scanned_coverage_rate": scanned_coverages_rate, # List(S): Float, range(0, 1)
|
||||||
"scanned_n_to_world_pose_9d": np.asarray(scanned_n_to_world_pose, dtype=np.float32), # Ndarray(S x 9)
|
"scanned_n_to_world_pose_9d": np.asarray(scanned_n_to_world_pose, dtype=np.float32), # Ndarray(S x 9)
|
||||||
"best_coverage_rate": nbv_coverage_rate, # Float, range(0, 1)
|
"best_coverage_rate": nbv_coverage_rate, # Float, range(0, 1)
|
||||||
@ -200,7 +223,9 @@ class NBVReconstructionDataset(BaseDataset):
|
|||||||
collate_data["scanned_n_to_world_pose_9d"] = [
|
collate_data["scanned_n_to_world_pose_9d"] = [
|
||||||
torch.tensor(item["scanned_n_to_world_pose_9d"]) for item in batch
|
torch.tensor(item["scanned_n_to_world_pose_9d"]) for item in batch
|
||||||
]
|
]
|
||||||
|
collate_data["scanned_pts_mask"] = [
|
||||||
|
torch.tensor(item["scanned_pts_mask"]) for item in batch
|
||||||
|
]
|
||||||
''' ------ Fixed Length ------ '''
|
''' ------ Fixed Length ------ '''
|
||||||
|
|
||||||
collate_data["best_to_world_pose_9d"] = torch.stack(
|
collate_data["best_to_world_pose_9d"] = torch.stack(
|
||||||
@ -209,12 +234,14 @@ class NBVReconstructionDataset(BaseDataset):
|
|||||||
collate_data["combined_scanned_pts"] = torch.stack(
|
collate_data["combined_scanned_pts"] = torch.stack(
|
||||||
[torch.tensor(item["combined_scanned_pts"]) for item in batch]
|
[torch.tensor(item["combined_scanned_pts"]) for item in batch]
|
||||||
)
|
)
|
||||||
|
|
||||||
for key in batch[0].keys():
|
for key in batch[0].keys():
|
||||||
if key not in [
|
if key not in [
|
||||||
"scanned_pts",
|
"scanned_pts",
|
||||||
"scanned_n_to_world_pose_9d",
|
"scanned_n_to_world_pose_9d",
|
||||||
"best_to_world_pose_9d",
|
"best_to_world_pose_9d",
|
||||||
"combined_scanned_pts",
|
"combined_scanned_pts",
|
||||||
|
"scanned_pts_mask",
|
||||||
]:
|
]:
|
||||||
collate_data[key] = [item[key] for item in batch]
|
collate_data[key] = [item[key] for item in batch]
|
||||||
return collate_data
|
return collate_data
|
||||||
@ -230,10 +257,9 @@ if __name__ == "__main__":
|
|||||||
torch.manual_seed(seed)
|
torch.manual_seed(seed)
|
||||||
np.random.seed(seed)
|
np.random.seed(seed)
|
||||||
config = {
|
config = {
|
||||||
"root_dir": "/data/hofee/data/new_full_data",
|
"root_dir": "/data/hofee/nbv_rec_part2_preprocessed",
|
||||||
"model_dir": "../data/scaled_object_meshes",
|
|
||||||
"source": "nbv_reconstruction_dataset",
|
"source": "nbv_reconstruction_dataset",
|
||||||
"split_file": "/data/hofee/data/new_full_data_list/OmniObject3d_train.txt",
|
"split_file": "/data/hofee/data/sample.txt",
|
||||||
"load_from_preprocess": True,
|
"load_from_preprocess": True,
|
||||||
"ratio": 0.5,
|
"ratio": 0.5,
|
||||||
"batch_size": 2,
|
"batch_size": 2,
|
||||||
|
@ -91,25 +91,49 @@ class NBVReconstructionPipeline(nn.Module):
|
|||||||
"scanned_n_to_world_pose_9d"
|
"scanned_n_to_world_pose_9d"
|
||||||
] # List(B): Tensor(S x 9)
|
] # List(B): Tensor(S x 9)
|
||||||
|
|
||||||
|
scanned_pts_mask_batch = data["scanned_pts_mask"] # List(B): Tensor(N)
|
||||||
|
|
||||||
device = next(self.parameters()).device
|
device = next(self.parameters()).device
|
||||||
|
|
||||||
embedding_list_batch = []
|
embedding_list_batch = []
|
||||||
|
|
||||||
combined_scanned_pts_batch = data["combined_scanned_pts"] # Tensor(B x N x 3)
|
combined_scanned_pts_batch = data["combined_scanned_pts"] # Tensor(B x N x 3)
|
||||||
global_scanned_feat = self.pts_encoder.encode_points(
|
global_scanned_feat, per_point_feat_batch = self.pts_encoder.encode_points(
|
||||||
combined_scanned_pts_batch, require_per_point_feat=False
|
combined_scanned_pts_batch, require_per_point_feat=True
|
||||||
) # global_scanned_feat: Tensor(B x Dg)
|
) # global_scanned_feat: Tensor(B x Dg)
|
||||||
|
batch_size = len(scanned_n_to_world_pose_9d_batch)
|
||||||
for scanned_n_to_world_pose_9d in scanned_n_to_world_pose_9d_batch:
|
for i in range(batch_size):
|
||||||
scanned_n_to_world_pose_9d = scanned_n_to_world_pose_9d.to(device) # Tensor(S x 9)
|
seq_len = len(scanned_n_to_world_pose_9d_batch[i])
|
||||||
|
scanned_n_to_world_pose_9d = scanned_n_to_world_pose_9d_batch[i].to(device) # Tensor(S x 9)
|
||||||
|
scanned_pts_mask = scanned_pts_mask_batch[i] # Tensor(S x N)
|
||||||
|
per_point_feat = per_point_feat_batch[i] # Tensor(N x Dp)
|
||||||
|
partial_point_feat_seq = []
|
||||||
|
for j in range(seq_len):
|
||||||
|
partial_per_point_feat = per_point_feat[scanned_pts_mask[j]]
|
||||||
|
if partial_per_point_feat.shape[0] == 0:
|
||||||
|
partial_point_feat = torch.zeros(per_point_feat.shape[1], device=device)
|
||||||
|
else:
|
||||||
|
partial_point_feat = torch.mean(partial_per_point_feat, dim=0) # Tensor(Dp)
|
||||||
|
partial_point_feat_seq.append(partial_point_feat)
|
||||||
|
partial_point_feat_seq = torch.stack(partial_point_feat_seq, dim=0) # Tensor(S x Dp)
|
||||||
|
|
||||||
pose_feat_seq = self.pose_encoder.encode_pose(scanned_n_to_world_pose_9d) # Tensor(S x Dp)
|
pose_feat_seq = self.pose_encoder.encode_pose(scanned_n_to_world_pose_9d) # Tensor(S x Dp)
|
||||||
seq_embedding = pose_feat_seq
|
|
||||||
|
seq_embedding = torch.cat([partial_point_feat_seq, pose_feat_seq], dim=-1)
|
||||||
|
|
||||||
embedding_list_batch.append(seq_embedding) # List(B): Tensor(S x (Dp))
|
embedding_list_batch.append(seq_embedding) # List(B): Tensor(S x (Dp))
|
||||||
|
|
||||||
seq_feat = self.seq_encoder.encode_sequence(embedding_list_batch) # Tensor(B x Ds)
|
seq_feat = self.seq_encoder.encode_sequence(embedding_list_batch) # Tensor(B x Ds)
|
||||||
main_feat = torch.cat([seq_feat, global_scanned_feat], dim=-1) # Tensor(B x (Ds+Dg))
|
main_feat = torch.cat([seq_feat, global_scanned_feat], dim=-1) # Tensor(B x (Ds+Dg))
|
||||||
|
|
||||||
if torch.isnan(main_feat).any():
|
if torch.isnan(main_feat).any():
|
||||||
|
for i in range(len(main_feat)):
|
||||||
|
if torch.isnan(main_feat[i]).any():
|
||||||
|
scanned_pts_mask = scanned_pts_mask_batch[i]
|
||||||
|
Log.info(f"scanned_pts_mask shape: {scanned_pts_mask.shape}")
|
||||||
|
Log.info(f"scanned_pts_mask sum: {scanned_pts_mask.sum()}")
|
||||||
|
import ipdb
|
||||||
|
ipdb.set_trace()
|
||||||
Log.error("nan in main_feat", True)
|
Log.error("nan in main_feat", True)
|
||||||
|
|
||||||
return main_feat
|
return main_feat
|
||||||
|
Loading…
x
Reference in New Issue
Block a user