-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRMFNet.py
130 lines (104 loc) · 3.36 KB
/
RMFNet.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
from tensorflow.keras.layers import (
Input,
Conv2D,
BatchNormalization,
ReLU,
MaxPooling2D,
Concatenate,
GlobalMaxPooling2D,
Dropout,
Dense,
Lambda,
Reshape,
Activation,
Multiply,
)
from tensorflow.keras.models import Model
from tensorflow.keras import backend as K
def block(inputs, filters):
a = Conv2D(filters, 3, padding='same')(inputs)
a = BatchNormalization()(a)
a = ReLU()(a)
b = Conv2D(filters, 3, padding='same')(a)
b = BatchNormalization()(b)
b = ReLU()(b)
c = Conv2D(filters, 3, padding='same')(b)
c = BatchNormalization()(c)
c = ReLU()(c)
d = Conv2D(filters, 3, padding='same')(c)
d = BatchNormalization()(d)
d = ReLU()(d)
mid = Concatenate()([a, b, c, d])
mid = Conv2D(2 * filters, 1, padding='same')(mid)
mid = BatchNormalization()(mid)
mid = ReLU()(mid)
x = Conv2D(filters * 2, 1)(inputs)
x = BatchNormalization()(x)
x = ReLU()(x)
x = Add()([mid, x])
y = Conv2D(filters * 2, 1)(x)
y = BatchNormalization()(y)
y = ReLU()(y)
return y
def Global_attention_block(C_A):
x = Lambda(lambda x: K.mean(x, axis=-1, keepdims=True))(C_A)
y = Lambda(lambda x: K.max(x, axis=-1, keepdims=True))(C_A)
x = Concatenate()([x, y])
x = Activation('relu')(x)
x = Conv2D(1, 1, padding='same')(x)
x = Activation('sigmoid')(x)
S_A = Multiply()([x, C_A])
return S_A
def self_attention(inp):
shp = inp.shape
a = Conv2D(shp[3] // 8, 1, padding='same')(inp)
a = Activation('relu')(a)
b = Conv2D(shp[3] // 8, 1, padding='same')(inp)
b = Activation('relu')(b)
c = Conv2D(shp[3] // 8, 1, padding='same')(inp)
c = Activation('relu')(c)
a = Reshape((shp[1] * shp[2], shp[3] // 8))(a)
b = Reshape((shp[1] * shp[2], shp[3] // 8))(b)
b = K.permute_dimensions(b, (0, 2, 1))
c = Reshape((shp[1] * shp[2], shp[3] // 8))(c)
inter = K.batch_dot(a, b)
inter = Activation('softmax')(inter)
out = K.batch_dot(inter, c)
out = Reshape((shp[1], shp[2], shp[3] // 8))(out)
out = Conv2D(shp[3], 1, padding='same')(out)
out = Activation('relu')(out)
return out
def channel_attention(inputs):
shape = K.int_shape(inputs)
x = MaxPooling2D(pool_size=(shape[1], shape[2]))(inputs)
x = Conv2D(shape[3] // 8, 1, padding='same', kernel_initializer='he_normal', use_bias=False)(x)
x = Activation('relu')(x)
x = Conv2D(shape[3], 1, padding='same', kernel_initializer='he_normal', use_bias=False)(x)
x = Activation('sigmoid')(x)
x = Multiply()([x, inputs])
return x
def load_model():
K.clear_session()
inputs = Input(shape=(224, 224, 3))
x = Conv2D(16, 3, padding='same')(inputs)
x = BatchNormalization()(x)
x = ReLU()(x)
x = MaxPooling2D()(x)
x = Conv2D(16, 3, padding='same')(x)
x = BatchNormalization()(x)
x = ReLU()(x)
x = MaxPooling2D()(x)
a1 = block(x, 32)
x = MaxPooling2D()(a1)
a2 = block(x, 64)
x = MaxPooling2D()(a2)
a3 = block(x, 128)
a31 = self_attention(a3)
a32 = Global_attention_block(a3)
a3 = Add()([a31, a32])
x = channel_attention(a3)
x = GlobalMaxPooling2D()(x)
x = Dropout(0.5)(x)
x = Dense(2, activation='softmax')(x)
model = Model(inputs=inputs, outputs=x)
return model