Spaces:
Runtime error
Runtime error
File size: 9,563 Bytes
899c526 |
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 |
import torch
import lietorch
from lietorch import SO3, RxSO3, SE3, Sim3
from gradcheck import gradcheck, get_analytical_jacobian
### forward tests ###
def make_homogeneous(p):
return torch.cat([p, torch.ones_like(p[...,:1])], dim=-1)
def matv(A, b):
return torch.matmul(A, b[...,None])[..., 0]
def test_exp_log(Group, device='cuda'):
""" check Log(Exp(x)) == x """
a = .2*torch.randn(2,3,4,5,6,7,Group.manifold_dim, device=device).double()
b = Group.exp(a).log()
assert torch.allclose(a,b,atol=1e-8), "should be identity"
print("\t-", Group, "Passed exp-log test")
def test_inv(Group, device='cuda'):
""" check X * X^{-1} == 0 """
X = Group.exp(.1*torch.randn(2,3,4,5,Group.manifold_dim, device=device).double())
a = (X * X.inv()).log()
assert torch.allclose(a, torch.zeros_like(a), atol=1e-8), "should be 0"
print("\t-", Group, "Passed inv test")
def test_adj(Group, device='cuda'):
""" check X * Exp(a) == Exp(Adj(X,a)) * X 0 """
X = Group.exp(torch.randn(2,3,4,5, Group.manifold_dim, device=device).double())
a = torch.randn(2,3,4,5, Group.manifold_dim, device=device).double()
b = X.adj(a)
Y1 = X * Group.exp(a)
Y2 = Group.exp(b) * X
c = (Y1 * Y2.inv()).log()
assert torch.allclose(c, torch.zeros_like(c), atol=1e-8), "should be 0"
print("\t-", Group, "Passed adj test")
def test_act(Group, device='cuda'):
X = Group.exp(torch.randn(1, Group.manifold_dim, device=device).double())
p = torch.randn(1,3,device=device).double()
p1 = X.act(p)
p2 = matv(X.matrix(), make_homogeneous(p))
assert torch.allclose(p1, p2[...,:3], atol=1e-8), "should be 0"
print("\t-", Group, "Passed act test")
### backward tests ###
def test_exp_log_grad(Group, device='cuda', tol=1e-8):
D = Group.manifold_dim
def fn(a):
return Group.exp(a).log()
a = torch.zeros(1, Group.manifold_dim, requires_grad=True, device=device).double()
analytical, reentrant, correct_grad_sizes, correct_grad_types = \
get_analytical_jacobian((a,), fn(a))
assert torch.allclose(analytical[0], torch.eye(D, device=device).double(), atol=tol)
a = .2 * torch.randn(1, Group.manifold_dim, requires_grad=True, device=device).double()
analytical, reentrant, correct_grad_sizes, correct_grad_types = \
get_analytical_jacobian((a,), fn(a))
assert torch.allclose(analytical[0], torch.eye(D, device=device).double(), atol=tol)
print("\t-", Group, "Passed eye-grad test")
def test_inv_log_grad(Group, device='cuda', tol=1e-8):
D = Group.manifold_dim
X = Group.exp(.2*torch.randn(1,D,device=device).double())
def fn(a):
return (Group.exp(a) * X).inv().log()
a = torch.zeros(1, D, requires_grad=True, device=device).double()
analytical, numerical = gradcheck(fn, [a], eps=1e-4)
# assert torch.allclose(analytical[0], numerical[0], atol=tol)
if not torch.allclose(analytical[0], numerical[0], atol=tol):
print(analytical[0])
print(numerical[0])
print("\t-", Group, "Passed inv-grad test")
def test_adj_grad(Group, device='cuda'):
D = Group.manifold_dim
X = Group.exp(.5*torch.randn(1,Group.manifold_dim, device=device).double())
def fn(a, b):
return (Group.exp(a) * X).adj(b)
a = torch.zeros(1, D, requires_grad=True, device=device).double()
b = torch.randn(1, D, requires_grad=True, device=device).double()
analytical, numerical = gradcheck(fn, [a, b], eps=1e-4)
assert torch.allclose(analytical[0], numerical[0], atol=1e-8)
assert torch.allclose(analytical[1], numerical[1], atol=1e-8)
print("\t-", Group, "Passed adj-grad test")
def test_adjT_grad(Group, device='cuda'):
D = Group.manifold_dim
X = Group.exp(.5*torch.randn(1,Group.manifold_dim, device=device).double())
def fn(a, b):
return (Group.exp(a) * X).adjT(b)
a = torch.zeros(1, D, requires_grad=True, device=device).double()
b = torch.randn(1, D, requires_grad=True, device=device).double()
analytical, numerical = gradcheck(fn, [a, b], eps=1e-4)
assert torch.allclose(analytical[0], numerical[0], atol=1e-8)
assert torch.allclose(analytical[1], numerical[1], atol=1e-8)
print("\t-", Group, "Passed adjT-grad test")
def test_act_grad(Group, device='cuda'):
D = Group.manifold_dim
X = Group.exp(5*torch.randn(1,D, device=device).double())
def fn(a, b):
return (X*Group.exp(a)).act(b)
a = torch.zeros(1, D, requires_grad=True, device=device).double()
b = torch.randn(1, 3, requires_grad=True, device=device).double()
analytical, numerical = gradcheck(fn, [a, b], eps=1e-4)
assert torch.allclose(analytical[0], numerical[0], atol=1e-8)
assert torch.allclose(analytical[1], numerical[1], atol=1e-8)
print("\t-", Group, "Passed act-grad test")
def test_matrix_grad(Group, device='cuda'):
D = Group.manifold_dim
X = Group.exp(torch.randn(1, D, device=device).double())
def fn(a):
return (Group.exp(a) * X).matrix()
a = torch.zeros(1, D, requires_grad=True, device=device).double()
analytical, numerical = gradcheck(fn, [a], eps=1e-4)
assert torch.allclose(analytical[0], numerical[0], atol=1e-6)
print("\t-", Group, "Passed matrix-grad test")
def extract_translation_grad(Group, device='cuda'):
""" prototype function """
D = Group.manifold_dim
X = Group.exp(5*torch.randn(1,D, device=device).double())
def fn(a):
return (Group.exp(a)*X).translation()
a = torch.zeros(1, D, requires_grad=True, device=device).double()
analytical, numerical = gradcheck(fn, [a], eps=1e-4)
assert torch.allclose(analytical[0], numerical[0], atol=1e-8)
print("\t-", Group, "Passed translation grad test")
def test_vec_grad(Group, device='cuda', tol=1e-6):
D = Group.manifold_dim
X = Group.exp(5*torch.randn(1,D, device=device).double())
def fn(a):
return (Group.exp(a)*X).vec()
a = torch.zeros(1, D, requires_grad=True, device=device).double()
analytical, numerical = gradcheck(fn, [a], eps=1e-4)
assert torch.allclose(analytical[0], numerical[0], atol=tol)
print("\t-", Group, "Passed tovec grad test")
def test_fromvec_grad(Group, device='cuda', tol=1e-6):
def fn(a):
if Group == SO3:
a = a / a.norm(dim=-1, keepdim=True)
elif Group == RxSO3:
q, s = a.split([4, 1], dim=-1)
q = q / q.norm(dim=-1, keepdim=True)
a = torch.cat([q, s.exp()], dim=-1)
elif Group == SE3:
t, q = a.split([3, 4], dim=-1)
q = q / q.norm(dim=-1, keepdim=True)
a = torch.cat([t, q], dim=-1)
elif Group == Sim3:
t, q, s = a.split([3, 4, 1], dim=-1)
q = q / q.norm(dim=-1, keepdim=True)
a = torch.cat([t, q, s.exp()], dim=-1)
return Group.InitFromVec(a).vec()
D = Group.embedded_dim
a = torch.randn(1, 2, D, requires_grad=True, device=device).double()
analytical, numerical = gradcheck(fn, [a], eps=1e-4)
assert torch.allclose(analytical[0], numerical[0], atol=tol)
print("\t-", Group, "Passed fromvec grad test")
def scale(device='cuda'):
def fn(a, s):
X = SE3.exp(a)
X.scale(s)
return X.log()
s = torch.rand(1, requires_grad=True, device=device).double()
a = torch.randn(1, 6, requires_grad=True, device=device).double()
analytical, numerical = gradcheck(fn, [a, s], eps=1e-3)
print(analytical[1])
print(numerical[1])
assert torch.allclose(analytical[0], numerical[0], atol=1e-8)
assert torch.allclose(analytical[1], numerical[1], atol=1e-8)
print("\t-", "Passed se3-to-sim3 test")
if __name__ == '__main__':
print("Testing lietorch forward pass (CPU) ...")
for Group in [SO3, RxSO3, SE3, Sim3]:
test_exp_log(Group, device='cpu')
test_inv(Group, device='cpu')
test_adj(Group, device='cpu')
test_act(Group, device='cpu')
print("Testing lietorch backward pass (CPU)...")
for Group in [SO3, RxSO3, SE3, Sim3]:
if Group == Sim3:
tol = 1e-3
else:
tol = 1e-8
test_exp_log_grad(Group, device='cpu', tol=tol)
test_inv_log_grad(Group, device='cpu', tol=tol)
test_adj_grad(Group, device='cpu')
test_adjT_grad(Group, device='cpu')
test_act_grad(Group, device='cpu')
test_matrix_grad(Group, device='cpu')
extract_translation_grad(Group, device='cpu')
test_vec_grad(Group, device='cpu')
test_fromvec_grad(Group, device='cpu')
print("Testing lietorch forward pass (GPU) ...")
for Group in [SO3, RxSO3, SE3, Sim3]:
test_exp_log(Group, device='cuda')
test_inv(Group, device='cuda')
test_adj(Group, device='cuda')
test_act(Group, device='cuda')
print("Testing lietorch backward pass (GPU)...")
for Group in [SO3, RxSO3, SE3, Sim3]:
if Group == Sim3:
tol = 1e-3
else:
tol = 1e-8
test_exp_log_grad(Group, device='cuda', tol=tol)
test_inv_log_grad(Group, device='cuda', tol=tol)
test_adj_grad(Group, device='cuda')
test_adjT_grad(Group, device='cuda')
test_act_grad(Group, device='cuda')
test_matrix_grad(Group, device='cuda')
extract_translation_grad(Group, device='cuda')
test_vec_grad(Group, device='cuda')
test_fromvec_grad(Group, device='cuda')
|