【问题标题】:Unfolding Keras model summary for two joint sequential models展开两个联合序列模型的 Keras 模型总结
【发布时间】:2022-01-11 04:49:03
【问题描述】:

我有两个名为编码器的 Keras 模型,一个使用以下代码加入的解码器:-

   model = tf.keras.Sequential()
   model.add(encoder)
   model.add(decoder)

在摘要中(使用 final_model.summary() ),我得到以下输出:-

有什么方法可以扩展sequential_16sequential_17(检查附上的图片)以查看所有图层?这是编码器和解码器的代码:-

def vgg16_encoder(input_shape):
    model = Sequential()
    model.add(Conv2D(64, (3,3), padding ="same", activation = "relu", input_shape=input_shape))
    model.add(Conv2D(64, (3,3), padding ="same", activation = "relu"))
    model.add(MaxPooling2D((2,2), strides=(2, 2)))
    model.add(Conv2D(128, (3,3), padding = "same", activation = "relu"))
    model.add(Conv2D(128, (3,3), padding = "same", activation = "relu"))
    model.add(MaxPooling2D((2,2), strides=(2, 2)))
    model.add(Conv2D(256, (3,3), padding = "same", activation = "relu"))
    model.add(Conv2D(256, (3,3), padding = "same", activation = "relu"))
    model.add(Conv2D(256, (3,3), padding = "same", activation = "relu"))
    model.add(MaxPooling2D((2,2), strides=(2, 2), name = 'block3_pool'))
    model.add(Conv2D(512, (3,3), padding = "same", activation = "relu"))
    model.add(Conv2D(512, (3,3), padding = "same", activation = "relu"))
    model.add(Conv2D(512, (3,3), padding = "same", activation = "relu"))
    model.add(MaxPooling2D((2,2), strides=(2, 2), name = 'block4_pool'))
    model.add(Conv2D(512, (3,3), padding = "same", activation = "relu"))
    model.add(Conv2D(512, (3,3), padding = "same", activation = "relu"))
    model.add(Conv2D(512, (3,3), padding = "same", activation = "relu"))
    model.add(MaxPooling2D((2,2), strides=(2, 2), name = 'block5_pool'))
    model.add(Flatten(name='flatten'))
    return model
def decoder():
    model = tf.keras.Sequential()
    dropout = 0.4 
    depth = 64 *4
    dim = 8
    model.add(Dense(dim*dim*depth, input_dim=2048))
    model.add(BatchNormalization(momentum=0.9)) 
    model.add(Activation('relu'))
    model.add(Reshape((dim, dim, depth))) 
    model.add(Dropout(dropout)) 
    model.add(UpSampling2D())
    model.add(Conv2DTranspose(int(depth/2), 5, padding='same'))
    model.add(BatchNormalization(momentum=0.9))
    model.add(Activation('relu'))
    model.add(UpSampling2D())
    model.add(Conv2DTranspose(int(depth/4), 5, padding='same')) 
    model.add(BatchNormalization(momentum=0.9))
    model.add(Activation('relu')) 
    model.add(Conv2DTranspose(int(depth/8), 5, padding='same')) 
    model.add(BatchNormalization(momentum=0.9)) 
    model.add(Activation('relu'))
    model.add(UpSampling2D())
    model.add(Conv2DTranspose(3, 5, padding='same'))
    model.add(Activation('tanh'))
    return model
def autoencoder(encoder , decoder):
    model = tf.keras.Sequential()
    model.add(encoder)
    model.add(decoder)
    return model
IMG_WIDTH = 64
IMG_HEIGHT = 64
encoder = vgg16_encoder((IMG_HEIGHT, IMG_WIDTH,3))
decoder=decoder()
model=autoencoder(encoder,decoder)

注意:我使用的是 Tensorflow 版本:- 2.4.0。我对查看单个模型(编码器、解码器)摘要不感兴趣,但对它们的联合模型摘要感兴趣。

【问题讨论】:

  • 给一个可复现的代码,你的tf版本是多少?
  • @M.Innat 我已将其添加到问题中。并且可能不是版本问题,而是 tf 的默认行为。
  • 请添加即插即用代码。使用当前给定的代码,它会给出TypeError: The added layer must be an instance of class Layer. Received: layer=<function encoder at 0x7fee874875f0> of type <class 'function'>
  • Ops,我已经放了功能 API 代码,检查新的,我也测试过。只需复制并粘贴所有代码

标签: python tensorflow keras


【解决方案1】:

tf 2.7 中有一个名为 expand_nested=True 的参数,用于公开内部嵌套循环层的模型汇总方法(issuepr)。但由于您使用的是相对较旧的版本,tf 2.4,您可以采用我的以下解决方法,

def summary_plus(layer, i=0):
    if hasattr(layer, 'layers'):
        if i != 0: 
            layer.summary()
        for l in layer.layers:
            i += 1
            summary_plus(l, i=i)

summary_plus(model) # OK 

【讨论】:

  • 打电话给print(model.layers[0].summary())print(model.layers[1].summary())不是更方便吗?
  • @AloneTogether,是的,这将有助于理解 i in range(0,len(model.layers)) 的代码: print(model.layers[i].summary()) 但是,M .Innat 你的代码信息量很大。感谢 M.Innat 和 AlongTogether 的回答。
  • @AloneTogether 当然。但是如果我们在autoencoder 函数中有更多的子模型怎么办。要获得摘要,我们需要手动浏览它们中的每一个。那么,拥有一个功能来完成这项工作不是很方便吗?同样,这只是一个简单的解决方法,可以有更好的方法。
