### Install Demos and Examples Dependencies Source: https://github.com/facebookresearch/pytorch3d/blob/main/INSTALL.md Installs Python packages required for running PyTorch3D demos, examples, and tutorials using Conda and pip. ```bash # Demos and examples conda install jupyter pip install scikit-image matplotlib imageio plotly opencv-python ``` -------------------------------- ### Install PyTorch3D on Windows Source: https://github.com/facebookresearch/pytorch3d/blob/main/INSTALL.md Navigate to the cloned directory and use setup.py for installation on Windows. Ensure you are in the correct command prompt environment. ```bash cd pytorch3d python3 setup.py install ``` -------------------------------- ### Install PyTorch3D from Local Clone Source: https://github.com/facebookresearch/pytorch3d/blob/main/INSTALL.md Clone the repository and install using pip. This is the standard method for development or custom builds. ```bash git clone https://github.com/facebookresearch/pytorch3d.git cd pytorch3d && pip install -e . ``` -------------------------------- ### Install Tutorial Build Dependencies Source: https://github.com/facebookresearch/pytorch3d/blob/main/website/README.md Installs Python dependencies required for parsing Jupyter notebooks into HTML for the website tutorials. ```sh pip install nbformat==4.4.0 nbconvert==5.3.1 ipywidgets==7.5.1 tornado==4.2 bs4 ``` -------------------------------- ### Start Development Server Source: https://github.com/facebookresearch/pytorch3d/blob/main/website/README.md Starts the Docusaurus development server to preview website changes locally. Use either yarn or npm. ```sh # Start the site $ yarn start or $ ./node_modules/docusaurus/lib/start-server.js ``` -------------------------------- ### Install omegaconf Source: https://github.com/facebookresearch/pytorch3d/blob/main/docs/tutorials/implicitron_config_system.ipynb Installs the omegaconf library using pip, which is required for managing configurations in Implicitron. ```python !pip install omegaconf ``` -------------------------------- ### Install ffmpeg Source: https://github.com/facebookresearch/pytorch3d/blob/main/projects/implicitron_trainer/README.md Install ffmpeg using conda, which is required for the visualization script to produce videos. ```shell conda install ffmpeg ``` -------------------------------- ### Install PyTorch3D Source: https://github.com/facebookresearch/pytorch3d/blob/main/docs/tutorials/deform_source_mesh_to_target_mesh.ipynb Installs PyTorch3D and its dependencies. It checks for an existing installation and attempts to install a pre-compiled wheel if available, otherwise falls back to installing from source. ```python import os import sys import torch import subprocess need_pytorch3d=False try: import pytorch3d except ModuleNotFoundError: need_pytorch3d=True if need_pytorch3d: pyt_version_str=torch.__version__.split("+")[0].replace(".", "") version_str="".join([ f"py3{sys.version_info.minor}_cu", torch.version.cuda.replace(".", רבי), f"_pyt{pyt_version_str}" ]) !pip install iopath if sys.platform.startswith("linux"): print("Trying to install wheel for PyTorch3D") !pip install --no-index --no-cache-dir pytorch3d -f https://dl.fbaipublicfiles.com/pytorch3d/packaging/wheels/{version_str}/download.html pip_list = !pip freeze need_pytorch3d = not any(i.startswith("pytorch3d==") for i in pip_list) if need_pytorch3d: print(f"failed to find/install wheel for {version_str}") if need_pytorch3d: print("Installing PyTorch3D from source") !pip install ninja !pip install 'git+https://github.com/facebookresearch/pytorch3d.git@stable' ``` -------------------------------- ### Serve Documentation Locally Source: https://github.com/facebookresearch/pytorch3d/blob/main/docs/README.md Starts a simple Python HTTP server to view the generated HTML documentation locally. Navigate to http://0.0.0.0:8000/ in your browser. ```bash > python -m http.server ``` -------------------------------- ### Install PyTorch3D Source: https://github.com/facebookresearch/pytorch3d/blob/main/docs/tutorials/render_textured_meshes.ipynb Installs PyTorch3D and its dependencies. This script checks for an existing installation and attempts to install a pre-compiled wheel or falls back to installing from source if necessary. It requires `torch` and `torchvision` to be installed. ```python import os import sys import torch import subprocess need_pytorch3d=False try: import pytorch3d except ModuleNotFoundError: need_pytorch3d=True if need_pytorch3d: pyt_version_str=torch.__version__.split("+")[0].replace(".", "") version_str="".join([ f"py3{sys.version_info.minor}_cu", torch.version.cuda.replace("."," "), f"_pyt{pyt_version_str}" ]) !pip install iopath if sys.platform.startswith("linux"): print("Trying to install wheel for PyTorch3D") !pip install --no-index --no-cache-dir pytorch3d -f https://dl.fbaipublicfiles.com/pytorch3d/packaging/wheels/{version_str}/download.html pip_list = !pip freeze need_pytorch3d = not any(i.startswith("pytorch3d==") for i in pip_list) if need_pytorch3d: print(f"failed to find/install wheel for {version_str}") if need_pytorch3d: print("Installing PyTorch3D from source") !pip install ninja !pip install 'git+https://github.com/facebookresearch/pytorch3d.git@stable' ``` -------------------------------- ### Start Visdom Server Source: https://github.com/facebookresearch/pytorch3d/blob/main/projects/nerf/README.md Launches the Visdom server, which is used for outputting visualizations during NeRF training. ```bash python -m visdom.server ``` -------------------------------- ### Install PyTorch3D Source: https://github.com/facebookresearch/pytorch3d/blob/main/docs/tutorials/implicitron_volumes.ipynb Installs PyTorch3D and its dependencies, including iopath and omegaconf. It attempts to install a pre-built wheel if available, otherwise falls back to installing from source. ```python import os import sys import torch import subprocess need_pytorch3d=False try: import pytorch3d except ModuleNotFoundError: need_pytorch3d=True if need_pytorch3d: pyt_version_str=torch.__version__.split("+")[0].replace(".", "") version_str="".join([ f"py3{sys.version_info.minor}_cu", torch.version.cuda.replace("."," "), f"_pyt{pyt_version_str}" ]) !pip install iopath if sys.platform.startswith("linux"): print("Trying to install wheel for PyTorch3D") !pip install --no-index --no-cache-dir pytorch3d -f https://dl.fbaipublicfiles.com/pytorch3d/packaging/wheels/{version_str}/download.html pip_list = !pip freeze need_pytorch3d = not any(i.startswith("pytorch3d==") for i in pip_list) if need_pytorch3d: print(f"failed to find/install wheel for {version_str}") if need_pytorch3d: print("Installing PyTorch3D from source") !pip install ninja !pip install 'git+https://github.com/facebookresearch/pytorch3d.git@stable' ``` ```python !pip install omegaconf visdom ``` -------------------------------- ### Install PyTorch3D from GitHub (Stable Branch) Source: https://github.com/facebookresearch/pytorch3d/blob/main/INSTALL.md Installs PyTorch3D from the 'stable' branch of its GitHub repository using pip. This installs the latest released version. ```bash pip install "git+https://github.com/facebookresearch/pytorch3d.git@stable" ``` -------------------------------- ### Install Documentation Dependencies Source: https://github.com/facebookresearch/pytorch3d/blob/main/docs/README.md Installs essential Python packages for Sphinx documentation generation and Markdown table support. ```bash pip install -U recommonmark sphinx sphinx_rtd_theme sphinx_markdown_tables ``` -------------------------------- ### Install PyTorch3D Source: https://github.com/facebookresearch/pytorch3d/blob/main/docs/tutorials/camera_position_optimization_with_differentiable_rendering.ipynb Installs PyTorch3D from wheels or source, depending on availability and system configuration. Ensure PyTorch and torchvision are installed. ```python import os import sys import torch import subprocess need_pytorch3d=False try: import pytorch3d except ModuleNotFoundError: need_pytorch3d=True if need_pytorch3d: pyt_version_str=torch.__version__.split("+")[0].replace(".", "") version_str="".join([ f"py3{sys.version_info.minor}_cu", torch.version.cuda.replace(".", ""), f"_pyt{pyt_version_str}" ]) !pip install iopath if sys.platform.startswith("linux"): print("Trying to install wheel for PyTorch3D") !pip install --no-index --no-cache-dir pytorch3d -f https://dl.fbaipublicfiles.com/pytorch3d/packaging/wheels/{version_str}/download.html pip_list = !pip freeze need_pytorch3d = not any(i.startswith("pytorch3d==") for i in pip_list) if need_pytorch3d: print(f"failed to find/install wheel for {version_str}") if need_pytorch3d: print("Installing PyTorch3D from source") !pip install ninja !pip install 'git+https://github.com/facebookresearch/pytorch3d.git@stable' ``` -------------------------------- ### Install Dependencies with Pip Source: https://github.com/facebookresearch/pytorch3d/blob/main/projects/nerf/README.md Installs necessary Python packages for PyTorch3D NeRF implementation using pip. ```bash pip install visdom pip install hydra-core --upgrade pip install Pillow pip install requests ``` -------------------------------- ### Install Chumpy Source: https://github.com/facebookresearch/pytorch3d/blob/main/docs/tutorials/render_densepose.ipynb Installs the chumpy library, which is required for loading SMPL model pickle files. ```python # We also install chumpy as it is needed to load the SMPL model pickle file. !pip install chumpy ``` -------------------------------- ### Setup PyTorch3D Renderer Source: https://github.com/facebookresearch/pytorch3d/blob/main/docs/tutorials/render_densepose.ipynb Configures a MeshRenderer with a camera, rasterization settings, and a Phong shader. This setup is for visualizing the textured mesh. ```python # Initialize a camera. # World coordinates +Y up, +X left and +Z in. R, T = look_at_view_transform(2.7, 0, 0) cameras = FoVPerspectiveCameras(device=device, R=R, T=T) # Define the settings for rasterization and shading. Here we set the output image to be of size # 512x512. As we are rendering images for visualization purposes only we will set faces_per_pixel=1 # and blur_radius=0.0. raster_settings = RasterizationSettings( image_size=512, blur_radius=0.0, faces_per_pixel=1, ) # Place a point light in front of the person. lights = PointLights(device=device, location=[[0.0, 0.0, 2.0]]) # Create a Phong renderer by composing a rasterizer and a shader. The textured Phong shader will # interpolate the texture uv coordinates for each vertex, sample from a texture image and # apply the Phong lighting model renderer = MeshRenderer( rasterizer=MeshRasterizer( cameras=cameras, raster_settings=raster_settings ), shader=SoftPhongShader( device=device, cameras=cameras, lights=lights ) ) ``` -------------------------------- ### Install Kaleido for Plotly Image Export Source: https://github.com/facebookresearch/pytorch3d/blob/main/docs/notes/visualization.md Install the Kaleido library to enable saving Plotly figures as static images. ```bash $ pip install Kaleido ``` -------------------------------- ### Initialize Renderer Components Source: https://github.com/facebookresearch/pytorch3d/blob/main/docs/tutorials/camera_position_optimization_with_differentiable_rendering.ipynb Initializes a perspective camera, blend parameters for rendering, and rasterization settings. This setup is for silhouette rendering. ```python # Initialize a perspective camera. cameras = FoVPerspectiveCameras(device=device) # To blend the 100 faces we set a few parameters which control the opacity and the sharpness of # edges. Refer to blending.py for more details. blend_params = BlendParams(sigma=1e-4, gamma=1e-4) # Define the settings for rasterization and shading. Here we set the output image to be of size # 256x256. To form the blended image we use 100 faces for each pixel. We also set bin_size and max_faces_per_bin to None which ensure that ``` -------------------------------- ### Install Website Dependencies Source: https://github.com/facebookresearch/pytorch3d/blob/main/website/README.md Installs the necessary Node.js dependencies for the Docusaurus website. Use either yarn or npm. ```sh # Install dependencies $ yarn or $ npm install docusaurus-init ``` -------------------------------- ### Import PyTorch3D Modules and Setup Device Source: https://github.com/facebookresearch/pytorch3d/blob/main/docs/tutorials/deform_source_mesh_to_target_mesh.ipynb Imports necessary PyTorch3D modules and sets up the computation device (CUDA if available, otherwise CPU). Includes plotting configurations. ```python import os import torch from pytorch3d.io import load_obj, save_obj from pytorch3d.structures import Meshes from pytorch3d.utils import ico_sphere from pytorch3d.ops import sample_points_from_meshes from pytorch3d.loss import ( chamfer_distance, mesh_edge_loss, mesh_laplacian_smoothing, mesh_normal_consistency, ) import numpy as np from tqdm.notebook import tqdm %matplotlib notebook from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt import matplotlib as mpl mpl.rcParams['savefig.dpi'] = 80 mpl.rcParams['figure.dpi'] = 80 # Set the device if torch.cuda.is_available(): device = torch.device("cuda:0") else: device = torch.device("cpu") print("WARNING: CPU only, this will be slow!") ``` -------------------------------- ### Instantiating PerspectiveCameras in NDC and Screen Space Source: https://github.com/facebookresearch/pytorch3d/blob/main/docs/notes/cameras.md Example demonstrating how to create equivalent PerspectiveCameras instances in both NDC and screen coordinate spaces. For screen space, image size and `in_ndc=False` must be provided. ```python # NDC space camera fcl_ndc = (1.2,) prp_ndc = ((0.2, 0.5),) cameras_ndc = PerspectiveCameras(focal_length=fcl_ndc, principal_point=prp_ndc) # Screen space camera image_size = ((128, 256),) # (h, w) fcl_screen = (76.8,) # fcl_ndc * min(image_size) / 2 prp_screen = ((115.2, 32), ) # w / 2 - px_ndc * min(image_size) / 2, h / 2 - py_ndc * min(image_size) / 2 cameras_screen = PerspectiveCameras(focal_length=fcl_screen, principal_point=prp_screen, in_ndc=False, image_size=image_size) ``` -------------------------------- ### Install PyTorch3D from GitHub (Main Branch) Source: https://github.com/facebookresearch/pytorch3d/blob/main/INSTALL.md Installs PyTorch3D directly from the main branch of its GitHub repository using pip. This provides the latest development version. ```bash pip install "git+https://github.com/facebookresearch/pytorch3d.git" ``` -------------------------------- ### PerspectiveCameras Setup and Projection Source: https://context7.com/facebookresearch/pytorch3d/llms.txt Configures a perspective camera using focal length and principal point, then projects 3D world points to screen coordinates. Ensure correct device placement. ```python import torch from pytorch3d.renderer import PerspectiveCameras # Batch of 2 cameras R = torch.eye(3).unsqueeze(0).expand(2, -1, -1) # (2, 3, 3) T = torch.zeros(2, 3) # (2, 3) cameras = PerspectiveCameras( focal_length=((500., 500.),), # (fx, fy) in pixels principal_point=((256., 256.),), image_size=((512, 512),), in_ndc=False, R=R[:1], T=T[:1], device="cpu", ) # Project 3D world points to 2D screen pts_world = torch.randn(1, 10, 3) pts_screen = cameras.transform_points_screen(pts_world, image_size=(512, 512)) # pts_screen: (1, 10, 3) — xy pixel coords + depth # Get camera center in world coordinates center = cameras.get_camera_center() # (1, 3) ``` -------------------------------- ### Install PyTorch3D with CUDA from Anaconda Cloud Source: https://github.com/facebookresearch/pytorch3d/blob/main/INSTALL.md Installs the prebuilt PyTorch3D binary with CUDA support from the Anaconda Cloud channel. This is for Linux only. ```bash conda install pytorch3d -c pytorch3d ``` -------------------------------- ### Install PyTorch3D Wheels Dynamically Source: https://github.com/facebookresearch/pytorch3d/blob/main/INSTALL.md Installs PyTorch3D using prebuilt wheels by dynamically determining the correct wheel URL based on the current Python, CUDA, and PyTorch versions. Useful within IPython, Google Colab, or Jupyter notebooks. ```python import sys import torch pyt_version_str=torch.__version__.split("+")[0].replace(".", "") version_str="".join([ f"py3{sys.version_info.minor}_cu", torch.version.cuda.replace(".", ""), f"_pyt{pyt_version_str}" ]) !pip install iopath !pip install --no-index --no-cache-dir pytorch3d -f https://dl.fbaipublicfiles.com/pytorch3d/packaging/wheels/{version_str}/download.html ``` -------------------------------- ### Install PyTorch3D on macOS Source: https://github.com/facebookresearch/pytorch3d/blob/main/INSTALL.md Use specific environment variables for the build process on macOS. Ensure CC and CXX are set to clang. ```bash MACOSX_DEPLOYMENT_TARGET=10.14 CC=clang CXX=clang++ pip install -e . ``` -------------------------------- ### Initialize Source Mesh Source: https://github.com/facebookresearch/pytorch3d/blob/main/docs/tutorials/fit_textured_mesh.ipynb Loads the initial sphere mesh which will be deformed. This is the starting point for the optimization process. ```python # We initialize the source shape to be a sphere of radius 1. src_mesh = ico_sphere(4, device) ``` -------------------------------- ### Setup Optimizer Source: https://github.com/facebookresearch/pytorch3d/blob/main/docs/tutorials/deform_source_mesh_to_target_mesh.ipynb Configures the SGD optimizer to update the `deform_verts` tensor. ```python # The optimizer optimizer = torch.optim.SGD([deform_verts], lr=1.0, momentum=0.9) ``` -------------------------------- ### Install PyTorch3D from GitHub on macOS Source: https://github.com/facebookresearch/pytorch3d/blob/main/INSTALL.md Installs PyTorch3D from GitHub on macOS, setting necessary environment variables for the compiler and deployment target. This ensures compatibility with macOS build environments. ```bash MACOSX_DEPLOYMENT_TARGET=10.14 CC=clang CXX=clang++ pip install "git+https://github.com/facebookresearch/pytorch3d.git" ``` -------------------------------- ### Install PyTorch3D Wheels for Linux (Specific Version) Source: https://github.com/facebookresearch/pytorch3d/blob/main/INSTALL.md Installs PyTorch3D using prebuilt wheels for Linux, specifying Python, CUDA, and PyTorch versions. This is useful for environments like Google Colab. ```bash pip install --no-index --no-cache-dir pytorch3d -f https://dl.fbaipublicfiles.com/pytorch3d/packaging/wheels/py38_cu113_pyt1110/download.html ``` -------------------------------- ### Fetch Mesh and Texture Files Source: https://github.com/facebookresearch/pytorch3d/blob/main/docs/tutorials/render_textured_meshes.ipynb Downloads the `cow.obj`, `cow.mtl`, and `cow_texture.png` files required for the tutorial. These files are saved into the `data/cow_mesh` directory. This step is for Google Colab users; local installations typically have these files pre-existing. ```shell !mkdir -p data/cow_mesh !wget -P data/cow_mesh https://dl.fbaipublicfiles.com/pytorch3d/data/cow_mesh/cow.obj !wget -P data/cow_mesh https://dl.fbaipublicfiles.com/pytorch3d/data/cow_mesh/cow.mtl !wget -P data/cow_mesh https://dl.fbaipublicfiles.com/pytorch3d/data/cow_mesh/cow_texture.png ``` -------------------------------- ### Initialize Renderer Components Source: https://github.com/facebookresearch/pytorch3d/blob/main/docs/tutorials/render_textured_meshes.ipynb Set up a perspective camera, point lights, and rasterization settings for rendering. Ensure the camera is oriented correctly towards the object. Use default values for unspecified components. ```python R, T = look_at_view_transform(2.7, 0, 180) cameras = FoVPerspectiveCameras(device=device, R=R, T=T) raster_settings = RasterizationSettings( image_size=512, blur_radius=0.0, faces_per_pixel=1, ) lights = PointLights(device=device, location=[[0.0, 0.0, -3.0]]) renderer = MeshRenderer( rasterizer=MeshRasterizer( cameras=cameras, raster_settings=raster_settings ), shader=SoftPhongShader( device=device, cameras=cameras, lights=lights ) ) ``` -------------------------------- ### Initialize ShapeNetCore Dataset Source: https://github.com/facebookresearch/pytorch3d/blob/main/docs/tutorials/dataloaders_ShapeNetCore_R2N2.ipynb Set up the device and initialize the ShapeNetCore dataset. Ensure SHAPENET_PATH is correctly set to your dataset location. ```python if torch.cuda.is_available(): device = torch.device("cuda:0") torch.cuda.set_device(device) else: device = torch.device("cpu") SHAPENET_PATH = "" shapenet_dataset = ShapeNetCore(SHAPENET_PATH) ``` -------------------------------- ### Initialize Cameras and Lights Source: https://github.com/facebookresearch/pytorch3d/blob/main/docs/tutorials/render_textured_meshes.ipynb Set up camera transformations using look_at_view_transform and define light locations for rendering. ```python R, T = look_at_view_transform(dist=2.7, elev=elev, azim=azim) cameras = FoVPerspectiveCameras(device=device, R=R, T=T) # Move the light back in front of the cow which is facing the -z direction. lights.location = torch.tensor([[0.0, 0.0, -3.0]], device=device) ``` -------------------------------- ### Initialize Cameras and Lights Source: https://github.com/facebookresearch/pytorch3d/blob/main/docs/tutorials/fit_textured_mesh.ipynb Sets up multiple camera viewpoints and a point light for rendering. Use `look_at_view_transform` to define camera extrinsics. ```python num_views = 20 elev = torch.linspace(0, 360, num_views) azim = torch.linspace(-180, 180, num_views) lights = PointLights(device=device, location=[[0.0, 0.0, -3.0]]) R, T = look_at_view_transform(dist=2.7, elev=elev, azim=azim) cameras = FoVPerspectiveCameras(device=device, R=R, T=T) camera = FoVPerspectiveCameras(device=device, R=R[None, 1, ...], T=T[None, 1, ...]) ``` -------------------------------- ### Install Implicitron Dependencies Source: https://github.com/facebookresearch/pytorch3d/blob/main/projects/implicitron_trainer/README.md Install necessary dependencies for Implicitron, including Hydra, Visdom, LPIPS, Matplotlib, and Accelerate. This is required for Option 1 and Option 3 installations. ```shell pip install "hydra-core>=1.1" visdom lpips matplotlib accelerate ``` -------------------------------- ### Install ioPath with Conda Source: https://github.com/facebookresearch/pytorch3d/blob/main/INSTALL.md Installs the ioPath library, a dependency for PyTorch3D, using Conda. ```bash conda install -c iopath iopath ``` -------------------------------- ### Initialize Loss Dictionary and Parameters Source: https://github.com/facebookresearch/pytorch3d/blob/main/docs/tutorials/fit_textured_mesh.ipynb Sets up the loss components, weights, and storage for their values, along with parameters for mesh deformation and vertex colors. Requires PyTorch and device configuration. ```python # Number of views to optimize over in each SGD iteration num_views_per_iteration = 2 # Number of optimization steps Niter = 2000 # Plot period for the losses plot_period = 250 %matplotlib inline # Optimize using rendered RGB image loss, rendered silhouette image loss, mesh # edge loss, mesh normal consistency, and mesh laplacian smoothing losses = {"rgb": {"weight": 1.0, "values": []}, "silhouette": {"weight": 1.0, "values": []}, "edge": {"weight": 1.0, "values": []}, "normal": {"weight": 0.01, "values": []}, "laplacian": {"weight": 1.0, "values": []}, } # We will learn to deform the source mesh by offsetting its vertices # The shape of the deform parameters is equal to the total number of vertices in # src_mesh verts_shape = src_mesh.verts_packed().shape deform_verts = torch.full(verts_shape, 0.0, device=device, requires_grad=True) # We will also learn per vertex colors for our sphere mesh that define texture # of the mesh sphere_verts_rgb = torch.full([1, verts_shape[0], 3], 0.5, device=device, requires_grad=True) ``` -------------------------------- ### Install PyTorch3D Wheels in Jupyter/Colab Source: https://github.com/facebookresearch/pytorch3d/blob/main/packaging/linux_wheels/README.md Installs PyTorch3D wheels in a Jupyter or Colab notebook. It first determines the correct version string based on PyTorch and CUDA versions, then installs iopath and the PyTorch3D wheel from a specified URL. ```python import sys import torch pyt_version_str=torch.__version__.split("+")[0].replace(".", "") version_str="".join([ f"py3{sys.version_info.minor}_cu", torch.version.cuda.replace(".","",2), f"_pyt{pyt_version_str}" ]) !pip install iopath !pip install --no-index --no-cache-dir pytorch3d -f https://dl.fbaipublicfiles.com/pytorch3d/packaging/wheels/{version_str}/download.html ``` -------------------------------- ### Install Tests/Linting Dependencies Source: https://github.com/facebookresearch/pytorch3d/blob/main/INSTALL.md Installs Python packages for linting and testing PyTorch3D using Conda and pip. ```bash # Tests/Linting conda install -c fvcore -c conda-forge fvcore pip install black usort flake8 flake8-bugbear flake8-comprehensions ``` -------------------------------- ### Instantiating and Using a Configurable Module Source: https://github.com/facebookresearch/pytorch3d/blob/main/docs/tutorials/implicitron_config_system.ipynb Create an instance of `MyLinear`, pass a dummy input, and print the shape of the output. This demonstrates basic usage of the configurable PyTorch module. ```python my_linear = MyLinear() input = torch.zeros(2) output = my_linear(input) print("output shape:", output.shape) ``` -------------------------------- ### Install PyTorch3D Main Branch from Source in Colab Source: https://github.com/facebookresearch/pytorch3d/blob/main/docs/tutorials/README.md This command installs the latest PyTorch3D from the main branch via pip, suitable for Colab environments. It's an alternative to the stable installation and may be necessary if you are working with the most recent development versions. ```bash !pip install ‘git+https://github.com/facebookresearch/pytorch3d.git’ ``` -------------------------------- ### Initialize PyTorch3D Renderer Source: https://github.com/facebookresearch/pytorch3d/blob/main/docs/notes/renderer_getting_started.md Sets up a basic PyTorch3D renderer using a perspective camera, rasterization settings, and a Phong shader. Ensure the 'device' variable is defined before use. ```python from pytorch3d.renderer import ( FoVPerspectiveCameras, look_at_view_transform, RasterizationSettings, BlendParams, MeshRenderer, MeshRasterizer, HardPhongShader ) # Initialize an OpenGL perspective camera. R, T = look_at_view_transform(2.7, 10, 20) cameras = FoVPerspectiveCameras(device=device, R=R, T=T) # Define the settings for rasterization and shading. Here we set the output image to be of size # 512x512. As we are rendering images for visualization purposes only we will set faces_per_pixel=1 # and blur_radius=0.0. Refer to rasterize_meshes.py for explanations of these parameters. raster_settings = RasterizationSettings( image_size=512, blur_radius=0.0, faces_per_pixel=1, ) # Create a Phong renderer by composing a rasterizer and a shader. Here we can use a predefined # PhongShader, passing in the device on which to initialize the default parameters renderer = MeshRenderer( rasterizer=MeshRasterizer(cameras=cameras, raster_settings=raster_settings), shader=HardPhongShader(device=device, cameras=cameras) # ) ``` -------------------------------- ### Load and prepare point cloud data Source: https://github.com/facebookresearch/pytorch3d/blob/main/docs/tutorials/render_colored_points.ipynb Sets up the device (GPU or CPU), defines data paths, loads point cloud vertices and RGB colors from a .npz file, and creates a PyTorch3D Pointclouds object. ```python # Setup if torch.cuda.is_available(): device = torch.device("cuda:0") torch.cuda.set_device(device) else: device = torch.device("cpu") # Set paths DATA_DIR = "./data" obj_filename = os.path.join(DATA_DIR, "PittsburghBridge/pointcloud.npz") # Load point cloud pointcloud = np.load(obj_filename) verts = torch.Tensor(pointcloud['verts']).to(device) rgb = torch.Tensor(pointcloud['rgb']).to(device) point_cloud = Pointclouds(points=[verts], features=[rgb]) ``` -------------------------------- ### Import Modules and Set Up Device Source: https://github.com/facebookresearch/pytorch3d/blob/main/docs/tutorials/bundle_adjustment.ipynb Imports necessary PyTorch3D modules for camera transformations and rendering, sets up the device (GPU or CPU), and configures manual seeding for reproducibility. ```python # imports import torch from pytorch3d.transforms.so3 import ( so3_exp_map, so3_relative_angle, ) from pytorch3d.renderer.cameras import ( SfMPerspectiveCameras, ) # add path for demo utils import sys import os sys.path.append(os.path.abspath('')) # set for reproducibility torch.manual_seed(42) if torch.cuda.is_available(): device = torch.device("cuda:0") else: device = torch.device("cpu") print("WARNING: CPU only, this will be slow!") ``` -------------------------------- ### Initialize Model and Optimizer Source: https://github.com/facebookresearch/pytorch3d/blob/main/docs/tutorials/camera_position_optimization_with_differentiable_rendering.ipynb Initializes the optimization model and sets up an Adam optimizer to train the camera position parameter. Also prepares an image writer for saving optimization progress. ```python # We will save images periodically and compose them into a GIF. filename_output = "./teapot_optimization_demo.gif" writer = imageio.get_writer(filename_output, mode='I', duration=0.3) # Initialize a model using the renderer, mesh and reference image model = Model(meshes=teapot_mesh, renderer=silhouette_renderer, image_ref=image_ref).to(device) # Create an optimizer. Here we are using Adam and we pass in the parameters of the model optimizer = torch.optim.Adam(model.parameters(), lr=0.05) ``` -------------------------------- ### Initialize Volume Model and Optimizer Source: https://github.com/facebookresearch/pytorch3d/blob/main/docs/tutorials/fit_textured_volume.ipynb Sets up the volumetric model and the Adam optimizer. The learning rate is initialized to 0.1 and can be decreased during training. ```python volume_size = 128 volume_model = VolumeModel( renderer, volume_size=[volume_size] * 3, voxel_size = volume_extent_world / volume_size, ).to(device) # Instantiate the Adam optimizer. We set its master learning rate to 0.1. lr = 0.1 optimizer = torch.optim.Adam(volume_model.parameters(), lr=lr) ``` -------------------------------- ### Build Website and Parse Tutorials Source: https://github.com/facebookresearch/pytorch3d/blob/main/website/README.md Executes a script to build the Docusaurus website and parse Jupyter notebooks into HTML, JS, and other assets. ```sh bash scripts/build_website.sh ``` -------------------------------- ### Install CUB with Conda for older CUDA Source: https://github.com/facebookresearch/pytorch3d/blob/main/INSTALL.md Installs the CUB library using Conda, which is a build-time dependency for CUDA versions older than 11.7. ```bash conda install -c bottler nvidiacub ``` -------------------------------- ### Download utility scripts Source: https://github.com/facebookresearch/pytorch3d/blob/main/docs/tutorials/fit_textured_volume.ipynb Downloads helper scripts for plotting image grids and generating cow renders, which are used later in the tutorial. These are necessary for data generation and visualization. ```python !wget https://raw.githubusercontent.com/facebookresearch/pytorch3d/main/docs/tutorials/utils/plot_image_grid.py !wget https://raw.githubusercontent.com/facebookresearch/pytorch3d/main/docs/tutorials/utils/generate_cow_renders.py from plot_image_grid import image_grid from generate_cow_renders import generate_cow_renders ``` -------------------------------- ### Rebuild PyTorch3D After Installation Source: https://github.com/facebookresearch/pytorch3d/blob/main/INSTALL.md To rebuild the project after installing from a local clone, remove previous build artifacts and reinstall. This is often necessary after updating PyTorch. ```bash rm -rf build/ **/*.so pip install -e . ``` -------------------------------- ### Initialize Model and Optimizer Source: https://github.com/facebookresearch/pytorch3d/blob/main/docs/tutorials/fit_simple_neural_radiance_field.ipynb Move relevant variables to the correct device and instantiate the NeuralRadianceField model and Adam optimizer. Set the learning rate for the optimizer. ```python renderer_grid = renderer_grid.to(device) renderer_mc = renderer_mc.to(device) target_cameras = target_cameras.to(device) target_images = target_images.to(device) target_silhouettes = target_silhouettes.to(device) torch.manual_seed(1) neural_radiance_field = NeuralRadianceField().to(device) lr = 1e-3 optimizer = torch.optim.Adam(neural_radiance_field.parameters(), lr=lr) ``` -------------------------------- ### Install Nightly PyTorch3D Build from Anaconda Cloud Source: https://github.com/facebookresearch/pytorch3d/blob/main/INSTALL.md Installs a nightly (alpha) build of PyTorch3D with CUDA support from the Anaconda Cloud channel. This is for Linux only. ```bash conda install pytorch3d -c pytorch3d-nightly ``` -------------------------------- ### Create and Access DictConfig Source: https://github.com/facebookresearch/pytorch3d/blob/main/docs/tutorials/implicitron_config_system.ipynb Demonstrates creating a DictConfig from a dictionary and accessing its elements using both dot notation and dictionary-style keys. ```python dc = DictConfig({"a": 2, "b": True, "c": None, "d": "hello"}) assert dc.a == dc["a"] == 2 ``` -------------------------------- ### Install Yarn Package Manager Source: https://github.com/facebookresearch/pytorch3d/blob/main/website/README.md Installs the Yarn package manager using Homebrew or a direct curl script. Yarn is used for managing website dependencies. ```sh brew install yarn # or curl -o- -L https://yarnpkg.com/install.sh | bash ``` -------------------------------- ### Import utility functions Source: https://github.com/facebookresearch/pytorch3d/blob/main/docs/tutorials/fit_simple_neural_radiance_field.ipynb Imports the `image_grid` and `generate_cow_renders` functions after they have been downloaded. This makes them available for use in the rest of the notebook. ```python from plot_image_grid import image_grid from generate_cow_renders import generate_cow_renders ``` -------------------------------- ### Build HTML Documentation Source: https://github.com/facebookresearch/pytorch3d/blob/main/docs/README.md Command to build the HTML version of the documentation from the docs directory using the Sphinx make utility. ```bash > make html ``` -------------------------------- ### Install PyTorch, Torchvision, and CUDA with Conda Source: https://github.com/facebookresearch/pytorch3d/blob/main/INSTALL.md Installs specific versions of PyTorch, torchvision, and CUDA using Conda channels. This is a common step for setting up the core PyTorch environment. ```bash conda install pytorch=1.13.0 torchvision pytorch-cuda=11.6 -c pytorch -c nvidia ``` -------------------------------- ### Visualize Initial and Reference Camera Positions Source: https://github.com/facebookresearch/pytorch3d/blob/main/docs/tutorials/camera_position_optimization_with_differentiable_rendering.ipynb Displays the initial rendered image from the model and the reference silhouette for comparison. Requires matplotlib for plotting. ```python plt.figure(figsize=(10, 10)) _, image_init = model() plt.subplot(1, 2, 1) plt.imshow(image_init.detach().squeeze().cpu().numpy()[..., 3]) plt.grid(False) plt.title("Starting position") plt.subplot(1, 2, 2) plt.imshow(model.image_ref.cpu().numpy().squeeze()) plt.grid(False) plt.title("Reference silhouette"); ``` -------------------------------- ### Render Mesh with Initial Settings Source: https://github.com/facebookresearch/pytorch3d/blob/main/docs/tutorials/render_textured_meshes.ipynb Render the mesh using the initialized renderer. This will produce an image with lighting and highlights based on the initial camera and light positions. ```python images = renderer(mesh) plt.figure(figsize=(10, 10)) plt.imshow(images[0, ..., :3].cpu().numpy()) plt.axis("off") ``` -------------------------------- ### Initialize Dataclass from Structured DictConfig Source: https://github.com/facebookresearch/pytorch3d/blob/main/docs/tutorials/implicitron_config_system.ipynb Demonstrates how to populate a structured DictConfig with values and then use it to initialize an instance of the original dataclass. ```python structured.a = 21 my_dataclass_instance2 = MyDataclass(**structured) print(my_dataclass_instance2) ``` -------------------------------- ### Install PyTorch3D from Source in Colab Source: https://github.com/facebookresearch/pytorch3d/blob/main/docs/tutorials/README.md Use this command to install PyTorch3D from source within a Google Colab notebook, especially when GPU support or specific dependencies like CUB are needed. This method ensures compatibility with the main branch of PyTorch3D. ```bash import os !curl -LO https://github.com/NVIDIA/cub/archive/1.10.0.tar.gz !tar xzf 1.10.0.tar.gz os.environ["CUB_HOME"] = os.getcwd() + "/cub-1.10.0" !pip install 'git+https://github.com/facebookresearch/pytorch3d.git@stable' ``` -------------------------------- ### Set up Implicit/NeRF-style Rendering Components Source: https://context7.com/facebookresearch/pytorch3d/llms.txt Configure `ImplicitRenderer`, `NDCMultinomialRaysampler`, and `EmissionAbsorptionRaymarcher` for NeRF-style rendering. Define camera, ray sampling parameters (number of points, depth range), and image dimensions. ```python import torch from pytorch3d.renderer import ( FoVPerspectiveCameras, NDCMultinomialRaysampler, EmissionAbsorptionRaymarcher, ImplicitRenderer, ) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") cameras = FoVPerspectiveCameras(device=device) # Raysampler: creates a grid of rays from the image plane raysampler = NDCMultinomialRaysampler( image_height=64, image_width=64, n_pts_per_ray=128, min_depth=0.1, max_depth=4.0, ) ``` -------------------------------- ### Initialize Optimization Parameters Source: https://github.com/facebookresearch/pytorch3d/blob/main/docs/tutorials/bundle_adjustment.ipynb Initializes the absolute camera log-rotations and translations for optimization. Sets the first camera to identity and creates a mask for optimization. ```python # initialize the absolute log-rotations/translations with random entries log_R_absolute_init = torch.randn(N, 3, dtype=torch.float32, device=device) T_absolute_init = torch.randn(N, 3, dtype=torch.float32, device=device) # furthermore, we know that the first camera is a trivial one # (see the description above) log_R_absolute_init[0, :] = 0. T_absolute_init[0, :] = 0. # instantiate a copy of the initialization of log_R / T log_R_absolute = log_R_absolute_init.clone().detach() log_R_absolute.requires_grad = True T_absolute = T_absolute_init.clone().detach() T_absolute.requires_grad = True # the mask the specifies which cameras are going to be optimized # (since we know the first camera is already correct, # we only optimize over the 2nd-to-last cameras) camera_mask = torch.ones(N, 1, dtype=torch.float32, device=device) camera_mask[0] = 0. # init the optimizer optimizer = torch.optim.SGD([log_R_absolute, T_absolute], lr=.1, momentum=0.9) ``` -------------------------------- ### Markdown Document Structure Source: https://github.com/facebookresearch/pytorch3d/blob/main/website/README.md Example front matter for a Docusaurus markdown document. Includes 'id' for linking and 'title' for display. ```markdown --- id: page-needs-edit title: This Doc Needs To Be Edited --- Edit me... ``` -------------------------------- ### Prepare for Batched Rendering Source: https://github.com/facebookresearch/pytorch3d/blob/main/docs/tutorials/render_textured_meshes.ipynb Set up for batched rendering by extending a single mesh to create a batch of identical meshes. This prepares the input for rendering the same object from multiple viewpoints in a single forward pass. ```python batch_size = 20 meshes = mesh.extend(batch_size) elev = torch.linspace(0, 180, batch_size) azim = torch.linspace(-180, 180, batch_size) ``` -------------------------------- ### Blog Post Markdown Structure Source: https://github.com/facebookresearch/pytorch3d/blob/main/website/README.md Example front matter for a Docusaurus blog post. Includes 'id', 'title', and author information. ```markdown --- id: post-needs-edit title: This Blog Post Needs To Be Edited --- Edit me... ``` -------------------------------- ### Getting Default Arguments for a Configurable Class Source: https://github.com/facebookresearch/pytorch3d/blob/main/docs/tutorials/implicitron_config_system.ipynb Use `get_default_args` to retrieve the default configuration arguments for a configurable class, which can then be printed as YAML. ```python Out_dc = get_default_args(Out) print(OmegaConf.to_yaml(Out_dc)) ``` -------------------------------- ### pytorch3d.common.get_device Source: https://github.com/facebookresearch/pytorch3d/blob/main/docs/modules/common.md Gets the device of a PyTorch tensor, falling back to a default CPU device if necessary. Allows overriding with an explicit device. ```APIDOC ## pytorch3d.common.get_device ### Description Gets the device of the specified variable x if it is a tensor, or falls back to a default CPU device otherwise. Allows overriding by providing an explicit device. ### Parameters #### Parameters - **x** (torch.Tensor | other type) - The tensor or object to get the device from. - **device** (str | torch.device | None) - Optional. Device (as str or torch.device) to fall back to if x is not a tensor or does not have a device. ### Returns - **torch.device** - A matching torch.device object. ``` -------------------------------- ### Create Silhouette and Phong Renderers Source: https://github.com/facebookresearch/pytorch3d/blob/main/docs/tutorials/camera_position_optimization_with_differentiable_rendering.ipynb Sets up two mesh renderers: one for silhouette generation and another for Phong shading. Requires camera and blend parameters. ```python raster_settings = RasterizationSettings( image_size=256, blur_radius=np.log(1. / 1e-4 - 1.) * blend_params.sigma, faces_per_pixel=100, ) # Create a silhouette mesh renderer by composing a rasterizer and a shader. silhouette_renderer = MeshRenderer( rasterizer=MeshRasterizer( cameras=cameras, raster_settings=raster_settings ), shader=SoftSilhouetteShader(blend_params=blend_params) ) # We will also create a Phong renderer. This is simpler and only needs to render one face per pixel. raster_settings = RasterizationSettings( image_size=256, blur_radius=0.0, faces_per_pixel=1, ) # We can add a point light in front of the object. lights = PointLights(device=device, location=((2.0, 2.0, -2.0),)) phong_renderer = MeshRenderer( rasterizer=MeshRasterizer( cameras=cameras, raster_settings=raster_settings ), shader=HardPhongShader(device=device, cameras=cameras, lights=lights) ) ```