forked from raymon-tian/hourglass-facekeypoints-detection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
attention.py
29 lines (23 loc) · 1.24 KB
/
attention.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
def CBAM(input, reduction):
"""
@Convolutional Block Attention Module
"""
_, width, height, channel = input.get_shape() # (B, W, H, C)
# channel attention
x_mean = tf.reduce_mean(input, axis=(1, 2), keepdims=True) # (B, 1, 1, C)
x_mean = tf.layers.conv2d(x_mean, channel // reduction, 1, activation=tf.nn.relu, name='CA1') # (B, 1, 1, C // r)
x_mean = tf.layers.conv2d(x_mean, channel, 1, name='CA2') # (B, 1, 1, C)
x_max = tf.reduce_max(input, axis=(1, 2), keepdims=True) # (B, 1, 1, C)
x_max = tf.layers.conv2d(x_max, channel // reduction, 1, activation=tf.nn.relu, name='CA1', reuse=True)
# (B, 1, 1, C // r)
x_max = tf.layers.conv2d(x_max, channel, 1, name='CA2', reuse=True) # (B, 1, 1, C)
x = tf.add(x_mean, x_max) # (B, 1, 1, C)
x = tf.nn.sigmoid(x) # (B, 1, 1, C)
x = tf.multiply(input, x) # (B, W, H, C)
# spatial attention
y_mean = tf.reduce_mean(x, axis=3, keepdims=True) # (B, W, H, 1)
y_max = tf.reduce_max(x, axis=3, keepdims=True) # (B, W, H, 1)
y = tf.concat([y_mean, y_max], axis=-1) # (B, W, H, 2)
y = tf.layers.conv2d(y, 1, 7, padding='same', activation=tf.nn.sigmoid) # (B, W, H, 1)
y = tf.multiply(x, y) # (B, W, H, C)
return y