首页 > 解决方案 > Keras model.summary 函数显示不一致的输出格式

问题描述

我正在研究 Keras 的进出。因此,在这方面,我正在检查model.summary()功能。

我使用的是 Keras 本身提供的简单图像分类示例,并加载了提供的各种预训练模型(Xception、VGG16 等)。

model.summary()我使用上述方法检查了每个模型架构。然后我注意到由于某种原因该列Connected to(即第 4 列)并未出现在每个模型摘要中。例如MobileNetV2我得到(只显示前几行):

__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_1 (InputLayer)            (None, 224, 224, 3)  0                                            
__________________________________________________________________________________________________
Conv1_pad (ZeroPadding2D)       (None, 225, 225, 3)  0           input_1[0][0]                    
__________________________________________________________________________________________________
Conv1 (Conv2D)                  (None, 112, 112, 32) 864         Conv1_pad[0][0]      

MobileNet我得到:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         (None, 224, 224, 3)       0         
_________________________________________________________________
conv1_pad (ZeroPadding2D)    (None, 225, 225, 3)       0         
_________________________________________________________________
conv1 (Conv2D)               (None, 112, 112, 32)      864       

此输出在模型加载后执行,无需采取任何额外操作(无训练、无推理等)。

这似乎很奇怪,我不确定这里发生了什么。例如,当从这里的这个问题(直到model0.fit(...)部分)创建这个简单的模型并运行时model0.summary(),我给出了一个没有Connected to列的摘要,也与这个问题中发布的摘要相反。

那么,输出的这种变化呢?有什么关系model.summary()?我们是否对输出有一些控制(尽管上面的例子并不暗示)?或者输出与模型的结构方式有关?

编辑:

我添加了(简单的)代码,用于根据评论中的要求重现两个模型的摘要。

from keras.applications.mobilenet_v2 import MobileNetV2
from keras.applications.mobilenet import MobileNet

model1 = MobileNetV2(weights='imagenet')
print(model1.summary())
model2 = MobileNet(weights='imagenet')
print(model2.summary())

此外,如果这些信息在某种程度上有用,我的系统使用 Keras 2.2.4、Tensorflow 1.12.0 和 Ubuntu 16.04。

标签: pythonkeras

解决方案


我想原因是:MobileNetV2已实现keras.Model,但 MobileNet 是keras.Sequential.

两者Model都有Sequential方法summary。在运行时,它调用print_summary方法,该方法对顺序模型和非顺序模型的作用不同:

if sequential_like:
    line_length = line_length or 65
    positions = positions or [.45, .85, 1.]
    if positions[-1] <= 1:
        positions = [int(line_length * p) for p in positions]
    # header names for the different log elements
    to_display = ['Layer (type)', 'Output Shape', 'Param #']
else:
    line_length = line_length or 98
    positions = positions or [.33, .55, .67, 1.]
    if positions[-1] <= 1:
        positions = [int(line_length * p) for p in positions]
    # header names for the different log elements
    to_display = ['Layer (type)',
                  'Output Shape',
                  'Param #',
                  'Connected to']
    relevant_nodes = []
    for v in model._nodes_by_depth.values():
        relevant_nodes += v 

链接)。如您所见,它只是不打印'Connected to'类似顺序的模型。
我猜原因是顺序模型不允许以非顺序顺序连接层 - 所以,它们只是一个接一个地连接。

model.__class__.__name__ == 'Sequential'此外,它通过(链接)检查模型类型。我怀疑尝试“即时”更改它以获得不同的输出是一个好主意。


推荐阅读