Keras Conv1D 和 VGG 的问题

Issues with Keras Conv1D and VGG

我正在尝试构建一个基于 VGG16 的深度学习模型。我已经使用以下代码在 Keras 中实现了它:

image_input = Input(shape=(224, 224, 3))

model = VGG16(input_tensor=image_input, include_top=True,weights='imagenet')
model.summary()
fc7 = model.get_layer('fc2').output
conv1d = Conv1D(1,5,activation='relu', name="conv1d",input_shape=(1,4096)) (fc7) #error appears here
# flat = Flatten()(conv1d)
fc8 = Dense(512, activation='relu', name="fc8")(conv1d)
#x= Flatten(name='flatten')(last_layer)
out = Dense(num_classes, activation='softmax', name='output')(fc8)
custom_vgg_model = Model(image_input, out)
custom_vgg_model.summary()

我收到以下错误:

ValueError: Input 0 is incompatible with layer conv1d: expected ndim=3, found ndim=2

为什么我们不能像下图那样对连续的特征向量进行 1d 卷积? enter link description here

VGG 中的全连接层是 2D,而 1D 卷积层需要 3D 数据。

在 VGG 添加 Dense 层的地方,它通过展平或全局池化破坏了图像格式 (4D),将其转换为普通数据 (2D)。您不再有维度来使用卷积。

如果您尝试解释您想要 Conv1D 的原因,您对它有何期望,那么我们可以考虑替代方案。


示例模型:

movie_data = any_data_with_shape((number_of_videos, frames, 224, 224, 3))
movie_input = Input((None,224,224,3)) #None means any number of frames

vgg = VGG16(include_top=True,weights='imagenet')

仅当您从 vgg 获得中间输出时才需要此部分:

vgg_in = vgg.input
vgg_out = vgg.get_layer('fc2').output #make sure this layer exists
vgg = Model(vgg_in, vgg_out)

继续:

vgg_outs = TimeDistributed(vgg)(movie_input) #out shape (None, frames, fc2_units)

outs = Conv1D(.....)(vgg_outs)
outs = GlobalAveragePooling1D()(outs)
outs = Dense(....)(outs)
.....

your_model = model(move_input, outs)