我正在使用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])