pointnet语义分割_训练并预测自己的数据

这几天想用深度学习做一下点云的分割试验,网上搜了一下相关标题的blog有很多,但大部分只是简单的介绍文章内容,说明具体操作流程的干货并不多。在缺乏相关资料和帮助的情况下,本人大致搞清楚了pointnet进行sem_seg的流程。可能步骤存在问题甚至是错的,因为也没有人交流,但从试验结果看来还行。欢迎大家批评指正。点云的标注用cloudcompare(下面简称cc)就可以,后面obj文件的可视化由于cc对其支持不太好,改用meshlab。
欢迎阅读我的其他文章:
windows下运行pointnet(全)
pointnet C++推理部署(一)
pointnet C++推理部署(二)

训练

首先准备自己的数据集。txt格式的点云文件就行,我随便找了个斯坦福兔子点云文件,里面是xyz三维点的坐标。把兔子用cc软件分割成头(head)和身子(body),并分别保存成两个txt文件。
因为我比较懒所以只标注了这一个兔子,将其随机平移和旋转变换得到100个兔子的头和身子的txt,将其分别保存在编号为i(i=0,1,2,…,99)的文件夹下分别命名为head_i.txt和body_i.txt(i=0,1,2,…,99)。
修改sem_seg\meta\class_name.txt内容为head和body。
修改sem_seg\meta\anno_path.txt内容为训练标注的路径。
修改indoor3d_util.py中的g_class2color如下

g_class2color = {
    
    'head':	[255,0,0],
                 'body':	[0,0,255]}

修改collect_point_label函数里面的内容,给点云数据增加rgb信息:

   for f in glob.glob(os.path.join(anno_path, '*.txt')):
       print(f)
       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文件,注意要修改sem_seg\meta\all_data_label.txt内容为上面生成的npy文件的路径。
得到h5文件后就可以进行训练了。train.py文件有几个地方要修改:NUM_CLASSES、train_idxs、test_idxs;还需要修改model.py文件中网络最后一个卷积层的通道数为要分类的数目。另外,输入参数num_point、max_epoch、batch_size可以根据自己情况修改。我的数据量较少,几分钟就能训练完毕。

预测

运行batch_inference.py程序即可。同理,需要修改NUM_CLASSES,以及修改sem_seg\meta\data_label.txt中的待预测模型的路径。运行需要提供的参数可以参考我的上一篇文章:windows下运行pointnet(全)
随便做了一些npy文件用于测试,制作过程和训练部分相似。用于预测的数据集包括随机平移旋转的模型,添加噪声的模型,裁剪的模型以及混合前三种的模型。红色为预测出的兔子头部,蓝色为预测出的兔子身体,黑色为未识别出的部分。因为我自始至终只是用了一个原始模型进行不同变换进行的测试,所以正确率都在99%以上。
随机平移旋转的模型预测结果:
在这里插入图片描述
添加噪声的模型预测结果:
在这里插入图片描述
裁剪的模型预测结果:在这里插入图片描述
混合平移旋转、添加噪声、裁剪的模型预测结果:在这里插入图片描述
整个工程文件打包:链接:链接: https://pan.baidu.com/s/1bJMcOdA2Uz4fGW8m891XoA?pwd=ic7z 提取码: ic7z 复制这段内容后打开百度网盘手机App,操作更方便哦
–来自百度网盘超级会员v3的分享

本文参考的文章如下:
PointNet学习+训练自己的模型并实际使用测试成功

附预处理程序(Python实现和C++实现):
随机位姿变换

import numpy as np
import random

#文件名
old_file=r"rabbit.txt"
new_file=r"rabbit_change.txt"

#偏移参数
x_offset=random.uniform(-10, 10)
y_offset=random.uniform(-10, 10)
z_offset=random.uniform(-10, 10)

#缩放参数
scale=1.0

#旋转角度
roate_x=random.uniform(-np.pi/10, np.pi/10)
roate_y=random.uniform(-np.pi/10, np.pi/10)
roate_z=random.uniform(-np.pi/10, np.pi/10)

roate_x_matrix=np.array([
                    [1,0,0,0],
                    [0,np.cos(roate_x),-np.sin(roate_x),0],
                    [0,np.sin(roate_x),np.cos(roate_x),0],
                    [0,0,0,1]
                    ])
roate_y_matrix=np.array([
                    [np.cos(roate_y),0,np.sin(roate_y),0],
                    [0,1,0,0],
                    [-np.sin(roate_y),0,np.cos(roate_y),0],
                    [0,0,0,1]
                    ])
