当前位置: 首页 > news >正文

SparseConv 的学习笔记

安装

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

命令行如下:

需要 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.lryc.cn/news/424793.html

相关文章:

  • vscode 快速生成vue 格式
  • react笔记:redux
  • 数据结构与算法--图的应用
  • 【leetcode图文详解】特殊数组II : 空间换时间的“记忆化”,越多越好吗?
  • 离线安装prometheus与Grafana实现可视化监控
  • 【Python学习-UI界面】PyQt5 小部件7-QSpinBox 计数器
  • [二次元]个人主页搭建
  • Spring Data JPA 自动创建时间的相关注解和用法
  • Java基础之隐式类型转换
  • 【数据结构与算法 | 图篇】Dijkstra算法(单源最短路径算法)
  • windows c转linux c要做的事情。
  • 【高等代数笔记】002.高等代数研究对象(二)
  • ubuntu服务器部署的mysql本地连不上的问题
  • python redis安装
  • YJ0043定制版抖音电商卷抢购系统带回收商城抖音电商优惠卷投资理财系统
  • 如何选择图片和视频
  • html+css网页制作 电商华为商城首页 ui还原度100%
  • EDAS(企业级应用服务)
  • 简单工厂,工厂方法 和 抽象工厂
  • python 压力测试脚本
  • 【Linux】多线程7——线程池
  • Linux Shell实例
  • Linux~MySQL数据库具体操作
  • Unity WebGL平台Hybrid Generate All报错undefined symbol sendfile
  • Java高级Day28-多线程
  • 0003 保险的会计要素及其计量属性
  • Swift版本控制的艺术:掌握代码演化的魔杖
  • 学习实战:生活垃圾自动识别与分类系统的实现
  • Swift模块化构建:解锁代码重用的金钥匙
  • 【计算机网络】CIDR无分类编址知识学习