PyTorch3D
PyTorch3D is FAIR's library of reusable components for deep learning with 3D data, currently at version 0.7.6. It provides efficient data structures for storing and manipulating triangle meshes, optimized operations on 3D data (like projective transformations, graph convolution, sampling, and loss functions), and a modular differentiable mesh renderer. The library is actively developed and designed to integrate smoothly with PyTorch for predicting and manipulating 3D data, with operators that can handle minibatches, are differentiable, and can utilize GPUs.
Warnings
- breaking PyTorch3D does not guarantee backward-compatibility between releases. Best efforts are made to communicate breaking changes and facilitate code migration, but users should review release notes for significant version bumps.
- gotcha The differentiable renderer API in PyTorch3D is marked as experimental and subject to change. Applications relying heavily on specific renderer configurations might require updates in future versions.
- gotcha PyTorch3D uses different coordinate system conventions compared to OpenGL (e.g., +Z direction). This can lead to unexpected rendering results or model transformations if not accounted for.
- gotcha PyTorch3D has strict compatibility requirements with its underlying PyTorch version. Installing `pipablepytorch3d` does not automatically guarantee a compatible PyTorch version, which can lead to runtime errors or installation failures.
Install
-
pip install pipablepytorch3d
Imports
- Meshes
from pytorch3d.structures import Meshes
- load_obj
import pytorch3d.io.load_obj
from pytorch3d.io import load_obj
- ico_sphere
from pytorch3d.utils import ico_sphere
- sample_points_from_meshes
from pytorch3d.ops import sample_points_from_meshes
- chamfer_distance
from pytorch3d.loss import chamfer_distance
Quickstart
import torch
from pytorch3d.utils import ico_sphere
from pytorch3d.ops import sample_points_from_meshes
from pytorch3d.loss import chamfer_distance
# Set device
if torch.cuda.is_available():
device = torch.device("cuda:0")
else:
device = torch.device("cpu")
# Create two ico_sphere meshes with different levels of detail
sphere_mesh_1 = ico_sphere(level=3, device=device)
sphere_mesh_2 = ico_sphere(level=4, device=device)
# Differentiably sample 5k points from the surface of each mesh
sample_points_1 = sample_points_from_meshes(sphere_mesh_1, 5000)
sample_points_2 = sample_points_from_meshes(sphere_mesh_2, 5000)
# Compute the Chamfer distance between the two sets of points
loss_chamfer, _ = chamfer_distance(sample_points_1, sample_points_2)
print(f"Chamfer Distance: {loss_chamfer.item():.4f}")