我有兩個名為編碼器的 Keras 模型,一個使用以下代碼加入的解碼器:-
model = tf.keras.Sequential()
model.add(encoder)
model.add(decoder)
在摘要中(使用 final_model.summary() ),我得到以下輸出:-

有什么方法可以擴展sequential_16& sequential_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。我對查看單個模型(編碼器、解碼器)摘要不感興趣,但對它們的聯合模型摘要感興趣。
uj5u.com熱心網友回復:
我建議嘗試將expanded_nested引數設定為model.summary()to True,這將擴展檔案中所述的嵌套模型(舊 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()).
uj5u.com熱心網友回復:
有一個expand_nested=True從tf 2.7呼叫的引數用于模型摘要方法,該方法將公開內部嵌套回圈層(issue,pr)。但是由于您使用的是相對較舊的版本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
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/408853.html
標籤:
