-
Notifications
You must be signed in to change notification settings - Fork 5
/
keras_model.py
70 lines (62 loc) · 3.25 KB
/
keras_model.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
import tensorflow.keras as keras
from tensorflow.keras.layers import Conv2D, Input, MaxPool2D, add, Flatten, Dense, DepthwiseConv2D
def PEPXModel(input_tensor, filters, name):
x = Conv2D(filters=filters, kernel_size=(1, 1), activation='relu', name=name + 'FP')(input_tensor)
x = Conv2D(filters=filters, kernel_size=(1, 1), activation='relu', name=name + 'Expansion')(x)
x = DepthwiseConv2D(kernel_size=(3, 3), activation='relu', padding='same', name=name + 'DWConv3_3')(x)
x = Conv2D(filters=filters, kernel_size=(1, 1), activation='relu', name=name + 'SP')(x)
x = Conv2D(filters=filters, kernel_size=(1, 1), activation='relu', name=name + 'Extension')(x)
return x
def keras_model_build(input_size=(224, 224, 3)):
# 输入
input = Input(shape=input_size, name='input')
x = Conv2D(input_shape=input_size, filters=64, kernel_size=(7, 7), activation='relu', padding='same',
strides=(2, 2))(input)
x = MaxPool2D(pool_size=(2, 2))(x)
# PEPX1_Conv1x1
p_1_y = Conv2D(256, (1, 1), padding='same', activation='relu', name='PEPX1_Conv')(x)
# Stage1结构
y_1_1 = PEPXModel(x, 256, 'PEPX1.1')
y_1_2 = PEPXModel(add([y_1_1, p_1_y]), 256, 'PEPX1.2')
y_1_3 = PEPXModel(add([y_1_1, y_1_2, p_1_y]), 256, 'PEPX1.3')
# PEPX2_Conv1x1
p_2_y = Conv2D(512, (1, 1), padding='same', activation='relu', name='PEPX2_Conv')(add([p_1_y, y_1_1, y_1_2, y_1_3]))
p_2_y = MaxPool2D(pool_size=(2, 2))(p_2_y)
# Stage2结构
y_2_1 = PEPXModel(add([y_1_3, y_1_2, y_1_1, p_1_y]), 512, 'PEPX2.1')
y_2_1 = MaxPool2D(pool_size=(2, 2))(y_2_1)
y_2_2 = PEPXModel(add([y_2_1, p_2_y]), 512, 'PEPX2.2')
y_2_3 = PEPXModel(add([y_2_1, y_2_2, p_2_y]), 512, 'PEPX2.3')
y_2_4 = PEPXModel(add([y_2_1, y_2_2, y_2_3, p_2_y]), 512, 'PEPX2.4')
# PEPX3_Conv1x1
p_3_y = Conv2D(1024, (1, 1), padding='same', activation='relu', name='PEPX3_Conv')(
add([p_2_y, y_2_1, y_2_2, y_2_3, y_2_4])
)
p_3_y = MaxPool2D(pool_size=(2, 2))(p_3_y)
# Stage3结构
y_3_1 = PEPXModel(add([y_2_1, y_2_2, y_2_3, y_2_4, p_2_y]), 1024, 'PEPX3.1')
y_3_1 = MaxPool2D(pool_size=(2, 2))(y_3_1)
y_3_2 = PEPXModel(y_3_1, 1024, 'PEPX3.2')
y_3_3 = PEPXModel(add([y_3_1, y_3_2]), 1024, 'PEPX3.3')
y_3_4 = PEPXModel(add([y_3_1, y_3_2, y_3_3]), 1024, 'PEPX3.4')
y_3_5 = PEPXModel(add([y_3_1, y_3_2, y_3_3, y_3_4]), 1024, 'PEPX3.5')
y_3_6 = PEPXModel(add([y_3_1, y_3_2, y_3_3, y_3_4, y_3_5]), 1024, 'PEPX3.6')
# PEPX4_Conv1x1
p_4_y = Conv2D(2048, (1, 1), padding='same', activation='relu', name='PEPX4_Conv1')(
add([p_3_y, y_3_1, y_3_2, y_3_3, y_3_4, y_3_5, y_3_6])
)
p_4_y = MaxPool2D(pool_size=(2, 2))(p_4_y)
# Stage4结构
y_4_1 = PEPXModel(add([y_3_1, y_3_2, y_3_3, y_3_4, y_3_5, y_3_6, p_3_y]), 2048, 'PEPX4.1')
y_4_1 = MaxPool2D(pool_size=(2, 2))(y_4_1)
y_4_2 = PEPXModel(add([y_4_1, p_4_y]), 2048, 'PEPX4.2')
y_4_3 = PEPXModel(add([y_4_1, y_4_2, p_4_y]), 2048, 'PEPX4.3')
# FC
fla = Flatten()(add([y_4_1, y_4_2, y_4_3, p_4_y]))
d1 = Dense(1024, activation='relu')(fla)
d2 = Dense(256, activation='relu')(d1)
output = Dense(4, activation='softmax')(d2)
return keras.models.Model(input, output)
if __name__ == '__main__':
model = keras_model_build()
model.summary()