首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >添加注意力机制,以便从神经网络生成热图输出

添加注意力机制,以便从神经网络生成热图输出
EN

Stack Overflow用户
提问于 2020-04-21 21:54:35
回答 1查看 370关注 0票数 0

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

代码语言:javascript
复制
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

不幸的是,这是我第一次使用这样的机械化。从我的研究来看,我应该在连接层和密集层之间插入一个关注层。但是,这是生成热图作为输出的正确方式吗?

EN

回答 1

Stack Overflow用户

发布于 2020-04-22 00:00:14

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

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

高斯自注意机制(通过在你想要关注的图层之后添加1 x 1卷积和1个过滤器,在特定内核上具有sigmoid activation.

  • Using
  1. 2D注意,其中指定了x和y坐标,检查这个伟大的medium高斯权重图预测。

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

为了从层中获取热图,我通常提取平均激活值最高的内核。这通常是不够的,所以我使用具有最高平均激活值的前k个内核。

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

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

提取您想要的热图的层的输出。这将是形状(1,h,w,filter)。将其赋值给一个变量(比如“output”)

对“输出”执行GAP并压缩,以获得形状的向量(滤波器,)。这个向量的argmax将给出具有最高平均激活度的内核。让我们将argmax的输出称为"kernel_number“

绘制"outputs"0、:、:、"kernel_number“。这就是您要查找的热图。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61345295

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档