|
Algorithm 1 Tensorflow-like implementation of SimAM |
class SimAM(tf.keras.layers.Layer):
def __init__(self, lambda = , **kwargs):
super(SimAM, self).__init__(**kwargs)
self.lambda = lambda
def call(self, X, **kwargs):
# X Input feature [N, H, W, C]
height, width = X.shape[1:2]
# spatial size
n = width * height − 1
# square of (X − u)
u = tf.math.reduce_mean (X, axis = (1,2), True)
d = tf.math.square (X − u)
# d.sum()/n is channel variance
v = tf.math.reduce_sum (d, axis = (1,2), True)/n
# E_inv groups all importance of X
E_inv = d/(4 * tf.maximum (v, self.lambda) + 0.5)
# return attended features
return X * tf.keras.activations.sigmoid(E_inv)
|