【发布时间】: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