当前位置:网站首页>Geo_ CNN (tensorflow version)
Geo_ CNN (tensorflow version)
2022-07-19 05:45:00 【weixin_ forty-two million seven hundred and seven thousand and 】
The Geo_CNN The source code comes from https://github.com/voidrank/Geo-CNN
1、perceptron function : perceptron
def perceptron(inputs,
num_output_channels,
scope,
is_training=None,
data_format='NHWC',
user_xavier=True,
stddev=1e-3,
weight_decay=None,
activation_fn=tf.nn.relu,
use_xavier=True,
bn=False,
bn_decay=None):
with tf.variable_scope(scope) as sc:
assert(data_format=='NHWC' or data_format=='NCHW')
#obtain [B,N,C]
b, n, c = inputs.get_shape().as_list()
#tf.reshape(tensor, shape, name=None) The delta function is going to tensor Change to parameter shape In the form of
#inputs:[B*N,C]
inputs = tf.reshape(inputs, (-1, c))
#_variable_with_weight_decay Add for variable weight decay
#weights=[C,num_output_channels]
weights = _variable_with_weight_decay('weights',
shape=[c, num_output_channels],
use_xavier=use_xavier,
stddev=stddev,
wd=weight_decay)
#tf.matmul() The matrix a Multiply by matrix b, Generate a * b.
#outputs=[B*N,num_output_channels]
outputs = tf.matmul(inputs, weights)
biases = _variable_on_cpu('biases', [num_output_channels],
tf.constant_initializer(0.0))
# Add bias
outputs = tf.nn.bias_add(outputs, biases)
#outputs=[B,N,num_output_channels]
outputs = tf.reshape(outputs, (b, n, -1))
#bn by True when , Add corresponding bn Layer normalization
if bn:
""" Batch normalization on FC data.
Args:
inputs: Tensor, 2D BxC input
is_training: boolean tf.Varialbe, true indicates training phase
bn_decay: float or float tensor variable, controling moving average weight
scope: string, variable scope Variable scope
Return:
normed: batch-normalized maps
"""
outputs = batch_norm_for_fc(outputs, is_training, bn_decay, 'bn')
# If there is an activation function , Then add the corresponding activation function layer , I think so Relu
if activation_fn is not None:
outputs = activation_fn(outputs)
return outputs
Two 、aggregate
from __future__ import print_function
import tensorflow as tf
from tensorflow.python.framework import ops
import sys
import os
import numpy as np
##os.path.dirname(__file__) The return is .py File directory :C:\Users\owolf\Desktop
#os.path.abspath(__file__) The return is .py The absolute path to the file ( The full path ):C:\Users\owolf\Desktop\1.py
# Use a combination of os.path.dirname(os.path.abspath(__file__)):C:\Users\owolf\Desktop
#os.path.join() Splicing path os.path.join(os.path.dirname(os.path.abspath(__file__)),'1.py'):C:\Users\owolf\Desktop\1.py
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
# It means importing modules under the corresponding directory
sys.path.append(BASE_DIR)
geoconv_module=tf.load_op_library(os.path.join(BASE_DIR, 'tf_geoconv_so.so'))
def aggregate(feat, xyz, radius, decay_radius, delta=0):
'''
inputs:
feature: batch_size * num_points * num_channels float32
xyz: batch_size * num_points * 3 float32
radius: float32
decay_radius: float32
delta int
returns:
output feature: batch_size * num_points * (num_channels/6) float32
#6 It stands for 6 The meaning of a base
norm feature: batch_size * num_points
'''
return geoconv_module.aggregate(feat, xyz, radius, decay_radius, delta)
#tf.RegisterGradient() Register for new gradients
# Use decorators , @ops.RegisterGradient("OpName"), Establish gradient back propagation function .
# among op.input Contains the input value 、 Output value ,grad Including gradients coming from the top
@tf.RegisterGradient('Aggregate')
# The signature of the gradient function is def _computer_gradient(op, grad), The first one is used to receive To calculate the gradient op, The second one is used to receive The gradient from the upper layer .
def _aggregate_grad(op, *out_g):
feat = op.inputs[0] # Take out the current input
xyz = op.inputs[1]
top_g = out_g[0]
norm_buffer = out_g[1]
#op.get_attr() obtain op Properties of
radius = op.get_attr("radius")
decay_radius = op.get_attr("decayradius")
delta = op.get_attr("delta")
return [geoconv_module.aggregate_grad(feat, xyz, top_g, radius, decay_radius, delta)[0], None]
The aggregate The function uses tf Of c++ End defined aggregate. The following is to verify the gradient difference :
class AggregateTest(tf.test.TestCase):
def test(self):
pass
def test_grad(self):
with tf.device('/gpu:0'):
feats = tf.constant(np.random.random((8, 128, 192)).astype('float32'))
xyz = tf.constant(np.random.random((8, 128, 3)).astype('float32'))
#ag=[8,128,192/6=32]
ag, _ = aggregate(feats, xyz, 0.3, 0.6)
print(ag)
with self.test_session():
print("------ Going to compute gradient error")
#compute_gradient_error() Calculate the gradient numerically , Return to the difference between the theoretical gradient , What we expect is a very small difference
#tf.test.compute_gradient_error(x, x_shape, y, y_shape, x_init_value=None, delta=0.001, init_targets=None)
# Calculate the gradient error. In the calculation and numerical estimation Jacobian in by dy/dx Calculate the biggest error
err = tf.test.compute_gradient_error(feats, (8, 128, 192), ag, (8, 128, 32))
print(err)
self.assertLess(err, 1e-4)
if __name__ == "__main__":
tf.test.main()
3、 ... and 、geo_conv
def geoconv(feat, xyz, num_outputs, bypass_num_outputs,
radius, decay_radius, bn=True, is_training=False,
scope=None, bn_decay=None, activation_fn=tf.nn.relu,
delta=False):
''' GeoCNN Geo-Conv
Input:
feat: (batch_size, num_point, input_channels) Input characteristics
points: (batch_size, num_point, 3) TF tensor
num_outputs: the count of output channels
bypass_num_outputs: the count of output channels of bypass
radius: the inner radius of local ball of each point.
decay radius: the outer radius of local ball of each point
...
'''
# tf.variable_scope() In order to achieve variable sharing
with tf.variable_scope(scope) as sc:
# perceptron self=[Batchsize,Num_points,num_outputs],self Represents the center point
self = perceptron(feat, num_outputs, scope='self', is_training=is_training, bn=False, activation_fn=None)
#matual=[Batchsize,Num_points, bypass_num_outputs * 6],
mutual = perceptron(feat, bypass_num_outputs * 6, scope='mutual', is_training=is_training, bn=False, activation_fn=None)
#ag:[batch_size, num_points, bypass_num_outputs]
ag, _ = aggregate(mutual, xyz, radius, decay_radius, delta)
#b=batch_size, n=num_points, bc=bypass_num_outputs
b, n, bc = ag.get_shape().as_list()
#c=num_outputs
_, _, c = self.get_shape().as_list()
#ag=[batch_size * num_points, bypass_num_outputs]
ag = tf.reshape(ag, (-1, bc))
#self=[Batchsize * Num_points, num_outputs]
self = tf.reshape(self, (-1, c))
if bn:
ag = batch_norm_for_fc(ag, is_training, bn_decay, scope='mutual_bn')
if activation_fn is not None:
ag = activation_fn(ag)
#ag=[batch_size , num_points, bypass_num_outputs]
ag = tf.reshape(ag, (b, n, bc))
#ag=[batch_size , num_points, num_outputs]
ag = perceptron(ag, num_outputs, scope='enlarge', is_training=is_training, bn=False, activation_fn=None)
#ag=[batch_size * num_points, num_outputs]
ag = tf.reshape(ag, (-1, c))
#outputs=[batch_size * num_points, num_outputs]
outputs = self + ag
if bn:
outputs = batch_norm_for_fc(outputs, is_training, bn_decay, scope='enlarge_bn')
if activation_fn is not None:
outputs = activation_fn(outputs)
#[batch_size , num_points, num_outputs]
outputs = tf.reshape(outputs, (b, n, c))
return outputs
Corresponding to
First : It represents the extraction of the central weight matrix Wc obtain Cout
Secondly, it represents the matrix of extracting other adjacent points Wx,Wy,Wz obtain Creduc
Then it means to get Wenl Matrix obtained Cout, The formula corresponding to the paper is :
Input is g(p,q),Wenl yes
Last , Add the features to get the local features and the corresponding formula as follows :
边栏推荐
猜你喜欢
static 关键字对作用域和生命周期的影响
Edge AI edge Intelligence: Communication EF "city edge ai: algorithms and systems (to be continued)
E-commerce user behavior real-time analysis system (flink1.10.1)
Pointnet++ code explanation (I): farthest_ point_ Sample function
INRIAPerson数据集转化为yolo训练格式并可视化
软件过程与管理复习(六)
JNA loading DLL and its application in jar
10. DWD layer construction of data warehouse construction
PyTorch学习笔记【1】:使用张量表征真实数据
gradle自定义插件
随机推荐
Kotlin scope function
微信小程序的常用組件
基于四叉树的图像压缩问题
Simple application of COAP in andorid
【语音识别入门】基础概念与框架
PCM静默检测
JVM learning
Problems encountered by kotlin generics
SnackBar source code analysis and packaging
微信小程序的页面导航
Hanoi Tower problem -- > recursive implementation
配置tabBar和request网络数据请求
Ambari2.7.5 integration es6.4.2
Development progress of Neusoft cross border e-commerce warehouse
Pointnet++ code explanation (I): farthest_ point_ Sample function
Wxml template syntax in wechat applet
记录:YOLOv5模型剪枝轻量化
Pointnet++代码详解(一):farthest_point_sample函数
Coap在Andorid中的简单应用
Pointnet++代码详解(二):square_distance函数