通道注意力机制keras_keras, TensorFlow中加入注意力机制

keras, TensorFlow中加入注意力机制原文:https://blog.csdn.net/qq_38410428/article/details/103695032

第一步:找到要修改文件的源代码

在里面添加通道注意力机制和空间注意力机制

所需库

from keras.layers import GlobalAveragePooling2D, GlobalMaxPooling2D, Reshape, Dense, multiply, Permute, Concatenate, Conv2D, Add, Activation, Lambda

from keras import backend as K

from keras.activations import sigmoid

1

2

3

通道注意力机制

def channel_attention(input_feature, ratio=8):

channel_axis = 1 if K.image_data_format() == "channels_first" else -1

channel = input_feature._keras_shape[channel_axis]

shared_layer_one = Dense(channel//ratio,

kernel_initializer='he_normal',

activation = 'relu',

use_bias=True,

bias_initializer='zeros')

shared_layer_two = Dense(channel,

kernel_initializer='he_normal',

use_bias=True,

bias_initializer='zeros')

avg_pool = GlobalAveragePooling2D()(input_feature)

avg_pool = Reshape((1,1,channel))(avg_pool)

assert avg_pool._keras_shape[1:] == (1,1,channel)

avg_pool = shared_layer_one(avg_pool)

assert avg_pool._keras_shape[1:] == (1,1,channel//ratio)

avg_pool = shared_layer_two(avg_pool)

assert avg_pool._keras_shape[1:] == (1,1,channel)

max_pool = GlobalMaxPooling2D()(input_feature)

max_pool = Reshape((1,1,channel))(max_pool)

assert max_pool._keras_shape[1:] == (1,1,channel)

max_pool = shared_layer_one(max_pool)

assert max_pool._keras_shape[1:] == (1,1,channel//ratio)

max_pool = shared_layer_two(max_pool)

assert max_pool._keras_shape[1:] == (1,1,channel)

cbam_feature = Add()([avg_pool,max_pool])

cbam_feature = Activation('hard_sigmoid')(cbam_feature)

if K.image_data_format() == "channels_first":

cbam_feature = Permute((3, 1, 2))(cbam_feature)

return multiply([input_feature, cbam_feature])

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

空间注意力机制

def spatial_attention(input_feature):

kernel_size = 7

if K.image_data_format() == "channels_first":

channel = input_feature._keras_shape[1]

cbam_feature = Permute((2,3,1))(input_feature)

else:

channel = input_feature._keras_shape[-1]

cbam_feature = input_feature

avg_pool = Lambda(lambda x: K.mean(x, axis=3, keepdims=True))(cbam_feature)

assert avg_pool._keras_shape[-1] == 1

max_pool = Lambda(lambda x: K.max(x, axis=3, keepdims=True))(cbam_feature)

assert max_pool._keras_shape[-1] == 1

concat = Concatenate(axis=3)([avg_pool, max_pool])

assert concat._keras_shape[-1] == 2

cbam_feature = Conv2D(filters = 1,

kernel_size=kernel_size,

activation = 'hard_sigmoid',

strides=1,

padding='same',

kernel_initializer='he_normal',

use_bias=False)(concat)

assert cbam_feature._keras_shape[-1] == 1

if K.image_data_format() == "channels_first":

cbam_feature = Permute((3, 1, 2))(cbam_feature)

return multiply([input_feature, cbam_feature])

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

构建CBAM

def cbam_block(cbam_feature,ratio=8):

"""Contains the implementation of Convolutional Block Attention Module(CBAM) block.

As described in https://arxiv.org/abs/1807.06521.

"""

cbam_feature = channel_attention(cbam_feature, ratio)

cbam_feature = spatial_attention(cbam_feature, )

return cbam_feature

1

2

3

4

5

6

7

8

在相应的位置添加CBAM

inputs = x

residual = layers.Conv2D(filter, kernel_size = (1, 1), strides = strides, padding = 'same')(inputs)

residual = layers.BatchNormalization(axis = bn_axis)(residual)

cbam = cbam_block(residual)

x = layers.add([x, residual, cbam])

1

2

3

4

5

6

这样就在任意位置加入了注意力机制啦。返回列表华为云比赛-垃圾分类挑战-数据集、源代码解析与下载

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Keras的`Attention`层来为ResNet34加入注意力机制,以下是一个示例代码: ```python from tensorflow.keras.layers import Input, Conv2D, BatchNormalization, Activation, MaxPooling2D, Flatten, Dense, Add, GlobalAveragePooling2D, Multiply from tensorflow.keras.layers import Reshape, Permute, multiply, LSTM, Lambda, TimeDistributed, concatenate from tensorflow.keras import Model def attention_block(inputs, time_steps): a = Permute((2,1))(inputs) # 将时间步和特征维度进行交换,方便后续计算 a = Dense(time_steps, activation='softmax')(a) # 经过全连接层后进行softmax操作,得到每个时间步的权重 a = Permute((2,1))(a) # 再将时间步和特征维度进行交换,还原原始维度 a = multiply([inputs, a]) # 将输入和注意力权重相乘得到加权输入 return a def resnet_block(inputs, filters, kernel_size, strides): x = Conv2D(filters=filters, kernel_size=kernel_size, strides=strides, padding='same')(inputs) x = BatchNormalization()(x) x = Activation('relu')(x) x = Conv2D(filters=filters, kernel_size=kernel_size, strides=1, padding='same')(x) x = BatchNormalization()(x) shortcut = Conv2D(filters=filters, kernel_size=1, strides=strides, padding='same')(inputs) shortcut = BatchNormalization()(shortcut) x = Add()([x, shortcut]) x = Activation('relu')(x) return x def ResNet34_attention(input_shape, time_steps): inputs = Input(shape=input_shape) x = Conv2D(filters=64, kernel_size=7, strides=2, padding='same')(inputs) x = BatchNormalization()(x) x = Activation('relu')(x) x = MaxPooling2D(pool_size=3, strides=2, padding='same')(x) for i in range(3): x = resnet_block(x, filters=64, kernel_size=3, strides=1) x = attention_block(x, time_steps=time_steps) for i in range(4): if i==0: x = resnet_block(x, filters=128, kernel_size=3, strides=2) else: x = resnet_block(x, filters=128, kernel_size=3, strides=1) x = attention_block(x, time_steps=time_steps) for i in range(6): if i==0: x = resnet_block(x, filters=256, kernel_size=3, strides=2) else: x = resnet_block(x, filters=256, kernel_size=3, strides=1) x = attention_block(x, time_steps=time_steps) for i in range(3): if i==0: x = resnet_block(x, filters=512, kernel_size=3, strides=2) else: x = resnet_block(x, filters=512, kernel_size=3, strides=1) x = GlobalAveragePooling2D()(x) x = Dense(1024, activation='relu')(x) x = Dense(512, activation='relu')(x) outputs = Dense(1, activation='sigmoid')(x) model = Model(inputs=inputs, outputs=outputs) return model ``` 在这个示例代码,我们定义了一个`attention_block`函数,用于为ResNet34的每个`resnet_block`加入注意力机制。在`ResNet34_attention`函数,我们先构建了ResNet34的主体结构,然后在第1、2、3、4个阶段的最后一个`resnet_block`后都加入了一个`attention_block`。最后,我们再添加全局平均池化层和全连接层进行分类。 需要注意的是,在`attention_block`函数,我们使用了`Permute`层将时间步和特征维度进行交换,这是因为我们需要对每个时间步进行softmax计算。计算完成后,我们再将时间步和特征维度交换回来,得到加权输入。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值