Visualkeras is a Python package to help visualize Keras (either standalone or included in tensorflow) neural network architectures. It allows easy styling to fit most needs. This module supports layered style architecture generation which is great for CNNs (Convolutional Neural Networks), and a graph style architecture, which works great for most models including plain feed-forward networks.
Mode | Sequential | Functional | Subclassed models |
---|---|---|---|
visualkeras.layered_view() |
yes(1) | partially(1,2) | not tested |
visualkeras.graph_view() |
yes | yes | not tested |
1: Any tensor with more than 3 dimensions will be rendered as 3D tensor with elongated z-axis.
2: Only linear models where each layer has no more than one in or output. Non-linear models will be shown in sequential order.
To install published releases from PyPi execute:
pip install visualkeras
To update visualkeras to the latest version, add the --upgrade
flag to the above commands.
If you want the latest (potentially unstable) features you can also directly install from the github master branch:
pip install git+https://github.com/paulgavrikov/visualkeras
Generating neural network architectures is easy:
import visualkeras
model = ...
visualkeras.layered_view(model).show() # display using your system viewer
visualkeras.layered_view(model, to_file='output.png') # write to disk
visualkeras.layered_view(model, to_file='output.png').show() # write and show
To help understand some of the most important parameters we are going to use a VGG16 CNN architecture (see example.py).
visualkeras.layered_view(model)
You can set the legend parameter to describe the relationship between color and layer types. It is also possible to pass
a custom PIL.ImageFont
to use (or just leave it out and visualkeras will use the default PIL font). Please note that
you may need to provide the full path of the desired font depending on your OS.
from PIL import ImageFont
font = ImageFont.truetype("arial.ttf", 32) # using comic sans is strictly prohibited!
visualkeras.layered_view(model, legend=True, font=font) # font is optional!
visualkeras.layered_view(model, draw_volume=False)
The global distance between two layers can be controlled with spacing
. To generate logical groups a special dummy
keras layer visualkeras.SpacingDummyLayer()
can be added.
model = ...
...
model.add(visualkeras.SpacingDummyLayer(spacing=100))
...
visualkeras.layered_view(model, spacing=0)
It is possible to provide a custom color map for fill and outline per layer type.
from tensorflow.python.keras.layers import Dense, Conv2D, Flatten, Dropout, MaxPooling2D, ZeroPadding2D
from collections import defaultdict
color_map = defaultdict(dict)
color_map[Conv2D]['fill'] = 'orange'
color_map[ZeroPadding2D]['fill'] = 'gray'
color_map[Dropout]['fill'] = 'pink'
color_map[MaxPooling2D]['fill'] = 'red'
color_map[Dense]['fill'] = 'green'
color_map[Flatten]['fill'] = 'teal'
visualkeras.layered_view(model, color_map=color_map)
Some models may consist of too many layers to visualize or to comprehend the model. In this case it can be helpful to
hide (ignore) certain layers of the keras model without modifying it. Visualkeras allows ignoring layers by their type
(type_ignore
) or index in the keras layer sequence (index_ignore
).
visualkeras.layered_view(model, type_ignore=[ZeroPadding2D, Dropout, Flatten])
Visualkeras computes the size of each layer by the output shape. Values are transformed into pixels. Then, scaling is
applied. By default visualkeras will enlarge the x and y dimension and reduce the size of the z dimensions as this has
deemed visually most appealing. However, it is possible to control scaling using scale_xy
and scale_z
. Additionally,
to prevent to small or large options minimum and maximum values can be set (min_xy
, min_z
, max_xy
, max_z
).
visualkeras.layered_view(model, scale_xy=1, scale_z=1, max_z=1000)
Note: Scaled models may hide the true complexity of a layer, but are visually more appealing.
For an encoder-like architecture the above looks good but for a decoder-like architecture we would look at the back of
each layer. For this type of models we can switch draw_reversed to True. With this option enabled we need to adjust the
padding_left parameter so that the leftmost layer is still in view.
In addition to this we can enable draw_shapes, which results in printing the output shapes of each layer under the
respective box. draw_shapes can be 0 (no shapes), 1 (shapes beneath every box), 2 (shapes alternating beneath and above
every box), 3 (treat boxes between two spacing layers as one unit with same output shapes). The output quality may vary
with the used font. An extra font for the output shapes can be given with font_shapes. Additionally we need to set
padding_vertical so that we can the output shape of the largest box.
Note: Showing output shapes does not work with a Sequential model. See reversed_view.py in examples.
If you find this project helpful for your research please consider citing it in your publication as follows.
@misc{Gavrikov2020VisualKeras,
author = {Gavrikov, Paul},
title = {visualkeras},
year = {2020},
publisher = {GitHub},
journal = {GitHub repository},
howpublished = {\url{https://github.com/paulgavrikov/visualkeras}},
}