SparseConv 的学习笔记

server/2024/9/23 6:29:50/

安装

环境设置在74.183 sdfstudio 里面,SparseNeus 推荐的版本是是 torchsparse = 2.0.0版本

文档

https://torchsparse-docs.github.io/

命令行如下:

需要 C++ 的 sudo 权限指定安装:


## 安装依赖项
conda install -c conda-forge sparsehash
sudo apt-get install libsparsehash-dev  

进入官网下载源代码安装torchsparse, 中间最好打开服务器的外网,需要下载好7~8个python 对应的 wheel , 大约有20分钟的等待时间:

git clone --recursive https://github.com/mit-han-lab/torchsparse
python setup.py install

学习(以SparseNeus的 code 为例子)

  1. 将一个空间Volume 划分成 96*96*96 的小的 voxel ,每一个小的 voxel 都有一个当前相机系的(齐次)坐标,变量名是up_coords 对应的 shape(241788,4). 每一个小的 voxel 都会向 reference image 进行投影,插值得到2D 的feature,最后得到变量 multiview_features,对应的 shape 是(241788,6,16),表示每个点在6张 reference image 上投影得到 C=32的 feature.
multiview_features, multiview_masks = back_project_sparse_type(up_coords, partial_vol_origin, self.voxel_size, feats,KRcam, sizeH=sizeH, sizeW=sizeW)  # (num of voxels, num_of_views, c), (num of voxels, num_of_views)
  1. 对于每一个小的 Voxel 内部的 feature 进行 方差的计算去得到 costvolume
volume = self.aggregate_multiview_features(multiview_features, multiview_masks)

volume 的shape 是 (241788,6,32)

  1. 使用 sparseConv 对于点进行卷积:
sparse_feat = SparseTensor(feat, r_coords.to(torch.int32))  # - directly use sparse tensor to avoid point2voxel operations
feat = self.sparse_costreg_net(sparse_feat)

SparseConv 是如何工作的

教程的网址:https://towardsdatascience.com/how-does-sparse-convolution-work-3257a0a8fd1

SparseConv 通常用于对于 3D 点云的处理, 3D 点云 在空间中的绝大多数的地方是 empty,因此使用 Dense 的 3DCNN 实际上是效率很低下的。本质上是建立 Hash Table,保存特定位置的计算结果。 每一个稀疏的元素 可以被 数据和对应的 索引所表示。 SparseConv 使用 HashTable 存储了所有的 active input sites, 然后通过 一个叫做 RuleBook的数据结构,实现向 输出的映射。

使用

SparseConv 还是和普通的 Conv3d 一样,只能处理规则化的离散数据,因此 对于每一个有效数据,都也要输入对应的 index. 这里的 r_coords 是 每个点对应的 index,数据类型是 torch.int32, 维度是[X,Y,Z,B] (V2.0) 或者 [B,X,Y,Z] (V2.1).

此处需要严格注意 torch.sparse 的版本, 决定batch_size 放在哪一块。

  1. 转换成 sparseTensor 的结构
feat [N,16] r_coords [N,4], 4 维度分别存储[X,Y,Z,B]
sparse_feat = SparseTensor(feat, r_coords.to(torch.int32))
  1. 使用SparseCNN 对于 SparseTensor 进行卷积
feat = self.sparse_costreg_net(sparse_feat)

实际测试调用 Sparse Tensor 的例子:

   import numpy as npresolution = np.array([101, 101, 301])X_range = np.array([0, 100])Y_range = np.array([0, 100])Z_range = np.array([0, 300])xs = np.linspace(X_range[0],X_range[1],resolution[0])ys = np.linspace(Y_range[0],Y_range[1],resolution[1])zs = np.linspace(Z_range[0],Z_range[1],resolution[2])xx, yy, zz = np.meshgrid(xs, ys, zs, indexing="ij")coord = torch.tensor(np.vstack([xx.ravel(), yy.ravel(), zz.ravel()]).T, dtype=torch.float).contiguous().cuda()point_nums = coord.shape[0]# batch index is in the last position in v2.0zeros = torch.zeros(point_nums, 1).cuda()coord = torch.cat((zeros,coord), dim=1).to(torch.int32)feat = torch.randn(point_nums, 3).cuda()sparse_feat= SparseTensor(feat,coords=coord)  net = SparseCostRegNet(d_in=3, d_out=16).cuda()ans = net.forward(sparse_feat)print(ans.shape)exit()

http://www.ppmy.cn/server/103129.html

相关文章

[译]开发者与熵的博弈

原文:https://itnext.io/entropy-in-software-development-77ed9110ef28 翻译:我的文章翻译智能体 文章润色智能体 文章转脑图智能体 人工校对 文章脉络: 文章概括: 文章通过热力学的视角,深入探讨了软件开发中的复…

保存数据至后台表

保存数据至后台表-供大数据平台使用-JOB程序 *&---------------------------------------------------------------------* *&程序名称 :ZBD_JOB_001 *&程序描述 : 保存数据至后台表-供大数据平台使用-JOB程序 *…

线程第二部分

一、线程退出 1.线程结束方式: 1.pthread_exit 2.在线程执行函数中return (此时与1式相等) 3.pthread_cancel: 4.任何一个线程调用了exit 或者 主线程main函数return 都会使进程结束 2.pthread_cancel:int pthread_cancel()pthread_t thread); 参数&a…

构建多商户AI智能名片小程序创意内容库的策略与实践

摘要:在数字化转型的浪潮中,多商户AI智能名片小程序凭借其高效、智能、个性化的特点,成为了企业市场推广与用户互动的新宠。然而,要保持小程序的持续吸引力和用户粘性,构建一个内容丰富、创意独特的创意内容库显得尤为…

pip install 遇到ValueError: check_hostname requires server_hostname的解决办法

我需要下载Cython来将py编译成c,结果在pip install的时候报错这个: ERROR: Exception: Traceback (most recent call last):File "F:\Anaconda3\envs\DouyinLive32\lib\site-packages\pip\_internal\cli\base_command.py", line 173, in _mai…

【LeetCode热题100】双指针

class Solution { public:void moveZeroes(vector<int>& nums) {int dst -1,cur 0;while(cur<nums.size()){if(nums[cur] 0){cur;}else{swap(nums[dst1],nums[cur]);cur;dst;}}} }; 题目分析&#xff1a;对于数组分块/数组划分的问题&#xff0c;我们可以使用双…

0815,析构函数,拷贝构造函数,赋值运算符函数

来自同济医院的问候 目录 01&#xff1a;对象创建 001.cc 003size.cc 02&#xff1a;对象销毁 004pointer.cc 005destroytime.cc 03&#xff1a;本类型对象的复制 3.1 拷贝构造函数 006cp.cc 007cptime.cc 008recursion.cc 009rightleft.cc 3.2 赋值运算符函数 …

Linux之进程间通信(下)

目录 命名管道 命名管道的创建 匿名管道和命名管道的区别 命名管道的代码实现 共享内存 创建共享内存 关联共享内存 去关联共享内存 删除共享内存 共享内存特点 共享内存代码实现 IPC资源总结 命名管道 上期我们学习了匿名管道&#xff0c;匿名管道本质就是一个…