mirror of
https://github.com/wassname/Flex-Convolution.git
synced 2026-09-11 11:52:15 +08:00
Add the most simple training example to show-case training
This commit is contained in:
@@ -33,6 +33,7 @@ set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS} -std=c++11 -O3 -Xptxas=-v --expt-relaxed
|
||||
|
||||
# quick fix for drone-ci
|
||||
include_directories(SYSTEM "/usr/local/")
|
||||
include_directories(SYSTEM "/extra/samples/Common/")
|
||||
# fix cgtuebingen
|
||||
include_directories(SYSTEM "/graphics/opt/opt_Ubuntu18.04/cuda/toolkit_9.2")
|
||||
|
||||
|
||||
@@ -21,8 +21,9 @@
|
||||
import numpy as np
|
||||
import tensorflow as tf
|
||||
|
||||
from PointTestCase import FakePointCloud, random_values
|
||||
from __init__ import flex_conv
|
||||
from misc import FakePointCloud
|
||||
from __init__ import flex_convolution
|
||||
|
||||
|
||||
"""
|
||||
export LD_LIBRARY_PATH=/graphics/opt/opt_Ubuntu16.04/cuda/toolkit_9.0/cuda/extras/CUPTI/lib64:$LD_LIBRARY_PATH
|
||||
@@ -32,85 +33,42 @@ export LD_LIBRARY_PATH=/graphics/opt/opt_Ubuntu16.04/cuda/toolkit_9.0/cuda/extra
|
||||
np.random.seed(42)
|
||||
tf.set_random_seed(42)
|
||||
|
||||
N = 8
|
||||
TPC = FakePointCloud(8, 4096, N, 64, 64, 2, 1, N)
|
||||
|
||||
case = FakePointCloud(B=8, N=4096, K=8, Din=64, Dout=64, Dp=3)
|
||||
case.init_ops(dtype=np.float32)
|
||||
|
||||
class PointTestCase(object):
|
||||
|
||||
def __init__(self, data):
|
||||
self.position = random_values([data.B, data.Dp, data.N])
|
||||
self.features = random_values([data.B, data.Din, data.N])
|
||||
|
||||
# make sure, each neighbor hood has no duplicates and first entry is point n
|
||||
# THIS IS IMPORTANT!!
|
||||
self.neighborhood = np.zeros((data.B, data.K, data.N), dtype=np.int32)
|
||||
for b in range(data.B):
|
||||
for n in range(data.N):
|
||||
x = np.arange(data.N)
|
||||
# does not support axis, hence the loop
|
||||
np.random.shuffle(x)
|
||||
offset = np.argwhere(x == n)[0][0]
|
||||
# roll array such that n is first entry
|
||||
x = np.roll(x, -offset)
|
||||
self.neighborhood[b, :, n] = x[:data.K].astype(np.int32)
|
||||
|
||||
self.neighborhood_ds = np.zeros((data.B, data.K, data.N2), dtype=np.int32)
|
||||
for b in range(data.B):
|
||||
for n in range(data.N2):
|
||||
x = np.arange(data.N2)
|
||||
# does not support axis, hence the loop
|
||||
np.random.shuffle(x)
|
||||
offset = np.argwhere(x == n)[0][0]
|
||||
# roll array such that n is first entry
|
||||
x = np.roll(x, -offset)
|
||||
self.neighborhood[b, :, n] = x[:data.K].astype(np.int32)
|
||||
|
||||
self.theta = random_values([data.Degree, data.Dp, data.Din, data.Dout])
|
||||
self.bias = random_values([data.Din, data.Dout])
|
||||
|
||||
def init_ops(self):
|
||||
self.features_op = tf.Variable(self.features, name='f')
|
||||
self.position_op = tf.Variable(self.position, name='p')
|
||||
self.neighborhood_op = tf.Variable(self.neighborhood, name='n')
|
||||
self.neighborhood_ds_op = tf.Variable(self.neighborhood_ds, name='m')
|
||||
self.theta_op = tf.Variable(self.theta, name='t')
|
||||
self.bias_op = tf.Variable(self.bias, name='b')
|
||||
|
||||
|
||||
TestCase = PointTestCase(data=TPC)
|
||||
TestCase.init_ops()
|
||||
|
||||
|
||||
forward_op = flex_conv(TestCase.features_op,
|
||||
TestCase.theta_op, TestCase.bias_op, TestCase.neighborhood_op,
|
||||
TestCase.position_op, degree=1)
|
||||
forward_op = flex_convolution(case.features_op,
|
||||
case.position_op,
|
||||
case.neighborhood_op,
|
||||
case.theta_op,
|
||||
case.bias_op)
|
||||
|
||||
builder = tf.profiler.ProfileOptionBuilder
|
||||
opts = builder(builder.time_and_memory()).order_by('micros').build()
|
||||
|
||||
with tf.contrib.tfprof.ProfileContext('./.profiling_outputs/flex_conv') as pctx:
|
||||
with tf.contrib.tfprof.ProfileContext('./.profiling_outputs/flex_convolution') as pctx:
|
||||
|
||||
with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess:
|
||||
sess.run(tf.global_variables_initializer())
|
||||
with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess:
|
||||
sess.run(tf.global_variables_initializer())
|
||||
|
||||
back_prop = tf.gradients(forward_op, [TestCase.theta_op, TestCase.bias_op, TestCase.features_op])
|
||||
back_prop2 = tf.gradients(forward_op, [TestCase.theta_op, TestCase.bias_op,
|
||||
TestCase.features_op, TestCase.position_op])
|
||||
back_prop = tf.gradients(
|
||||
forward_op, [case.theta_op, case.bias_op, case.features_op])
|
||||
back_prop2 = tf.gradients(forward_op, [case.theta_op, case.bias_op,
|
||||
case.features_op, case.position_op])
|
||||
|
||||
# warmup
|
||||
for i in range(2):
|
||||
actual = sess.run([forward_op, back_prop])
|
||||
# warmup
|
||||
for i in range(2):
|
||||
actual = sess.run([forward_op, back_prop])
|
||||
|
||||
# benchmark
|
||||
for i in range(10):
|
||||
pctx.trace_next_step()
|
||||
pctx.dump_next_step()
|
||||
_ = sess.run([forward_op])
|
||||
pctx.profiler.profile_operations(options=opts)
|
||||
# benchmark
|
||||
for i in range(10):
|
||||
pctx.trace_next_step()
|
||||
pctx.dump_next_step()
|
||||
_ = sess.run([forward_op])
|
||||
pctx.profiler.profile_operations(options=opts)
|
||||
|
||||
for i in range(10):
|
||||
pctx.trace_next_step()
|
||||
pctx.dump_next_step()
|
||||
_ = sess.run([back_prop])
|
||||
pctx.profiler.profile_operations(options=opts)
|
||||
for i in range(10):
|
||||
pctx.trace_next_step()
|
||||
pctx.dump_next_step()
|
||||
_ = sess.run([back_prop])
|
||||
pctx.profiler.profile_operations(options=opts)
|
||||
|
||||
@@ -114,23 +114,23 @@ class FlexConvTest(VerboseTestCase):
|
||||
actual, expected = self._backward_theta(use_gpu=True, dtype=np.float64)
|
||||
self.assertAllClose(actual, expected)
|
||||
|
||||
# float32 has some numerical instabilities due to summation
|
||||
# central difference as derivatives are totally instable
|
||||
# hence we just compare cpu and gpu outputs (float64 num-diff tests pass)
|
||||
def test_backward_features_gpu_float32(self, dtype=np.float32):
|
||||
cpu = self._backward_features(use_gpu=False, dtype=dtype, numdiff=False)
|
||||
gpu = self._backward_features(use_gpu=True, dtype=dtype, numdiff=False)
|
||||
self.assertAllClose(cpu, gpu)
|
||||
self.assertAllClose(cpu, gpu, 1e-3)
|
||||
|
||||
def test_backward_bias_gpu_float32(self, dtype=np.float32):
|
||||
cpu = self._backward_bias(use_gpu=False, dtype=dtype, numdiff=False)
|
||||
gpu = self._backward_bias(use_gpu=True, dtype=dtype, numdiff=False)
|
||||
self.assertAllClose(cpu, gpu, 1e-5)
|
||||
self.assertAllClose(cpu, gpu, 1e-4)
|
||||
|
||||
def test_backward_theta_gpu_float32(self, dtype=np.float32):
|
||||
cpu = self._backward_theta(use_gpu=False, dtype=dtype, numdiff=False)
|
||||
gpu = self._backward_theta(use_gpu=True, dtype=dtype, numdiff=False)
|
||||
self.assertAllClose(cpu, gpu)
|
||||
|
||||
|
||||
|
||||
self.assertAllClose(cpu, gpu, 1e-4)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
Reference in New Issue
Block a user