值得学习点:

  1. def pc_normalize(pc): # 点云归一化,对点云进行缩放
  2. def farthest_point_sample(point, npoint): # 单个样本的fps,后面还有 批量的 fps
  3. 将预处理(fps)后的数据 持久化, 方便以后直接load【fps 耗时】,用到的模块pickle
"""from: https://github.com/yanx27/Pointnet_Pointnet2_pytorch/blob/master/data_utils/ModelNetDataLoader.py
"""'''
@author: Xu Yan
@file: ModelNet.py
@time: 2021/3/19 15:51
'''
import os
import numpy as np
import warnings
import picklefrom tqdm import tqdm
from torch.utils.data import Datasetwarnings.filterwarnings('ignore')def pc_normalize(pc): # 点云归一化,对点云进行缩放centroid = np.mean(pc, axis=0)pc = pc - centroidm = np.max(np.sqrt(np.sum(pc**2, axis=1))) #  max(sqrt(p1^2 + p2^2 + .. + pi^2))pc = pc / m # 对点云进行缩放,方便 神经网络优化return pcdef farthest_point_sample(point, npoint): # 单个样本的fps,后面还有 批量的 fps"""Input:xyz: pointcloud data, [N, D]npoint: number of samplesReturn:centroids: sampled pointcloud index, [npoint, D]"""N, D = point.shapexyz = point[:,:3]centroids = np.zeros((npoint,))distance = np.ones((N,)) * 1e10farthest = np.random.randint(0, N)for i in range(npoint):centroids[i] = farthest # 取一个最远,刚开始是随机一个centroid = xyz[farthest, :]dist = np.sum((xyz - centroid) ** 2, -1)mask = dist < distancedistance[mask] = dist[mask] # fps 选取1个点, 到 点集{} 最远的点farthest = np.argmax(distance, -1)point = point[centroids.astype(np.int32)]return pointclass ModelNetDataLoader(Dataset):def __init__(self, root, args, split='train', process_data=False):"""Args:root: 文件路径, 如:data\modelnet40_normal_resampledargs: 参数process_data:  是否 直接加载 fps处理好的数据,如果是第一次运行 会将数据 持久化,方便下次直接load进来,学到了!"""self.root = rootself.npoints = args.num_pointself.process_data = process_dataself.uniform = args.use_uniform_sampleself.use_normals = args.use_normalsself.num_category = args.num_categoryif self.num_category == 10:self.catfile = os.path.join(self.root, 'modelnet10_shape_names.txt')else:self.catfile = os.path.join(self.root, 'modelnet40_shape_names.txt')self.cat = [line.rstrip() for line in open(self.catfile)]self.classes = dict(zip(self.cat, range(len(self.cat))))shape_ids = {}if self.num_category == 10:shape_ids['train'] = [line.rstrip() for line in open(os.path.join(self.root, 'modelnet10_train.txt'))]shape_ids['test'] = [line.rstrip() for line in open(os.path.join(self.root, 'modelnet10_test.txt'))]else:shape_ids['train'] = [line.rstrip() for line in open(os.path.join(self.root, 'modelnet40_train.txt'))]shape_ids['test'] = [line.rstrip() for line in open(os.path.join(self.root, 'modelnet40_test.txt'))]assert (split == 'train' or split == 'test')shape_names = ['_'.join(x.split('_')[0:-1]) for x in shape_ids[split]]self.datapath = [(shape_names[i], os.path.join(self.root, shape_names[i], shape_ids[split][i]) + '.txt') for iin range(len(shape_ids[split]))]print('The size of %s data is %d' % (split, len(self.datapath)))if self.uniform:self.save_path = os.path.join(root, 'modelnet%d_%s_%dpts_fps.dat' % (self.num_category, split, self.npoints)) # 命名学习else:self.save_path = os.path.join(root, 'modelnet%d_%s_%dpts.dat' % (self.num_category, split, self.npoints))if self.process_data:  # process_data 保存下来,下次直接加载就可以了 , 学到了!if not os.path.exists(self.save_path):print('Processing data %s (only running in the first time)...' % self.save_path)self.list_of_points = [None] * len(self.datapath)self.list_of_labels = [None] * len(self.datapath)for index in tqdm(range(len(self.datapath)), total=len(self.datapath)):fn = self.datapath[index]cls = self.classes[self.datapath[index][0]]cls = np.array([cls]).astype(np.int32)point_set = np.loadtxt(fn[1], delimiter=',').astype(np.float32)if self.uniform: # 均匀采样 fpspoint_set = farthest_point_sample(point_set, self.npoints)else:point_set = point_set[0:self.npoints, :]self.list_of_points[index] = point_setself.list_of_labels[index] = clswith open(self.save_path, 'wb') as f:pickle.dump([self.list_of_points, self.list_of_labels], f) # pickle.dump() 持久化存储else:print('Load processed data from %s...' % self.save_path)with open(self.save_path, 'rb') as f:self.list_of_points, self.list_of_labels = pickle.load(f) # pickle.load(f) 读取def __len__(self):return len(self.datapath)def _get_item(self, index):if self.process_data:point_set, label = self.list_of_points[index], self.list_of_labels[index]else:fn = self.datapath[index]cls = self.classes[self.datapath[index][0]]label = np.array([cls]).astype(np.int32)point_set = np.loadtxt(fn[1], delimiter=',').astype(np.float32)if self.uniform:point_set = farthest_point_sample(point_set, self.npoints)else:point_set = point_set[0:self.npoints, :]point_set[:, 0:3] = pc_normalize(point_set[:, 0:3]) if not self.use_normals:point_set = point_set[:, 0:3]return point_set, label[0]def __getitem__(self, index):return self._get_item(index)if __name__ == '__main__':import torchdata = ModelNetDataLoader(root = '/data/modelnet40_normal_resampled/', split='train')DataLoader = torch.utils.data.DataLoader(data, batch_size=12, shuffle=True)for point, label in DataLoader:print(point.shape)print(label.shape)

