PyTorch3D

PyTorch3D

  • ドキュメント
  • チュートリアル
  • API
  • GitHub

›

チュートリアル

  • 概要

3D演算子

  • メッシュのフィッティング
  • バンドル調整

レンダリング

  • テクスチャ付きメッシュのレンダリング
  • DensePoseメッシュのレンダリング
  • 色付き点群のレンダリング
  • レンダリングによるテクスチャ付きメッシュのフィッティング
  • 微分可能なレンダリングによるカメラ位置の最適化
  • レイマーチングによるボリュームのフィッティング
  • レイマーチングによる簡略化されたNeRFのフィッティング

データローダー

  • ShapeNetCoreおよびR2N2のデータローダー

Implicitron

  • implicitronを使用したカスタムボリューム関数のトレーニング
  • Implicitron構成システムの詳細
Google Colabで実行
チュートリアルJupyter Notebookをダウンロード
チュートリアルソースコードをダウンロード
In [ ]
# Copyright (c) Meta Platforms, Inc. and affiliates. All rights reserved.

色付き点群のレンダリング¶

このチュートリアルでは、以下の方法を示します。

  • レンダラーの設定
  • 点群のレンダリング
  • 合成やカメラ位置などのレンダリング設定の変更

モジュールのインポート¶

torchとtorchvisionがインストールされていることを確認してください。pytorch3dがインストールされていない場合は、次のセルを使用してインストールしてください。

In [ ]
import os
import sys
import torch
need_pytorch3d=False
try:
    import pytorch3d
except ModuleNotFoundError:
    need_pytorch3d=True
if need_pytorch3d:
    if torch.__version__.startswith("2.2.") and sys.platform.startswith("linux"):
        # We try to install PyTorch3D via a released wheel.
        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 fvcore iopath
        !pip install --no-index --no-cache-dir pytorch3d -f https://dl.fbaipublicfiles.com/pytorch3d/packaging/wheels/{version_str}/download.html
    else:
        # We try to install PyTorch3D from source.
        !pip install 'git+https://github.com/facebookresearch/pytorch3d.git@stable'
In [ ]
import os
import torch
import torch.nn.functional as F
import matplotlib.pyplot as plt

# Util function for loading point clouds|
import numpy as np

# Data structures and functions for rendering
from pytorch3d.structures import Pointclouds
from pytorch3d.vis.plotly_vis import AxisArgs, plot_batch_individually, plot_scene
from pytorch3d.renderer import (
    look_at_view_transform,
    FoVOrthographicCameras, 
    PointsRasterizationSettings,
    PointsRenderer,
    PulsarPointsRenderer,
    PointsRasterizer,
    AlphaCompositor,
    NormWeightedCompositor
)

点群と対応する色の読み込み¶

点群オブジェクトを読み込み、作成します。

Pointcloudsは、PyTorch3Dで提供される、さまざまなサイズの点群のバッチを扱うための独自のデータ構造です。

Google Colabを使用してこのノートブックを実行する場合は、次のセルを実行して点群データをフェッチし、パスdata/PittsburghBridgeに保存してください。ローカルで実行する場合は、データはすでに正しいパスにあります。

In [ ]
!mkdir -p data/PittsburghBridge
!wget -P data/PittsburghBridge https://dl.fbaipublicfiles.com/pytorch3d/data/PittsburghBridge/pointcloud.npz
In [ ]
# 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])

レンダラーの作成¶

PyTorch3Dのレンダラーは、ラスタライザーとシェーダーで構成されており、それぞれにカメラ(正投影/透視投影)などの多くのサブコンポーネントがあります。ここでは、これらのコンポーネントの一部を初期化し、残りの部分にはデフォルト値を使用します。

この例では、最初に正投影カメラを使用し、アルファ合成を適用するレンダラーを作成します。次に、モジュール式APIを使用してさまざまなコンポーネントを変更する方法を学習します。

[1] SynSin: End to end View Synthesis from a Single Image. Olivia Wiles, Georgia Gkioxari, Richard Szeliski, Justin Johnson. CVPR 2020.

In [ ]
# Initialize a camera.
R, T = look_at_view_transform(20, 10, 0)
cameras = FoVOrthographicCameras(device=device, R=R, T=T, znear=0.01)

# 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 raster_points.py for explanations of these parameters. 
raster_settings = PointsRasterizationSettings(
    image_size=512, 
    radius = 0.003,
    points_per_pixel = 10
)


# Create a points renderer by compositing points using an alpha compositor (nearer points
# are weighted more heavily). See [1] for an explanation.
rasterizer = PointsRasterizer(cameras=cameras, raster_settings=raster_settings)
renderer = PointsRenderer(
    rasterizer=rasterizer,
    compositor=AlphaCompositor()
)
In [ ]
images = renderer(point_cloud)
plt.figure(figsize=(10, 10))
plt.imshow(images[0, ..., :3].cpu().numpy())
plt.axis("off");

次に、設定された背景色でアルファ合成を使用するようにレンダラーを変更します。

In [ ]
renderer = PointsRenderer(
    rasterizer=rasterizer,
    # Pass in background_color to the alpha compositor, setting the background color 
    # to the 3 item tuple, representing rgb on a scale of 0 -> 1, in this case blue
    compositor=AlphaCompositor(background_color=(0, 0, 1))
)
images = renderer(point_cloud)

