【问题标题】:Keras model.summary function displays incosistent output formatKeras model.summary 函数显示不一致的输出格式
【发布时间】:2019-06-25 00:52:28
【问题描述】:

我正在研究 Keras 的来龙去脉。所以,在这方面,我正在检查model.summary() 函数。

我使用的是 Keras 自己提供的简单 image classification example,并加载了提供的各种预训练模型(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       

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

这看起来很奇怪,我不确定这里发生了什么。例如,从这个问题here(直到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。

【问题讨论】:

  • 您应该包含产生这两种结果的实际代码,包括导入。

标签: python keras


【解决方案1】:

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

ModelSequential 都有一个 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 

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

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

【讨论】:

  • 是的,您是对的,但有两个观察结果:1) API 在过去应该有所不同,因为我引用的问题使用了Sequential 模型并且它打印了Connected to 和 2) 似乎 Keras在显示 Connected to 之前检查实际的非顺序状态(即使选择了模型)。
  • 你说得对,是我的错,我不专心。实际上print_summary 不仅可以处理Sequential,还可以处理任何没有复杂连接层的模型-here 以及在此行之上放置类型选择逻辑。可能我会编辑我的答案
猜你喜欢
  • 2020-03-15
  • 1970-01-01
  • 2019-07-23
  • 2012-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多