|
| 1 | +""" |
| 2 | +Get Started Tutorial |
| 3 | +================== |
| 4 | +
|
| 5 | +""" |
| 6 | + |
| 7 | +###################################################################### |
| 8 | +# Uncomment this if you're using google colab to run this script |
| 9 | +# |
| 10 | + |
| 11 | +# !pip install pypose |
| 12 | + |
| 13 | + |
| 14 | +###################################################################### |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | +###################################################################### |
| 20 | +# Sample Code of LieTensor |
| 21 | +# --------------------------------------- |
| 22 | +# The following code sample shows how to rotate random |
| 23 | +# points and compute the gradient of batched rotation. |
| 24 | +# |
| 25 | + |
| 26 | +import torch |
| 27 | +import pypose as pp |
| 28 | + |
| 29 | + |
| 30 | +###################################################################### |
| 31 | +# Create a random so(3) LieTensor |
| 32 | +# |
| 33 | + |
| 34 | +r = pp.randn_so3(2, requires_grad=True) |
| 35 | +print(r) |
| 36 | + |
| 37 | + |
| 38 | +###################################################################### |
| 39 | +# Get the Lie Group of the Lie Algebra |
| 40 | +# |
| 41 | + |
| 42 | +R = r.Exp() # Equivalent to: R = pp.Exp(r) |
| 43 | +print(R) |
| 44 | + |
| 45 | + |
| 46 | +###################################################################### |
| 47 | +# Create a random point and rotate it based on the Lie Group rotation tensor |
| 48 | +# |
| 49 | + |
| 50 | +p = R @ torch.randn(3) # Rotate random point |
| 51 | +print(p) |
| 52 | + |
| 53 | + |
| 54 | +###################################################################### |
| 55 | +# Compute the gradient and print it |
| 56 | +# |
| 57 | + |
| 58 | +p.sum().backward() # Compute gradient |
| 59 | +r.grad # Print gradient |
| 60 | + |
| 61 | + |
| 62 | +###################################################################### |
| 63 | +# Sample code of optimizer |
| 64 | +# --------------------------------------------- |
| 65 | +# We show how to estimate batched transform inverse by a |
| 66 | +# 2nd-order optimizer. Two usage options for a scheduler |
| 67 | +# are provided, each of which can work independently. |
| 68 | +# |
| 69 | + |
| 70 | +from torch import nn |
| 71 | +import torch, pypose as pp |
| 72 | +from pypose.optim import LM |
| 73 | +from pypose.optim.strategy import Constant |
| 74 | +from pypose.optim.scheduler \ |
| 75 | +import StopOnPlateau |
| 76 | + |
| 77 | +class InvNet(nn.Module): |
| 78 | + |
| 79 | + def __init__(self, *dim): |
| 80 | + super().__init__() |
| 81 | + init = pp.randn_SE3(*dim) |
| 82 | + self.pose = pp.Parameter(init) |
| 83 | + |
| 84 | + def forward(self, input): |
| 85 | + error = (self.pose @ input).Log() |
| 86 | + return error.tensor() |
| 87 | + |
| 88 | +device = torch.device("cuda") |
| 89 | +input = pp.randn_SE3(2, 2, device=device) |
| 90 | +invnet = InvNet(2, 2).to(device) |
| 91 | +strategy = Constant(damping=1e-4) |
| 92 | +optimizer = LM(invnet, strategy=strategy) |
| 93 | +scheduler = StopOnPlateau(optimizer, |
| 94 | + steps=10, |
| 95 | + patience=3, |
| 96 | + decreasing=1e-3, |
| 97 | + verbose=True) |
| 98 | + |
| 99 | +# 1st option, full optimization |
| 100 | +scheduler.optimize(input=input) |
| 101 | + |
| 102 | +# 2nd option, step optimization |
| 103 | +while scheduler.continual(): |
| 104 | + loss = optimizer.step(input) |
| 105 | + scheduler.step(loss) |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | +###################################################################### |
| 110 | +# And then we are finished with the two sample codes mentioned in our paper. |
| 111 | +# |
| 112 | +# Now you may be free to explore other tutorials. |
| 113 | +# See How PyPose can be utilized in real robotics applications. |
| 114 | +# |
0 commit comments