我试图用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为什么我们不能像下面的图像那样进行连续的特征向量一维卷积?在这里输入链接描述
发布于 2020-03-18 18:30:04
VGG中的完全连接层是2D,一维卷积层需要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)https://stackoverflow.com/questions/60745393
复制相似问题