- Notifications
You must be signed in to change notification settings - Fork0
Pytorch package to compute Chamfer distance between point sets (pointclouds).
License
haichen-ber/chamferdist
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
NOTE: This implementation was stolen from thepytorch3d repo, and all I did was to simply repackage it.
A simple example Pytorch module to compute Chamfer distance between two pointclouds.
You can install the package usingpip
.
pip install chamferdist
In your favourite python/conda virtual environment, execute the following commands.
NOTE: This assumes you have PyTorch installed already (preferably, >= 1.5.0; untested for earlier releases).
pythonsetup.pyinstall
That's it! You're now ready to go. Here's a quick guide to using the package. Fire up a terminal. Import the package.
>>>importtorch>>>fromchamferdistimportChamferDistance
Create two random pointclouds. Each pointcloud is a3D tensor with dimensionsbatchsize
xnumber of points
xnumber of dimensions
.
>>>source_cloud=torch.randn(1,100,3).cuda()>>>target_cloud=torch.randn(1,50,3).cuda()
Initialize aChamferDistance
object.
>>>chamferDist=ChamferDistance()
Now, compute Chamfer distance.
>>>dist_forward=chamferDist(source_cloud,target_cloud)>>>print(dist_forward.detach().cpu().item())
Here,dist
is the Chamfer distance betweensource_cloud
andtarget_cloud
. Note that Chamfer distance is not bidirectional (and, in stricter parlance, it is not adistance metric).
The Chamfer distance in the backward direction, i.e.,target_cloud
tosource_cloud
can be computed in two ways. The naive way is to simply flip the order of the arguments, i.e.,
>>>dist_backward=chamferDist(target_cloud,source_cloud)
Another way is to use thereverse
flag provided by theChamferDistance
module, i.e.,
>>>dist_backward=chamferDist(source_cloud,target_cloud,reverse=True)>>>print(dist_backward.detach().cpu().item())
Typically, a symmetric version of the Chamfer distance is obtained, by summing the "forward" and the "backward" Chamfer distances. This is supported by thebidirectional
flag.
>>>dist_bidirectional=chamferDist(source_cloud,target_cloud,bidirectional=True)>>>print(dist_bidirectional.detach().cpu().item())
Look at the example script for more details:example.py
If you find this work useful, you might want to cite theoriginal implementation from which this codebase was borrowed (stolen!) - PyTorch3D.
@article{ravi2020pytorch3d, author = {Nikhila Ravi and Jeremy Reizenstein and David Novotny and Taylor Gordon and Wan-Yen Lo and Justin Johnson and Georgia Gkioxari}, title = {Accelerating 3D Deep Learning with PyTorch3D}, journal = {arXiv:2007.08501}, year = {2020},}