3DGS CUDA代码笔记

2024-04-24 11:28
文章标签 代码 笔记 cuda 3dgs

本文主要是介绍3DGS CUDA代码笔记,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

本篇文章 一Scaffold GS 为例子。 目标在里面添加 Render Depth 的代码:

将可见的 Gaussians Render 到 2D 图像上面

from diff_gaussian_rasterization import GaussianRasterizationSettings, GaussianRasterizer
.......rasterizer = GaussianRasterizer(raster_settings=raster_settings)
rendered_image, radii = rasterizer(means3D = xyz,             #  高斯的中心点 means2D = screenspace_points,   ##  return 数值shs = None,colors_precomp = color,opacities = opacity,scales = scaling,rotations = rot,cov3D_precomp = None)

上面那个 GuassianRasterizer 是从 diff_gaussian_rasterization 这个 package 中导入进来的、 因此,我找到 diff_gaussian_rasterization 这个文件夹,首先 看其对应的 init.py 这个函数。因为每次 import 这个package 的时候 都会首先执行一次 这个文件夹下面的 init.py 作为包的初始化函数

这个 Init 函数里面 回调用 rasterize_gaussians CUDA 的 函数:

 return rasterize_gaussians(means3D,means2D,shs,colors_precomp,opacities,scales, rotations,cov3D_precomp,raster_settings, )继续调用:
def rasterize_gaussians(means3D,means2D,sh,colors_precomp,opacities,scales,rotations,cov3Ds_precomp,raster_settings,
):return _RasterizeGaussians.apply(means3D,means2D,sh,colors_precomp,opacities,scales,rotations,cov3Ds_precomp,raster_settings,)

最后跳入到 _RasterizeGaussians 的 forward 函数当中

num_rendered, color, radii, geomBuffer, binningBuffer, imgBuffer = _C.rasterize_gaussians(*args)

_C 表示 在当前的 Cpp 文件中去 找这个 rasterize_gaussians 的函数,

PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {m.def("rasterize_gaussians", &RasterizeGaussiansCUDA);m.def("rasterize_gaussians_backward", &RasterizeGaussiansBackwardCUDA);m.def("rasterize_aussians_filter", &RasterizeGaussiansfilterCUDA);m.def("mark_visible", &markVisible);}

发现对应的 RasterizeGaussiansCUDA 函数

这个 函数 通过 ext.
cpp
查询可以发现是 rasterize_points.cu 里面的 函数:

下面这个函数主要是定义了 一些需要返回的 变量,启动 核函数

RasterizeGaussiansCUDA(const torch::Tensor& background,const torch::Tensor& means3D,const torch::Tensor& colors,const torch::Tensor& opacity,const torch::Tensor& scales,const torch::Tensor& rotations,const float scale_modifier,const torch::Tensor& cov3D_precomp,const torch::Tensor& viewmatrix,const torch::Tensor& projmatrix,const float tan_fovx, const float tan_fovy,const int image_height,const int image_width,const torch::Tensor& sh,const int degree,const torch::Tensor& campos,const bool prefiltered,const bool debug)
{if (means3D.ndimension() != 2 || means3D.size(1) != 3) {AT_ERROR("means3D must have dimensions (num_points, 3)");}const int P = means3D.size(0);const int H = image_height;const int W = image_width;auto int_opts = means3D.options().dtype(torch::kInt32);auto float_opts = means3D.options().dtype(torch::kFloat32);torch::Tensor out_color = torch::full({NUM_CHANNELS, H, W}, 0.0, float_opts);torch::Tensor radii = torch::full({P}, 0, means3D.options().dtype(torch::kInt32));torch::Device device(torch::kCUDA);torch::TensorOptions options(torch::kByte);torch::Tensor geomBuffer = torch::empty({0}, options.device(device));torch::Tensor binningBuffer = torch::empty({0}, options.device(device));torch::Tensor imgBuffer = torch::empty({0}, options.device(device));std::function<char*(size_t)> geomFunc = resizeFunctional(geomBuffer);std::function<char*(size_t)> binningFunc = resizeFunctional(binningBuffer);std::function<char*(size_t)> imgFunc = resizeFunctional(imgBuffer);int rendered = 0;if(P != 0){int M = 0;if(sh.size(0) != 0){M = sh.size(1);}rendered = CudaRasterizer::Rasterizer::forward(geomFunc,binningFunc,imgFunc,P, degree, M,background.contiguous().data<float>(),W, H,means3D.contiguous().data<float>(),sh.contiguous().data_ptr<float>(),colors.contiguous().data<float>(), opacity.contiguous().data<float>(), scales.contiguous().data_ptr<float>(),scale_modifier,rotations.contiguous().data_ptr<float>(),cov3D_precomp.contiguous().data<float>(), viewmatrix.contiguous().data<float>(), projmatrix.contiguous().data<float>(),campos.contiguous().data<float>(),tan_fovx,tan_fovy,prefiltered,out_color.contiguous().data<float>(),radii.contiguous().data<int>(),debug);}return std::make_tuple(rendered, out_color, radii, geomBuffer, binningBuffer, imgBuffer);
}

之后 进入 CudaRasterizer::Rasterizer::forward 函数,其定义在 rasterizer_impl.cu 对应的文件。 这个 forward 函数 最后会调用 FORWARD 类里面的 render 函数, 真实的 Render 过程是在下面这个函数执行的,我们

CHECK_CUDA(FORWARD::render(tile_grid, block,imgState.ranges,binningState.point_list,width, height,geomState.means2D,feature_ptr,geomState.conic_opacity,imgState.accum_alpha,out_alpha,imgState.n_contrib,background,out_color,out_depth), debug)

最后的 Depth Render 的函数是在 renderCUDA 函数中进行的。
实际修改的代码:

## 定义 CUDA 的变量
float weight = 0;
float D = 0;## 使用深度的加权去 计算真实的depth 的数值
weight += alpha * T;
D += depths[collected_id[j]] * alpha * T;

在 添加的时候,也添加了对于 Depth 的 BP 的操作,但是如果只需要可视化 depth 的话可以不用添加。

参考网址: https://github.com/ashawkey/diff-gaussian-rasterization

这篇关于3DGS CUDA代码笔记的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/931620

相关文章

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部

Java中Map.Entry()含义及方法使用代码

《Java中Map.Entry()含义及方法使用代码》:本文主要介绍Java中Map.Entry()含义及方法使用的相关资料,Map.Entry是Java中Map的静态内部接口,用于表示键值对,其... 目录前言 Map.Entry作用核心方法常见使用场景1. 遍历 Map 的所有键值对2. 直接修改 Ma

深入解析 Java Future 类及代码示例

《深入解析JavaFuture类及代码示例》JavaFuture是java.util.concurrent包中用于表示异步计算结果的核心接口,下面给大家介绍JavaFuture类及实例代码,感兴... 目录一、Future 类概述二、核心工作机制代码示例执行流程2. 状态机模型3. 核心方法解析行为总结:三

python获取cmd环境变量值的实现代码

《python获取cmd环境变量值的实现代码》:本文主要介绍在Python中获取命令行(cmd)环境变量的值,可以使用标准库中的os模块,需要的朋友可以参考下... 前言全局说明在执行py过程中,总要使用到系统环境变量一、说明1.1 环境:Windows 11 家庭版 24H2 26100.4061

pandas实现数据concat拼接的示例代码

《pandas实现数据concat拼接的示例代码》pandas.concat用于合并DataFrame或Series,本文主要介绍了pandas实现数据concat拼接的示例代码,具有一定的参考价值,... 目录语法示例:使用pandas.concat合并数据默认的concat:参数axis=0,join=

C#代码实现解析WTGPS和BD数据

《C#代码实现解析WTGPS和BD数据》在现代的导航与定位应用中,准确解析GPS和北斗(BD)等卫星定位数据至关重要,本文将使用C#语言实现解析WTGPS和BD数据,需要的可以了解下... 目录一、代码结构概览1. 核心解析方法2. 位置信息解析3. 经纬度转换方法4. 日期和时间戳解析5. 辅助方法二、L

Python使用Code2flow将代码转化为流程图的操作教程

《Python使用Code2flow将代码转化为流程图的操作教程》Code2flow是一款开源工具,能够将代码自动转换为流程图,该工具对于代码审查、调试和理解大型代码库非常有用,在这篇博客中,我们将深... 目录引言1nVflRA、为什么选择 Code2flow?2、安装 Code2flow3、基本功能演示

IIS 7.0 及更高版本中的 FTP 状态代码

《IIS7.0及更高版本中的FTP状态代码》本文介绍IIS7.0中的FTP状态代码,方便大家在使用iis中发现ftp的问题... 简介尝试使用 FTP 访问运行 Internet Information Services (IIS) 7.0 或更高版本的服务器上的内容时,IIS 将返回指示响应状态的数字代

MySQL 添加索引5种方式示例详解(实用sql代码)

《MySQL添加索引5种方式示例详解(实用sql代码)》在MySQL数据库中添加索引可以帮助提高查询性能,尤其是在数据量大的表中,下面给大家分享MySQL添加索引5种方式示例详解(实用sql代码),... 在mysql数据库中添加索引可以帮助提高查询性能,尤其是在数据量大的表中。索引可以在创建表时定义,也可

使用C#删除Excel表格中的重复行数据的代码详解

《使用C#删除Excel表格中的重复行数据的代码详解》重复行是指在Excel表格中完全相同的多行数据,删除这些重复行至关重要,因为它们不仅会干扰数据分析,还可能导致错误的决策和结论,所以本文给大家介绍... 目录简介使用工具C# 删除Excel工作表中的重复行语法工作原理实现代码C# 删除指定Excel单元