File size: 14,565 Bytes
cf84204 | 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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 | import numpy as np
from nltk.tree import Tree
import random
class Compose(object):
def __init__(self, transforms):
self.transforms = transforms
def __call__(self, coords_and_feats):
for t in self.transforms:
coords_and_feats = t(coords_and_feats)
return coords_and_feats
def __str__(self) -> str:
sep = "\n\t"
msg = "Compose of ["
for t in self.transforms:
msg += f"{sep}{t},"
msg += "]\n"
return msg
def __repr__(self) -> str:
sep = "\n\t"
msg = "Compose of ["
for t in self.transforms:
msg += f"{sep}{t}"
msg += "]\n"
return msg
class RandomDropSubTrees(object):
def __init__(self, probs=[0.1, 0.2, 0.3, 0.4, 0.5], max_cnt=5):
self.probs = probs
self.max_cnt = max_cnt
self.cnt = 0
def remove_subtrees(self, root, level_idx):
reduced_root = Tree(root.label(), [])
if len(root) == 0:
return reduced_root
p = np.random.uniform(0, 1, len(root))
# padding the dropping probabilities
if level_idx >= len(self.probs):
level_idx = len(self.probs) - 1
for child_idx in range(len(root)):
if self.cnt > self.max_cnt:
reduced_root.append(root[child_idx])
continue
else:
if p[child_idx] > self.probs[level_idx]:
reduced_root.append(
self.remove_subtrees(root[child_idx], level_idx + 1)
)
else:
self.cnt += 1
return reduced_root
def __call__(self, tree):
self.cnt = 0
return self.remove_subtrees(tree, level_idx=0)
def __str__(self) -> str:
return f"RandomDropSubTrees(probs={self.probs}, max_cnt={self.max_cnt})"
def __repr__(self) -> str:
return f"RandomDropSubTrees(probs={self.probs}, max_cnt={self.max_cnt})"
class RandomSkipParentNode(object):
def __init__(self, probs=[0.1], max_cnt=5):
self.probs = probs
self.max_cnt = max_cnt
self.cnt = 0
def move_grandson_to_son(self, root, level_idx):
if len(root) == 0 or len(root) == 1:
return root
p = np.random.uniform(0, 1, len(root))
# padding the dropping probabilities
if level_idx >= len(self.probs):
level_idx = len(self.probs) - 1
for child_idx in range(len(root)):
if self.cnt >= self.max_cnt:
break
if p[child_idx] < self.probs[level_idx]:
if len(root[child_idx]) == 0 or len(root[child_idx]) == 1:
continue
else:
idx = random.randint(0, len(root[child_idx]) - 1)
root[child_idx] = root[child_idx][idx]
self.cnt += 1
else:
root[child_idx] = self.move_grandson_to_son(
root[child_idx], level_idx + 1
)
return root
def __call__(self, tree):
self.cnt = 0
return self.move_grandson_to_son(tree, level_idx=0)
def __str__(self) -> str:
return f"RandomSkipParentNode(probs={self.probs}, max_cnt={self.max_cnt})"
def __repr__(self) -> str:
return f"RandomSkipParentNode(probs={self.probs}, max_cnt={self.max_cnt})"
class RandomSwapSiblingSubTrees(object):
def __init__(self, probs=[0.1], max_cnt=5):
self.probs = probs
self.max_cnt = max_cnt
self.cnt = 0
def swap_sibling_subtrees(self, root, level_idx):
if len(root) < 2:
return root
p = np.random.uniform(0, 1, len(root))
# padding the dropping probabilities
if level_idx >= len(self.probs):
level_idx = len(self.probs) - 1
for child_idx in range(len(root)):
if self.cnt >= self.max_cnt:
break
if p[child_idx] < self.probs[level_idx]:
if len(root[child_idx]) < 2:
continue
else:
my_subtree_idx = random.randint(0, len(root[child_idx]) - 1)
sibling_idx = random.randint(0, len(root) - 1)
if len(root[sibling_idx]) == 0:
continue
sibling_subtree_idx = random.randint(0, len(root[sibling_idx]) - 1)
my_subtree = root[child_idx][my_subtree_idx].copy()
sibling_subtree = root[sibling_idx][sibling_subtree_idx].copy()
root[child_idx][my_subtree_idx] = sibling_subtree
root[sibling_idx][sibling_subtree_idx] = my_subtree
self.cnt += 1
else:
root[child_idx] = self.swap_sibling_subtrees(
root[child_idx], level_idx + 1
)
return root
def __call__(self, tree):
self.cnt = 0
return self.swap_sibling_subtrees(tree, level_idx=0)
def __str__(self) -> str:
return f"RandomSwapSiblingSubTrees(probs={self.probs}, max_cnt={self.max_cnt})"
def __repr__(self) -> str:
return f"RandomSwapSiblingSubTrees(probs={self.probs}, max_cnt={self.max_cnt})"
class RandomRotateAligned(object):
def __init__(self, p=0.5, axis=2):
self.prob = p
self.axis = axis
def __call__(self, coords_and_feats):
coord = coords_and_feats[:, :3]
if np.random.rand() < self.prob:
angle = np.random.uniform() * 2 * np.pi
cos, sin = np.cos(angle), np.sin(angle)
R_x = np.array([[1, 0, 0], [0, cos, -sin], [0, sin, cos]])
R_y = np.array([[cos, 0, sin], [0, 1, 0], [-sin, 0, cos]])
R_z = np.array([[cos, -sin, 0], [sin, cos, 0], [0, 0, 1]])
R = [R_x, R_y, R_z][self.axis]
coord = np.dot(coord, R)
coords_and_feats[:, :3] = coord
return coords_and_feats
def __str__(self) -> str:
return f"RandomRotateAligned(p={self.prob},axis={self.axis})"
def __repr__(self) -> str:
return f"RandomRotateAligned(p={self.prob},axis={self.axis})"
class RandomRotate(object):
def __init__(self, sigma=0.03, clip=0.09, p=0.5):
self.sigma = sigma
self.clip = clip
self.prob = p
def __call__(self, coords_and_feats):
coord = coords_and_feats[:, :3]
if np.random.rand() < self.prob:
angle_x = np.random.uniform() * 2 * np.pi
angle_y = np.random.uniform() * 2 * np.pi
angle_z = np.random.uniform() * 2 * np.pi
cos_x, sin_x = np.cos(angle_x), np.sin(angle_x)
cos_y, sin_y = np.cos(angle_y), np.sin(angle_y)
cos_z, sin_z = np.cos(angle_z), np.sin(angle_z)
R_x = np.array([[1, 0, 0], [0, cos_x, -sin_x], [0, sin_x, cos_x]])
R_y = np.array([[cos_y, 0, sin_y], [0, 1, 0], [-sin_y, 0, cos_y]])
R_z = np.array([[cos_z, -sin_z, 0], [sin_z, cos_z, 0], [0, 0, 1]])
R = np.dot(R_z, np.dot(R_y, R_x))
coord = np.dot(coord, R)
coords_and_feats[:, :3] = coord
return coords_and_feats
def __str__(self) -> str:
return f"RandomRotate(p={self.prob})"
def __repr__(self) -> str:
return f"RandomRotate(p={self.prob})"
class RandomMaskFeats(object):
def __init__(self, p=0.2):
self.prob = p
def __call__(self, coords_and_feats):
if len(coords_and_feats[0]) > 5:
feats = coords_and_feats[:, 5:]
feats[
:,
np.random.choice(
np.arange(len(feats[0])), int(len(feats[0]) * self.prob)
),
] = 0
coords_and_feats[:, 5:] = feats
return coords_and_feats
def __str__(self) -> str:
return f"RandomMaskFeats(p={self.prob})"
def __repr__(self) -> str:
return f"RandomMaskFeats(p={self.prob})"
class RandomElasticate(object):
def __init__(self, p=0.2, scales=[0.8, 1.2]):
self.prob = p
self.scales = scales
def __call__(self, coords_and_feats):
if len(coords_and_feats[0]) > 5:
if np.random.rand() < self.prob:
branches = coords_and_feats[:, 5:]
scales = np.random.uniform(
self.scales[0], self.scales[1], branches.shape
)
branches *= scales
coords_and_feats[:, 5:] = branches
return coords_and_feats
def __str__(self) -> str:
return f"RandomElasticate(p={self.prob}, scales={self.scales})"
def __repr__(self) -> str:
return f"RandomElasticate(p={self.prob}, scales={self.scales})"
class RandomScaleCoords(object):
def __init__(self, scale=[0.8, 1.2], p=0.5):
self.scale = scale
self.prob = p
def __call__(self, coords_and_feats):
if np.random.rand() < self.prob:
scale = np.random.uniform(self.scale[0], self.scale[1])
coords_and_feats[:, :4] *= scale
if len(coords_and_feats[0]) > 5:
coords_and_feats[:, 5:] *= scale
return coords_and_feats
def __str__(self) -> str:
return f"RandomScaleCoords(p={self.prob}, scale={self.scale})"
def __repr__(self) -> str:
return f"RandomScaleCoords(p={self.prob}, scale={self.scale})"
class RandomScaleCoordsTranslation(object):
def __init__(self, scale=[0.5, 2], p=0.5):
self.scale = scale
self.prob = p
def __call__(self, coords_and_feats):
if np.random.rand() < self.prob:
scale = np.random.uniform(self.scale[0], self.scale[1])
coord1 = coords_and_feats[:, :4]
coord1 *= scale
coords_and_feats[:, :4] = coord1
if len(coords_and_feats[0]) > 5:
coord2 = coords_and_feats[:, 5:]
coord2 *= scale
coords_and_feats[:, 5:] = coord2
return coords_and_feats
def __str__(self) -> str:
return f"RandomScaleCoordsTranslation(p={self.prob}, scale={self.scale})"
def __repr__(self) -> str:
return f"RandomScaleCoordsTranslation(p={self.prob}, scale={self.scale})"
class RandomScaleFeats(object):
def __init__(self, scale=[0.5, 2], p=0.5):
self.scale = scale
self.prob = p
def __call__(self, coords_and_feats):
feats = coords_and_feats[:, 4:]
if np.random.rand() < self.prob:
scale = np.random.uniform(self.scale[0], self.scale[1])
feats *= scale
coords_and_feats[:, 4:] = feats
return coords_and_feats
def __str__(self) -> str:
return f"RandomScaleFeats(p={self.prob}, scale={self.scale})"
def __repr__(self) -> str:
return f"RandomScaleFeats(p={self.prob}, scale={self.scale})"
class RandomShift(object):
def __init__(self, shift=[5, 5, 5], p=0.5):
self.shift = shift
self.prob = p
def __call__(self, coords_and_feats):
coord = coords_and_feats[:, :3]
if np.random.rand() < self.prob:
shift_x = np.random.uniform(-self.shift[0], self.shift[0])
shift_y = np.random.uniform(-self.shift[1], self.shift[1])
shift_z = np.random.uniform(-self.shift[2], self.shift[2])
coord += [shift_x, shift_y, shift_z]
coords_and_feats[:, :3] = coord
return coords_and_feats
def __str__(self) -> str:
return f"RandomShift(p={self.prob}, shift={self.shift})"
def __repr__(self) -> str:
return f"RandomShift(p={self.prob}, shift={self.shift})"
class RandomFlip(object):
def __init__(self, p=0.5):
self.prob = p
def __call__(self, coords_and_feats):
coord = coords_and_feats[:, :3]
if np.random.rand() < self.prob:
if np.random.rand() < 0.5:
coord[:, 0] = -coord[:, 0]
if np.random.rand() < 0.5:
coord[:, 1] = -coord[:, 1]
coords_and_feats[:, :3] = coord
return coords_and_feats
def __str__(self) -> str:
return f"RandomFlip(p={self.prob})"
def __repr__(self) -> str:
return f"RandomFlip(p={self.prob})"
class RandomJitter(object):
def __init__(self, sigma=1, clip=5, p=0.5):
self.sigma = sigma
self.clip = clip
self.prob = p
def __call__(self, coords_and_feats):
coord = coords_and_feats[:, :3]
assert self.clip > 0
if np.random.rand() < self.prob:
jitter = np.clip(
self.sigma * np.random.randn(coord.shape[0], 3), -self.clip, self.clip
)
coord += jitter
coords_and_feats[:, :3] = coord
return coords_and_feats
def __str__(self) -> str:
return f"RandomJitter(p={self.prob}, sigma={self.sigma}, clip={self.clip})"
def __repr__(self) -> str:
return f"RandomJitter(p={self.prob}, sigma={self.sigma}, clip={self.clip})"
class RandomJitterLength(object):
def __init__(self, sigma=0.1, clip=1, p=0.5):
self.sigma = sigma
self.clip = clip
self.prob = p
def __call__(self, coords_and_feats):
feats1 = coords_and_feats[:, 3:4]
assert self.clip > 0
if np.random.rand() < self.prob:
jitter1 = np.clip(
self.sigma * np.random.randn(*feats1.shape), -self.clip, self.clip
)
feats1 += jitter1
if len(coords_and_feats[0]) > 5:
feats2 = coords_and_feats[:, 5:]
jitter2 = np.clip(
self.sigma * np.random.randn(*feats2.shape), -self.clip, self.clip
)
feats2 += jitter2
coords_and_feats[:, 5:] = feats2
coords_and_feats[:, 3:4] = feats1
return coords_and_feats
def __str__(self) -> str:
return (
f"RandomJitterLength(p={self.prob}, sigma={self.sigma}, clip={self.clip})"
)
def __repr__(self) -> str:
return (
f"RandomJitterLength(p={self.prob}, sigma={self.sigma}, clip={self.clip})"
)
if __name__ == "__main__":
transform = RandomScaleCoordsTranslation(p=1)
coords_feats = np.random.rand(1024, 3)
# coords_feats = np.concatenate([
# np.zeros((1,29)),
# coords_feats
# ])
print(coords_feats[-1])
transformed = transform(coords_feats)
print(transformed[-1])
print(coords_feats[-1])
|