【点云数据预处理】ModelNetDataLoader.py相关推荐

  1. 从零实现一个3D目标检测算法(2):点云数据预处理

    在上一篇文章<从零实现一个3D目标检测算法(1):3D目标检测概述>对3D目标检测研究现状和PointPillars模型进行了介绍,在本文中我们开始写代码一步步实现PointPillars ...

  2. 【三维激光扫描】实验05:点云数据预处理操作

    SiScan软件可以对三维激光扫描的点云数据进行一些列的预处理操作,如:粗差剔除.点云抽稀.点云分割.调整水平面.删除分割面等等. 粗差剔除 剔除粗差点 抽稀 抽稀数据 点云分割 根据点云的三维特征进 ...

  3. 基于变电站3D点云数据的目标识别与检测学习总结(一)

    最近开始学习有关三维点云的知识,由于工程项目原因,首先学习了一篇与变电站相关的文章–<基于三维点云的变电站设备分类识别研究–李玲侠>,该文采用三维激光扫描仪来获取点云数据. 文章摘要 1. ...

  4. 云原生架构下日志服务数据预处理

    简介:本篇实践将以某家国际教育机构为例,为大家详细介绍云原生架构下日志服务数据预处理以及对应的解决方案和最佳实践操作手册,方便用户快速对号入座,解决云原生架构下的常见日志难题. 直达最佳实践:[htt ...

  5. Py之scikit-learn:机器学习sklearn库的简介、六大基本功能介绍(数据预处理/数据降维/模型选择/分类/回归/聚类)、安装、使用方法(实际问题中如何选择最合适的机器学习算法)之详细攻略

    Py之scikit-learn:机器学习sklearn库的简介(组件/版本迭代).六大基本功能介绍(数据预处理/数据降维/模型选择/分类/回归/聚类).安装.使用方法(实际问题中如何选择最合适的机器学 ...

  6. LIO-SAM:点云预处理前端---畸变矫正数据预处理

    LIO-SAM框架:点云预处理前端---畸变矫正数据预处理 前言 激光雷达畸变矫正 畸变矫正数据预处理 总结 前言 LIO-SAM的全称是:Tightly-coupled Lidar Inertial ...

  7. 【点云预处理】N种点云数据数据预处理方法 — 持续总结和更新(二)

    1~10种点云预处理方法请参考:10种点云数据数据预处理方法 - 持续总结和更新(一)_Coding的叶子的博客-CSDN博客_点云预处理.深度学习中点云基本数据处理和增强方式,包括点云归一化.随机打 ...

  8. PTMs:QLoRA技巧之源码解读(qlora.py文件)—解析命令与加载参数→数据预处理→模型训练+评估+推理

    PTMs:QLoRA技巧之源码解读(qlora.py文件)-解析命令与加载参数→数据预处理→模型训练+评估+推理 目录 QLoRA技巧之源码解读(qlora.py文件)-解析命令与加载参数→数据预处理 ...

  9. Halcon三维模型预处理(2):点云数据去噪+连通域分割

    点云数据是庞大的点集合,点云模型去噪就是删除多余的点集. 点云去噪的方式有两种: 1.通过在x,y,z的方向去除固定范围的点云数据,提取想要的点云ROI 2.通过选取连通域根据点云特征,筛选点云. 一 ...

最新文章

  1. 对 Jenkins+ANT+Jmeter 接口测试的实践
  2. python基础代码事例-零基础学习Python开发练习100题实例(2)
  3. c# 泛型study
  4. 032_jdbc-mysql批量操作
  5. 【STM32】STM32f4学习之路--时钟
  6. c++枚举类型(二) 命名空间
  7. VS 2005 WEB PROJECT包括Crystal Report水晶报表的发布
  8. python 版本分布式锁
  9. css 文本类属性 0302
  10. Android -- 自定义View小Demo,绘制四位数随机码(一)
  11. Python建立ip代理池(多线程)
  12. STM32CubeMX使用(七)之通用定时器和系统定时器
  13. 项目管理计划_通用模板
  14. 《算法笔记》胡凡 配套刷题网站
  15. TCP/IP协议新手入门学习
  16. Python编程 基础篇(一)
  17. python 去除文本空行
  18. Maven概念,项目目录结构
  19. 高德地图API POI分类编码表(mysql版)
  20. OpenStack Swift学习笔记

热门文章

  1. The label does not denote a loop in forEach
  2. jupyter python视频教程_【邢不行|量化小讲堂22】优雅的Python编程方式:Jupyter Notebook视频教程...
  3. 全球与中国投资管理软件市场深度研究分析报告
  4. 在fomc会议中建模主题趋势
  5. 太牛了,用python 居然可以生成 情话、床头诗、对联
  6. 选择云计算机时首先考虑的是,用户选择云计算时的首要考虑因素是什么?
  7. 软件测试面试 对一个输入框的测试要点
  8. 网络安全–21世纪的挑战
  9. PowerPoint to JPEG convertor
  10. 水性拉伸油增稠剂的非离子性能分析