Spatial Attention model

    这个方法来自于论文:《Context Aware Query Image Representation for Particular Object Retrieval》

  

    在描述待查询图像时,很多方法都利用到了region或者proposal的概念,旨在提高图中object的显著性。

    在此文中,作者提出了一种Spatial Attention model,即空间注意力模型,来提高对图中object的注意力。

    

    1. saliency map

    假设卷积网络结构中,提取特征的layer产生的feature map大小为W*H*K。

    其中所有K层相同位置的激活值进行相加求和,形成W*H大小。

    之后进行归一化,使所有值介于[0,1]。

  • 11
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
首先,我们需要定义L2池化和随机池化的函数: ```python import tensorflow as tf def l2_pool(x, pool_size): return tf.math.sqrt(tf.nn.avg_pool(tf.square(x), pool_size, strides=1, padding='SAME')) def rand_pool(x, pool_size): random_mat = tf.random.uniform(x.shape[:-1], dtype=tf.float32) threshold = tf.math.reduce_max(random_mat) * 0.5 return tf.where(random_mat > threshold, tf.nn.avg_pool(x, pool_size, strides=1, padding='SAME'), tf.nn.max_pool(x, pool_size, strides=1, padding='SAME')) ``` 然后,我们可以定义新的CBAM模块: ```python import tensorflow as tf class NewCBAM(tf.keras.layers.Layer): def __init__(self, reduction_ratio=16): super(NewCBAM, self).__init__() self.reduction_ratio = reduction_ratio self.gamma = tf.Variable(0.0, trainable=True) # Channel Attention self.avg_pool = tf.keras.layers.GlobalAveragePooling2D() self.max_pool = tf.keras.layers.GlobalMaxPooling2D() self.l2_pool = tf.keras.layers.Lambda(lambda x: l2_pool(x, pool_size=(1,1))) self.dense1 = tf.keras.layers.Dense(units=tf.keras.backend.int_shape(x)[-1]//self.reduction_ratio, activation='relu') self.dense2 = tf.keras.layers.Dense(units=tf.keras.backend.int_shape(x)[-1], activation='sigmoid') # Spatial Attention self.rand_pool = tf.keras.layers.Lambda(lambda x: rand_pool(x, pool_size=(3,3))) self.l2_pool2 = tf.keras.layers.Lambda(lambda x: l2_pool(x, pool_size=(3,3))) self.concat1 = tf.keras.layers.Concatenate(axis=-1) self.conv1 = tf.keras.layers.Conv2D(filters=1, kernel_size=1, activation='sigmoid') def call(self, x): # Channel Attention avg_pool = self.avg_pool(x) max_pool = self.max_pool(x) l2_pool = self.l2_pool(x) concat = tf.concat([avg_pool, max_pool, l2_pool], axis=-1) dense1 = self.dense1(concat) dense2 = self.dense2(dense1) channel_attention = tf.reshape(dense2, shape=(-1,1,1,tf.keras.backend.int_shape(x)[-1])) channel_attention = tf.math.multiply(x, channel_attention) # Spatial Attention rand_pool = self.rand_pool(x) l2_pool2 = self.l2_pool2(x) concat2 = tf.concat([rand_pool, l2_pool2], axis=-1) conv1 = self.conv1(concat2) spatial_attention = tf.math.multiply(x, conv1) # Fusion fusion = tf.math.add(channel_attention, spatial_attention) return tf.math.add(tf.math.multiply(x, 1-self.gamma), tf.math.multiply(fusion, self.gamma)) ``` 最后,我们可以使用新的CBAM模块: ```python import tensorflow as tf inputs = tf.keras.layers.Input(shape=(224, 224, 3)) x = tf.keras.layers.Conv2D(filters=64, kernel_size=3, padding='SAME')(inputs) x = NewCBAM()(x) x = tf.keras.layers.Conv2D(filters=128, kernel_size=3, padding='SAME')(x) x = NewCBAM()(x) x = tf.keras.layers.Conv2D(filters=256, kernel_size=3, padding='SAME')(x) x = NewCBAM()(x) x = tf.keras.layers.Conv2D(filters=512, kernel_size=3, padding='SAME')(x) x = NewCBAM()(x) x = tf.keras.layers.Conv2D(filters=1024, kernel_size=3, padding='SAME')(x) x = NewCBAM()(x) outputs = tf.keras.layers.GlobalAveragePooling2D()(x) model = tf.keras.Model(inputs=inputs, outputs=outputs) ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值