坐标注意力机制-Tensorflow2

"""
    坐标注意力机制代码
"""
import numpy as np
from tensorflow.keras.layers import AvgPool2D, Conv2D, BatchNormalization, Activation
from tensorflow.keras import Model
from tensorflow.keras.activations import sigmoid
from tensorflow import split,concat,transpose
import tensorflow as tf



def ac_swish(x):
    temp = tf.nn.relu6(x+3) / 6
    return x * temp

class Coordinate_Attention(Model):
    def __init__(self, w, h, inp, oup, groups=32):
        """
        :param w:   width
        :param h:   height
        :param inp: input channels
        :param oup: output channels
        :param groups:
        """
        super(Coordinate_Attention, self).__init__()
        self.w = w
        self.h = h
        self.inp = inp
        self.oup = oup
        self.groups = groups

        self.pool_h = AvgPool2D(pool_size=(1, self.w), strides=1, padding='same')
        self.pool_w = AvgPool2D(pool_size=(self.h, 1), strides=1, padding='same')

        self.mip = max(8, self.inp // self.groups)

        self.conv1 = Conv2D(filters=self.mip, kernel_size=(1,1), strides=1, padding='same')
        self.bn1 = BatchNormalization()
        self.conv2 = Conv2D(filters=self.oup, kernel_size=(1,1), strides=1, padding='same')
        self.conv3 = Conv2D(filters=self.oup, kernel_size=(1,1), strides=1, padding='same')

        self.ac = Activation(ac_swish)

    def call(self, inputs):
        residual = inputs
        x = residual
        # n, c, h, w = x.shape

        x_h = self.pool_h(x)
        x_w = self.pool_w(x)

        x_w = transpose(x_w, [0, 2, 1, 3])

        y = concat([x_h, x_w], axis=1)
        y = self.conv1(y)
        y = self.bn1(y)
        y = self.ac(y)

        x_h, x_w = split(y, 2, axis=1)
        x_w = transpose(x_w, [0, 2, 1, 3])

        x_h = sigmoid(self.conv2(x_h))
        x_w = sigmoid(self.conv3(x_w))

        y = residual * x_w * x_h
        print(np.shape(y))
        return y


if __name__ == '__main__':
    model = Coordinate_Attention(w=14, h=14, inp=512, oup=512)
    # inputs = tf.keras.Input(shape=(14,14,512))
    # model.call(inputs=inputs)
    model.build((1, 14, 14, 512))
    model.summary()

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

早起学习晚上搬砖

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

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

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

打赏作者

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

抵扣说明:

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

余额充值