【问题标题】:how does input_shape in keras.applications work?keras.applications 中的 input_shape 是如何工作的?
【发布时间】:2019-07-14 01:08:15
【问题描述】:

我已经使用了 Keras documentation,但我仍然无法弄清楚 input_shape 参数是如何工作的,以及为什么当我将自定义输入形状传递给我的 DenseNet 模型时它不会改变我的 DenseNet 模型的参数数量。一个例子:

import keras
from keras import applications
from keras.layers import Conv3D, MaxPool3D, Flatten, Dense
from keras.layers import Dropout, Input, BatchNormalization
from keras import Model

# define model 1
INPUT_SHAPE = (224, 224, 1) # used to define the input size to the model
n_output_units = 2
activation_fn = 'sigmoid'
densenet_121_model = applications.densenet.DenseNet121(include_top=False, weights=None, input_shape=INPUT_SHAPE, pooling='avg')
inputs = Input(shape=INPUT_SHAPE, name='input')
model_base = densenet_121_model(inputs)
output = Dense(units=n_output_units, activation=activation_fn)(model_base)
model = Model(inputs=inputs, outputs=output)
model.summary()

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input (InputLayer)           (None, 224, 224, 1)       0         
_________________________________________________________________
densenet121 (Model)          (None, 1024)              7031232   
_________________________________________________________________
dense_1 (Dense)              (None, 2)                 2050      
=================================================================
Total params: 7,033,282
Trainable params: 6,949,634
Non-trainable params: 83,648
_________________________________________________________________



# define model 2
INPUT_SHAPE = (512, 512, 1) # used to define the input size to the model
n_output_units = 2
activation_fn = 'sigmoid'
densenet_121_model = applications.densenet.DenseNet121(include_top=False, weights=None, input_shape=INPUT_SHAPE, pooling='avg')
inputs = Input(shape=INPUT_SHAPE, name='input')
model_base = densenet_121_model(inputs)
output = Dense(units=n_output_units, activation=activation_fn)(model_base)
model = Model(inputs=inputs, outputs=output)
model.summary()
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input (InputLayer)           (None, 512, 512, 1)       0         
_________________________________________________________________
densenet121 (Model)          (None, 1024)              7031232   
_________________________________________________________________
dense_2 (Dense)              (None, 2)                 2050      
=================================================================
Total params: 7,033,282
Trainable params: 6,949,634
Non-trainable params: 83,648
_________________________________________________________________

理想情况下,随着输入形状的增加,参数的数量应该会增加,但是您可以看到它们保持完全相同。因此,我的问题是:

  1. 为什么参数个数不会随着input_shape的变化而变化?
  2. 我在input_shape 中只定义了一个通道,在这种情况下我的模型训练会发生什么情况?文档说明如下:

input_shape:可选的形状元组,仅在 include_top 时指定 为 False (否则输入形状必须为 (224, 224, 3) (使用 'channels_last' 数据格式)或 (3, 224, 224) (使用 'channels_first' 数据格式)。它应该正好有 3 个输入通道,宽度和 高度不应小于 32。例如(200, 200, 3) 将是一 有效值。

但是,当我使用此配置运行模型时,它运行时没有任何问题。我会错过什么吗?

使用 Keras 2.2.4 和 Tensorflow 1.12.0 作为后端。

【问题讨论】:

    标签: python tensorflow keras


    【解决方案1】:

    DenseNet由两部分组成,卷积部分和全局池化部分。

    卷积部分的可训练权重的数量不取决于输入形状。

    通常,分类网络应该使用全连接层来推断分类,但是,在DenseNet 中,使用了global pooling,并且不会带来任何可训练的权重。

    因此,输入形状不会影响整个网络的权重数量。

    【讨论】:

      【解决方案2】:

      1。 在卷积层中,输入大小不影响权重的数量,因为权重的数量由内核矩阵维度决定。较大的输入尺寸会导致较大的输出尺寸,但不会导致权重数量的增加。

      这意味着,第二个模型的卷积层的输出大小将大于第一个模型,这将增加后续密集层的权重数量。但是,如果您查看 DenseNet 的架构,您会注意到在所有卷积层之后有一个GlobalMaxPooling2D 层,它平均每个输出通道的所有值。这就是 DenseNet 的输出大小为 1024 的原因,无论输入形状如何。

      2。 是的,该模型仍然有效。我对此并不完全确定,但我的猜测是单个频道将被广播(复制)以填充所有三个频道。至少这些事情通常是这样处理的(例如参见tensorflownumpy)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-12-24
        • 2019-06-28
        • 1970-01-01
        • 2020-01-22
        • 2021-08-28
        • 2017-05-13
        • 2021-10-23
        • 2020-07-24
        相关资源
        最近更新 更多