深度学习3类注意力模块代码(函数)

代码如下:

def seBlock(input_feature):
    """
    SE注意力模块:Squeeze-and-Excitation Block.

    Returns
        output

    """
    x = layers.GlobalAveragePooling2D()(input_feature)
    x = layers.Dense(units=input_feature.shape[-1] // 8, use_bias=False)(x)
    x = layers.BatchNormalization()(x)
    x = layers.Activation('relu')(x)
    x = layers.Dense(units=input_feature.shape[-1], use_bias=False)(x)
    x = layers.BatchNormalization()(x)
    x = layers.Activation('sigmoid')(x)
    outputs = layers.Multiply()([input_feature, x])  # 对输入的特征图进行缩放
    return outputs


def cbamBlock(input_feature, reduction=16):
    """
    CBAM注意力模块:Convolutional Block Attention Module

    Args:
        input_feature: 输入特征张量。
        reduction: 可选参数,默认值为16。
                控制注意力模块中全连接层输出特征大小的减少倍数。
    这是一个定义 CBAM 模块的函数 cbamBlock
    它实现了 Convolutional Block Attention Module(卷积块注意力模块)
    该模块接受 4 维张量 (batch_size, height, width, channel) 作为输入特征。

    Returns
        output

    """
    x = layers.GlobalAveragePooling2D()(input_feature)
    x = layers.Dense(input_feature.shape[-1] // reduction,
                     activation='relu', kernel_initializer='he_normal')(x)
    x = layers.Dense(input_feature.shape[-1],
                     activation='sigmoid', kernel_initializer='he_normal')(x)
    x = layers.Reshape((1, 1, input_feature.shape[-1]))(x)
    x = layers.Multiply()([input_feature, x])

    y = layers.GlobalMaxPooling2D()(x)
    y = layers.Dense(input_feature.shape[-1] // reduction,
                     activation='relu', kernel_initializer='he_normal')(y)
    y = layers.Dense(input_feature.shape[-1],
                     activation='sigmoid', kernel_initializer='he_normal')(y)
    y = layers.Reshape((1, 1, input_feature.shape[-1]))(y)
    y = layers.Multiply()([input_feature, y])

    return layers.Add()([x, y])


def ecaBlock(input_feature, b=1, gamma=2):
    """
    ECA注意力模块:Efficient Channel Attention.

    Args:
        input_feature: 输入特征张量。
        b: 可选参数,默认值为1。我们在计算ECA卷积核大小时使用的偏置项。
        gamma: 可选参数,默认值为2。用于调整 ECA 卷积核大小随着输入通道数量的变化而变化的速度。
    这是一个定义 ECA 模块的函数 eca_block
    它的输入特征是一个 4 维张量 (batch_size, height, width, channel)
    其中第四维为通道数.

    Returns
        output

    """
    channel = input_feature.shape[-1]
    kernel_size = int(abs((math.log2(channel) + b) / gamma)) | 1

    avg_pool = layers.GlobalAveragePooling2D(data_format='channels_last')(input_feature)

    x = layers.Reshape((-1, 1))(avg_pool)
    x = layers.Conv1D(filters=1, kernel_size=kernel_size,
                      padding="same", use_bias=False)(x)
    x = layers.Activation('sigmoid')(x)
    x = layers.Reshape((1, 1, -1))(x)

    output = layers.multiply([input_feature, x])
    return output

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

John H.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值