-
Notifications
You must be signed in to change notification settings - Fork 0
/
MCGINetworkRT.py
executable file
·222 lines (194 loc) · 9.26 KB
/
MCGINetworkRT.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
\file MCGINetworkRT.py
\copyright Copyright (c) 2019 Visual Computing group of Ulm University,
Germany. See the LICENSE file at the top-level directory of
this distribution.
\author pedro hermosilla ([email protected])
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
import sys
import os
import math
import tensorflow as tf
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.join(ROOT_DIR, 'MCCNN/tf_ops'))
sys.path.append(os.path.join(ROOT_DIR, 'MCCNN/utils'))
from MCConvBuilder import PointHierarchy, ConvolutionBuilder
from MCNetworkUtils import conv_1x1
def compute_initial_pts(points, batchIds, features, batchSize):
mPointHierarchy = PointHierarchy(points, features, batchIds, [0.01], "MCGIInit_PH1", batchSize, relativeRadius=False)
return mPointHierarchy.sampledIndexs_[0], mPointHierarchy.points_[1]
def create_network(points, batchIds, features, numInFeatures, numInFeatures2,
points2, batchIds2, features2, outputs,
numOutputs, batchSize, k, isTraining,
bnMomentum, brnMomentum, brnClipping,
keepProbConv, keepProbFull,
useConvDropOut = False, useDropOutFull = True,
dataset = 0):
def BN_NL_DP_Conv(layerName, inFeatures):
inFeatures = tf.layers.batch_normalization(inputs = inFeatures, momentum=bnMomentum,
trainable = True, training = isTraining, name = layerName+"_BN", renorm=True,
renorm_clipping=brnClipping, renorm_momentum=brnMomentum)
inFeatures = tf.nn.leaky_relu(inFeatures)
if useConvDropOut:
inFeatures = tf.nn.dropout(inFeatures, keepProbConv)
return inFeatures
def BN_NL_DP_F(layerName, inFeatures):
inFeatures = tf.layers.batch_normalization(inputs = inFeatures, momentum=bnMomentum,
trainable = True, training = isTraining, name = layerName+"_BN", renorm=True,
renorm_clipping=brnClipping, renorm_momentum=brnMomentum)
inFeatures = tf.nn.leaky_relu(inFeatures)
if useDropOutFull:
inFeatures = tf.nn.dropout(inFeatures, keepProbFull)
return inFeatures
############################################ Compute point hierarchy
mPointHierarchy1 = PointHierarchy(points, features, batchIds, [0.05, 0.1, 0.2], "MCGI_PH1", batchSize, relativeRadius=False)
mPointHierarchy2 = PointHierarchy(points2, features2, batchIds2, [], "MCGI_PH2", batchSize, relativeRadius=False)
############################################ Convolutions
mConvBuilder = ConvolutionBuilder(KDEWindow=0.25, relativeRadius=False)
with tf.variable_scope("feature_channel_scope", reuse=tf.AUTO_REUSE):
############################################ Encoder
# First convolution
convFeatures1 = mConvBuilder.create_convolution(
convName="Conv_1",
inPointHierarchy=mPointHierarchy1,
inPointLevel=0,
inFeatures=features,
inNumFeatures=numInFeatures,
outNumFeatures=k,
convRadius=0.025,
multiFeatureConv=True)
# First pooling
bnConvFeatures1 = BN_NL_DP_Conv("Reduce_Pool_1_In_BN", convFeatures1)
bnConvFeatures1 = conv_1x1("Reduce_Pool_1", bnConvFeatures1, k, k*2)
bnConvFeatures1 = BN_NL_DP_Conv("Reduce_Pool_1_Out_BN", bnConvFeatures1)
poolFeatures1 = mConvBuilder.create_convolution(
convName="Pool_1",
inPointHierarchy=mPointHierarchy1,
inPointLevel=0,
outPointLevel=1,
inFeatures=bnConvFeatures1,
inNumFeatures=k*2,
convRadius=0.05,
KDEWindow= 0.2)
# Second convolution
bnPoolFeatures1 = BN_NL_DP_Conv("Reduce_Conv_2_In_BN", poolFeatures1)
bnPoolFeatures1 = conv_1x1("Reduce_Conv_2", bnPoolFeatures1, k*2, k*2)
bnPoolFeatures1 = BN_NL_DP_Conv("Reduce_Conv_2_Out_BN", bnPoolFeatures1)
convFeatures2 = mConvBuilder.create_convolution(
convName="Conv_2",
inPointHierarchy=mPointHierarchy1,
inPointLevel=1,
inFeatures=bnPoolFeatures1,
inNumFeatures=k*2,
convRadius=0.1)
convFeatures2 = tf.concat([poolFeatures1, convFeatures2], 1)
# Second pooling
bnConvFeatures2 = BN_NL_DP_Conv("Reduce_Pool_2_In_BN", convFeatures2)
bnConvFeatures2 = conv_1x1("Reduce_Pool_2", bnConvFeatures2, k*4, k*4)
bnConvFeatures2 = BN_NL_DP_Conv("Reduce_Pool_2_Out_BN", bnConvFeatures2)
poolFeatures2 = mConvBuilder.create_convolution(
convName="Pool_2",
inPointHierarchy=mPointHierarchy1,
inPointLevel=1,
outPointLevel=2,
inFeatures=bnConvFeatures2,
inNumFeatures=k*4,
convRadius=0.2,
KDEWindow= 0.2)
# Third convolution
bnPoolFeatures2 = BN_NL_DP_Conv("Reduce_Conv_3_In_BN", poolFeatures2)
bnPoolFeatures2 = conv_1x1("Reduce_Conv_3", bnPoolFeatures2, k*4, k*4)
bnPoolFeatures2 = BN_NL_DP_Conv("Reduce_Conv_3_Out_BN", bnPoolFeatures2)
convFeatures3 = mConvBuilder.create_convolution(
convName="Conv_3",
inPointHierarchy=mPointHierarchy1,
inPointLevel=2,
inFeatures=bnPoolFeatures2,
inNumFeatures=k*4,
convRadius=0.2)
convFeatures3 = tf.concat([poolFeatures2, convFeatures3], 1)
# Third pooling
bnConvFeatures3 = BN_NL_DP_Conv("Reduce_Pool_3_In_BN", convFeatures3)
bnConvFeatures3 = conv_1x1("Reduce_Pool_3", bnConvFeatures3, k*8, k*8)
bnConvFeatures3 = BN_NL_DP_Conv("Reduce_Pool_3_Out_BN", bnConvFeatures3)
poolFeatures3 = mConvBuilder.create_convolution(
convName="Pool_3",
inPointHierarchy=mPointHierarchy1,
inPointLevel=2,
outPointLevel=3,
inFeatures=bnConvFeatures3,
inNumFeatures=k*8,
convRadius=0.4,
KDEWindow= 0.2)
# Fourth convolution
bnPoolFeatures3 = BN_NL_DP_Conv("Reduce_Conv_4_In_BN", poolFeatures3)
bnPoolFeatures3 = conv_1x1("Reduce_Conv_4", bnPoolFeatures3, k*8, k*8)
bnPoolFeatures3 = BN_NL_DP_Conv("Reduce_Conv_4_Out_BN", bnPoolFeatures3)
convFeatures4 = mConvBuilder.create_convolution(
convName="Conv_4",
inPointHierarchy=mPointHierarchy1,
inPointLevel=3,
inFeatures=bnPoolFeatures3,
inNumFeatures=k*8,
convRadius=1.0,
KDEWindow= 0.2)
############################################ Decoder
# Third upsampling
bnConvFeatures4 = BN_NL_DP_Conv("Up_3_BN", convFeatures4)
upFeatures3 = mConvBuilder.create_convolution(
convName="Up_3",
inPointHierarchy=mPointHierarchy1,
inPointLevel=3,
outPointLevel=2,
inFeatures=bnConvFeatures4,
inNumFeatures=k*8,
convRadius=0.4,
KDEWindow= 0.2)
upFeatures3 = tf.concat([convFeatures3, upFeatures3], 1)
upFeatures3 = BN_NL_DP_Conv("Up_3_Reduce_BN", upFeatures3)
upFeatures3 = conv_1x1("Up_3_Reduce", upFeatures3, k*16, k*4)
# Second upsampling
bnUpFeatures3 = BN_NL_DP_Conv("Up_2_BN", upFeatures3)
upFeatures2 = mConvBuilder.create_convolution(
convName="Up_2",
inPointHierarchy=mPointHierarchy1,
inPointLevel=2,
outPointLevel=1,
inFeatures=bnUpFeatures3,
inNumFeatures=k*4,
convRadius=0.2,
KDEWindow= 0.2)
upFeatures2 = tf.concat([convFeatures2, upFeatures2], 1)
upFeatures2 = BN_NL_DP_Conv("Up_2_Reduce_BN", upFeatures2)
upFeatures2 = conv_1x1("Up_2_Reduce", upFeatures2, k*8, k*2)
# First upsampling
bnUpFeatures2 = BN_NL_DP_Conv("Up_1_2_BN", upFeatures2)
upFeatures1 = mConvBuilder.create_convolution(
convName="Up_1_2",
inPointHierarchy=mPointHierarchy1,
inPointLevel=1,
outPointLevel=0,
inFeatures=bnUpFeatures2,
inNumFeatures=k*2,
convRadius=0.1,
KDEWindow= 0.2)
upFeatures1 = tf.concat([convFeatures1, upFeatures1], 1)
upFeatures1 = BN_NL_DP_Conv("Up_1_Reduce_BN", upFeatures1)
upFeatures1 = conv_1x1("Up_1_Reduce", upFeatures1, k*3, k)
bnUpFeatures1 = BN_NL_DP_Conv("Up_1_BN", upFeatures1)
finalFeatures = mConvBuilder.create_convolution(
convName="Final_Conv",
inPointHierarchy=mPointHierarchy1,
outPointHierarchy=mPointHierarchy2,
inPointLevel=0,
outPointLevel=0,
inFeatures=bnUpFeatures1,
inNumFeatures=k,
convRadius=0.05,
KDEWindow= 0.2)
finalFeatures = BN_NL_DP_F("Final_MLP1_BN", finalFeatures)
finalFeatures = tf.concat([finalFeatures, features2], 1)
finalFeatures = conv_1x1("Final_MLP1", finalFeatures, k + numInFeatures2, k)
finalFeatures = BN_NL_DP_F("Final_MLP2_BN", finalFeatures)
predVals = conv_1x1("Final_MLP2", finalFeatures, k, numOutputs)
return predVals