File size: 12,806 Bytes
97aa5af | 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 | import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
from . import quaternion # works with (w, x, y, z) quaternions
from scipy.spatial.transform import Rotation
from . import se3
def quat2mat(quat):
x, y, z, w = quat[:, 0], quat[:, 1], quat[:, 2], quat[:, 3]
B = quat.size(0)
w2, x2, y2, z2 = w.pow(2), x.pow(2), y.pow(2), z.pow(2)
wx, wy, wz = w*x, w*y, w*z
xy, xz, yz = x*y, x*z, y*z
rotMat = torch.stack([w2 + x2 - y2 - z2, 2*xy - 2*wz, 2*wy + 2*xz,
2*wz + 2*xy, w2 - x2 + y2 - z2, 2*yz - 2*wx,
2*xz - 2*wy, 2*wx + 2*yz, w2 - x2 - y2 + z2], dim=1).reshape(B, 3, 3)
return rotMat
def transform_point_cloud(point_cloud: torch.Tensor, rotation: torch.Tensor, translation: torch.Tensor):
if len(rotation.size()) == 2:
rot_mat = quat2mat(rotation)
else:
rot_mat = rotation
return (torch.matmul(rot_mat, point_cloud.permute(0, 2, 1)) + translation.unsqueeze(2)).permute(0, 2, 1)
def convert2transformation(rotation_matrix: torch.Tensor, translation_vector: torch.Tensor):
one_ = torch.tensor([[[0.0, 0.0, 0.0, 1.0]]]).repeat(rotation_matrix.shape[0], 1, 1).to(rotation_matrix) # (Bx1x4)
transformation_matrix = torch.cat([rotation_matrix, translation_vector.unsqueeze(-1)], dim=2) # (Bx3x4)
transformation_matrix = torch.cat([transformation_matrix, one_], dim=1) # (Bx4x4)
return transformation_matrix
def qmul(q, r):
"""
Multiply quaternion(s) q with quaternion(s) r.
Expects two equally-sized tensors of shape (*, 4), where * denotes any number of dimensions.
Returns q*r as a tensor of shape (*, 4).
"""
assert q.shape[-1] == 4
assert r.shape[-1] == 4
original_shape = q.shape
# Compute outer product
terms = torch.bmm(r.view(-1, 4, 1), q.view(-1, 1, 4))
w = terms[:, 0, 0] - terms[:, 1, 1] - terms[:, 2, 2] - terms[:, 3, 3]
x = terms[:, 0, 1] + terms[:, 1, 0] - terms[:, 2, 3] + terms[:, 3, 2]
y = terms[:, 0, 2] + terms[:, 1, 3] + terms[:, 2, 0] - terms[:, 3, 1]
z = terms[:, 0, 3] - terms[:, 1, 2] + terms[:, 2, 1] + terms[:, 3, 0]
return torch.stack((w, x, y, z), dim=1).view(original_shape)
def qmul_np(q, r):
q = torch.from_numpy(q).contiguous()
r = torch.from_numpy(r).contiguous()
return qmul(q, r).numpy()
def euler_to_quaternion(e, order):
"""
Convert Euler angles to quaternions.
"""
assert e.shape[-1] == 3
original_shape = list(e.shape)
original_shape[-1] = 4
e = e.reshape(-1, 3)
x = e[:, 0]
y = e[:, 1]
z = e[:, 2]
rx = np.stack(
(np.cos(x / 2), np.sin(x / 2), np.zeros_like(x), np.zeros_like(x)), axis=1
)
ry = np.stack(
(np.cos(y / 2), np.zeros_like(y), np.sin(y / 2), np.zeros_like(y)), axis=1
)
rz = np.stack(
(np.cos(z / 2), np.zeros_like(z), np.zeros_like(z), np.sin(z / 2)), axis=1
)
result = None
for coord in order:
if coord == "x":
r = rx
elif coord == "y":
r = ry
elif coord == "z":
r = rz
else:
raise
if result is None:
result = r
else:
result = qmul_np(result, r)
# Reverse antipodal representation to have a non-negative "w"
if order in ["xyz", "yzx", "zxy"]:
result *= -1
return result.reshape(original_shape)
class PNLKTransform:
""" rigid motion """
def __init__(self, mag=1, mag_randomly=False):
self.mag = mag
self.randomly = mag_randomly
self.gt = None
self.igt = None
self.index = 0
def generate_transform(self):
# return: a twist-vector
amp = self.mag
if self.randomly:
amp = torch.rand(1, 1) * self.mag
x = torch.randn(1, 6)
x = x / x.norm(p=2, dim=1, keepdim=True) * amp
return x # [1, 6]
def apply_transform(self, p0, x):
# p0: [N, 3]
# x: [1, 6]
g = se3.exp(x).to(p0) # [1, 4, 4]
gt = se3.exp(-x).to(p0) # [1, 4, 4]
p1 = se3.transform(g, p0)
self.gt = gt.squeeze(0) # gt: p1 -> p0
self.igt = g.squeeze(0) # igt: p0 -> p1
return p1
def transform(self, tensor):
x = self.generate_transform()
return self.apply_transform(tensor, x)
def __call__(self, tensor):
return self.transform(tensor)
class RPMNetTransform:
""" rigid motion """
def __init__(self, mag=1, mag_randomly=False):
self.mag = mag
self.randomly = mag_randomly
self.gt = None
self.igt = None
self.index = 0
def generate_transform(self):
# return: a twist-vector
amp = self.mag
if self.randomly:
amp = torch.rand(1, 1) * self.mag
x = torch.randn(1, 6)
x = x / x.norm(p=2, dim=1, keepdim=True) * amp
return x # [1, 6]
def apply_transform(self, p0, x):
# p0: [N, 3]
# x: [1, 6]
g = se3.exp(x).to(p0) # [1, 4, 4]
gt = se3.exp(-x).to(p0) # [1, 4, 4]
p1 = se3.transform(g, p0[:, :3])
if p0.shape[1] == 6: # Need to rotate normals also
g_n = g.clone()
g_n[:, :3, 3] = 0.0
n1 = se3.transform(g_n, p0[:, 3:6])
p1 = torch.cat([p1, n1], axis=-1)
self.gt = gt.squeeze(0) # gt: p1 -> p0
self.igt = g.squeeze(0) # igt: p0 -> p1
return p1
def transform(self, tensor):
x = self.generate_transform()
return self.apply_transform(tensor, x)
def __call__(self, tensor):
return self.transform(tensor)
class PCRNetTransform:
def __init__(self, data_size, angle_range=45, translation_range=1):
self.angle_range = angle_range
self.translation_range = translation_range
self.dtype = torch.float32
self.transformations = [self.create_random_transform(torch.float32, self.angle_range, self.translation_range) for _ in range(data_size)]
self.index = 0
@staticmethod
def deg_to_rad(deg):
return np.pi / 180 * deg
def create_random_transform(self, dtype, max_rotation_deg, max_translation):
max_rotation = self.deg_to_rad(max_rotation_deg)
rot = np.random.uniform(-max_rotation, max_rotation, [1, 3])
trans = np.random.uniform(-max_translation, max_translation, [1, 3])
quat = euler_to_quaternion(rot, "xyz")
vec = np.concatenate([quat, trans], axis=1)
vec = torch.tensor(vec, dtype=dtype)
return vec
@staticmethod
def create_pose_7d(vector: torch.Tensor):
# Normalize the quaternion.
pre_normalized_quaternion = vector[:, 0:4]
normalized_quaternion = F.normalize(pre_normalized_quaternion, dim=1)
# B x 7 vector of 4 quaternions and 3 translation parameters
translation = vector[:, 4:]
vector = torch.cat([normalized_quaternion, translation], dim=1)
return vector.view([-1, 7])
@staticmethod
def get_quaternion(pose_7d: torch.Tensor):
return pose_7d[:, 0:4]
@staticmethod
def get_translation(pose_7d: torch.Tensor):
return pose_7d[:, 4:]
@staticmethod
def quaternion_rotate(point_cloud: torch.Tensor, pose_7d: torch.Tensor):
ndim = point_cloud.dim()
if ndim == 2:
N, _ = point_cloud.shape
assert pose_7d.shape[0] == 1
# repeat transformation vector for each point in shape
quat = PCRNetTransform.get_quaternion(pose_7d).expand([N, -1])
rotated_point_cloud = quaternion.qrot(quat, point_cloud)
elif ndim == 3:
B, N, _ = point_cloud.shape
quat = PCRNetTransform.get_quaternion(pose_7d).unsqueeze(1).expand([-1, N, -1]).contiguous()
rotated_point_cloud = quaternion.qrot(quat, point_cloud)
return rotated_point_cloud
@staticmethod
def quaternion_transform(point_cloud: torch.Tensor, pose_7d: torch.Tensor):
transformed_point_cloud = PCRNetTransform.quaternion_rotate(point_cloud, pose_7d) + PCRNetTransform.get_translation(pose_7d).view(-1, 1, 3).repeat(1, point_cloud.shape[1], 1) # Ps' = R*Ps + t
return transformed_point_cloud
@staticmethod
def convert2transformation(rotation_matrix: torch.Tensor, translation_vector: torch.Tensor):
one_ = torch.tensor([[[0.0, 0.0, 0.0, 1.0]]]).repeat(rotation_matrix.shape[0], 1, 1).to(rotation_matrix) # (Bx1x4)
transformation_matrix = torch.cat([rotation_matrix, translation_vector[:,0,:].unsqueeze(-1)], dim=2) # (Bx3x4)
transformation_matrix = torch.cat([transformation_matrix, one_], dim=1) # (Bx4x4)
return transformation_matrix
def __call__(self, template):
self.igt = self.transformations[self.index]
gt = self.create_pose_7d(self.igt)
source = self.quaternion_rotate(template, gt) + self.get_translation(gt)
return source
class DCPTransform:
def __init__(self, angle_range=45, translation_range=1):
self.angle_range = angle_range*(np.pi/180)
self.translation_range = translation_range
self.index = 0
def generate_transform(self):
self.anglex = np.random.uniform() * self.angle_range
self.angley = np.random.uniform() * self.angle_range
self.anglez = np.random.uniform() * self.angle_range
self.translation = np.array([np.random.uniform(-self.translation_range, self.translation_range),
np.random.uniform(-self.translation_range, self.translation_range),
np.random.uniform(-self.translation_range, self.translation_range)])
# cosx = np.cos(self.anglex)
# cosy = np.cos(self.angley)
# cosz = np.cos(self.anglez)
# sinx = np.sin(self.anglex)
# siny = np.sin(self.angley)
# sinz = np.sin(self.anglez)
# Rx = np.array([[1, 0, 0],
# [0, cosx, -sinx],
# [0, sinx, cosx]])
# Ry = np.array([[cosy, 0, siny],
# [0, 1, 0],
# [-siny, 0, cosy]])
# Rz = np.array([[cosz, -sinz, 0],
# [sinz, cosz, 0],
# [0, 0, 1]])
# self.R_ab = Rx.dot(Ry).dot(Rz)
# last_row = np.array([[0., 0., 0., 1.]])
# self.igt = np.concatenate([self.R_ab, self.translation_ab.reshape(-1,1)], axis=1)
# self.igt = np.concatenate([self.igt, last_row], axis=0)
def apply_transformation(self, template):
rotation = Rotation.from_euler('zyx', [self.anglez, self.angley, self.anglex])
self.igt = rotation.apply(np.eye(3))
self.igt = np.concatenate([self.igt, self.translation.reshape(-1,1)], axis=1)
self.igt = torch.from_numpy(np.concatenate([self.igt, np.array([[0., 0., 0., 1.]])], axis=0)).float()
source = rotation.apply(template) + np.expand_dims(self.translation, axis=0)
return source
def __call__(self, template):
template = template.numpy()
self.generate_transform()
return torch.from_numpy(self.apply_transformation(template)).float()
class DeepGMRTransform:
def __init__(self, angle_range=45, translation_range=1):
self.angle_range = angle_range*(np.pi/180)
self.translation_range = translation_range
self.index = 0
def generate_transform(self):
self.anglex = np.random.uniform() * self.angle_range
self.angley = np.random.uniform() * self.angle_range
self.anglez = np.random.uniform() * self.angle_range
self.translation = np.array([np.random.uniform(-self.translation_range, self.translation_range),
np.random.uniform(-self.translation_range, self.translation_range),
np.random.uniform(-self.translation_range, self.translation_range)])
def apply_transformation(self, template):
rotation = Rotation.from_euler('zyx', [self.anglez, self.angley, self.anglex])
self.igt = rotation.apply(np.eye(3))
self.igt = np.concatenate([self.igt, self.translation.reshape(-1,1)], axis=1)
self.igt = torch.from_numpy(np.concatenate([self.igt, np.array([[0., 0., 0., 1.]])], axis=0)).float()
source = rotation.apply(template) + np.expand_dims(self.translation, axis=0)
return source
def __call__(self, template):
template = template.numpy()
self.generate_transform()
return torch.from_numpy(self.apply_transformation(template)).float() |