python - 了解 model.summary Keras

标签 python tensorflow keras

我试图理解 Keras 中的 model.summary()。我有以下卷积神经网络。第一个卷积的值是:

conv2d_4 (Conv2D)            (None, 148, 148, 16)      448 

148和448从何而来?

代码

image_input = layers.Input(shape=(150, 150, 3))
x = layers.Conv2D(16, 3, activation='relu')(image_input)

x = layers.MaxPooling2D(2)(x)
x = layers.Conv2D(32, 3, activation='relu')(x)

x = layers.MaxPooling2D(2)(x)
x = layers.Conv2D(64, 3, activation='relu')(x)

x = layers.MaxPooling2D(2)(x)
x = layers.Flatten()(x)
x = layers.Dense(512, activation='relu')(x)
output = layers.Dense(1, activation='sigmoid')(x)

# Keras Model definition
# input = input feature map
# output = input feature map + stacked convolution/maxpooling layers + fully connected layer + sigmoid output layer
model = Model(image_input, output)
model.summary()

输出

Layer (type)                 Output Shape              Param #   
=================================================================
input_2 (InputLayer)         (None, 150, 150, 3)       0         
_________________________________________________________________
conv2d_4 (Conv2D)            (None, 148, 148, 16)      448       
_________________________________________________________________
max_pooling2d_4 (MaxPooling2 (None, 74, 74, 16)        0         
_________________________________________________________________
conv2d_5 (Conv2D)            (None, 72, 72, 32)        4640      
_________________________________________________________________
max_pooling2d_5 (MaxPooling2 (None, 36, 36, 32)        0         
_________________________________________________________________
conv2d_6 (Conv2D)            (None, 34, 34, 64)        18496     
_________________________________________________________________
max_pooling2d_6 (MaxPooling2 (None, 17, 17, 64)        0         
_________________________________________________________________
flatten_1 (Flatten)          (None, 18496)             0         
_________________________________________________________________
dense_1 (Dense)              (None, 512)               9470464   
_________________________________________________________________
dense_2 (Dense)              (None, 1)                 513     

最佳答案

来自Keras documentation ,你可以看到填充是 default=valid 所以没有填充,步幅大小是 1。那么你的输出形状显然是 148 x 148。

要计算这个,您可以使用以下公式:

O = (W - K + 2P)/S + 1

其中 O 是输出高度/宽度,W 是输入高度/宽度,K 是过滤器大小,P 是填充,S 是步幅大小。

关于第二个参数,你有一个 16 的特征图,你的内核大小是 3 x 3,所以你有 16 x (3 x 3),也就是 144。然后你有三个颜色 channel ,所以 144 x 3 = 432,然后你需要添加 16 个偏差,这就是 448;)希望这有帮助!

关于python - 了解 model.summary Keras,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45561306/

相关文章:

python - 如何在 Python 中创建和加载 egg 文件?

python - Scapy - 发送 IPv6 路由器广告失败,生命周期 > 0

tensorflow - 如何在 TensorFlow-Slim 中使用正则化?

python - Keras 中自定义损失函数中的访问层属性

python - AttributeError: 'Model' 对象没有属性 'epoch' - Keras

Python:使用 lxml + objectify + findall 或 fromstring 获取特定节点值和属性

java - 微服务应用程序的 kubernetes 架构 - 建议

Tensorflow:使用 tf.parse_example 进行 jpeg 批处理

tensorflow - 在 Tensorflow 中 global_step 是什么意思?

python - 为什么使用 Keras 制作的恢复后的 TensorFlow 模型给出的预测为 0?