【问题标题】:How can I change the shape of a feature map with dimension 1x1xC into NxNxC dimension feature map in CNN?如何在 CNN 中将维度为 1x1xC 的特征图的形状更改为 NxNxC 维度的特征图?
【发布时间】:2021-12-27 12:05:57
【问题描述】:
image_size = 256 #image size which is input shape of the densenet121
base_model=get_base_model('densenet121',image_size) #calling densenet121 using transfer learning
base_in=base_model.input #base_in.shape = (None, 256, 256, 3)
base_out=base_model.output #base_out.shape = (None, 8, 8, 1024)

#function which performs attention which is to be added on the densenet121
def BAM_Channel_Attention(inputs):
  shape=K.int_shape(inputs)
  x=tf.keras.layers.AveragePooling2D(pool_size=(shape[1],shape[2]))(inputs)
  dense = layers.Dense(1280, activation="relu")(x)
  reduction = Conv2D(shape[3]/16,1, padding='same')(dense)
  dense = layers.Dense(1280, activation="relu")(reduction)
  out = tf.keras.layers.BatchNormalization()(dense)
  return out

上面的函数在这里被调用:

#calling the attention function with the output of the densenet121
x=BAM_Channel_Attention(base_out) #

print(x.shape)

我得到了输出(None, 1, 1, 1280)。如何将特征图从 1x1x1280 更改为 8x8x1280?

【问题讨论】:

    标签: python tensorflow keras deep-learning conv-neural-network


    【解决方案1】:

    最简单但可能不是最实用的选项是在BAM_Channel_Attention 输出上使用tf.repeat

    import tensorflow as tf
    
    batch_size = 2
    data = tf.random.normal((batch_size, 1, 1, 1280))
    output = tf.repeat(tf.repeat(data, repeats=8, axis=1), repeats=8, axis=2)
    print(output.shape)
    
    (2, 8, 8, 1280)
    

    可能更好的方法是在BAM_Channel_Attention 函数中更改AveragePooling2D 层中的pool_size,因为它会将您的空间维度从(8, 8) 下采样到(1,1)。您可以将 pool_size 从 (8, 8) 更改为 (1, 1):

    import tensorflow as tf
    
    batch_size = 2
    inputs = tf.keras.layers.Input((8, 8, 1024))
    x = tf.keras.layers.AveragePooling2D(pool_size=(1,1))(inputs)
    dense = tf.keras.layers.Dense(1280, activation="relu")(inputs)
    reduction = tf.keras.layers.Conv2D(1024/16, 1, padding='same')(dense)
    dense = tf.keras.layers.Dense(1280, activation="relu")(reduction)
    outputs = tf.keras.layers.BatchNormalization()(dense)
    
    model = tf.keras.Model(inputs, outputs)
    print(model(tf.random.normal((batch_size, 8, 8, 1024))).shape)
    
    (2, 8, 8, 1280)
    

    或者完全省略AveragePooling2D层,你也会得到你想要的结果。

    【讨论】:

    • @arvind okram 如果对您有帮助,请接受/支持答案
    猜你喜欢
    • 2021-03-27
    • 2014-03-10
    • 1970-01-01
    • 1970-01-01
    • 2017-02-08
    • 2016-07-19
    • 1970-01-01
    • 1970-01-01
    • 2018-03-25
    相关资源
    最近更新 更多