我们知道一张图片经过几个卷积层会得到一个特征矩阵,这个矩阵的通道数就是卷积层核的个数。那么,一个常见的卷积核经常达到1024,2048个,并不是每个通道都对于信息传递非常有用了的。因此,通过对这些通道进行过滤,也就是注意,来得到优化后的特征。主要思路就是:增大有效通道权重,减少无效通道的权重
#实现方式1
class ChannelAttention(layers.Layer):
def __init__(self, in_planes, ratio=8):
super(ChannelAttention, self).__init__()
self.avg_out= layers.GlobalAveragePooling2D()
self.max_out= layers.GlobalMaxPooling2D()
self.fc1 = layers.Dense(in_planes//ratio, kernel_initializer='he_normal',
kernel_regularizer=regularizers.l2(5e-4),
activation=tf.nn.relu,
use_bias=True, bias_initializer='zeros')
self.fc2 = layers.Dense(in_planes, kernel_initializer='he_normal',
kernel_regularizer=regularizers.l2(5e-4),
use_bias=True, bias_initializer='zeros')
def call(self, inputs):
avg_out = self.avg_out(inputs)
max_out = self.max_out(inputs)
out = tf.stack([avg_out, max_out], axis=1) # shape=(None, 2, fea_num)
out = self.fc2(self.fc1(out))
out = tf.reduce_sum(out, axis=1) # shape=(256, 512)
out = tf.nn.sigmoid(out)
out = layers.Reshape((1, 1, out.shape[1]))(out)
return out
#实现方式2
class ChannelAttention(layers.Layer):
def __init__(self, in_planes):
super(ChannelAttention, self).__init__()
self.avg= layers.GlobalAveragePooling2D()
self.max= layers.GlobalMaxPooling2D()
self.fc1 = layers.Dense(in_planes//16, kernel_initializer='he_normal', activation='relu',
use_bias=True, bias_initializer='zeros')
self.fc2 = layers.Dense(in_planes, kernel_initializer='he_normal', use_bias=True,
bias_initializer='zeros')
def call(self, inputs):
avg_out = self.fc2(self.fc1(self.avg(inputs)))
max_out = self.fc2(self.fc1(self.max(inputs)))
out = avg_out + max_out
out = tf.nn.sigmoid(out)
out = tf.reshape(out, [out.shape[0], 1, 1, out.shape[1]])
out = tf.tile(out, [1, inputs.shape[1], inputs.shape[2], 1])
return out