forked from rykov8/ssd_keras
-
Notifications
You must be signed in to change notification settings - Fork 85
/
ssd_model_separable.py
92 lines (71 loc) · 3.29 KB
/
ssd_model_separable.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
import keras.backend as K
from keras.layers import Activation
from keras.layers import Conv2D, SeparableConv2D, DepthwiseConv2D
from keras.layers import MaxPooling2D
from keras.layers import BatchNormalization
from keras.layers import concatenate
from utils.layers import leaky_relu
def bn_acti_conv(x, filters, kernel_size=1, stride=1, padding='same', activation='relu'):
x = BatchNormalization(scale=True)(x)
x = Activation(activation)(x)
if kernel_size > 1:
x = SeparableConv2D(filters, kernel_size, depth_multiplier=1, strides=stride, padding=padding)(x)
else:
x = Conv2D(filters, kernel_size, strides=stride, padding=padding)(x)
return x
def dense_block(x, n, growth_rate, width=4, activation='relu'):
input_shape = K.int_shape(x)
c = input_shape[3]
for i in range(n):
x1 = x
x2 = bn_acti_conv(x, growth_rate*width, 1, 1, activation=activation)
x2 = bn_acti_conv(x2, growth_rate, 3, 1, activation=activation)
x = concatenate([x1, x2], axis=3)
c += growth_rate
return x
def downsampling_block(x, filters, width, padding='same', activation='relu'):
x = BatchNormalization(scale=True)(x)
x = Activation(activation)(x)
x1 = MaxPooling2D(pool_size=2, strides=2, padding=padding)(x)
x2 = DepthwiseConv2D(3, depth_multiplier=1, strides=2, padding=padding)(x)
x = concatenate([x1, x2], axis=3)
x = Conv2D(filters, 1, strides=1)(x)
return x
def ssd512_dense_separable_body(x, activation='relu'):
# used for SegLink and TextBoxes++ variantes with separable convolution
if activation == 'leaky_relu':
activation = leaky_relu
growth_rate = 48
compressed_features = 224
source_layers = []
x = SeparableConv2D(96, 3, depth_multiplier=32, strides=2, padding='same')(x)
x = BatchNormalization(scale=True)(x)
x = Activation(activation)(x)
x = SeparableConv2D(96, 3, depth_multiplier=1, strides=1, padding='same')(x)
x = BatchNormalization(scale=True)(x)
x = Activation(activation)(x)
x = SeparableConv2D(96, 3, depth_multiplier=1, strides=1, padding='same')(x)
x = BatchNormalization(scale=True)(x)
x = Activation(activation)(x)
x = MaxPooling2D(pool_size=2, strides=2)(x)
x = dense_block(x, 6, growth_rate, 4, activation)
x = bn_acti_conv(x, compressed_features, 1, 1, activation=activation)
x = MaxPooling2D(pool_size=2, strides=2, padding='same')(x)
x = dense_block(x, 6, growth_rate, 4, activation)
x = bn_acti_conv(x, compressed_features, 1, 1, activation=activation)
source_layers.append(x) # 64x64
x = MaxPooling2D(pool_size=2, strides=2)(x)
x = dense_block(x, 6, growth_rate, 4, activation)
x = bn_acti_conv(x, compressed_features, 1, 1, activation=activation)
source_layers.append(x) # 32x32
x = downsampling_block(x, 192, 1, activation=activation)
source_layers.append(x) # 16x16
x = downsampling_block(x, 160, 1, activation=activation)
source_layers.append(x) # 8x8
x = downsampling_block(x, 128, 1, activation=activation)
source_layers.append(x) # 4x4
x = downsampling_block(x, 96, 1, activation=activation)
source_layers.append(x) # 2x2
x = downsampling_block(x, 64, 1, activation=activation)
source_layers.append(x) # 1x1
return source_layers