【问题标题】:How does Keras determine the weights in the first layer of pre-trained models?Keras如何确定第一层预训练模型的权重?
【发布时间】:2021-12-09 11:55:16
【问题描述】:

Keras 预训练模型(VGG、ResNet、DenseNet 等)在 ImageNet 上以输入形状(224、224、3)训练后建立了权重。但是,Keras 允许我们指定任何其他输入形状(宽度和高度不应小于 32)。当输入形状不是(224,224,3)时,Keras如何确定第一个隐藏层的初始权重?

【问题讨论】:

    标签: tensorflow keras conv-neural-network


    【解决方案1】:

    这取决于参数include_top

    例子:

    import tensorflow as tf
    
    model = tf.keras.applications.VGG16(include_top = True, input_shape=(299, 299, 3))
    model.summary()
    

    这将引发错误,因为当您传递 include_top = True 时,整个 VGG16 架构将被加载,包括 Dense 层。

    由于Dense 层关心形状,它会抛出错误。由于Dense 层采用的操作,必须定义形状并与输入形状匹配。

    -- Source Code--


    第二个例子:

    import tensorflow as tf
    
    model = tf.keras.applications.VGG16(include_top = False, input_shape=(299, 299, 3))
    model.summary()
    

    这一次,模型只有卷积层,因为include_top = False卷积层只是图像上的滑动过滤器。所以输入形状对于普通卷积来说不是问题。

    当您传递input_shape 时,Keras 会为该形状创建一个输入层。然后创建模型,然后加载权重。

    -- Source Code--

    这里唯一的限制是,由于这些模型是在 RGB 图像上训练的,因此新图像也应该有 3 个通道。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-12
      • 2018-10-08
      • 2017-10-07
      • 2019-11-28
      • 1970-01-01
      • 1970-01-01
      • 2020-11-28
      相关资源
      最近更新 更多