MinkowskiEngine demo ModelNet40分类

本文将看一个简单的演示示例,该示例训练用于分类的3D卷积神经网络。输入是稀疏张量,卷积也定义在稀疏张量上。该网络是以下体系结构的扩展,但具有剩余的块和更多的层。

创建ModelNet40数据加载器

首先,需要创建一个数据加载器,以返回网格的稀疏张量表示。如果仅使用顶点,则3D模型的网格表示可能会稀疏。

首先以相同的密度采样点。

def resample_mesh(mesh_cad, density=1):

'''

    https://chrischoy.github.io/research/barycentric-coordinate-for-mesh-sampling/

    Samples point cloud on the surface of the model defined as vectices and

    faces. This function uses vectorized operations so fast at the cost of some

    memory.

    param mesh_cad: low-polygon triangle mesh in o3d.geometry.TriangleMesh

    param density: density of the point cloud per unit area

    param return_numpy: return numpy format or open3d pointcloud format

    return resampled point cloud

    Reference :

      [1] Barycentric coordinate system

      \begin{align}

        P = (1 - \sqrt{r_1})A + \sqrt{r_1} (1 - r_2) B + \sqrt{r_1} r_2 C

      \end{align}

    '''

faces = np.array(mesh_cad.triangles).astype(int)

vertices = np.array(mesh_cad.vertices)

vec_cross = np.cross(vertices[faces[:, 0], :] - vertices[faces[:, 2], :],

vertices[faces[:, 1], :] - vertices[faces[:, 2], :])

face_areas = np.sqrt(np.sum(vec_cross**2, 1))

n_samples = (np.sum(face_areas) * density).astype(int)

n_samples_per_face = np.ceil(density * face_areas).astype(int)

floor_num = np.sum(n_samples_per_face) - n_samples

if floor_num > 0:

indices = np.where(n_samples_per_face > 0)[0]

floor_indices = np.random.choice(indices, floor_num, replace=True)

n_samples_per_face[floor_indices] -= 1

n_samples = np.sum(n_samples_per_face)

# Create a vector that contains the face indices

sample_face_idx = np.zeros((n_samples,), dtype=int)

acc = 0

for face_idx, _n_sample in enumerate(n_samples_per_face):

sample_face_idx[acc:acc + _n_sample] = face_idx

acc += _n_sample

r = np.random.rand(n_samples, 2)

A = vertices[faces[sample_face_idx, 0], :]

B = vertices[faces[sample_face_idx, 1], :]

C = vertices[faces[sample_face_idx, 2], :]

P = (1 - np.sqrt(r[:, 0:1])) * A + \

np.sqrt(r[:, 0:1]) * (1 - r[:, 1:]) * B + \

np.sqrt(r[:, 0:1]) * r[:, 1:] * C

return P

上面的函数将以相同的密度对网格上的点进行采样。接下来,在量化步骤之前经历了一系列数据扩充步骤。

数据扩充

稀疏张量由两个部分组成:1)坐标和2)与这些坐标关联的特征。必须对这两个组件都应用数据增强,以最大化固定数据集的效用,并使网络对噪声具有鲁棒性。

这在图像数据增强中并不是什么新鲜事。对图像应用随机平移,剪切,缩放,所有这些都是坐标数据扩充。颜色失真(例如色平移,颜色通道上的高斯噪声,色相饱和度增强)都具有数据增强功能。

由于在ModelNet40数据集中只有坐标作为数据,将仅应用坐标数据增强。

class RandomRotation:

def _M(self, axis, theta):

return expm(np.cross(np.eye(3), axis / norm(axis) * theta))

def __call__(self, coords, feats):

R = self._M(

np.random.rand(3) - 0.5, 2 * np.pi * (np.random.rand(1) - 0.5))

return coords @ R, feats

class RandomScale:

def __init__(self, min, max):

self.scale = max - min

self.bias = min

def __call__(self, coords, feats):

s = self.scale * np.random.rand(1) + self.bias

return coords * s, feats

class RandomShear:

def __call__(self, coords, feats):

T = np.eye(3) + np.random.randn(3, 3)

return coords @ T, feats

class RandomTranslation:

def __call__(self, coords, feats):

trans = 0.05 * np.random.randn(1, 3)

return coords + trans, feats

训练ResNet进行ModelNet40分类

主要训练功能很简单。没有使用基于时间的训练,而是使用了基于迭代的训练。与基于时间的训练相比,基于迭代的训练的一个优势在于,训练逻辑独立于批处理大小。

def train(net, device, config):

optimizer = optim.SGD(

net.parameters(),

lr=config.lr,

momentum=config.momentum,

weight_decay=config.weight_decay)

scheduler = optim.lr_scheduler.ExponentialLR(optimizer, 0.95)

crit = torch.nn.CrossEntropyLoss()

...

net.train()

train_iter = iter(train_dataloader)

val_iter = iter(val_dataloader)

logging.info(f'LR: {scheduler.get_lr()}')

for i in range(curr_iter, config.max_iter):

s = time()

data_dict = train_iter.next()

d = time() - s

optimizer.zero_grad()

sin = ME.SparseTensor(data_dict['feats'],

data_dict['coords'].int()).to(device)

sout = net(sin)

loss = crit(sout.F, data_dict['labels'].to(device))

loss.backward()

optimizer.step()

t = time() - s

...

运行示例

集成所有代码块时,可以运行自主ModelNet40分类网络。

python -m examples.modelnet40 --batch_size 128 --stat_freq 100

完整的代码可以在example / modelnet40.py找到。