roate_z_matrix=np.array([
                    [np.cos(roate_z),-np.sin(roate_z),0,0],
                    [np.sin(roate_z),np.cos(roate_z),0,0],
                    [0,0,1,0],
                    [0,0,0,1]
                    ])

#变换矩阵
transformation_matrix=np.array([
                            [scale,0,0,x_offset],
                            [0,scale,0,y_offset],
                            [0,0,scale,z_offset],
                            [0,0,0,1]
                            ]).dot(roate_z_matrix).dot(roate_y_matrix).dot(roate_x_matrix)


#加载文件
old_array=np.loadtxt(old_file)
old_xyz=old_array[:,:3]

#补充数据为齐次项
ones_data=np.ones(old_xyz.shape[0])
old_xyz=np.insert(old_xyz,3,values=ones_data,axis=1)

#变换数据
new_xyz = np.dot(transformation_matrix,old_xyz.T)
new_array=np.concatenate((new_xyz.T[:,:3],old_array[:,3:]),axis=1)
np.savetxt(new_file,new_array,fmt='%.06f')
Eigen::Matrix4f random_transformation()
{
    
    
	//按三个欧拉角随机旋转:yaw和droll的范围是(- M_PI / 10, M_PI / 10),pitching的范围是(- M_PI / 20, M_PI / 20)
	float yaw = rand() * 2 /10.0 * M_PI / RAND_MAX - M_PI / 10.0;
	float pitching = rand() * M_PI / 10.0 / RAND_MAX - M_PI / 20.0;
	float droll = rand() * 2 * M_PI / 10.0 / RAND_MAX - M_PI / 10.0;
	
	Eigen::Vector3f ea0(yaw, pitching, droll);
	Eigen::Matrix3f R;
	R = Eigen::AngleAxisf(ea0[0], Eigen::Vector3f::UnitZ())*
		Eigen::AngleAxisf(ea0[1], Eigen::Vector3f::UnitY()) *
		Eigen::AngleAxisf(ea0[2], Eigen::Vector3f::UnitX());

	//按三坐标值平移,x、y、z平移范围均为(- 10, 10)
	float x = rand() * 20.0 / RAND_MAX - 10.0;
	float y = rand() * 20.0 / RAND_MAX - 10.0;
	float z = rand() * 20.0 / RAND_MAX - 10.0;

	Eigen::Vector3f T(x, y, z);
	Eigen::Matrix4f H;
	H << R, T, 0, 0, 0, 1;
	return H;
}

添加高斯噪声

import numpy as np

#文件名
old_file=r"rabbit.txt"
new_file=r"rabbit_change.txt"

def add_noise(point, sigma=0.1, clip=0.1):
    point = point.reshape(-1,3)
    Row, Col = point.shape
    noisy_point = np.clip(sigma * np.random.randn(Row, Col), -1*clip, clip)
    noisy_point += point
    return noisy_point
  
  
#加载文件
old_array=np.loadtxt(old_file)
old_xyz=old_array[:,:3]

new_xyz=add_noise(old_xyz)
new_array=np.concatenate((new_xyz,old_array[:,3:]),axis=1)
np.savetxt(new_file,new_array,fmt='%.06f')
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/point_cloud.h>
#include <boost/random.hpp>

int main(int argc, char** argv)
{
    
    
	boost::mt19937 rng;
	rng.seed(static_cast<unsigned int>(time(0)));
	boost::normal_distribution<> nd(0, 0.01);
	boost::variate_generator<boost::mt19937&, boost::normal_distribution<>> var_nor(rng, nd);

	std::ifstream infile;
	std::fstream outfile;
	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
	pcl::PointCloud<pcl::PointXYZ>::Ptr cloudfiltered(new pcl::PointCloud<pcl::PointXYZ>());
	float x, y, z;

	infile.open("17/head_17.txt");
	outfile.open("19/head_19.txt", 'w');
	while (infile >> x >> y >> z)
	{
    
    
		x += static_cast<float> (var_nor());
		y += static_cast<float> (var_nor());
		z += static_cast<float> (var_nor());
		outfile << x << " " << y << " " << z << std::endl;
	}
	infile.close();
	outfile.close();

	infile.open("17/body_17.txt");
	outfile.open("19/body_19.txt", 'w');
	while (infile >> x >> y >> z)
	{
    
    
		x += static_cast<float> (var_nor());
		y += static_cast<float> (var_nor());
		z += static_cast<float> (var_nor());
		outfile << x << " " << y << " " << z << std::endl;
	}
	infile.close();
	outfile.close();

	return EXIT_SUCCESS;
}

猜你喜欢

转载自blog.csdn.net/taifyang/article/details/115441212#comments_21987209