-
Notifications
You must be signed in to change notification settings - Fork 0
/
aspp.py
132 lines (107 loc) · 5.11 KB
/
aspp.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
"""ASPP block."""
import tensorflow as tf
import numpy as np
LAYERS = tf.keras.layers
L2 = tf.keras.regularizers.l2
def sep_conv_bn_relu(inputs,
filters=256,
kernel_size=3,
strides=1,
dilation_rate=1,
weight_decay=1e-5,
batch_normalization=False,
name="sepconv_bn"):
"""An separable convolution with batch_normalization and relu
activation after depthwise and pointwise convolutions"""
with tf.name_scope(name):
result = LAYERS.DepthwiseConv2D(kernel_size=kernel_size,
strides=strides,
padding='same',
depth_multiplier=1,
use_bias=False,
depthwise_regularizer=L2(weight_decay),
dilation_rate=dilation_rate)(inputs)
if batch_normalization:
result = LAYERS.BatchNormalization()(result)
result = LAYERS.Activation('relu')(result)
result = LAYERS.Conv2D(filters=filters,
kernel_size=1,
use_bias=False,
kernel_regularizer=L2(weight_decay))(result)
if batch_normalization:
result = LAYERS.BatchNormalization()(result)
result = LAYERS.Activation('relu')(result)
return result
def aspp(inputs, input_shape, weight_decay=1e-5, batch_normalization=False):
"""the DeepLabv3 ASPP module"""
output_stride = 32
# Employ a 1x1 convolution.
aspp_1x1 = LAYERS.Conv2D(filters=256,
kernel_size=1,
padding='same',
use_bias=False,
kernel_regularizer=L2(weight_decay),
name='aspp/1x1/con2d')(inputs)
if batch_normalization:
aspp_1x1 = LAYERS.BatchNormalization(name='aspp/1x1/bn',
epsilon=1e-5)(aspp_1x1)
aspp_1x1 = LAYERS.Activation('relu', name='aspp/1x1/relu')(aspp_1x1)
# Employ 3x3 convolutions with atrous rate = 6
aspp_3x3_r6 = sep_conv_bn_relu(inputs=inputs,
filters=256,
kernel_size=3,
strides=1,
dilation_rate=6,
weight_decay=weight_decay,
batch_normalization=batch_normalization,
name='aspp/3x3_r6')
# Employ 3x3 convolutions with atrous rate = 12
aspp_3x3_r12 = sep_conv_bn_relu(inputs=inputs,
filters=256,
kernel_size=3,
strides=1,
dilation_rate=12,
weight_decay=weight_decay,
batch_normalization=batch_normalization,
name='aspp/3x3_r12')
# Employ 3x3 convolutions with atrous rate = 18
aspp_3x3_r18 = sep_conv_bn_relu(inputs=inputs,
filters=256,
kernel_size=3,
strides=1,
dilation_rate=18,
weight_decay=weight_decay,
batch_normalization=batch_normalization,
name='aspp/3x3_r18')
# Image Feature branch
pool_height = int(np.ceil(input_shape[0] / output_stride))
pool_width = int(np.ceil(input_shape[1] / output_stride))
aspp_image_features = LAYERS.AveragePooling2D(
pool_size=[pool_height, pool_width],
name='aspp/image_features/average_pooling')(inputs)
aspp_image_features = LAYERS.Conv2D(
filters=256,
kernel_size=1,
padding='same',
use_bias=False,
kernel_regularizer=L2(weight_decay),
name='aspp/image_features/conv2d')(aspp_image_features)
if batch_normalization:
aspp_image_features = LAYERS.BatchNormalization(
name='aspp/image_features/image_pooling_BN',
epsilon=1e-5)(aspp_image_features)
aspp_image_features = LAYERS.Activation(
'relu', name='aspp/image_features/relu')(aspp_image_features)
aspp_image_features = LAYERS.UpSampling2D(
(pool_height, pool_width),
name='aspp/image_features/up_sampling')(aspp_image_features)
# concatenate ASPP branches & project
result = LAYERS.Concatenate(name='aspp/concat')([
aspp_1x1, aspp_3x3_r6, aspp_3x3_r12, aspp_3x3_r18, aspp_image_features
])
result = LAYERS.Conv2D(filters=256,
kernel_size=1,
name='aspp/conv_1x1',
kernel_regularizer=L2(weight_decay))(result)
result = LAYERS.Dropout(rate=0.9, name='aspp/dropout')(result)
return result