【发布时间】: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
_________________________________________________________________
理想情况下,随着输入形状的增加,参数的数量应该会增加,但是您可以看到它们保持完全相同。因此,我的问题是:
- 为什么参数个数不会随着
input_shape的变化而变化? - 我在
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