-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattentionModule.py
82 lines (56 loc) · 3.75 KB
/
attentionModule.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
import tensorflow as tf
from keras.layers.convolutional import UpSampling2D
from BaseLayerFile import ResidualBlock
class AttentionModule(object):
def __init__(self):
self.p = 1
self.t = 2
self.r = 3
self.residual_block = ResidualBlock()
def f_prop(self, input, input_channels, scope="attention_module", is_training=True):
with tf.variable_scope(scope):
with tf.variable_scope("first_residual_blocks"):
for i in range(self.p):
input = self.residual_block.forward_prop(input, input_channels, scope="num_blocks_{}".format(i), is_training=is_training)
with tf.variable_scope("trunk_branch"):
output_trunk = input
for i in range(self.t):
output_trunk = self.residual_block.forward_prop(output_trunk, input_channels, scope="num_blocks_{}".format(i), is_training=is_training)
with tf.variable_scope("soft_mask_branch"):
with tf.variable_scope("down_sampling_1"):
# max pooling
filter_ = [1, 2, 2, 1]
output_soft_mask = tf.nn.max_pool(input, ksize=filter_, strides=filter_, padding='SAME')
for i in range(self.r):
output_soft_mask = self.residual_block.forward_prop(output_soft_mask, input_channels, scope="num_blocks_{}".format(i), is_training=is_training)
with tf.variable_scope("skip_connection"):
output_skip_connection = self.residual_block.forward_prop(output_soft_mask, input_channels, is_training=is_training)
with tf.variable_scope("down_sampling_2"):
# max pooling
filter_ = [1, 2, 2, 1]
output_soft_mask = tf.nn.max_pool(output_soft_mask, ksize=filter_, strides=filter_, padding='SAME')
for i in range(self.r):
output_soft_mask = self.residual_block.forward_prop(output_soft_mask, input_channels, scope="num_blocks_{}".format(i), is_training=is_training)
with tf.variable_scope("up_sampling_1"):
for i in range(self.r):
output_soft_mask = self.residual_block.forward_prop(output_soft_mask, input_channels, scope="num_blocks_{}".format(i), is_training=is_training)
# interpolation
output_soft_mask = UpSampling2D([2, 2])(output_soft_mask)
# add skip connection
output_soft_mask += output_skip_connection
with tf.variable_scope("up_sampling_2"):
for i in range(self.r):
output_soft_mask = self.residual_block.forward_prop(output_soft_mask, input_channels, scope="num_blocks_{}".format(i), is_training=is_training)
# interpolation
output_soft_mask = UpSampling2D([2, 2])(output_soft_mask)
with tf.variable_scope("output"):
output_soft_mask = tf.layers.conv2d(output_soft_mask, filters=input_channels, kernel_size=1)
output_soft_mask = tf.layers.conv2d(output_soft_mask, filters=input_channels, kernel_size=1)
# sigmoid
output_soft_mask = tf.nn.sigmoid(output_soft_mask)
with tf.variable_scope("attention"):
output = (1 + output_soft_mask) * output_trunk
with tf.variable_scope("last_residual_blocks"):
for i in range(self.p):
output = self.residual_block.forward_prop(output, input_channels, scope="num_blocks_{}".format(i), is_training=is_training)
return output