-
Notifications
You must be signed in to change notification settings - Fork 0
/
point_loader.py
161 lines (132 loc) · 5.52 KB
/
point_loader.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
'''Dataloader for 3D points.'''
from glob import glob
import multiprocessing as mp
from os.path import join, exists
import numpy as np
import torch
import SharedArray as SA
import dataset.augmentation as t
from dataset.voxelizer import Voxelizer
def sa_create(name, var):
'''Create share memory.'''
shared_mem = SA.create(name, var.shape, dtype=var.dtype)
shared_mem[...] = var[...]
shared_mem.flags.writeable = False
return shared_mem
def collation_fn_raw(batch):
'''
:param batch:
:return: coords_batch: N x 4 (x,y,z,batch)
'''
coords, feats, inds, path, color = list(zip(*batch))
for i, coord in enumerate(coords):
coord[:, 0] *= i
return torch.cat(coords), torch.cat(feats),torch.cat(inds), path, torch.cat(color)
def collation_fn(batch):
'''
:param batch:
:return: coords_batch: N x 4 (x,y,z,batch)
'''
coords, feats, labels = list(zip(*batch))
for i, coord in enumerate(coords):
coord[:, 0] *= i
return torch.cat(coords), torch.cat(feats), torch.cat(labels)
def collation_fn_eval_all(batch):
'''
:param batch:
:return: coords_batch: N x 4 (x,y,z,batch)
'''
coords, feats, labels, inds_recons = list(zip(*batch))
inds_recons = list(inds_recons)
accmulate_points_num = 0
for i, coord in enumerate(coords):
coord[:, 0] *= i
inds_recons[i] = accmulate_points_num + inds_recons[i]
accmulate_points_num += coords[i].shape[0]
return torch.cat(coords), torch.cat(feats), torch.cat(labels), torch.cat(inds_recons)
class Point3DLoader(torch.utils.data.Dataset):
'''Dataloader for 3D points and labels.'''
# Augmentation arguments
SCALE_AUGMENTATION_BOUND = (0.9, 1.1)
ROTATION_AUGMENTATION_BOUND = ((-np.pi / 64, np.pi / 64), (-np.pi / 64, np.pi / 64), (-np.pi,
np.pi))
TRANSLATION_AUGMENTATION_RATIO_BOUND = ((-0.2, 0.2), (-0.2, 0.2), (0, 0))
ELASTIC_DISTORT_PARAMS = ((0.2, 0.4), (0.8, 1.6))
ROTATION_AXIS = 'z'
LOCFEAT_IDX = 2
def __init__(self, datapath_prefix='../Dataset/iccvw/ChallengeDevelopmentSet', voxel_size=0.05,
split='train', aug=False, memcache_init=False, identifier=1233, loop=1,
data_aug_color_trans_ratio=0.1,
data_aug_color_jitter_std=0.05,
data_aug_hue_max=0.5,
data_aug_saturation_max=0.2,
eval_all=False, input_color=False
):
super().__init__()
self.split = split
if split is None:
split = ''
self.identifier = identifier
# PointCloud
self.data_paths = sorted(glob(join(datapath_prefix, 'pcl', '*.pth')))
if len(self.data_paths) == 0:
raise Exception('0 file is loaded in the point loader.')
self.input_color = input_color
self.voxel_size = voxel_size
self.aug = aug
self.loop = loop
self.eval_all = eval_all
self.use_shm = memcache_init
self.voxelizer = Voxelizer(
voxel_size=voxel_size,
clip_bound=None,
use_augmentation=True,
scale_augmentation_bound=self.SCALE_AUGMENTATION_BOUND,
rotation_augmentation_bound=self.ROTATION_AUGMENTATION_BOUND,
translation_augmentation_ratio_bound=self.TRANSLATION_AUGMENTATION_RATIO_BOUND)
if aug:
prevoxel_transform_train = [
t.ElasticDistortion(self.ELASTIC_DISTORT_PARAMS)]
self.prevoxel_transforms = t.Compose(prevoxel_transform_train)
input_transforms = [
t.RandomHorizontalFlip(self.ROTATION_AXIS, is_temporal=False),
t.ChromaticAutoContrast(),
t.ChromaticTranslation(data_aug_color_trans_ratio),
t.ChromaticJitter(data_aug_color_jitter_std),
t.HueSaturationTranslation(
data_aug_hue_max, data_aug_saturation_max),
]
self.input_transforms = t.Compose(input_transforms)
def __getitem__(self, index_long):
index = index_long % len(self.data_paths)
if True:
locs_in, feats_in = torch.load(self.data_paths[index])
# no color in the input point cloud, e.g nuscenes
if np.isscalar(feats_in) and feats_in == 0:
feats_in = np.zeros_like(locs_in)
feats_in = (feats_in + 1.) * 127.5
color = feats_in
locs = self.prevoxel_transforms(locs_in) if self.aug else locs_in
locs, feats, inds_reconstruct = self.voxelizer.voxelize(
locs, feats_in, labels=None)
if self.eval_all:
# labels = labels_in
pass
if self.aug:
# locs, feats, labels = self.input_transforms(locs, feats, labels)
pass
coords = torch.from_numpy(locs).int()
coords = torch.cat(
(torch.ones(coords.shape[0], 1, dtype=torch.int), coords), dim=1)
# No color
if self.input_color:
feats = torch.from_numpy(feats).float() / 127.5 - 1.
else:
feats = torch.ones(coords.shape[0], 3)
# labels = torch.from_numpy(labels).long()
if self.eval_all:
# return coords, feats, labels, torch.from_numpy(inds_reconstruct).long()
pass
return coords, feats, torch.from_numpy(inds_reconstruct).long(), self.data_paths[index], torch.from_numpy(color).long()
def __len__(self):
return len(self.data_paths) * self.loop