layered_view#

scikitplot.visualkeras.layered_view(model, to_file=None, min_z=20, min_xy=20, max_z=400, max_xy=2000, scale_z=0.1, scale_xy=4, type_ignore=None, index_ignore=None, color_map=None, one_dim_orientation='z', index_2d=None, background_fill='white', draw_volume=True, draw_reversed=False, padding=10, text_callable=None, text_vspacing=4, spacing=10, draw_funnel=True, shade_step=10, legend=False, legend_text_spacing_offset=15, font=None, font_color='black', show_dimension=False, backend=None, show_os_viewer=False, save_fig=True, save_fig_filename='', overwrite=True, add_timestamp=False, verbose=False)[source]#

Generates an architectural visualization for a given linear Keras tf.keras.Model model (i.e., one input and output tensor for each layer) in a layered style, which is particularly suitable for convolutional neural networks (CNNs).

Parameters:
modeltensorflow.keras.Model

A Keras tf.keras.Model model to be visualized.

to_filestr or None

Path to the file where the generated image will be saved. If the image does not exist yet it will be created, else overwritten. The file type is inferred from the file extension. If None, no file is created.

Changed in version 0.4.0: The to_file is now deprecated, and will be removed in a future release. Users are encouraged to use 'save_fig' and 'save_fig_filename' instead for improved compatibility.

min_zint

Minimum z-dimension size (in pixels) for a layer.

min_xyint

Minimum x- and y-dimension size (in pixels) for a layer.

max_zint

Maximum z-dimension size (in pixels) for a layer.

max_xyint

Maximum x- and y-dimension size (in pixels) for a layer.

scale_zfloat

Scalar multiplier for the z-dimension size of each layer.

scale_xyfloat

Scalar multiplier for the x- and y-dimension size of each layer.

type_ignorelist of str

List of layer types to ignore when visualizing the model.

index_ignorelist of int

List of layer indices to ignore when visualizing the model.

color_mapdict

A dictionary mapping layer defining fill and outline for each layer by class type. Layers not specified in the dictionary will use default colors.

one_dim_orientation{‘x’, ‘y’, ‘z’}

Axis along which one-dimensional layers should be drawn.

index_2dlist of int

Indices of layers to be drawn in 2D when draw_volume is True.

background_fillstr or tuple

Background color of the image. Can be a string or a tuple (R, G, B, A).

draw_volumebool

Whether to use a 3D volumetric view (True) or a 2D box view (False).

draw_reversedbool

Whether to draw 3D boxes in reverse order, from front-right to back-left.

paddingint

Distance in pixels before the first and after the last layer.

text_callable{callable, ‘default’, None}

A callable that generates text for layers, ‘default’ to use default behavior, or None to skip. The callable should take two arguments: the layer index (int) and the layer (Layer).

text_vspacingint

Vertical spacing in pixels between lines of text produced by text_callable.

spacingint

Horizontal spacing in pixels between consecutive layers.

draw_funnelbool

If set to True, a funnel will be drawn between consecutive layers.

shade_stepfloat

Lightness deviation step for shades in the visualization (only applicable in 3D volumetric view).

legendbool

Whether to include a legend of the layers in the image.

legend_text_spacing_offsetfloat

Offset for the space allocated to legend text. Useful for preventing text cutoff in the legend.

fontUnion[ImageFont.ImageFont, Dict[str, Any]], optional

Font to be used for text rendering (e.g., legend or labels).

  • If an ImageFont.ImageFont object is provided, it is used directly.

  • If a dictionary is provided, it can include customization options:
    • font_pathstr, optional

      Path to a one of ‘.ttf .otf .ttc’ font file.

    • font_sizeint, optional

      Size of the font. Must be a positive integer.

    • use_default_fontbool, optional

      If True, uses the default system font. Default is True.

If None, the default font is used.

font_colorstr or tuple

Color of the font. Can be a string or a tuple (R, G, B, A).

show_dimensionbool

Whether to display layer dimensions in the legend (only when legend is True).

backendbool, str, optional

Specifies the backend used to process and save the image. If the value is one of 'matplotlib', 'true', or 'none' (case-insensitive), the Matplotlib backend will be used. This is useful for better DPI control and consistent rendering. Any other value will fall back to using the PIL backend. Common values include:

  • 'matplotlib', 'true', 'none' : Use Matplotlib

  • 'pil', 'fast', etc. : Use PIL (Python Imaging Library)

Default is None.

Added in version 0.4.0: The backend parameter was added to allow switching between PIL and Matplotlib.

show_os_viewerbool, optional

If True, displays the saved image (by PIL) in the system’s default image viewer using PIL’s .show() method. Default is False.

Added in version 0.4.0.

save_figbool, default=True

Save the plot.

Added in version 0.4.0.

save_fig_filenamestr, optional, default=’’

Specify the path and filetype to save the plot. If nothing specified, the plot will be saved as png to the current working directory. Defaults to name to use func.__name__.

Added in version 0.4.0.

overwritebool, optional, default=True

If False and a file exists, auto-increments the filename to avoid overwriting.

Added in version 0.4.0.

add_timestampbool, optional, default=False

Whether to append a timestamp to the filename. Default is False.

Added in version 0.4.0.

verbosebool, optional

If True, enables verbose output with informative messages during execution. Useful for debugging or understanding internal operations such as backend selection, font loading, and file saving status. If False, runs silently unless errors occur.

Default is False.

Added in version 0.4.0: The verbose parameter was added to control logging and user feedback verbosity.

Returns:
image

The generated architecture visualization image.

Parameters:
  • to_file (Optional[str])

  • min_z (int)

  • min_xy (int)

  • max_z (int)

  • max_xy (int)

  • scale_z (float)

  • scale_xy (float)

  • type_ignore (list)

  • index_ignore (list)

  • color_map (dict)

  • one_dim_orientation (str)

  • index_2d (list)

  • background_fill (Any)

  • draw_volume (bool)

  • draw_reversed (bool)

  • padding (int)

  • text_callable (Optional[ Union[Callable[[int, Layer], Tuple[str, bool]], str] ])

  • text_vspacing (int)

  • spacing (int)

  • draw_funnel (bool)

  • legend (bool)

  • font (Optional[ Union[ImageFont.ImageFont, Dict[str, "Any"]] ])

  • font_color (Any)

  • backend (Optional[Union[bool,str]])

  • show_os_viewer (bool)

  • save_fig (bool)

  • save_fig_filename (str)

  • overwrite (bool)

  • verbose (bool)

Return type:

PIL.Image.Image

Notes

This function calls get_font(font) internally to normalize the input.