in utils.py [0:0]
def attention(
inp,
q,
q_b,
k,
k_b,
v,
v_b,
gamma,
reuse,
scope,
stop_at_grad=False,
seperate=False,
scale=False,
train=False,
dropout=0.0):
conv_q = conv_block(
inp,
q,
q_b,
reuse=reuse,
scope=scope,
use_stride=False,
activation=None,
use_bias=True,
pn=False,
bn=False,
gn=False)
conv_k = conv_block(
inp,
k,
k_b,
reuse=reuse,
scope=scope,
use_stride=False,
activation=None,
use_bias=True,
pn=False,
bn=False,
gn=False)
conv_v = conv_block(
inp,
v,
v_b,
reuse=reuse,
scope=scope,
use_stride=False,
pn=False,
bn=False,
gn=False)
c_num = float(conv_q.get_shape().as_list()[-1])
s = tf.matmul(hw_flatten(conv_q), hw_flatten(conv_k), transpose_b=True)
if scale:
s = s / (c_num) ** 0.5
if train:
s = tf.nn.dropout(s, 0.9)
beta = tf.nn.softmax(s, axis=-1)
o = tf.matmul(beta, hw_flatten(conv_v))
o = tf.reshape(o, shape=tf.shape(inp))
inp = inp + gamma * o
if not seperate:
return inp
else:
return gamma * o