Skip to content

feat: Add layered architecture visualization#22780

Open
AlanPonnachan wants to merge 2 commits into
keras-team:masterfrom
AlanPonnachan:feat/layered-visualization
Open

feat: Add layered architecture visualization#22780
AlanPonnachan wants to merge 2 commits into
keras-team:masterfrom
AlanPonnachan:feat/layered-visualization

Conversation

@AlanPonnachan
Copy link
Copy Markdown
Contributor

Description

This PR introduces keras.visualization.layered_view(), a new built-in visualization feature that renders Keras models as highly customizable 3D isometric block diagrams. This provides an "at a glance" understanding of model architectures, complementing the existing 2D plot_model utility.

Related issue #21421.

Usage Example

import keras
from keras import layers

model = keras.Sequential([
    keras.Input(shape=(32, 32, 3), name="images"),
    layers.Conv2D(32, 3, activation="relu", padding="same"),
    layers.MaxPooling2D(),
    layers.Conv2D(64, 3, activation="relu", padding="same"),
    layers.MaxPooling2D(),
    layers.Flatten(),
    layers.Dense(128, activation="relu"),
    layers.Dense(10, activation="softmax"),
])

# Generate a 3D isometric view
keras.visualization.layered_view(model, to_file="model.png")

Example outputs

Default 3D View with funnels
1_pr_default_3d

2D Flat Mode
2_pr_flat_2d

3D View without Funnels
3_pr_no_funnels

Adding a Legend
4_pr_legend_and_functional

Custom Text
5_pr_custom_text

Custom Color Map and Spacing
6_pr_custom_colors_spacing

Contributor Agreement

  • I am a human, and not a bot.
  • I will be responsible for responding to review comments in a timely manner.
  • I will work with the maintainers to push this PR forward until submission.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces layered_view, a new visualization tool for Keras models that renders architecture diagrams using aggdraw and Pillow. The implementation includes lazy loading for dependencies and comprehensive tests. Feedback focuses on ensuring the visualization accurately represents non-linear model topologies, adhering to Keras API design guidelines regarding argument count and docstring formatting (including missing Arguments and Example sections), improving error message clarity for unbuilt models, and using more robust introspection for the text_callable argument.

Comment thread keras/src/visualization/layered_view.py
Comment thread keras/src/visualization/layered_view.py
Comment thread keras/src/visualization/layered_view.py Outdated
Comment thread keras/src/visualization/layered_view.py Outdated
Comment thread keras/src/visualization/layered_view.py
@AlanPonnachan
Copy link
Copy Markdown
Contributor Author

@hertschuh Could you please take a look at this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants