-
Notifications
You must be signed in to change notification settings - Fork 24
/
tf_2_0_fast_scnn.py
137 lines (88 loc) · 4.59 KB
/
tf_2_0_fast_scnn.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
# -*- coding: utf-8 -*-
"""TF 2.0 Fast-SCNN.ipynb
"""
# !pip install tensorflow-gpu==2.0.0-alpha0
import tensorflow as tf
"""
# Model Architecture
#### Custom function for conv2d: conv_block
"""
def conv_block(inputs, conv_type, kernel, kernel_size, strides, padding='same', relu=True):
if(conv_type == 'ds'):
x = tf.keras.layers.SeparableConv2D(kernel, kernel_size, padding=padding, strides = strides)(inputs)
else:
x = tf.keras.layers.Conv2D(kernel, kernel_size, padding=padding, strides = strides)(inputs)
x = tf.keras.layers.BatchNormalization()(x)
if (relu):
x = tf.keras.activations.relu(x)
return x
"""## Step 1: Learning to DownSample"""
# Input Layer
input_layer = tf.keras.layers.Input(shape=(2048, 1024, 3), name = 'input_layer')
lds_layer = conv_block(input_layer, 'conv', 32, (3, 3), strides = (2, 2))
lds_layer = conv_block(lds_layer, 'ds', 48, (3, 3), strides = (2, 2))
lds_layer = conv_block(lds_layer, 'ds', 64, (3, 3), strides = (2, 2))
"""## Step 2: Global Feature Extractor
#### residual custom method
"""
def _res_bottleneck(inputs, filters, kernel, t, s, r=False):
tchannel = tf.keras.backend.int_shape(inputs)[-1] * t
x = conv_block(inputs, 'conv', tchannel, (1, 1), strides=(1, 1))
x = tf.keras.layers.DepthwiseConv2D(kernel, strides=(s, s), depth_multiplier=1, padding='same')(x)
x = tf.keras.layers.BatchNormalization()(x)
x = tf.keras.activations.relu(x)
x = conv_block(x, 'conv', filters, (1, 1), strides=(1, 1), padding='same', relu=False)
if r:
x = tf.keras.layers.add([x, inputs])
return x
"""#### Bottleneck custom method"""
def bottleneck_block(inputs, filters, kernel, t, strides, n):
x = _res_bottleneck(inputs, filters, kernel, t, strides)
for i in range(1, n):
x = _res_bottleneck(x, filters, kernel, t, 1, True)
return x
"""#### PPM Method"""
def pyramid_pooling_block(input_tensor, bin_sizes):
concat_list = [input_tensor]
w = 64
h = 32
for bin_size in bin_sizes:
x = tf.keras.layers.AveragePooling2D(pool_size=(w//bin_size, h//bin_size), strides=(w//bin_size, h//bin_size))(input_tensor)
x = tf.keras.layers.Conv2D(128, 3, 2, padding='same')(x)
x = tf.keras.layers.Lambda(lambda x: tf.image.resize(x, (w,h)))(x)
concat_list.append(x)
return tf.keras.layers.concatenate(concat_list)
"""#### Assembling all the methods"""
gfe_layer = bottleneck_block(lds_layer, 64, (3, 3), t=6, strides=2, n=3)
gfe_layer = bottleneck_block(gfe_layer, 96, (3, 3), t=6, strides=2, n=3)
gfe_layer = bottleneck_block(gfe_layer, 128, (3, 3), t=6, strides=1, n=3)
gfe_layer = pyramid_pooling_block(gfe_layer, [2,4,6,8])
"""## Step 3: Feature Fusion"""
ff_layer1 = conv_block(lds_layer, 'conv', 128, (1,1), padding='same', strides= (1,1), relu=False)
ff_layer2 = tf.keras.layers.UpSampling2D((4, 4))(gfe_layer)
ff_layer2 = tf.keras.layers.SeparableConv2D(128, (3, 3), padding='same', strides = (1, 1), activation=None, dilation_rate=(4, 4))(ff_layer2)
# old approach with DepthWiseConv2d
#ff_layer2 = tf.keras.layers.DepthwiseConv2D((3,3), strides=(1, 1), depth_multiplier=1, padding='same')(ff_layer2)
ff_layer2 = tf.keras.layers.BatchNormalization()(ff_layer2)
ff_layer2 = tf.keras.activations.relu(ff_layer2)
ff_layer2 = tf.keras.layers.Conv2D(128, 1, 1, padding='same', activation=None)(ff_layer2)
ff_final = tf.keras.layers.add([ff_layer1, ff_layer2])
ff_final = tf.keras.layers.BatchNormalization()(ff_final)
ff_final = tf.keras.activations.relu(ff_final)
"""## Step 4: Classifier"""
classifier = tf.keras.layers.SeparableConv2D(128, (3, 3), padding='same', strides = (1, 1), name = 'DSConv1_classifier')(ff_final)
classifier = tf.keras.layers.BatchNormalization()(classifier)
classifier = tf.keras.activations.relu(classifier)
classifier = tf.keras.layers.SeparableConv2D(128, (3, 3), padding='same', strides = (1, 1), name = 'DSConv2_classifier')(classifier)
classifier = tf.keras.layers.BatchNormalization()(classifier)
classifier = tf.keras.activations.relu(classifier)
classifier = conv_block(classifier, 'conv', 19, (1, 1), strides=(1, 1), padding='same', relu=False)
classifier = tf.keras.layers.Dropout(0.3)(classifier)
classifier = tf.keras.layers.UpSampling2D((8, 8))(classifier)
classifier = tf.keras.activations.softmax(classifier)
"""## Model Compilation"""
fast_scnn = tf.keras.Model(inputs = input_layer , outputs = classifier, name = 'Fast_SCNN')
optimizer = tf.keras.optimizers.SGD(momentum=0.9, lr=0.045)
fast_scnn.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])
fast_scnn.summary()
tf.keras.utils.plot_model(fast_scnn, show_layer_names=True, show_shapes=True)