You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于自定义代码绘制Keras CNN模型架构?

Hi there! Let's figure out how to visualize your Keras CNN architecture properly, and fix the bugs in your current code first—since those are preventing your visualization (and model definition) from working correctly.

可行的Keras CNN可视化方法

Here are three reliable approaches to visualize your model:

Keras has a built-in function plot_model that generates clean, customizable architecture diagrams. It’s the easiest way without relying on external custom tools.

2. Custom Utilities (Like Your pptx_util/matplotlib_util)

If you want to stick with your custom tools, you’ll need to fix your model code first so these utilities can parse the model structure correctly.

3. Third-Party Tools (e.g., Netron)

For quick, interactive visualization, save your Keras model as an .h5 or .keras file, then open it with Netron—a free open-source tool that supports Keras model files and lets you inspect layers, shapes, and parameters interactively.


Fixing Your Model Code & Adding Visualization

First, let's correct the errors in your code—these are stopping your model from being defined properly, which breaks any visualization attempt:

Original Code Issues:

  • Typo: sequential() → should be Sequential() (capitalized)
  • Wrong module path: keras.layer.* → keras.layers.* (plural "layers")
  • BatchNormalization is a class—you need to instantiate it with ()
  • Missing import: regularizer.l2 requires importing keras.regularizers
  • Layer order mistake: Flatten() must come before dense layers (you can't feed 2D feature maps directly to Dense layers)
  • model.save_fig isn't a native Keras method—replace it with plot_model

Corrected Code with Visualization:

import keras
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, BatchNormalization, Dropout
from keras.regularizers import l2  # Import the L2 regularizer
from keras.utils import plot_model  # Built-in visualization tool

# Your custom utilities (if they work with corrected model)
from pptx_util import save_model_to_pptx
from matplotlib_util import save_model_to_file

input_shape=(33,3840,1)
model = Sequential()  # Fixed capitalization

# C1 Block
model.add(Conv2D(16, (5,5), strides=(2,2), padding='same', activation='relu', input_shape=input_shape))
model.add(MaxPooling2D(pool_size=(2,2), padding='same'))  # Fixed module path
model.add(BatchNormalization())  # Added parentheses to instantiate

# C2 Block
model.add(Conv2D(32, (3,3), strides=(1,1), padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2), padding='same'))  # Fixed module path
model.add(BatchNormalization())  # Added parentheses

# Moved Flatten BEFORE dense layers (critical fix)
model.add(Flatten())

# Fully Connected Block
model.add(Dense(32, kernel_regularizer=l2(0.1)))  # Removed redundant input_dim (inferred from Flatten)
model.add(Dropout(0.6))  # Fixed module path (we imported from keras.layers directly)
model.add(Dense(256, activation='sigmoid'))
model.add(Dropout(0.6))
model.add(Dense(2, activation='softmax'))

# 1. Use Keras built-in plot_model to save visualization
plot_model(
    model,
    to_file="model_architecture.png",
    show_shapes=True,  # Shows input/output shapes for each layer
    show_layer_names=True,
    rankdir="TB"  # Vertical layout (use "LR" for horizontal)
)

# 2. Use your custom utilities (now with a valid model)
save_model_to_pptx(model, "example.pptx")
save_model_to_file(model, "example.pdf")

Additional Tips
  • If you want to visualize the model interactively (zoom, pan, inspect layers), save your model first with model.save("my_cnn.keras"), then open it in Netron.
  • For the custom pptx_util/matplotlib_util tools, make sure they're compatible with your Keras version—if they throw errors, check that they're correctly accessing model.layers and extracting layer properties like type, filters, kernel size, etc.

内容的提问来源于stack exchange,提问作者Edayildiz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:23:44