在Keras中对两个模型的输出进行矩阵乘法

我正在使用CNN开发一个分类模型,现在我想在我的问题中应用一种分类算法(Bilinear CNN Models for Fine-grained Visual Recognition Tsung-Yu Lin Aruni RoyChowdhury Subhransu MajiUniversity of Massachusetts, Amherst)。

具体来说,现在我想对两个CNN模型的输出矩阵进行外积,我已经完成了矩阵的转置,现在我只想在keras中将两个矩阵相乘,它们的尺寸分别是(None, 512, 49)和(None, 49, 512)。

我尝试使用keras中的Merge层,但出现了一些错误:

当我使用dot模式时,

ValueError: Dimension incompatibility using dot mode: 49 != 512. Layer shapes: (None, 512, 49), (None, 49, 512)

当我使用multiply模式时,

ValueError: Only layers of same output shape can be merged using mul mode. Layer shapes: [(None, 512, 49), (None, 49, 512)]

我不知道如何解决这个问题,请帮助我!这是我问题的部分代码:

t_model = applications.VGG16(weights='imagenet', include_top=False,                                 input_shape=(224, 224, 3))model_a = Sequential()model_a.add(t_model)def trans_1(conv):    conv = tf.reshape(conv, [-1, 49, 512])    return convmodel_a.add(Lambda(trans_1, output_shape=[49, 512]))s_model = applications.VGG16(weights='imagenet', include_top=False,                                 input_shape=(224, 224, 3))model_b = Sequential()model_b.add(s_model)def trans_2(conv):    conv = tf.reshape(conv, [-1, 49, 512])    conv = tf.transpose(conv, perm = [0, 2, 1])    return convmodel_b.add(Lambda(trans_2, output_shape=[512, 49]))f_model = Sequential()f_model.add(Merge([model_b, model_a], mode='dot'))

回答:

首先,如果你的模型不是顺序模型,就不要使用Sequential()。请改用函数式API

另外,

  • 由于两个VGG16模型共享相同的输入图像,你可以使用input_tensor参数来提供共享输入。
  • 请注意,VGG16有固定的层名称。你必须更改其中一个模型的层名称,以避免“所有层名称应唯一”的错误。
  • Keras内置了Reshape层,这里不需要使用TF。
  • 使用Dot代替已废弃的Merge层。

回到你的问题,Dot中的axes参数指定了将被减少的轴。因此,你不需要在应用之前对张量进行转置。

input_tensor = Input(shape=(224, 224, 3))t_model = applications.VGG16(weights='imagenet', include_top=False, input_tensor=input_tensor)t_output = Reshape((49, 512))(t_model.output)s_model = applications.VGG16(weights='imagenet', include_top=False, input_tensor=input_tensor)for layer in s_model.layers:    layer.name += '_1's_output = Reshape((49, 512))(s_model.output)merged = Dot(axes=1)([s_output, t_output])

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注