-
Notifications
You must be signed in to change notification settings - Fork 89
/
bonnet.py
353 lines (306 loc) · 16.3 KB
/
bonnet.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#!/usr/bin/python3
# Copyright 2017 Andres Milioto, Cyrill Stachniss. All Rights Reserved.
#
# This file is part of Bonnet.
#
# Bonnet is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Bonnet is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Bonnet. If not, see <http://www.gnu.org/licenses/>.
'''
Network class, containing definition of the graph
API Style should be the same for all nets (Same class name and member functions)
'''
# tf
import tensorflow as tf
# common layers
from arch.abstract_net import AbstractNetwork
import arch.layer as lyr
class Network(AbstractNetwork):
def __init__(self, DATA, NET, TRAIN, logdir):
# init parent
super().__init__(DATA, NET, TRAIN, logdir)
def build_graph(self, img_pl, train_stage, data_format="NCHW"):
# some graph info depending on what I will do with it
summary = self.TRAIN['summary']
train_lyr = self.NET['train_lyr']
n_k_lyr = self.NET['n_k_lyr']
if len(train_lyr) != 9:
print("Wrong length in train list for network. Exiting...")
quit()
self.num_classes = len(self.DATA['label_map'])
# build the graph
print("Building graph")
with tf.variable_scope('images'):
# resize input to desired size
img_resized = tf.image.resize_images(img_pl,
[self.DATA["img_prop"]["height"],
self.DATA["img_prop"]["width"]])
# if on GPU. transpose to NCHW
if data_format == "NCHW":
# convert from NHWC to NCHW (faster on GPU)
img_transposed = tf.transpose(img_resized, [0, 3, 1, 2])
else:
img_transposed = img_resized
# normalization of input
n_img = (img_transposed - 128) / 128
with tf.variable_scope("encoder"):
print("encoder")
with tf.variable_scope("downsample1"):
print("downsample1")
# input image 1024*512 - 960*720
down_lyr1 = lyr.uERF_downsample(n_img, n_k_lyr[0], 5,
train_stage and train_lyr[0],
summary,
data_format=data_format)
with tf.variable_scope("non-bt-1"):
non_bt_1_lyr1 = lyr.uERF_non_bt(down_lyr1, 3,
train_stage and train_lyr[0],
summary,
data_format=data_format,
dropout=self.NET["dropout"],
bn_decay=self.NET["bn_decay"])
with tf.variable_scope("non-bt-2"):
non_bt_2_lyr1 = lyr.uERF_non_bt(non_bt_1_lyr1, 3,
train_stage and train_lyr[0],
summary,
data_format=data_format,
dropout=self.NET["dropout"],
bn_decay=self.NET["bn_decay"])
with tf.variable_scope("downsample2"):
print("downsample2")
# input image 512*256 - 480*360
down_lyr2 = lyr.uERF_downsample(non_bt_2_lyr1, n_k_lyr[1], 5,
train_stage and train_lyr[1],
summary,
data_format=data_format)
with tf.variable_scope("non-bt-1"):
print("non-bt-1")
non_bt_1_lyr2 = lyr.uERF_non_bt(down_lyr2, 5,
train_stage and train_lyr[1],
summary,
data_format=data_format,
dropout=self.NET["dropout"],
bn_decay=self.NET["bn_decay"])
with tf.variable_scope("non-bt-2"):
print("non-bt-2")
non_bt_2_lyr2 = lyr.uERF_non_bt(non_bt_1_lyr2, 5,
train_stage and train_lyr[1],
summary,
data_format=data_format,
dropout=self.NET["dropout"],
bn_decay=self.NET["bn_decay"])
with tf.variable_scope("non-bt-3"):
print("non-bt-3")
non_bt_3_lyr2 = lyr.uERF_non_bt(non_bt_2_lyr2, 5,
train_stage and train_lyr[1],
summary,
data_format=data_format,
dropout=self.NET["dropout"],
bn_decay=self.NET["bn_decay"])
with tf.variable_scope("non-bt-4"):
print("non-bt-4")
non_bt_4_lyr2 = lyr.uERF_non_bt(non_bt_3_lyr2, 5,
train_stage and train_lyr[1],
summary,
data_format=data_format,
dropout=self.NET["dropout"],
bn_decay=self.NET["bn_decay"])
non_bt_lyr2 = non_bt_4_lyr2
with tf.variable_scope("downsample3"):
print("downsample3")
# input image 256*128 - 240*180
down_lyr3 = lyr.uERF_downsample(non_bt_lyr2, n_k_lyr[2], 5,
train_stage and train_lyr[2],
summary,
data_format=data_format)
with tf.variable_scope("non-bt-1"):
print("non-bt-1")
non_bt_1_lyr3 = lyr.uERF_non_bt(down_lyr3, 7,
train_stage and train_lyr[2],
summary,
data_format=data_format,
dropout=self.NET["dropout"],
bn_decay=self.NET["bn_decay"])
with tf.variable_scope("non-bt-2"):
print("non-bt-2")
non_bt_2_lyr3 = lyr.uERF_non_bt(non_bt_1_lyr3, 7,
train_stage and train_lyr[2],
summary,
data_format=data_format,
dropout=self.NET["dropout"],
bn_decay=self.NET["bn_decay"])
with tf.variable_scope("non-bt-3"):
print("non-bt-3")
non_bt_3_lyr3 = lyr.uERF_non_bt(non_bt_2_lyr3, 7,
train_stage and train_lyr[2],
summary,
data_format=data_format,
dropout=self.NET["dropout"],
bn_decay=self.NET["bn_decay"])
with tf.variable_scope("non-bt-4"):
print("non-bt-4")
non_bt_4_lyr3 = lyr.uERF_non_bt(non_bt_3_lyr3, 7,
train_stage and train_lyr[2],
summary,
data_format=data_format,
dropout=self.NET["dropout"],
bn_decay=self.NET["bn_decay"])
downsampled = non_bt_4_lyr3
with tf.variable_scope("godeep"):
print("godeep")
# input image 64*32 - 60*45
with tf.variable_scope("non-bt-1"):
print("non-bt-1")
godeep_ly1 = lyr.uERF_non_bt(downsampled, 7,
train_stage and train_lyr[3],
summary,
data_format=data_format,
dropout=self.NET["dropout"],
bn_decay=self.NET["bn_decay"])
with tf.variable_scope("non-bt-2"):
print("non-bt-2")
godeep_ly2 = lyr.uERF_non_bt(godeep_ly1, 7,
train_stage and train_lyr[3],
summary,
data_format=data_format,
dropout=self.NET["dropout"],
bn_decay=self.NET["bn_decay"])
with tf.variable_scope("non-bt-3"):
print("non-bt-3")
godeep_ly3 = lyr.uERF_non_bt(godeep_ly2, 7,
train_stage and train_lyr[3],
summary,
data_format=data_format,
dropout=self.NET["dropout"],
bn_decay=self.NET["bn_decay"])
with tf.variable_scope("non-bt-4"):
print("non-bt-4")
godeep_ly4 = lyr.uERF_non_bt(godeep_ly3, 7,
train_stage and train_lyr[3],
summary,
data_format=data_format,
dropout=self.NET["dropout"],
bn_decay=self.NET["bn_decay"])
godeep = godeep_ly4
code = godeep
# end encoder, start decoder
print("============= End of encoder ===============")
print("size of code: ", code.get_shape().as_list())
print("=========== Beginning of decoder============")
with tf.variable_scope("decoder"):
print("decoder")
with tf.variable_scope("upsample"):
print("upsample")
with tf.variable_scope("unpool1"):
print("unpool1")
# input image 64*32 - 60*45
unpool_lyr1 = lyr.upsample_layer(code,
train_stage and train_lyr[5],
kernels=n_k_lyr[3],
data_format=data_format)
with tf.variable_scope("non-bt-1"):
un_non_bt_1_lyr1 = lyr.uERF_non_bt(unpool_lyr1, 3,
train_stage and train_lyr[5],
summary,
data_format=data_format,
dropout=self.NET["dropout"],
bn_decay=self.NET["bn_decay"])
with tf.variable_scope("non-bt-2"):
un_non_bt_2_lyr1 = lyr.uERF_non_bt(un_non_bt_1_lyr1, 3,
train_stage and train_lyr[5],
summary,
data_format=data_format,
dropout=self.NET["dropout"],
bn_decay=self.NET["bn_decay"])
with tf.variable_scope("non-bt-3"):
un_non_bt_3_lyr1 = lyr.uERF_non_bt(un_non_bt_2_lyr1, 3,
train_stage and train_lyr[5],
summary,
data_format=data_format,
dropout=self.NET["dropout"],
bn_decay=self.NET["bn_decay"])
with tf.variable_scope("non-bt-4"):
un_non_bt_4_lyr1 = lyr.uERF_non_bt(un_non_bt_3_lyr1, 3,
train_stage and train_lyr[5],
summary,
data_format=data_format,
dropout=self.NET["dropout"],
bn_decay=self.NET["bn_decay"])
with tf.variable_scope("unpool2"):
print("unpool2")
# input image 128*64 - 120*90
unpool_lyr2 = lyr.upsample_layer(un_non_bt_4_lyr1,
train_stage and train_lyr[6],
kernels=n_k_lyr[4],
data_format=data_format)
with tf.variable_scope("non-bt-1"):
un_non_bt_1_lyr2 = lyr.uERF_non_bt(unpool_lyr2, 3,
train_stage and train_lyr[6],
summary,
data_format=data_format,
dropout=self.NET["dropout"],
bn_decay=self.NET["bn_decay"])
with tf.variable_scope("non-bt-2"):
un_non_bt_2_lyr2 = lyr.uERF_non_bt(un_non_bt_1_lyr2, 3,
train_stage and train_lyr[6],
summary,
data_format=data_format,
dropout=self.NET["dropout"],
bn_decay=self.NET["bn_decay"])
with tf.variable_scope("non-bt-3"):
un_non_bt_3_lyr2 = lyr.uERF_non_bt(un_non_bt_2_lyr2, 3,
train_stage and train_lyr[6],
summary,
data_format=data_format,
dropout=self.NET["dropout"],
bn_decay=self.NET["bn_decay"])
with tf.variable_scope("non-bt-4"):
un_non_bt_4_lyr2 = lyr.uERF_non_bt(un_non_bt_3_lyr2, 3,
train_stage and train_lyr[6],
summary,
data_format=data_format,
dropout=self.NET["dropout"],
bn_decay=self.NET["bn_decay"])
with tf.variable_scope("unpool3"):
print("unpool3")
# input image 256*128 - 240*180
unpool_lyr3 = lyr.upsample_layer(un_non_bt_4_lyr2,
train_stage and train_lyr[7],
kernels=n_k_lyr[5],
data_format=data_format)
with tf.variable_scope("non-bt-1"):
un_non_bt_1_lyr3 = lyr.uERF_non_bt(unpool_lyr3, 3,
train_stage and train_lyr[7],
summary,
data_format=data_format,
dropout=self.NET["dropout"],
bn_decay=self.NET["bn_decay"])
with tf.variable_scope("non-bt-2"):
un_non_bt_2_lyr3 = lyr.uERF_non_bt(un_non_bt_1_lyr3, 3,
train_stage and train_lyr[7],
summary,
data_format=data_format,
dropout=self.NET["dropout"],
bn_decay=self.NET["bn_decay"])
unpooled = un_non_bt_2_lyr3
with tf.variable_scope("logits"):
# convert to logits with a linear layer
logits_linear = lyr.linear_layer(unpooled, self.num_classes,
train_stage and train_lyr[8],
summary=summary,
data_format=data_format)
# transpose logits back to NHWC
if data_format == "NCHW":
logits = tf.transpose(logits_linear, [0, 2, 3, 1])
else:
logits = logits_linear
return logits, code, n_img