Logo
Section 1 / 6

体素

学习如何在3D深度学习中使用体素网格和立方化(cubify)操作符。

导入

体素

学习如何在3D深度学习中使用体素网格和立方化(cubify)操作符。

...
体素网格示例
1import torch 2 3 4n_batch = 1 # 批次大小 5width = 8 # 每个维度的宽度 6val = 0 # 初始值 7 8vgrid = torch.full([n_batch, width, width, width], val) 9print(vgrid.shape)
1import warnings 2import torch 3from pytorch3d.ops import cubify 4from pytorch3d.renderer import ( 5 look_at_view_transform, 6 PerspectiveCameras 7) 8import matplotlib.pyplot as plt 9 10from renderer import render 11 12 13# 参数设置 14n_batch = 1 # 批次大小 15width = 3 # 每个维度的宽度 16val = 1.0 # 初始值 17thresh = 0.5 # 阈值 18 19# 创建一个仅包含单个非零元素的体素网格 20vgrid = torch.full([n_batch, width, width, width], 0.) 21vgrid[:, 1, 1, 1] = val 22 23# 应用cubify函数(当前版本会触发UserWarning) 24with warnings.catch_warnings(): 25 warnings.simplefilter('ignore') 26 mesh = cubify(vgrid, thresh) 27 28print(mesh.verts_padded()) 29 30# 渲染立方体图像 31R, T = look_at_view_transform(dist=10.0, elev=45, azim=45) 32cameras = PerspectiveCameras(focal_length=8.0, R=R, T=T) 33img = render(mesh, cameras)[0, ..., :3].numpy() 34plt.imshow(img) 35plt.savefig('./output/render.png')
1import torch 2from pytorch3d.structures import Volumes 3from pytorch3d.renderer import ( 4 VolumeRenderer, 5 NDCMultinomialRaysampler, 6 EmissionAbsorptionRaymarcher 7) 8from pytorch3d.renderer import ( 9 look_at_view_transform, 10 FoVPerspectiveCameras, 11) 12import matplotlib.pyplot as plt 13 14device = torch.device("cuda:0") if torch.cuda.is_available() else torch.device("cpu") 15 16 17# 设置参数 18n_batch = 1 # 批处理中的体积数量 19render_size = 128 # 渲染图像尺寸 20volume_size = 64 # 体积尺寸(即每个维度上的体素数) 21min_depth = 0.1 # 相机到采样点的最小深度 22max_depth = 6.1 # 相机到采样点的最大深度 23volume_extent_world = max_depth - min_depth # 世界坐标系下体积的空间范围 24 25# 创建用于体积渲染的光线采样器 26raysampler = NDCMultinomialRaysampler( 27 image_width=render_size, 28 image_height=render_size, 29 n_pts_per_ray=64, 30 min_depth=0.1, 31 max_depth=volume_extent_world, 32) 33 34# 创建光线行进器,用于对密度和特征应用发射-吸收算法 35raymarcher = EmissionAbsorptionRaymarcher() 36 37# 创建体积渲染器 38renderer = VolumeRenderer(raysampler=raysampler, raymarcher=raymarcher) 39 40# 创建张量存储体积密度 41densities = torch.full([n_batch, 1, *[volume_size] * 3], 0.1, device=device) 42 43# 创建沿y轴呈红色渐变的颜色张量 44colors = torch.stack([ 45 torch.linspace(1, 0., volume_size, device=device), 46 torch.zeros([volume_size], device=device), 47 torch.zeros([volume_size], device=device) 48]) 49 50# 将颜色张量扩展为体积的完整尺寸 51colors = colors.view(n_batch, 3, 1, volume_size, 1).expand(n_batch, 3, *[volume_size] * 3) 52 53# 创建相机视图变换 54R, T = look_at_view_transform(3.1, 0, 0) 55cameras = FoVPerspectiveCameras(device=device, R=R, T=T) 56 57# 创建Volumes实例 58volumes = Volumes( 59 densities=densities, 60 features=colors, 61 voxel_size=0.25 * (volume_extent_world / volume_size), 62) 63 64# 使用指定相机渲染该体积 65img = renderer(cameras=cameras, volumes=volumes)[0] 66 67# 显示并保存渲染结果 68plt.imshow(img.cpu().numpy()[0, ..., :3]) 69plt.savefig('./output/render.png')

附录

点云