如何使用 Python 可视化 CNN 模型

数据挖掘 Python
2022-03-14 13:12:38

我是深度学习的新手,一直试图在 Google Colab 中使用 Python 展示 CNN 架构图。除了导入必要的库外,我从其他资源中注意到,通常我们会声明一个模型 {model = sequence()},然后是 model.add(Conv2d 或 MaxPool 或 Activation 等),并用于可视化, print(model.概括())。

然而,我已经使用下面的代码开发了 CNN 架构,并努力生成模型打印。谁能告诉我这里缺少什么。谢谢

input = Input(shape=X.shape[1:])                                  # 154x154x3
x = Conv2D(12, (3, 3), padding='same', activation='relu')(input)  # 154x154x12
x = Conv2D(12, (2, 2), strides=(2, 2), activation='relu')(x)      # 77x77x12
x = Conv2D(16, (3, 3), padding='same', activation='relu')(x)      # 77x77x16
x = Conv2D(16, (2, 2), strides=(2, 2), activation='relu')(x)      # 38x38x16
x = Conv2D(24, (3, 3), padding='same', activation='relu')(x)      # 38x38x24
x = Conv2D(24, (2, 2), strides=(2, 2), activation='relu')(x)      # 19x19x24
x = Conv2D(32, (3, 3), padding='same', activation='relu')(x)      # 19x19x32
x = Conv2D(32, (2, 2), strides=(2, 2), activation='relu')(x)      # 9x9x32
x = Conv2D(48, (3, 3), padding='same', activation='relu')(x)      # 9x9x48
x = Conv2D(48, (2, 2), strides=(2, 2), activation='relu')(x)      # 4x4x48
x = Dropout(0.5)(x)                                               # 4x4x48
x = Conv2D(n_classes, (1, 1))(x)                                  # 4x4x62
x = GlobalAvgPool2D()(x)                                          # 62
output = Activation('softmax')(x)                                 # 62
1个回答

在代码下方添加这两行。

from keras.models import Model
model = Model(inputs=input, outputs=output) 
print(model.summery)