https://github.com/NVIDIA/MinkowskiEngine/blob/master/examples/modelnet40.py

警告

ModelNet40数据加载和体素化是训练中最耗时的部分。因此,该示例将所有ModelNet40数据缓存到内存中,这将占用大约10G的内存。

MinkowskiEngine demo ModelNet40分类的更多相关文章

  1. (转)JQM 日期插件 mobiscroll Demo

    (原)http://www.wglong.com/main/artical!details?id=11  JQM 日期插件 mobiscroll Demo 2013-04-25 / 分类:Jquery ...

  2. jQuery EasyUI 1.3.4 离线API、Demo

    [原]jQuery EasyUI 1.3.4 离线API.Demo (最新)   说明 本文下载包为 jQuery EasyUI 1.3.4 离线API.Demo. API 按照分类整理做成了离线版本 ...

  3. [原] jQuery EasyUI 1.3.4 离线API、Demo (最新)

    说明 本文下载包为 jQuery EasyUI 1.3.4 离线API.Demo. API 按照分类整理做成了离线版本,文档保证和官网完全一致: Demo 按照分类整理为合集. 1.3.3版本中新增 ...

  4. JS菜单条智能定位效果

    JS仿淘宝详情页菜单条智能定位效果 2014-01-15 15:40 by 龙恩0707, 1366 阅读, 9 评论, 收藏, 编辑 类似于淘宝详情页菜单条智能定位 对于每个人来说并不陌生!如下截图 ...

  5. JS数量输入控件

    JS数量输入控件 很早看到kissy首页 有数量输入控件,就随便看了下功能 感觉也不怎么难 所以也就试着自己也做了一个, 当然基本的功能和他们的一样,只是用了自己的编码思想来解决这么一个问题.特此给大 ...

  6. 动手Jquery插件

    自己动手Jquery插件 最近Web应用程序中越来越多地用到了JQuery等Web前端技术.这些技术框架有效地改善了用户的操作体验,同时也提高了开发人员构造丰富客户 端UI的效率.JQuery本身提供 ...

  7. JSON无限折叠菜单

    JSON无限折叠菜单编写 2013-12-14 22:37 by 龙恩0707, 103 阅读, 1 评论, 收藏, 编辑 最近看了一篇关于JSON无限折叠菜单的文章 感觉写的不错,也研究了下代码,所 ...

  8. YPreLoad

    Javascript库   发布我的控件系列:图片预加载控件YPreLoad v1.0 摘要: 介绍大家好!很高兴向大家介绍我的图片预加载控件YPreLoad.它可以帮助您预加载图片,并且能显示加载的 ...

  9. uploadify的使用

    uploadify的使用 课程设计需要实现上传文件模块,本来ASP.NET是有内置的控件,但是ASP.NET MVC没有,所以就有两种方法:自定义和采用第三方插件.由于时间的关系,故采用第三方插件:u ...

随机推荐

  1. 【小技巧】Windows 小技巧

    快捷键: 1.Win7窗口最大化和最小化快捷键 最大化:window+↑ 最小化:window+↓

  2. 使用EasySYS搭建驱动开发基本框架

    提供EasySYS的下载地址:http://bbs.pediy.com/showthread.php?p=956643,看雪上有提供下载,自行百度. EasySYS你能够帮我们快速的搭建驱动的开发框架 ...

  3. c# 通过 p/invoke 使用 c的加密程序 参数传递问题

    最近项目中使用需要上位机和下位机通过rs232通信,涉及到通讯加密问题, 硬件那边主要是pcb layout的,于是我就把加密的活拦了过来,锻炼锻炼 首先说明问题: 在c中,加密解密都测试通过,然后在 ...

  4. 【python】【补】Leetcode每日一题-合并两个有序数组

    [python]Leetcode每日一题-合并两个有序数组 [题目描述] 给你两个有序整数数组 nums1 和 nums2,请你将 nums2 合并到 nums1 中,使 nums1 成为一个有序数组 ...

  5. FileItem的部分方法解释

    FileItem的部分方法: boolean isFormField() isFormField() 方法用来判断FileItem对象里面封装的数据是一个普通文本表单字段,还是一个文件表单字段.如果是 ...

  6. PHP 调用请求外网接口

    1.类中定义静态方法 class FtpService{ /** * 请求外网 * @param $url 外网接口url * @param bool $params 参数,拼接字符串 post请求可 ...

  7. 【ECharts】报表联动,动态数据设计

    说明: 数据没有拉取后台数据,仅仅前端模拟数据,Copy即可有效果.联动后台时,使用异步获取数据即可.鼠标点击,动态展示点击项的数据.有关更多实例,请移步到echarts官网查看. 成果展示: 相关代 ...

  8. Python分支结构你真的搞定了吗?

    分支结构 分支结构能够让计算机像人一样进行思考,应对不同的场景做出不同的回应. Python中不支持switch语法,目前仅支持if/else形式,但是在Python3.10的测试版本中,貌似支持了s ...

  9. FFmpeg应用实践之命令查询

    0. 前言 FFmpeg 中常用的工具有三个,分别是多媒体编解码工具ffmpeg.多媒体内容分析工具ffprobe和多媒体播放器ffplay.本文介绍的指令都是与编解码工具 ffmpeg 相关的. 学 ...

  10. NIOSII IDE在WIN7下 couldn't allocate heap

    首先,所有的文件夹都不能有空格和中文 其次,出现这些SB错误 make -s all includes 3 [main] ? (3732) c:\altera\91\quartus\bin\cygwi ...