【问题标题】:Add the Attention mechanism for producing heatmaps output from the neural network添加用于从神经网络生成热图输出的注意力机制
【发布时间】:2020-04-21 13:54:34
【问题描述】:

我有这个神经网络,可以输入 rgb 图像和它的 2 个其他变体(固定和动态)。我想在输出测试实例的热图时添加“注意”机制。

def build_model():
  inputRGB = tf.keras.Input(shape=(128,128,3), name='train_ds')
  inputFixed = tf.keras.Input(shape=(128,128,3), name='fixed_ds')
  inputDinamic = tf.keras.Input(shape=(128,128,3), name='dinamic_ds')

    #  RGB images
  rgb = models.Sequential()  
  rgb = layers.Conv2D(32, (5, 5), padding='SAME')(inputRGB)
  rgb = layers.PReLU()(rgb)
  rgb = layers.MaxPooling2D((2, 2))(rgb)
  rgb = layers.BatchNormalization()(rgb)
  rgb = layers.Conv2D(64, (3, 3))(rgb)
  rgb = layers.PReLU()(rgb)
  rgb = layers.Conv2D(64, (3, 3))(rgb)
  rgb = layers.PReLU()(rgb)
  rgb = layers.Conv2D(64, (3, 3))(rgb)
  rgb = layers.PReLU()(rgb)
  rgb = layers.Dropout(0.5)(rgb)
  rgb = layers.GlobalAvgPool2D()(rgb)
  rgb = Model(inputs = inputRGB, outputs=rgb)

    # First type of density 
  fixed = models.Sequential()
  fixed = layers.Conv2D(32, (5, 5), padding='SAME')(inputFixed)
  fixed = layers.PReLU()(fixed)
  fixed = layers.MaxPooling2D((2, 2))(fixed)
  fixed = layers.BatchNormalization()(fixed)
  fixed = layers.Conv2D(64, (3, 3))(fixed)
  fixed = layers.PReLU()(fixed)
  fixed = layers.Conv2D(64, (3, 3))(fixed)
  fixed = layers.PReLU()(fixed)
  fixed = layers.Conv2D(64, (3, 3))(fixed)
  fixed = layers.PReLU()(fixed)
  fixed = layers.Dropout(0.5)(fixed)
  fixed = layers.GlobalAvgPool2D()(fixed)
  fixed = Model(inputs = inputFixed, outputs=fixed)

    # Second type of density
  dinamic = models.Sequential()  
  dinamic = layers.Conv2D(32, (5, 5), padding='SAME')(inputDinamic)
  dinamic = layers.PReLU()(dinamic)
  dinamic = layers.MaxPooling2D((2, 2))(dinamic)
  dinamic = layers.BatchNormalization()(dinamic)
  dinamic = layers.Conv2D(64, (3, 3))(dinamic)
  dinamic = layers.PReLU()(dinamic)
  dinamic = layers.Conv2D(64, (3, 3))(dinamic)
  dinamic = layers.PReLU()(dinamic)
  dinamic = layers.Conv2D(64, (3, 3))(dinamic)
  dinamic = layers.PReLU()(dinamic)
  dinamic = layers.Dropout(0.5)(dinamic)
  dinamic = layers.GlobalAvgPool2D()(dinamic)
  dinamic = Model(inputs = inputDinamic, outputs=dinamic)

  concat = layers.concatenate([rgb.output, fixed.output, dinamic.output])  # merge the outputs of the two models
  k = layers.Dense(1)(concat)

  modelFinal = Model(inputs={'train_ds':inputRGB, 'fixed_ds':inputFixed, 'dinamic_ds':inputDinamic}, outputs=[k])

  opt = tf.keras.optimizers.Adam(learning_rate=0.001, amsgrad=False)


  modelFinal.compile(optimizer=opt , loss='mae', metrics=['mae'])
  return modelFinal

很遗憾,这是我第一次使用这种机械化。根据我的研究,我应该在连接层和密集层之间插入一个注意力层。但是,这是生成热图作为输出的正确方法吗?

【问题讨论】:

  • 您想要热图在哪一层之后?创建实际上意味着某事的热图只能在 GAP 之前完成,最好是在中间或早期的 Conv 层中。
  • @SusmitAgrawal 我想它必须在 GAP 之前完成。解释一下:我想从 RGB 子网络生成热图。我对其他两个子网络(固定和动态)的热图输出不感兴趣
  • 您需要修复要从中获取热图的网络中的层,而不是整个网络。作为一般规则,来自更深层的注意力图在视觉上不会提供太多信息。
  • @SusmitAgrawal 我该怎么做?假设我想把它放在 GAP 之前,Dropout 之后。我在 tf.keras (tensorflow.org/api_docs/python/tf/keras/layers/Attention) 中看到了注意力层,但我不明白如何使用它来获取热图。

标签: keras neural-network conv-neural-network tf.keras attention-model


【解决方案1】:

Tensorflow 中的注意力层针对的是顺序数据。

图像卷积网络使用两种不同的注意力机制:

  1. 自我关注机制(查看这篇精彩的medium 文章)

  2. 直接权重图预测,通过在您要关注的层之后添加一个带有 1 个滤波器的 1 x 1 卷积,具有 sigmoid 激活。

  3. 在特定内核上使用高斯二维注意力,其中指定了 x 和 y 坐标。

但是,所有这些方法都是参数化的,需要训练。您的用例不同。

为了从层中获取热图,我通常会提取具有最高平均激活值的内核。仅此一项通常是不够的,因此我使用平均激活值最高的前 k 个内核。

要获得激活值最高的内核,伪代码为:

在输入上运行模型,批量大小为 1

提取您想要热图的图层的输出。这将是形状(1,h,w,过滤器)。将此分配给一个变量(例如“输出”)

对“输出”执行 GAP 并挤压以获得形状向量(过滤器)。该向量的 argmax 将使内核具有最高的平均激活。我们将 argmax 的输出称为“kernel_number”

绘制“输出”[0, :, :, “kernel_number”]。这是您要查找的热图。

【讨论】:

  • 我会尝试,但我还有一个问题:如果我想复制这个 (github.com/gvessio/uav-crowd-detection/blob/master/…) 但不使用 grad-cam 而是事后关注(在此处描述 @987654323 @),在这个从前面描述的多输入神经网络派生的训练模型中提取的子网络(pastebin.com/zb9Gkdng)上,有可能吗?
  • 是否可能:是的。怎么做:你必须尝试。
  • 好的,那我还能大致考虑一下你上面描述的伪代码吗?
猜你喜欢
  • 2020-01-28
  • 2017-08-11
  • 2017-04-10
  • 1970-01-01
  • 1970-01-01
  • 2015-02-23
  • 1970-01-01
  • 2020-03-15
相关资源
最近更新 更多