PyTorch3D

0.7.6 · active · verified Wed Apr 15

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

Install

Imports

Quickstart

This quickstart example demonstrates how to create two isometric sphere meshes, sample points from their surfaces, and compute the Chamfer distance between the sampled point clouds using PyTorch3D's utility functions and loss modules. This is a common operation in 3D shape comparison and optimization. Ensure you have a compatible PyTorch installation.

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}")

view raw JSON →