【问题标题】:Do matrix product to outputs of two models in keras对keras中两个模型的输出做矩阵乘积
【发布时间】:2018-02-23 02:28:36
【问题描述】:

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

具体来说,现在我想对两个CNN模型的两个输出矩阵做外积,并且我已经完成了矩阵的转置,现在我只想在keras中将两个矩阵相乘,其大小为 (None, 512, 49) 和 (None, 49, 512)。

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

当我使用模式时,

ValueError:使用点模式的维度不兼容:49!= 512。层 形状:(None, 512, 49), (None, 49, 512)

当我使用乘法模式时,

ValueError: 只能使用 mul 合并相同输出形状的层 模式。图层形状:[(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 conv
model_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 conv
model_b.add(Lambda(trans_2, output_shape=[512, 49]))

f_model = Sequential()
f_model.add(Merge([model_b, model_a], mode='dot'))

【问题讨论】:

    标签: python machine-learning computer-vision deep-learning keras


    【解决方案1】:

    首先,当您的模型不是连续的时,不要使用Sequential()。请改用functional 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])
    

    【讨论】:

      猜你喜欢
      • 2013-03-31
      • 2023-04-08
      • 1970-01-01
      • 2017-11-15
      • 2017-08-25
      • 2019-09-22
      • 1970-01-01
      • 1970-01-01
      • 2018-11-22
      相关资源
      最近更新 更多