【问题标题】:Adding new features to the output of Flatten() layer in Keras在 Keras 中向 Flatten() 层的输出添加新功能
【发布时间】:2018-07-06 12:53:09
【问题描述】:

我正在做图像分类。首先,我将图像输入到 Keras 中的 CNN 模型中。

我想在 keras 的 Flatten 层的输出中添加新特征,然后将其馈送到密集层。如何为它编写代码?

基本上我对图像使用卷积,最后我想添加其他功能,如年龄性别等。

max_pool_final = MaxPooling2D(pool_size=(2,2))(conv_final)
flat = Flatten()(max_pool_final)
dense = Dense(128)(flat)

在将平面作为密集层的输入之前,我想向平面添加一些特征。我该怎么做?

感谢您的帮助!

【问题讨论】:

    标签: machine-learning computer-vision keras keras-layer


    【解决方案1】:

    您只需使用 Concatenate 层将这些特征附加到带有新输入层的扁平化向量中:

    otherInp = Input(shape = (n_features, ))
    concatenatedFeatures = Concatenate(axis = 1)([flat, otherInp])
    dense = Dense(128)(concatenatedFeatures)
    

    【讨论】:

    • 我的附加特征的形状为 18324 x 4。但来自我的平面层的输出具有尺寸 (None,512)。这给我一个错误,说除了连接轴之外的尺寸必须完全匹配。那么我该怎么做呢
    • @NikhilMishra 那么你应该围绕轴 1 连接,我会更新我的答案。
    • 但是我如何将我的功能提供给其他输入。做 model.fit([X_train,agp],y) 就足够了吗? Keras 如何仅通过输入形状知道它?
    • @NikhilMishra 您将它们传递给 model.fit,例如 model.fit([images, features], Y, ...) 您的模型现在实际上有两个输入。确保更新您的模型定义以反映这两个输入。
    • Keras 如何区分图像和特征?只是通过输入形状?还是我们需要在模型中指定其他内容?
    猜你喜欢
    • 2017-10-06
    • 1970-01-01
    • 2022-01-23
    • 2018-06-20
    • 2021-12-04
    • 2019-05-07
    • 1970-01-01
    • 2019-01-24
    • 1970-01-01
    相关资源
    最近更新 更多