【问题标题】:Use keras mobilenet model使用 keras mobilenet 模型
【发布时间】:2018-10-07 20:16:18
【问题描述】:

我正在尝试使用 Keras 的 MobileNet 进行图像分类。我的输入形状是(64, 64, 3),我的数据集中有两个类。我不想使用经过训练的权重。

这是我的代码。

model = MobileNet(weights=None, include_top=True, input_shape=(64, 64, 3), classes=2)

我的问题是,include_top 应该是 True 还是 False? 既然官方这么说了,

input_shape: 可选形状元组,仅当 include_top 为 False 时才指定

include_top:是否在网络顶部包含全连接层。

我想做图像分类,所以我认为我的最后一层应该是全连接的。对吗?

谢谢。

【问题讨论】:

    标签: python keras


    【解决方案1】:

    如果您想传递 (64,64,3) 的输入形状,那么您需要

    include_top=False
    

    是的,你最后需要全连接层。你必须建立自己的网络顶层。您的模型将是这样的

    base_model= MobileNet(weights=None, include_top=False, input_shape=(64, 64, 3))
    x=base_model.output
    x=Flatten()(x)
    x=Dense(...)(x)
    .
    .
    # Softmax layer for classification
    predictions=Dense(2,activation='softmax')
    model=(x=base_model.input,output=predictions)
    

    我不知道为什么我们需要移除全连接层来改变 input_shape,但我认为上面的解决方案会起作用

    编辑:

    如果 input_shape 不同,为什么要将 include_top 设置为 False?

    因为它在最后将输入大小更改为全连接层。
    进一步解释参考this答案

    【讨论】:

    • 感谢您的回复。我知道我可以像你说的那样自己构建密集层。但我只是想确定我上面写的代码是否正确。其实我试过了,它奏效了。如果 input_shape 不同,为什么要将 include_top 设置为 False?
    • @TeresaK。是的,你的代码是正确的。参考编辑
    猜你喜欢
    • 2020-12-23
    • 1970-01-01
    • 2019-06-21
    • 2019-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多