【睿慕课点云处理】第九章-基于深度学习的点云分割方法
课程
课程汇总
作业
代码注释(tensorflow版本)
github
EdgeConv修改
增加余弦距离
def get_edge_feature(point_cloud, nn_idx, k=20):
"""Construct edge feature for each point
Args:
point_cloud: (batch_size, num_points, 1, num_dims)
nn_idx: (batch_size, num_points, k)
k: int
Returns:
edge features: (batch_size, num_points, k, num_dims)
"""
og_batch_size = point_cloud.get_shape().as_list()[0]
point_cloud = tf.squeeze(point_cloud) # B*N*num_dims
if og_batch_size == 1:
point_cloud = tf.expand_dims(point_cloud, 0)
point_cloud_central = point_cloud
point_cloud_shape = point_cloud.get_shape()
batch_size = point_cloud_shape[0].value
num_points = point_cloud_shape[1].value
num_dims = point_cloud_shape[2].value
# 计算pointcloud的中心点 in:B*N*num_dims,out:B*1*num_dims
cloud_center=tf.reduce_mean(point_cloud,axis=-2,keep_dims=True) # B*1*num_dims
cloud_center=tf.expand_dims(cloud_center,axis=-2) # B*1*1*num_dims
cloud_center=tf.tile(cloud_center,[1,num_points,k,1]) # B*N*K*num_dims
idx_ = tf.range(batch_size) * num_points
idx_ = tf.reshape(idx_, [batch_size, 1, 1])
point_cloud_flat = tf.reshape(point_cloud, [-1, num_dims])
point_cloud_neighbors = tf.gather(point_cloud_flat, nn_idx+idx_) # gather:抽取子集,邻域特征 B*N*K*3
# 每一个点
point_cloud_central = tf.expand_dims(point_cloud_central, axis=-2) # 自身点 B*N*1*3
point_cloud_central = tf.tile(point_cloud_central, [1, 1, k, 1]) # 赋值,第三个维度复制k倍 B*N*K*3
# 计算每个点到中心点的向量a、k邻域点到中心点的向量b
a=point_cloud_central-cloud_center # B*N*K*num_dims
b=point_cloud_neighbors-cloud_center # B*N*K*num_dims
# 求模
a_norm=tf.sqrt(tf.reduce_sum(tf.square(a), axis=-1))
b_norm=tf.sqrt(tf.reduce_sum(tf.square(b), axis=-1))
# 内积
a_b = tf.reduce_sum(tf.multiply(a, b), axis=-1)
# 余弦距离
cosin = tf.divide(a_b, tf.multiply(a_norm, b_norm)) # B*N*K
# 对余弦距离扩维
cosin=tf.expand_dims(cosin,axis=-1)
edge_feature = tf.concat([point_cloud_central, point_cloud_neighbors-point_cloud_central,cosin], axis=-1) # 点的特征和边的特征组合起来,在最后一个维度上相加 B*N*K*6
return edge_feature