-
Notifications
You must be signed in to change notification settings - Fork 1
/
modules.py
394 lines (378 loc) · 16.6 KB
/
modules.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import tensorflow as tf
import numpy as np
swish=tf.keras.layers.Lambda(lambda x:x*tf.math.sigmoid(x))
hard_sigmoid=tf.keras.layers.Lambda(lambda x:tf.nn.relu6(x+3.0)/6.0)
mish=tf.keras.layers.Lambda(lambda x:x*tf.math.tanh(tf.math.softplus(x)))
class ConvBN(tf.Module):
def __init__(self,
filters,
kernel_size,
strides=(1,1),
padding="same",
bias=False,
use_bn=True,
activation=None,
name="convbn"):
super(ConvBN,self).__init__()
self._filters=filters
self._kernel_size=kernel_size
self._strides=strides
self._padding=padding
self._bias=bias
self._use_bn=use_bn
self._activation=activation
self._name=name
self._Build()
@tf.Module.with_name_scope
def _Build(self):
self._conv=tf.keras.layers.Conv2D(filters=self._filters,
kernel_size=self._kernel_size,
strides=self._strides,
padding=self._padding,
use_bias=self._bias,
name=self._name+"_conv")
if(self._use_bn==True):self._bn=tf.keras.layers.BatchNormalization(momentum=0.997,epsilon=1e-4,name=self._name+"_bn")
self._act=tf.keras.layers.Activation(self._activation,name=self._name+"_act")
@tf.Module.with_name_scope
def __call__(self,input_ts):
x=self._conv(input_ts)
if(self._use_bn==True):x=self._bn(x)
x=self._act(x)
output_ts=x
return output_ts
class DepthConvBN(tf.Module):
def __init__(self,
kernel_size,
strides=(1,1),
padding="same",
bias=False,
use_bn=True,
activation=None,
name="depthconvbn"):
super(DepthConvBN,self).__init__(name=name)
self._kernel_size=kernel_size
self._strides=strides
self._padding=padding
self._bias=bias
self._use_bn=use_bn
self._activation=activation
self._name=name
self._Build()
@tf.Module.with_name_scope
def _Build(self):
self._depthconv=tf.keras.layers.DepthwiseConv2D(self._kernel_size,
self._strides,
depth_multiplier=1,
padding=self._padding,
use_bias=self._bias,
name=self._name+"_depthconv")
if(self._use_bn==True):self._bn=tf.keras.layers.BatchNormalization(momentum=0.997,epsilon=1e-4,name=self._name+"_bn")
self._act=tf.keras.layers.Activation(self._activation,name=self._name+"_act")
@tf.Module.with_name_scope
def __call__(self,input_ts):
x=self._depthconv(input_ts)
if(self._use_bn==True):x=self._bn(x)
x=self._act(x)
output_ts=x
return output_ts
class SeparableConvBN(tf.Module):
def __init__(self,
filters,
kernel_size,
strides=(1,1),
padding="same",
bias=False,
use_bn=True,
activation=None,
name="spbconvbn"):
super(SeparableConvBN,self).__init__(name=name)
self._filters=filters
self._kernel_size=kernel_size
self._strides=strides
self._padding=padding
self._bias=bias
self._use_bn=use_bn
self._activation=activation
self._name=name
self._Build()
@tf.Module.with_name_scope
def _Build(self):
self._spbconv=tf.keras.layers.SeparableConv2D(self._filters,
self._kernel_size,
self._strides,
depth_multiplier=1,
padding=self._padding,
use_bias=self._bias,
name=self._name+"_spbconv")
if(self._use_bn==True):self._bn=tf.keras.layers.BatchNormalization(momentum=0.997,epsilon=1e-4,name=self._name+"_bn")
self._act=tf.keras.layers.Activation(self._activation,name=self._name+"_act")
@tf.Module.with_name_scope
def __call__(self,input_ts):
x=self._spbconv(input_ts)
if(self._use_bn==True):x=self._bn(x)
x=self._act(x)
output_ts=x
return output_ts
class AdaptUpsample(tf.Module):
def __init__(self,output_hw,name="adaptupsample"):
super(AdaptUpsample,self).__init__(name=name)
self._output_hw=output_hw
self._name=name
@tf.Module.with_name_scope
def __call__(self,input_ts):
output_ts=tf.image.resize(input_ts,self._output_hw,method=tf.image.ResizeMethod.BILINEAR)
return output_ts
class AdaptPooling(tf.Module):
def __init__(self,output_hw,name="adaptpooling"):
super(AdaptPooling,self).__init__(name=name)
self._output_hw=output_hw
self._name=name
@tf.Module.with_name_scope
def __call__(self,input_ts):
output_ts=tf.image.resize(input_ts,self._output_hw,method=tf.image.ResizeMethod.BILINEAR)
return output_ts
class AdaptScaling(tf.Module):
def __init__(self,output_hw,name="adaptscaling"):
super(AdaptScaling,self).__init__(name=name)
self._output_hw=output_hw
self._name=name
@tf.Module.with_name_scope
def __call__(self,input_ts):
output_ts=tf.image.resize(input_ts,self._output_hw,method=tf.image.ResizeMethod.BILINEAR)
return output_ts
class InputBIFusion(tf.Module):
def __init__(self,name="inputbufusion"):
super(InputBIFusion,self).__init__(name=name)
self._name=name
@tf.Module.with_name_scope
def _Build(self,btm_shape,top_shape):
btm_shape=np.array(btm_shape)
top_shape=np.array(top_shape)
target_shape=np.round((btm_shape+top_shape)/2)
self._btm_down=AdaptPooling(target_shape[0:2],name=self._name+"_btm_down")
self._top_up=AdaptUpsample(target_shape[0:2],name=self._name+"_top_up")
self._conv=ConvBN(filters=top_shape[2],kernel_size=(3,3),activation=mish,name=self._name+"_conv")
@tf.Module.with_name_scope
def __call__(self,btm_ts,top_ts):
btm_shape=btm_ts.get_shape().as_list()[1:]
top_shape=top_ts.get_shape().as_list()[1:]
self._Build(btm_shape,top_shape)
btm_down=self._btm_down(btm_ts)
top_up=self._top_up(top_ts)
x=btm_down+top_up
output_ts=self._conv(x)
return output_ts
class LayerExpansion(tf.Module):
def __init__(self,out_layers=3,name="layerexpansion"):
super(LayerExpansion,self).__init__(name=name)
self._out_layers=out_layers
self._name=name
self._Build()
@tf.Module.with_name_scope
def _Build(self):
self._bifusion_list=[]
for i in range(self._out_layers-3):
self._bifusion_list.append(InputBIFusion(name=self._name+"_inbifusion"+str(i)))
@tf.Module.with_name_scope
def __call__(self,input_ts_list):
l1,l2,l3=input_ts_list
if(self._out_layers==3):
return [l1,l2,l3]
elif(self._out_layers==5):
l1l2=self._bifusion_list[0](l1,l2)
l2l3=self._bifusion_list[1](l2,l3)
return [l1,l1l2,l2,l2l3,l3]
elif(self._out_layers==9):
l1l2=self._bifusion_list[0](l1,l2)
l2l3=self._bifusion_list[1](l2,l3)
l1l1l2=self._bifusion_list[2](l1,l1l2)
l1l2l2=self._bifusion_list[3](l1l2,l2)
l2l2l3=self._bifusion_list[4](l2,l2l3)
l2l3l3=self._bifusion_list[5](l2l3,l3)
return [l1,l1l1l2,l1l2,l1l2l2,l2,l2l2l3,l2l3,l2l3l3,l3]
class BIFusion(tf.Module):
def __init__(self,name="bufusion"):
super(BIFusion,self).__init__(name=name)
self._name=name
@tf.Module.with_name_scope
def _Build(self,mid_shape):
target_shape=mid_shape
self._btm_down=AdaptPooling(target_shape[0:2],name=self._name+"_btm_down")
self._top_up=AdaptUpsample(target_shape[0:2],name=self._name+"_top_up")
self._conv=ConvBN(filters=target_shape[2],kernel_size=(3,3),activation=mish,name=self._name+"_conv")
@tf.Module.with_name_scope
def __call__(self,btm_ts,mid_ts,top_ts):
mid_shape=mid_ts.get_shape().as_list()[1:]
self._Build(mid_shape)
out_ts=mid_ts
if(btm_ts!=None):
btm_down=self._btm_down(btm_ts)
out_ts=out_ts+btm_down
if(top_ts!=None):
top_up=self._top_up(top_ts)
out_ts=out_ts+top_up
output_ts=self._conv(out_ts)
return output_ts
class FusionPhase1(tf.Module):
def __init__(self,name="fusionphase1"):
super(FusionPhase1,self).__init__(name=name)
self._name=name
@tf.Module.with_name_scope
def _Build(self,input_ts_len):
self._bidusion_list=[]
for i in range(input_ts_len-1):
self._bidusion_list.append(BIFusion(name=self._name+"_bifusion"+str(i)))
@tf.Module.with_name_scope
def __call__(self,input_ts_list):
input_ts_len=len(input_ts_list)
self._Build(input_ts_len)
if(input_ts_len==3):
l1,l2,l3=input_ts_list
l2=self._bidusion_list[0](l1,l2,l3)
return [l1,l2,l3]
elif(input_ts_len==5):
l1,l2,l3,l4,l5=input_ts_list
l2=self._bidusion_list[0](l1,l2,l3)
l4=self._bidusion_list[1](l3,l4,l5)
return [l1,l2,l3,l4,l5]
elif(input_ts_len==9):
l1,l2,l3,l4,l5,l6,l7,l8,l9=input_ts_list
l2=self._bidusion_list[0](l1,l2,l3)
l4=self._bidusion_list[1](l3,l4,l5)
l6=self._bidusion_list[2](l4,l6,l7)
l8=self._bidusion_list[3](l7,l8,l9)
return [l1,l2,l3,l4,l5,l6,l7,l8,l9]
class FusionPhase2(tf.Module):
def __init__(self,name="fusionphase2"):
super(FusionPhase2,self).__init__(name=name)
self._name=name
@tf.Module.with_name_scope
def _Build(self,input_ts_len):
self._bidusion_list=[]
for i in range(input_ts_len-1):
self._bidusion_list.append(BIFusion(name=self._name+"_bifusion"+str(i)))
@tf.Module.with_name_scope
def __call__(self,input_ts_list):
input_ts_len=len(input_ts_list)
self._Build(input_ts_len)
if(input_ts_len==3):
l1,l2,l3=input_ts_list
l1=self._bidusion_list[0](None,l1,l2)
l3=self._bidusion_list[1](l2,l3,None)
return [l1,l2,l3]
elif(input_ts_len==5):
l1,l2,l3,l4,l5=input_ts_list
l1=self._bidusion_list[0](None,l1,l2)
l3=self._bidusion_list[1](l2,l3,l4)
l5=self._bidusion_list[2](l4,l5,None)
return [l1,l2,l3,l4,l5]
elif(input_ts_len==9):
l1,l2,l3,l4,l5,l6,l7,l8,l9=input_ts_list
l1=self._bidusion_list[0](None,l1,l2)
l3=self._bidusion_list[1](l2,l3,l4)
l5=self._bidusion_list[2](l4,l5,l6)
l7=self._bidusion_list[3](l6,l7,l8)
l9=self._bidusion_list[4](l8,l9,None)
return [l1,l2,l3,l4,l5,l6,l7,l8,l9]
class CSLFPN(tf.Module):
def __init__(self,repeat=3,name="cslfpn"):
super(CSLFPN,self).__init__(name=name)
self._repeat=repeat
self._name=name
self._Build()
@tf.Module.with_name_scope
def _Build(self):
self._fusion_phase1_list=[]
self._fusion_phase2_list=[]
for i in range(self._repeat):
self._fusion_phase1_list.append(FusionPhase1(name=self._name+"_phase1_"+str(i)))
self._fusion_phase2_list.append(FusionPhase2(name=self._name+"_phase2_"+str(i)))
@tf.Module.with_name_scope
def __call__(self,input_ts_list):
out_ts_list=input_ts_list
for i in range(self._repeat):
last_out_ts_list=out_ts_list.copy()
out_ts_list=self._fusion_phase1_list[i](out_ts_list)
out_ts_list=self._fusion_phase2_list[i](out_ts_list)
for ts_idx in range(len(out_ts_list)):
out_ts_list[ts_idx]=out_ts_list[ts_idx]+last_out_ts_list[ts_idx]
return out_ts_list
class VanillaFPN(tf.Module):
def __init__(self,name="vanillafPN"):
super(VanillaFPN,self).__init__(name=name)
self._name=name
@tf.Module.with_name_scope
def _Build(self,l1_shape,l2_shape,l3_shape,l4_shape,l5_shape):
l1_shape=np.array(l1_shape)
l2_shape=np.array(l2_shape)
l3_shape=np.array(l3_shape)
l4_shape=np.array(l4_shape)
l5_shape=np.array(l5_shape)
self._l2_up=AdaptUpsample(l1_shape[0:2],name=self._name+"_l2_up")
self._l3_up=AdaptUpsample(l2_shape[0:2],name=self._name+"_l3_up")
self._l4_up=AdaptUpsample(l3_shape[0:2],name=self._name+"_l4_up")
self._l5_up=AdaptUpsample(l4_shape[0:2],name=self._name+"_l5_up")
self._l1_conv=ConvBN(filters=l1_shape[2],kernel_size=(3,3),activation=mish,name=self._name+"_l1_conv")
self._l2_conv=ConvBN(filters=l2_shape[2],kernel_size=(3,3),activation=mish,name=self._name+"_l2_conv")
self._l3_conv=ConvBN(filters=l3_shape[2],kernel_size=(3,3),activation=mish,name=self._name+"_l3_conv")
self._l4_conv=ConvBN(filters=l4_shape[2],kernel_size=(3,3),activation=mish,name=self._name+"_l4_conv")
self._l5_conv=ConvBN(filters=l5_shape[2],kernel_size=(3,3),activation=mish,name=self._name+"_l5_conv")
@tf.Module.with_name_scope
def __call__(self,input_ts_list):
l1,l2,l3,l4,l5=input_ts_list
l1_shape=l1.get_shape().as_list()[1:]
l2_shape=l2.get_shape().as_list()[1:]
l3_shape=l3.get_shape().as_list()[1:]
l4_shape=l4.get_shape().as_list()[1:]
l5_shape=l5.get_shape().as_list()[1:]
self._Build(l1_shape,l2_shape,l3_shape,l4_shape,l5_shape)
l4=l4+self._l5_up(l5)
l3=l3+self._l4_up(l4)
l2=l2+self._l3_up(l3)
l1=l1+self._l2_up(l2)
l1=self._l1_conv(l1)
l2=self._l2_conv(l2)
l3=self._l3_conv(l3)
l4=self._l4_conv(l4)
l5=self._l5_conv(l5)
out_ts_list=[l1,l2,l3,l4,l5]
return out_ts_list
class InvertedResidual(tf.Module):
def __init__(self,filters,t,kernel_size=(3,3),strides=(1,1),first_layer=False,name="invertedresidual"):
super(InvertedResidual,self).__init__(name=name)
self._filters=filters
self._t=t
self._kernel_size=kernel_size
self._strides=strides
self._first_layer=first_layer
self._name=name
@tf.Module.with_name_scope
def _Build(self,input_channel):
if(self._first_layer==True):input_channel=self._filters
tchannel=int(input_channel*self._t)
if(self._first_layer==True):
self._convbn_1=ConvBN(tchannel,self._kernel_size,(2,2),name=self._name+"_convbn_1")
else:
self._convbn_1=ConvBN(tchannel,(1,1),(1,1),name=self._name+"_convbn_1")
self._depthconv=tf.keras.layers.DepthwiseConv2D(self._kernel_size,
self._strides,
depth_multiplier=1,
padding="same",
use_bias=False,
name=self._name+"_depthconv")
self._bn=tf.keras.layers.BatchNormalization(name=self._name+"_bn")
self._relu6=tf.keras.layers.ReLU(max_value=6.0,name=self._name+"_relu")
self._convbn_2=ConvBN(self._filters,(1,1),(1,1),use_relu=False,name=self._name+"_convbn_2")
@tf.Module.with_name_scope
def __call__(self,input_ts):
input_ch=input_ts.get_shape().as_list()[3]
self._Build(input_ch)
x=self._convbn_1(input_ts)
x=self._depthconv(x)
x=self._bn(x)
x=self._relu6(x)
x=self._convbn_2(x)
if(self._strides==(1,1) and self._filters==input_ch):
x=(input_ts+x)
output_ts=x
return output_ts