[CUDA] 3DGS diff-gaussian-rasterization & diff-gaussian-rasterization-depth-acc
Original 3D Gaussian Splatting์ submodules/diff-gaussian-rasterization์ depth map์ ๋์คํฐํํ๋ ๋ชจ๋์ ์ถ๊ฐํ ์ ์์ต๋๋ค.
diff-gaussian-rasterization
diff-gaussian-rasterization-depth-acc
์๋ ๋๋ค Cuda ์ฝ๋๋ก ์์ฑ๋์์ผ๋ฉฐ, setup.py์์ _C
๊ฐ ๋น๋๋์ด, diff_gaussian_rasterization ํน์ diff_gaussian_rasterization_depth_acc์์
from . import _C
๋ก rasterization๊ด๋ จ .cu
์ฟ ๋ค ์ฝ๋๋ฅผ ๊ฐ์ ธ์์ ์ฌ์ฉํฉ๋๋ค.
diff_gaussian_rasterization/__init__.py
from . import _C
...
# Invoke C++/CUDA rasterizer
if raster_settings.debug:
cpu_args = cpu_deep_copy_tuple(args) # Copy them before they can be corrupted
try:
num_rendered, color, depth, acc, radii, geomBuffer, binningBuffer, imgBuffer = _C.rasterize_gaussians(*args)
except Exception as ex:
torch.save(cpu_args, "snapshot_fw.dump")
print("\nAn error occured in forward. Please forward snapshot_fw.dump for debugging.")
raise ex
else:
num_rendered, color, depth, acc, radii, geomBuffer, binningBuffer, imgBuffer = _C.rasterize_gaussians(*args)
# Keep relevant tensors for backward
ctx.raster_settings = raster_settings
ctx.num_rendered = num_rendered
ctx.save_for_backward(colors_precomp, depth, acc, means3D, scales, rotations, cov3Ds_precomp, radii, sh, geomBuffer, binningBuffer, imgBuffer)
return color, depth, acc, radii
@staticmethod
def backward(ctx, grad_out_color, grad_out_depth, grad_acc, grad_radii):
# Restore necessary values from context
num_rendered = ctx.num_rendered
raster_settings = ctx.raster_settings
colors_precomp, depth, acc, means3D, scales, rotations, cov3Ds_precomp, radii, sh, geomBuffer, binningBuffer, imgBuffer = ctx.saved_tensors
# Restructure args as C++ method expects them
args = (raster_settings.bg,
means3D,
radii,
acc,
depth,
colors_precomp,
scales,
rotations,
raster_settings.scale_modifier,
cov3Ds_precomp,
raster_settings.viewmatrix,
raster_settings.projmatrix,
raster_settings.tanfovx,
raster_settings.tanfovy,
grad_out_color,
grad_out_depth,
sh,
raster_settings.sh_degree,
raster_settings.campos,
geomBuffer,
num_rendered,
binningBuffer,
imgBuffer,
raster_settings.debug)
...
diff_gaussian_rasterization_depth_acc/__init__.py
- depth๊ฐ ์ถ๊ฐ๋ ๋ชจ๋์ ๊ฒฝ์ฐ depth, acc๋ ๊ฐ์ด rasterize ํฉ๋๋ค.
from . import _C
...
# Invoke C++/CUDA rasterizer
if raster_settings.debug:
cpu_args = cpu_deep_copy_tuple(args) # Copy them before they can be corrupted
try:
num_rendered, color, radii, geomBuffer, binningBuffer, imgBuffer = _C.rasterize_gaussians(*args)
except Exception as ex:
torch.save(cpu_args, "snapshot_fw.dump")
print("\nAn error occured in forward. Please forward snapshot_fw.dump for debugging.")
raise ex
else:
num_rendered, color, radii, geomBuffer, binningBuffer, imgBuffer = _C.rasterize_gaussians(*args)
# Keep relevant tensors for backward
ctx.raster_settings = raster_settings
ctx.num_rendered = num_rendered
ctx.save_for_backward(colors_precomp, means3D, scales, rotations, cov3Ds_precomp, radii, sh, geomBuffer, binningBuffer, imgBuffer)
return color, radii
@staticmethod
def backward(ctx, grad_out_color, _):
# Restore necessary values from context
num_rendered = ctx.num_rendered
raster_settings = ctx.raster_settings
colors_precomp, means3D, scales, rotations, cov3Ds_precomp, radii, sh, geomBuffer, binningBuffer, imgBuffer = ctx.saved_tensors
# Restructure args as C++ method expects them
args = (raster_settings.bg,
means3D,
radii,
colors_precomp,
scales,
rotations,
raster_settings.scale_modifier,
cov3Ds_precomp,
raster_settings.viewmatrix,
raster_settings.projmatrix,
raster_settings.tanfovx,
raster_settings.tanfovy,
grad_out_color,
sh,
raster_settings.sh_degree,
raster_settings.campos,
geomBuffer,
num_rendered,
binningBuffer,
imgBuffer,
raster_settings.debug)
...
Rasterization ๊ด๋ จ๋ Cuda ์ฝ๋๋ cuda_rasterizer/xxx.cu
, rasterize_points.cu
๊ด๋ จ ์ฝ๋๋ฅผ ๋ณ๊ฒฝํ์ฌ ์์ฑํ๊ณ setup.py
์์ build ํฉ๋๋ค.
- ์ค์ ๋ก
cuda_rasterizer/rasterizer_impl.cu
,cuda_rasterizer/forward.cu
,cuda_rasterizer.backward.cu
,rasterize_points.cu
์ฝ๋์ ๋ชจ๋ depth๋ฅผ rasterizeํ๊ธฐ ์ํ ์ฝ๋๊ฐ ์ถ๊ฐ๋์์์ ํ์ธํด๋ณผ ์ ์์ต๋๋ค. diff-gaussian-rasterization-depth-acc
์ ํด๋นํ๋ cuda ์ฝ๋๋out_depth
,out_acc
์ ๊ฐ์ depth ๊ด๋ จ ํ ์๊ฐ ์ถ๊ฐ์ ์ผ๋ก ์์ฑ๋์๊ณ , backpropagation ๊ณ์ฐ์accum_acc
,accum_depth
์ธ์๋ฅผ ํฌํจํฉ๋๋ค.
diff-gaussian-rasterization/setup.py
#
# Copyright (C) 2023, Inria
# GRAPHDECO research group, https://team.inria.fr/graphdeco
# All rights reserved.
#
# This software is free for non-commercial, research and evaluation use
# under the terms of the LICENSE.md file.
#
# For inquiries contact george.drettakis@inria.fr
#
from setuptools import setup
from torch.utils.cpp_extension import CUDAExtension, BuildExtension
import os
os.path.dirname(os.path.abspath(__file__))
setup(
name="diff_gaussian_rasterization",
packages=['diff_gaussian_rasterization'],
ext_modules=[
CUDAExtension(
name="diff_gaussian_rasterization._C",
sources=[
"cuda_rasterizer/rasterizer_impl.cu",
"cuda_rasterizer/forward.cu",
"cuda_rasterizer/backward.cu",
"rasterize_points.cu",
"ext.cpp"],
extra_compile_args={"nvcc": ["-I" + os.path.join(os.path.dirname(os.path.abspath(__file__)), "third_party/glm/")]})
],
cmdclass={
'build_ext': BuildExtension
}
)
diff-gaussian-rasterization-depth-acc/setup.py
#
# Copyright (C) 2023, Inria
# GRAPHDECO research group, https://team.inria.fr/graphdeco
# All rights reserved.
#
# This software is free for non-commercial, research and evaluation use
# under the terms of the LICENSE.md file.
#
# For inquiries contact george.drettakis@inria.fr
#
from setuptools import setup
from torch.utils.cpp_extension import CUDAExtension, BuildExtension
import os
os.path.dirname(os.path.abspath(__file__))
setup(
name="diff_gaussian_rasterization_depth_acc",
packages=['diff_gaussian_rasterization_depth_acc'],
ext_modules=[
CUDAExtension(
name="diff_gaussian_rasterization_depth_acc._C",
sources=[
"cuda_rasterizer/rasterizer_impl.cu",
"cuda_rasterizer/forward.cu",
"cuda_rasterizer/backward.cu",
"rasterize_points.cu",
"ext.cpp"],
extra_compile_args={"nvcc": ["-I" + os.path.join(os.path.dirname(os.path.abspath(__file__)), "third_party/glm/")]})
],
cmdclass={
'build_ext': BuildExtension
}
)
Leave a comment