File based header detection, collate_per_PC training.
This commit is contained in:
@ -20,6 +20,7 @@ class CustomShapeNet(InMemoryDataset):
|
||||
headers=True, **kwargs):
|
||||
self.has_headers = headers
|
||||
self.collate_per_element = collate_per_segment
|
||||
self.train = train
|
||||
super(CustomShapeNet, self).__init__(root, transform, pre_transform, pre_filter)
|
||||
path = self.processed_paths[0] if train else self.processed_paths[-1]
|
||||
self.data, self.slices = torch.load(path)
|
||||
@ -70,73 +71,71 @@ class CustomShapeNet(InMemoryDataset):
|
||||
return data
|
||||
|
||||
def process(self, delimiter=' '):
|
||||
# idx = self.categories[self.category]
|
||||
# paths = [osp.join(path, idx) for path in self.raw_paths]
|
||||
|
||||
datasets = defaultdict(list)
|
||||
for idx, setting in enumerate(self.raw_file_names):
|
||||
path_to_clouds = os.path.join(self.raw_dir, setting)
|
||||
idx, data_folder = (0, self.raw_file_names[0]) if self.train else (1, self.raw_file_names[1])
|
||||
path_to_clouds = os.path.join(self.raw_dir, data_folder)
|
||||
|
||||
if '.headers' in os.listdir(path_to_clouds):
|
||||
self.has_headers = True
|
||||
elif 'no.headers' in os.listdir(path_to_clouds):
|
||||
self.has_headers = False
|
||||
else:
|
||||
pass
|
||||
if '.headers' in os.listdir(path_to_clouds):
|
||||
self.has_headers = True
|
||||
elif 'no.headers' in os.listdir(path_to_clouds):
|
||||
self.has_headers = False
|
||||
else:
|
||||
pass
|
||||
|
||||
for pointcloud in tqdm(os.scandir(path_to_clouds)):
|
||||
for pointcloud in tqdm(os.scandir(path_to_clouds)):
|
||||
if not os.path.isdir(pointcloud):
|
||||
continue
|
||||
data, paths = None, list()
|
||||
for ext in ['dat', 'xyz']:
|
||||
paths.extend(glob.glob(os.path.join(pointcloud.path, f'*.{ext}')))
|
||||
|
||||
if not os.path.isdir(pointcloud):
|
||||
continue
|
||||
data, paths = None, list()
|
||||
for ext in ['dat', 'xyz']:
|
||||
paths.extend(glob.glob(os.path.join(pointcloud.path, f'*.{ext}')))
|
||||
for element in paths:
|
||||
if all([x not in os.path.split(element)[-1] for x in ['pc.dat', 'pc.xyz']]):
|
||||
# Assign training data to the data container
|
||||
# Following the original logic;
|
||||
# y should be the label;
|
||||
# pos should be the six dimensional vector describing: !its pos not points!!
|
||||
# x,y,z,x_rot,y_rot,z_rot
|
||||
for element in paths:
|
||||
if all([x not in os.path.split(element)[-1] for x in ['pc.dat', 'pc.xyz']]):
|
||||
# Assign training data to the data container
|
||||
# Following the original logic;
|
||||
# y should be the label;
|
||||
# pos should be the six dimensional vector describing: !its pos not points!!
|
||||
# x,y,z,x_rot,y_rot,z_rot
|
||||
|
||||
# Get the y - Label
|
||||
y_raw = next(i for i, v in enumerate(self.categories.keys()) if v.lower() in element.lower())
|
||||
# y_raw = os.path.splitext(element)[0].split('_')[-2]
|
||||
with open(element,'r') as f:
|
||||
if self.has_headers:
|
||||
headers = f.__next__()
|
||||
# Check if there are no useable nodes in this file, header says 0.
|
||||
if not int(headers.rstrip().split(delimiter)[0]):
|
||||
continue
|
||||
# Get the y - Label
|
||||
y_raw = next(i for i, v in enumerate(self.categories.keys()) if v.lower() in element.lower())
|
||||
# y_raw = os.path.splitext(element)[0].split('_')[-2]
|
||||
with open(element,'r') as f:
|
||||
if self.has_headers:
|
||||
headers = f.__next__()
|
||||
# Check if there are no useable nodes in this file, header says 0.
|
||||
if not int(headers.rstrip().split(delimiter)[0]):
|
||||
continue
|
||||
|
||||
# Iterate over all rows
|
||||
src = [[float(x) if x not in ['-nan(ind)', 'nan(ind)'] else 0
|
||||
for x in line.rstrip().split(delimiter)[None:None]] for line in f if line != '']
|
||||
points = torch.tensor(src, dtype=None).squeeze()
|
||||
if not len(points.shape) > 1:
|
||||
continue
|
||||
# pos = points[:, :3]
|
||||
# norm = points[:, 3:]
|
||||
y_all = [y_raw] * points.shape[0]
|
||||
y = torch.as_tensor(y_all, dtype=torch.int)
|
||||
# points = torch.as_tensor(points, dtype=torch.float)
|
||||
# norm = torch.as_tensor(norm, dtype=torch.float)
|
||||
if self.collate_per_element:
|
||||
data = Data(y=y, pos=points[:, :3])
|
||||
else:
|
||||
if not data:
|
||||
data = defaultdict(list)
|
||||
for key, val in dict(y=y, pos= points[:, :3]).items():
|
||||
data[key].append(val)
|
||||
# , points=points, norm=points[:3], )
|
||||
data = self._transform_and_filter(data)
|
||||
if self.collate_per_element:
|
||||
datasets[setting].append(data)
|
||||
if not self.collate_per_element:
|
||||
datasets[setting].append(Data(**{key: torch.cat(data[key]) for key in data.keys()}))
|
||||
# Iterate over all rows
|
||||
src = [[float(x) if x not in ['-nan(ind)', 'nan(ind)'] else 0
|
||||
for x in line.rstrip().split(delimiter)[None:None]] for line in f if line != '']
|
||||
points = torch.tensor(src, dtype=None).squeeze()
|
||||
if not len(points.shape) > 1:
|
||||
continue
|
||||
# pos = points[:, :3]
|
||||
# norm = points[:, 3:]
|
||||
y_all = [y_raw] * points.shape[0]
|
||||
y = torch.as_tensor(y_all, dtype=torch.int)
|
||||
# points = torch.as_tensor(points, dtype=torch.float)
|
||||
# norm = torch.as_tensor(norm, dtype=torch.float)
|
||||
if self.collate_per_element:
|
||||
data = Data(y=y, pos=points[:, :3])
|
||||
else:
|
||||
if not data:
|
||||
data = defaultdict(list)
|
||||
for key, val in dict(y=y, pos= points[:, :3]).items():
|
||||
data[key].append(val)
|
||||
# , points=points, norm=points[:3], )
|
||||
data = self._transform_and_filter(data)
|
||||
if self.collate_per_element:
|
||||
datasets[data_folder].append(data)
|
||||
if not self.collate_per_element:
|
||||
datasets[data_folder].append(Data(**{key: torch.cat(data[key]) for key in data.keys()}))
|
||||
|
||||
if datasets[data_folder]:
|
||||
os.makedirs(self.processed_dir, exist_ok=True)
|
||||
torch.save(self.collate(datasets[setting]), self.processed_paths[idx])
|
||||
torch.save(self.collate(datasets[data_folder]), self.processed_paths[idx])
|
||||
|
||||
def __repr__(self):
|
||||
return f'{self.__class__.__name__}({len(self)})'
|
||||
@ -291,7 +290,7 @@ class PredictNetPartSegDataset(Dataset):
|
||||
def __init__(self, root_dir, transform=None, npoints=2048, headers=True):
|
||||
super(PredictNetPartSegDataset, self).__init__()
|
||||
self.npoints = npoints
|
||||
self.dataset = PredictionShapeNet(root=root_dir, train=False, transform=transform, headers=headers)
|
||||
self.dataset = ShapeNetPartSegDataset(root=root_dir, train=False, transform=transform, headers=headers)
|
||||
|
||||
def __getitem__(self, index):
|
||||
data = self.dataset[index]
|
||||
|
Reference in New Issue
Block a user