plt.figure(figsize=(10, 10))
plt.imshow(images[0, ..., :3].cpu().numpy())
plt.axis("off");

この例では、最初に正投影カメラを使用し、重み付け合成を適用するレンダラーを作成します。

In [ ]
# Initialize a camera.
R, T = look_at_view_transform(20, 10, 0)
cameras = FoVOrthographicCameras(device=device, R=R, T=T, znear=0.01)

# 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_points.py for explanations of these parameters. 
raster_settings = PointsRasterizationSettings(
    image_size=512, 
    radius = 0.003,
    points_per_pixel = 10
)


# Create a points renderer by compositing points using an weighted compositor (3D points are
# weighted according to their distance to a pixel and accumulated using a weighted sum)
renderer = PointsRenderer(
    rasterizer=PointsRasterizer(cameras=cameras, raster_settings=raster_settings),
    compositor=NormWeightedCompositor()
)
In [ ]
images = renderer(point_cloud)
plt.figure(figsize=(10, 10))
plt.imshow(images[0, ..., :3].cpu().numpy())
plt.axis("off");

次に、設定された背景色で重み付け合成を使用するようにレンダラーを変更します。

In [ ]
renderer = PointsRenderer(
    rasterizer=PointsRasterizer(cameras=cameras, raster_settings=raster_settings),
    # Pass in background_color to the norm weighted compositor, setting the background color 
    # to the 3 item tuple, representing rgb on a scale of 0 -> 1, in this case red
    compositor=NormWeightedCompositor(background_color=(1,0,0))
)
images = renderer(point_cloud)
plt.figure(figsize=(10, 10))
plt.imshow(images[0, ..., :3].cpu().numpy())
plt.axis("off");

pulsarバックエンドの使用¶

pulsarバックエンドへの切り替えは簡単です! pulsarバックエンドにはコンポジターが組み込まれているため、作成時にcompositor引数は必要ありません(それでも提供すると警告が表示されます)。レンダリングデバイスにメモリを事前に割り当てるため、構築時にn_channelsが必要です。

レンダラーのforward関数のすべてのパラメーターは、背景色(この例ではgamma)を除いてバッチ単位であり、バッチ内の例と同じ数の値を指定する必要があります。背景色はオプションで、デフォルトではすべてゼロに設定されます。ガンマがレンダリング関数にどのように影響するかについての詳細な説明は、論文Fast Differentiable Raycasting for Neural Rendering using Sphere-based Representationsにあります。

また、点不透明度へのアクセスをすでに提供しているpulsarバックエンドにnativeバックエンドを使用することもできます。ネイティブバックエンドはpytorch3d.renderer.points.pulsarからインポートできます。これについては、フォルダdocs/examplesに例があります。

In [ ]
renderer = PulsarPointsRenderer(
    rasterizer=PointsRasterizer(cameras=cameras, raster_settings=raster_settings),
    n_channels=4
).to(device)

images = renderer(point_cloud, gamma=(1e-4,),
                  bg_col=torch.tensor([0.0, 1.0, 0.0, 1.0], dtype=torch.float32, device=device))
plt.figure(figsize=(10, 10))
plt.imshow(images[0, ..., :3].cpu().numpy())
plt.axis("off");

Plotly図での点群の表示¶

ここでは、PyTorch3D関数plot_sceneを使用して、点群をPlotly図にレンダリングします。plot_sceneは、入力によって定義されたトレースとサブプロットを含むplotly図を返します。

In [ ]
plot_scene({
    "Pointcloud": {
        "person": point_cloud
    }
})

次に、点群のバッチをレンダリングします。最初の点群は上記と同じで、2番目の点群はすべて黒で、すべての次元で2オフセットされているため、同じプロットで見ることができます。

In [ ]
point_cloud_batch = Pointclouds(points=[verts, verts + 2], features=[rgb, torch.zeros_like(rgb)])
# render both in the same plot in different traces
fig = plot_scene({
    "Pointcloud": {
        "person": point_cloud_batch[0],
        "person2": point_cloud_batch[1]
    }
})
fig.show()
In [ ]
# render both in the same plot in one trace
fig = plot_scene({
    "Pointcloud": {
        "2 people": point_cloud_batch
    }
})
fig.show()

バッチの場合、plot_batch_individuallyを使用してシーンディクショナリを自分で作成することを避けることもできます。

In [ ]
# render both in 1 row in different subplots
fig2 = plot_batch_individually(point_cloud_batch, ncols=2)
fig2.show()
In [ ]
# modify the plotly figure height and width
fig2.update_layout(height=500, width=500)
fig2.show()

また、どちらの関数でも軸の引数と軸の背景を変更したり、plot_batch_individuallyでプロットにタイトルを付けることもできます。

In [ ]
fig3 = plot_batch_individually(
    point_cloud_batch, 
    xaxis={"backgroundcolor":"rgb(200, 200, 230)"},
    yaxis={"backgroundcolor":"rgb(230, 200, 200)"},
    zaxis={"backgroundcolor":"rgb(200, 230, 200)"}, 
    subplot_titles=["Pointcloud1", "Pointcloud2"], # this should have a title for each subplot, titles can be ""
    axis_args=AxisArgs(showgrid=True))
fig3.show()
pytorch3d
Facebook Open Source
Copyright © 2024 Meta Platforms, Inc
法的情報:プライバシー利用規約