【问题标题】:Issues with Keras Conv1D and VGGKeras Conv1D 和 VGG 的问题
【发布时间】:2020-03-18 18:22:46
【问题描述】:

我尝试构建一个以 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

为什么我们不能像下图那样做连续特征向量的一维卷积? enter link description here

【问题讨论】:

    标签: tensorflow keras deep-learning vgg-net


    【解决方案1】:

    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)
    

    【讨论】:

    • 感谢您的回答。我正在使用视频帧来训练数据。我希望帧外的 4096 个向量,并通过使用 5 的内核大小连接 5 个帧来执行时间序列卷积。
    • 您需要一个输入形状为(5,224,224,3) 的模型,该模型使用TimeDistributed(model)(inputs),您的图层将紧随其后。
    • 您能否在您的回答中详细说明如何做到这一点!抱歉,我是 Keras 的新手
    • 实际上如何更改输入形状这将影响 vgg 架构,
    • 好的,但是请解释一下你想用那个卷积做什么,你没有在任何地方连接它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-06
    • 2021-12-31
    • 2017-08-27
    • 1970-01-01
    • 2022-07-11
    • 2019-01-31
    • 2018-12-07
    相关资源
    最近更新 更多