2 minute read

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