LeNet Style#
LeNet-style visualization for neural networks.
Overview#
The LeNet renderer displays neural networks using classic “feature map stack” diagrams. Each layer is rendered as a 2D representation of its output channels flowing left to right, creating distinctive, publication-quality architectural diagrams.
- When to use:
Academic papers and technical publications
CNNs where channel progression is important to visualize
Creating distinctive, retro-styled architecture diagrams
Detailed architectural analysis at the channel level
Presentations emphasizing classical CNN concepts
- When NOT to use:
Understanding computational structure (use graph or functional)
Simple learning/teaching (use layered mode)
Models with complex non-sequential components
Deep models (>100 layers) without customization
API Reference#
- visualkeras.lenet.lenet_view(model, to_file=None, min_xy=20, max_xy=220, scale_xy=4.0, type_ignore=None, index_ignore=None, color_map=None, background_fill='black', padding=20, layer_spacing=40, map_spacing=4, max_visual_channels=12, connector_fill='gray', connector_width=1, patch_fill='#7db7ff', patch_outline='black', patch_scale=1.0, patch_alpha_on_image=140, seed=None, draw_connections=True, draw_patches=True, font=None, font_color='white', top_label_callable=None, bottom_label_callable=None, top_label=True, bottom_label=True, top_label_padding=6, bottom_label_padding=6, styles=None, *, options=None, preset=None)[source]#
Render a Keras model using a LeNet-style feature-map diagram.
This renderer emphasizes stacked feature maps and left-to-right progression. It is especially useful for CNN-focused figures, publication graphics, and teaching material that should resemble classic LeNet-style architecture diagrams.
- Parameters:
model (Any) –
Keras model instance to visualize.
LeNet view works best for sequential or mostly sequential models where channel progression and stage-by-stage feature-map flow are the main story.
to_file (str, optional) –
Path to save the rendered image. The image format is inferred from the file extension.
The rendered
PIL.Imageis returned whether or not this value is supplied. Use this when you want to save the figure and keep working with the in-memory image.min_xy (int, default=20) –
Minimum rendered width and height for a feature-map face in pixels.
This prevents small feature maps from becoming visually insignificant in diagrams that also contain much larger spatial layers.
max_xy (int, default=220) –
Maximum rendered width and height for a feature-map face in pixels.
Use this to keep very large early feature maps from dominating the overall composition.
scale_xy (float, default=4.0) –
Multiplier applied to feature-map width and height before clamping.
This is one of the main controls for the overall apparent scale of the stacks in the figure.
type_ignore (sequence of type, optional) –
Layer classes to exclude from rendering.
This is useful for hiding utility layers that add noise to the diagram without changing the main architectural story.
index_ignore (sequence of int, optional) –
Layer indices to exclude from rendering.
Use this when you need precise control over individual layers rather than excluding every instance of a class.
color_map (mapping, optional) –
Mapping from layer class to broad style values such as
fillandoutline.This is the quickest way to define a consistent color language by layer type. Use
styleswhen you need per-layer overrides.background_fill (Any, default='black') –
Background color for the final image.
Dark backgrounds often work well in LeNet mode because they increase contrast with the stacked feature maps and connectors.
padding (int, default=20) –
Outer padding around the full diagram in pixels.
Increase this when labels or wide stacks feel too close to the image boundary.
layer_spacing (int, default=40) –
Horizontal spacing between successive stacks.
This is the main control for how compact or open the left-to-right flow feels.
map_spacing (int, default=4) –
Offset between visible feature maps within a stack.
Larger values emphasize the layered stack effect. Smaller values create a tighter and more compact look.
max_visual_channels (int, default=12) –
Maximum number of feature maps to draw for a single layer.
This keeps high-channel layers readable. Additional channels are still represented conceptually, but they are not all drawn individually.
connector_fill (Any, default='gray') –
Color used for connections between stacks.
Neutral connector colors are usually best because the stack fills and labels already carry most of the semantic styling.
connector_width (int, default=1) –
Line width used for connections between stacks.
Increase this for large exported figures or diagrams intended for presentation screens.
patch_fill (Any, default='#7db7ff') –
Default fill color used for receptive-field or projection patches.
Patches help explain how one stack maps into the next. Choose a color that remains visible against both the stack fill and the background.
patch_outline (Any, default='black') –
Outline color used for receptive-field or projection patches.
A clear outline helps patches remain visible even when the fill color is partially transparent.
patch_scale (float, default=1.0) –
Relative size multiplier applied to connector patches.
Increase this when patches should read more prominently. Reduce it when they distract from the stacks themselves.
patch_alpha_on_image (int, default=140) –
Alpha value used when a patch is drawn over an embedded face image.
Lower values let the underlying image remain more visible. Higher values make the patch read more strongly.
seed (int, optional) –
Seed used for deterministic placement of randomized patch elements.
Set this when you want repeated renders to remain visually consistent.
draw_connections (bool, default=True) –
If
True, draw connections between successive stacks.Disable this when you want a cleaner figure that focuses only on the stacks and labels.
draw_patches (bool, default=True) –
If
True, draw receptive-field and projection patches where applicable.Patches are often useful in explanatory figures, but turning them off can simplify the diagram considerably.
font (PIL.ImageFont.ImageFont, optional) –
Font used for top and bottom labels.
A custom font is useful when the figure needs to match an existing document or slide style.
font_color (Any, default='white') –
Text color used for labels.
This should contrast clearly with
background_filland remain legible against the rendered stacks.top_label_callable (callable, optional) –
Callable receiving
(layer, render_shape)and returning the label to place above a stack.This is the main hook for custom top annotations such as tensor sizes or stage identifiers.
bottom_label_callable (callable, optional) –
Callable receiving
(layer, render_shape)and returning the label to place below a stack.This is often used for layer names or layer types while the top label communicates tensor shape.
top_label (bool, default=True) –
If
True, render top labels.Disable this when the figure should stay compact or when only one label position is needed.
bottom_label (bool, default=True) –
If
True, render bottom labels.This is often paired with
top_labelto split different kinds of information across two lines of annotation.top_label_padding (int, default=6) –
Vertical padding between a stack and its top label.
Increase this when multiline labels or large fonts feel cramped.
bottom_label_padding (int, default=6) –
Vertical padding between a stack and its bottom label.
Increase this when bottom labels collide with other elements or need more breathing room.
styles (mapping, optional) –
Fine-grained per-layer style overrides keyed by layer name or layer class.
Use this for embedded images, per-layer patch settings, stack spacing overrides, and other local adjustments that are too specific for
color_map. Supported keys includeface_image,face_image_fit,face_image_alpha, andface_image_inset.options (LenetOptions or mapping, optional) –
Configuration bundle applied after
presetand before explicit keyword arguments.Use this when you want to reuse a LeNet-style configuration across multiple models or examples.
preset (str, optional) –
Name of a preset from
visualkeras.LENET_PRESETS. LeNet mode currently providesdefault,compact, andpresentation.Presets are intended as convenient starting points rather than fixed modes. They can be refined further with
optionsand explicit overrides.
- Returns:
Rendered LeNet-style diagram.
- Return type:
PIL.Image.Image
Notes
Configuration precedence is
presetfollowed byoptionsfollowed by explicit keyword arguments.Full documentation: https://visualkeras.readthedocs.io/en/latest/api/lenet_style.html
Configuration Options#
Use visualkeras.options.LenetOptions when you want to reuse a
LeNet-style configuration across several figures. Curated presets are available
through visualkeras.options.LENET_PRESETS.
Key Parameters#
Core Parameters
model: Keras model instance (typically Sequential or simple Functional CNNs)to_file: Path to save output (optional)preset: Use preset configuration (‘default’, ‘compact’, ‘presentation’)options: LenetOptions object for bundled configuration
Layout Control
layer_spacing: Space between layers (default: 40)map_spacing: Space between individual channels (default: 4)scale_xy: Width/height scale multiplier (default: 4.0)min_xy,max_xy: Min/max feature map size (pixels)max_visual_channels: Limit channels shown (default: 12)padding: Border space (default: 20)
Styling
background_fill: Background color (default: ‘black’)connector_fill: Connection line color (default: ‘gray’)connector_width: Line thickness (default: 1)patch_fill: Feature map color (default: ‘#7db7ff’)patch_alpha_on_image: Transparency (default: 140)color_map: Dict mapping layer types to{'fill': '...', ...}
Content Control
top_label_callable: Function controlling text above layersbottom_label_callable: Function controlling text below layersdraw_connections: Show connections between layers (default: True)draw_patches: Show patch visualizations (default: True)top_label,bottom_label: Show/hide label positions (default: True)
Advanced
styles: Per-layer style overrides includingface_imageseed: Fixed seed for reproducible randomizationtype_ignore: Layer types to skipindex_ignore: Layer indices to skip
Usage Examples#
Basic Usage#
import visualkeras
image = visualkeras.show(model, mode='lenet')
image.show()
With Presets#
# Publication quality
image = visualkeras.show(model, mode='lenet', preset='presentation')
# Compact for slides
image = visualkeras.show(model, mode='lenet', preset='compact')
Custom Label Functions#
import visualkeras
def top_label(layer, shape):
return f"{shape.dH}×{shape.dW}×{shape.dZ}"
def bottom_label(layer, shape):
return layer.__class__.__name__
image = visualkeras.show(
model,
mode='lenet',
top_label_callable=top_label,
bottom_label_callable=bottom_label,
)
With Embedded Images#
from visualkeras.options import LenetOptions
styles = {
0: {
'face_image': 'kernel_viz.png',
'face_image_fit': 'cover',
'face_image_alpha': 200,
}
}
options = LenetOptions(styles=styles)
image = visualkeras.show(model, mode='lenet', options=options)
See Also#
LeNet View for detailed LeNet examples
Tutorial 1: Basic Visualization for tutorial
Advanced Styling Reference for advanced styling
visualkeras.options.LenetOptionsfor the full options APIOptions for presets and shared options documentation