【解决方案2】:

我建议尝试将model.summary()expanded_nested 参数设置为True,这将扩展嵌套模型,如docs 中所述(在旧的TF 版本中不存在)。它不是最漂亮的输出,但它可以完成工作:

print(model.summary(expand_nested=True))
Model: "sequential_5"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 sequential_3 (Sequential)   (None, 2048)              14714688  
|¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯|
| conv2d_13 (Conv2D)        (None, 64, 64, 64)        1792      |
|                                                               |
| conv2d_14 (Conv2D)        (None, 64, 64, 64)        36928     |
|                                                               |
| max_pooling2d_2 (MaxPooling  (None, 32, 32, 64)     0         |
| 2D)                                                           |
|                                                               |
| conv2d_15 (Conv2D)        (None, 32, 32, 128)       73856     |
|                                                               |
| conv2d_16 (Conv2D)        (None, 32, 32, 128)       147584    |
|                                                               |
| max_pooling2d_3 (MaxPooling  (None, 16, 16, 128)    0         |
| 2D)                                                           |
|                                                               |
| conv2d_17 (Conv2D)        (None, 16, 16, 256)       295168    |
|                                                               |
| conv2d_18 (Conv2D)        (None, 16, 16, 256)       590080    |
|                                                               |
| conv2d_19 (Conv2D)        (None, 16, 16, 256)       590080    |
|                                                               |
| block3_pool (MaxPooling2D)  (None, 8, 8, 256)       0         |
|                                                               |
| conv2d_20 (Conv2D)        (None, 8, 8, 512)         1180160   |
|                                                               |
| conv2d_21 (Conv2D)        (None, 8, 8, 512)         2359808   |
|                                                               |
| conv2d_22 (Conv2D)        (None, 8, 8, 512)         2359808   |
|                                                               |
| block4_pool (MaxPooling2D)  (None, 4, 4, 512)       0         |
|                                                               |
| conv2d_23 (Conv2D)        (None, 4, 4, 512)         2359808   |
|                                                               |
| conv2d_24 (Conv2D)        (None, 4, 4, 512)         2359808   |
|                                                               |
| conv2d_25 (Conv2D)        (None, 4, 4, 512)         2359808   |
|                                                               |
| block5_pool (MaxPooling2D)  (None, 2, 2, 512)       0         |
|                                                               |
| flatten (Flatten)         (None, 2048)              0         |
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
 sequential_4 (Sequential)   (None, 64, 64, 3)         34715075  
|¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯|
| dense_1 (Dense)           (None, 16384)             33570816  |
|                                                               |
| batch_normalization_4 (Batc  (None, 16384)          65536     |
| hNormalization)                                               |
|                                                               |
| activation_5 (Activation)  (None, 16384)            0         |
|                                                               |
| reshape_1 (Reshape)       (None, 8, 8, 256)         0         |
|                                                               |
| dropout_1 (Dropout)       (None, 8, 8, 256)         0         |
|                                                               |
| up_sampling2d_3 (UpSampling  (None, 16, 16, 256)    0         |
| 2D)                                                           |
|                                                               |
| conv2d_transpose_4 (Conv2DT  (None, 16, 16, 128)    819328    |
| ranspose)                                                     |
|                                                               |
| batch_normalization_5 (Batc  (None, 16, 16, 128)    512       |
| hNormalization)                                               |
|                                                               |
| activation_6 (Activation)  (None, 16, 16, 128)      0         |
|                                                               |
| up_sampling2d_4 (UpSampling  (None, 32, 32, 128)    0         |
| 2D)                                                           |
|                                                               |
| conv2d_transpose_5 (Conv2DT  (None, 32, 32, 64)     204864    |
| ranspose)                                                     |
|                                                               |
| batch_normalization_6 (Batc  (None, 32, 32, 64)     256       |
| hNormalization)                                               |
|                                                               |
| activation_7 (Activation)  (None, 32, 32, 64)       0         |
|                                                               |
| conv2d_transpose_6 (Conv2DT  (None, 32, 32, 32)     51232     |
| ranspose)                                                     |
|                                                               |
| batch_normalization_7 (Batc  (None, 32, 32, 32)     128       |
| hNormalization)                                               |
|                                                               |
| activation_8 (Activation)  (None, 32, 32, 32)       0         |
|                                                               |
| up_sampling2d_5 (UpSampling  (None, 64, 64, 32)     0         |
| 2D)                                                           |
|                                                               |
| conv2d_transpose_7 (Conv2DT  (None, 64, 64, 3)      2403      |
| ranspose)                                                     |
|                                                               |
| activation_9 (Activation)  (None, 64, 64, 3)        0         |
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
=================================================================
Total params: 49,429,763
Trainable params: 49,396,547
Non-trainable params: 33,216
_________________________________________________________________
None

对于较旧的 TF 版本,只需运行 print(model.layers[0].summary())print(model.layers[1].summary())

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-24
    • 1970-01-01
    • 2019-11-11
    • 1970-01-01
    相关资源
    最近更新 更多