ZiyuG commited on
Commit
562fa4c
·
verified ·
1 Parent(s): 4c99777

Delete sam2point/voxelizer0.py

Browse files
Files changed (1) hide show
  1. sam2point/voxelizer0.py +0 -140
sam2point/voxelizer0.py DELETED
@@ -1,140 +0,0 @@
1
- # Codes are taken from BPNet, CVPR'21
2
- # https://github.com/wbhu/BPNet/blob/main/dataset/voxelizer.py
3
-
4
- import collections
5
- import numpy as np
6
- from sam2point.voxelization_utils import sparse_quantize
7
- from scipy.linalg import expm, norm
8
-
9
-
10
- # Rotation matrix along axis with angle theta
11
- def M(axis, theta):
12
- return expm(np.cross(np.eye(3), axis / norm(axis) * theta))
13
-
14
-
15
- class Voxelizer:
16
-
17
- def __init__(self,
18
- voxel_size=1,
19
- clip_bound=None,
20
- use_augmentation=False,
21
- scale_augmentation_bound=None,
22
- rotation_augmentation_bound=None,
23
- translation_augmentation_ratio_bound=None,
24
- ignore_label=255):
25
- '''
26
- Args:
27
- voxel_size: side length of a voxel
28
- clip_bound: boundary of the voxelizer. Points outside the bound will be deleted
29
- expects either None or an array like ((-100, 100), (-100, 100), (-100, 100)).
30
- scale_augmentation_bound: None or (0.9, 1.1)
31
- rotation_augmentation_bound: None or ((np.pi / 6, np.pi / 6), None, None) for 3 axis.
32
- Use random order of x, y, z to prevent bias.
33
- translation_augmentation_bound: ((-5, 5), (0, 0), (-10, 10))
34
- ignore_label: label assigned for ignore (not a training label).
35
- '''
36
- self.voxel_size = voxel_size
37
- self.clip_bound = clip_bound
38
- self.ignore_label = ignore_label
39
-
40
- # Augmentation
41
- self.use_augmentation = use_augmentation
42
- self.scale_augmentation_bound = scale_augmentation_bound
43
- self.rotation_augmentation_bound = rotation_augmentation_bound
44
- self.translation_augmentation_ratio_bound = translation_augmentation_ratio_bound
45
-
46
- def get_transformation_matrix(self):
47
- voxelization_matrix, rotation_matrix = np.eye(4), np.eye(4)
48
- # Get clip boundary from config or pointcloud.
49
- # Get inner clip bound to crop from.
50
-
51
- # Transform pointcloud coordinate to voxel coordinate.
52
- # 1. Random rotation
53
- rot_mat = np.eye(3)
54
- if self.use_augmentation and self.rotation_augmentation_bound is not None:
55
- if isinstance(self.rotation_augmentation_bound, collections.Iterable):
56
- rot_mats = []
57
- for axis_ind, rot_bound in enumerate(self.rotation_augmentation_bound):
58
- theta = 0
59
- axis = np.zeros(3)
60
- axis[axis_ind] = 1
61
- if rot_bound is not None:
62
- theta = np.random.uniform(*rot_bound)
63
- rot_mats.append(M(axis, theta))
64
- # Use random order
65
- np.random.shuffle(rot_mats)
66
- rot_mat = rot_mats[0] @ rot_mats[1] @ rot_mats[2]
67
- else:
68
- raise ValueError()
69
- rotation_matrix[:3, :3] = rot_mat
70
- # 2. Scale and translate to the voxel space.
71
- scale = 1 / self.voxel_size
72
- if self.use_augmentation and self.scale_augmentation_bound is not None:
73
- scale *= np.random.uniform(*self.scale_augmentation_bound)
74
- np.fill_diagonal(voxelization_matrix[:3, :3], scale)
75
- # Get final transformation matrix.
76
- return voxelization_matrix, rotation_matrix
77
-
78
- def clip(self, coords, center=None, trans_aug_ratio=None):
79
- bound_min = np.min(coords, 0).astype(float)
80
- bound_max = np.max(coords, 0).astype(float)
81
- bound_size = bound_max - bound_min
82
- if center is None:
83
- center = bound_min + bound_size * 0.5
84
- lim = self.clip_bound
85
- if trans_aug_ratio is not None:
86
- trans = np.multiply(trans_aug_ratio, bound_size)
87
- center += trans
88
- # Clip points outside the limit
89
- clip_inds = ((coords[:, 0] >= (lim[0][0] + center[0])) &
90
- (coords[:, 0] < (lim[0][1] + center[0])) &
91
- (coords[:, 1] >= (lim[1][0] + center[1])) &
92
- (coords[:, 1] < (lim[1][1] + center[1])) &
93
- (coords[:, 2] >= (lim[2][0] + center[2])) &
94
- (coords[:, 2] < (lim[2][1] + center[2])))
95
- return clip_inds
96
-
97
- def voxelize(self, coords, feats, labels, center=None, link=None, return_ind=False):
98
- assert coords.shape[1] == 3 and coords.shape[0] == feats.shape[0] and coords.shape[0]
99
- if self.clip_bound is not None:
100
- trans_aug_ratio = np.zeros(3)
101
- if self.use_augmentation and self.translation_augmentation_ratio_bound is not None:
102
- for axis_ind, trans_ratio_bound in enumerate(self.translation_augmentation_ratio_bound):
103
- trans_aug_ratio[axis_ind] = np.random.uniform(*trans_ratio_bound)
104
-
105
- clip_inds = self.clip(coords, center, trans_aug_ratio)
106
- if clip_inds.sum():
107
- coords, feats = coords[clip_inds], feats[clip_inds]
108
- if labels is not None:
109
- labels = labels[clip_inds]
110
-
111
- # Get rotation and scale
112
- M_v, M_r = self.get_transformation_matrix()
113
- # Apply transformations
114
- rigid_transformation = M_v
115
- if self.use_augmentation:
116
- rigid_transformation = M_r @ rigid_transformation
117
-
118
- homo_coords = np.hstack((coords, np.ones((coords.shape[0], 1), dtype=coords.dtype)))
119
- coords_aug = np.floor(homo_coords @ rigid_transformation.T[:, :3])
120
-
121
- # Align all coordinates to the origin.
122
- min_coords = coords_aug.min(0)
123
- M_t = np.eye(4)
124
- M_t[:3, -1] = -min_coords
125
- rigid_transformation = M_t @ rigid_transformation
126
- coords_aug = np.floor(coords_aug - min_coords)
127
-
128
- inds, inds_reconstruct = sparse_quantize(coords_aug, return_index=True)
129
- coords_aug, feats, labels = coords_aug[inds], feats[inds], labels[inds]
130
-
131
- # Normal rotation
132
- if feats.shape[1] > 6:
133
- feats[:, 3:6] = feats[:, 3:6] @ (M_r[:3, :3].T)
134
-
135
- if return_ind:
136
- return coords_aug, feats, labels, np.array(inds_reconstruct), inds
137
- if link is not None:
138
- return coords_aug, feats, labels, np.array(inds_reconstruct), link[inds]
139
-
140
- return coords_aug, feats, labels, np.array(inds_reconstruct)