赞
踩
没想到吧。。时隔一年我又回来了。这一年因为一些原因放弃了一些东西,也学到了一些东西。
这几天想用深度学习做一下点云的分割试验,网上搜了一下相关标题的blog有很多,但大部分只是简单的介绍文章内容,干活并不多。经过五天的摸索,在缺乏相关资料和帮助的情况下,本人大致搞清楚了pointnet进行sem_seg的流程。可能步骤存在问题甚至是错的,因为也没有人交流,但从试验结果看来还行。欢迎大家批评指正。点云的标注用cloudcompare就可以,后面obj文件的可视化由于cc对其支持不太好,改用meshlab。
欢迎阅读我的其他文章:
windows下运行pointnet(全)
windows下运行pointcnn
首先准备自己的数据集。txt格式的点云文件就行,我随便找了个斯坦福兔子点云文件,里面是xyz三维点的坐标,就像这样
另外还有扫描的一个零件的点云数据当背景,数据格式和上面的类似。对兔子(rabbit)和零件(clutter)的点云数据进行随机变换得到不同视角的点云文件,得到如下组织
修改indoor3d_util.py中的g_class2color如下
g_class2color = {'rabbit': [255,0,0],
'clutter': [50,50,50]}
修改collect_point_label函数里面的内容,给点云数据增加rgb信息:
for f in glob.glob(os.path.join(anno_path, '*.txt')):
cls = os.path.basename(f).split('_')[0]
print(cls)
#if cls not in g_classes: # note: in some room there is 'staris' class..
# cls = 'clutter'
points = np.loadtxt(f)
labels = np.ones((points.shape[0], 1)) * g_class2label[cls]
color = np.zeros((points.shape[0], 3))
color[:, 0:3] = 255
points_list.append(np.concatenate([points, color, labels], 1)) # Nx7
然后可以运行collect_indoor3d_data.py生成npy文件。再通过运行gen_indoor3d_h5.py生成h5文件。
得到h5文件后就可以进行训练了。train.py文件有几个地方要修改:NUM_CLASSES、train_idxs、test_idxs;还需要修改model.py文件中网络最后一个卷积层的通道数为要分类的数目。另外,输入参数num_point、max_epoch、batch_size可以根据自己情况修改。我的输入样本较少,十几分钟就能训练完毕。
运行batch_inference.py程序即可。同理,需要修改NUM_CLASSES。运行需要提供的参数可以参考我的上一篇文章:windows下运行pointnet(全)。
随便标注了几组数据用于测试,效果有限。红色为预测出的兔子,灰色为预测出的工件,黑色为未识别出的部分。
acc=0.967960
acc=0.989229
acc=0.811689
这组模型中最右边的兔子几乎都预测错误了。
附:
txt转npy
import numpy as np
file = 'rabbit8.txt'
res = np.loadtxt(file)
res = []
with open(file, ‘r’) as f:
data = f.readlines()
for i, line in enumerate(data):
tmp = list(map(float, line.split()))
res.append(tmp)
print(res)
np.save(‘inf8.npy’, res)
txt转h5
import os
import sys
import numpy as np
import h5py
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append(BASE_DIR)
def getDataFiles(list_filename):
return [line.rstrip() for line in open(list_filename)]
def loadDataFile(path):
data = np.loadtxt(path)
point_xyz = data[:,0:9]
label = (data[:,9]).astype(int)
return point_xyz, label
def change_scale(data):
#centre
xyz_min = np.min(data[:,0:3],axis=0)
xyz_max = np.max(data[:,0:3],axis=0)
xyz_move = xyz_min+(xyz_max-xyz_min)/2
data[:,0:3] = data[:,0:3]-xyz_move
#scale
scale = np.max(data[:,0:3])
return data[:,0:3]/scale
if name == “main”:
DATA_FILES = getDataFiles(os.path.join(BASE_DIR, ‘file_path.txt’))
num_sample = 4096
DATA_ALL = []
for fn in range(len(DATA_FILES)):
print(DATA_FILES[fn])
current_data, label = loadDataFile(DATA_FILES[fn])
#change_data = change_scale(current_data)
#print(change_data)
data_label = np.column_stack((current_data,label))
DATA_ALL.append(data_label)
output <span class="token operator">=</span> np<span class="token punctuation">.</span>vstack<span class="token punctuation">(</span>DATA_ALL<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>output<span class="token punctuation">.</span>shape<span class="token punctuation">)</span>
output <span class="token operator">=</span> output<span class="token punctuation">.</span>reshape<span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span>num_sample<span class="token punctuation">,</span><span class="token number">10</span><span class="token punctuation">)</span>
<span class="token comment"># 这里没将训练测试集单独分开 </span>
<span class="token keyword">if</span> <span class="token operator">not</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>exists<span class="token punctuation">(</span><span class="token string">'ply_data_train1.h5'</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token keyword">with</span> h5py<span class="token punctuation">.</span>File<span class="token punctuation">(</span><span class="token string">'ply_data_train1.h5'</span><span class="token punctuation">)</span> <span class="token keyword">as</span> f<span class="token punctuation">:</span>
f<span class="token punctuation">[</span><span class="token string">'data'</span><span class="token punctuation">]</span> <span class="token operator">=</span> output<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">,</span><span class="token punctuation">:</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">:</span><span class="token number">9</span><span class="token punctuation">]</span>
f<span class="token punctuation">[</span><span class="token string">'label'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">(</span>output<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">,</span><span class="token punctuation">:</span><span class="token punctuation">,</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">.</span>astype<span class="token punctuation">(</span><span class="token builtin">int</span><span class="token punctuation">)</span>
整个工程文件打包放这个链接里了,预测生成的obj太大了就不放了。
链接:https://pan.baidu.com/s/15dWJeUupYFH54crv2C1dzw
提取码:owvu
复制这段内容后打开百度网盘手机App,操作更方便哦
本文参考的文章如下:
PointNet学习+训练自己的模型并实际使用测试成功
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。