Skip to main content

Architecture visualization of Keras models

Project description

visualkeras for Keras / TensorFlow

Wheel Status Latest Version Build Status

Introduction

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.

Model Support

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.

Installation

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

Usage

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).

Default
visualkeras.layered_view(model)

Default view of a VGG16 CNN

Legend

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!

Layered view of a VGG16 CNN with legend

Flat Style
visualkeras.layered_view(model, draw_volume=False)

Flat view of a VGG16 CNN

Spacing and logic grouping

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)

Spaced and grouped view of a VGG16 CNN

Custom color map

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)

Custom colored view of a VGG16 CNN

Hiding layers

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])

Simplified view of a VGG16 CNN

Scaling dimensions

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)

True scale view of a VGG16 CNN Note: Scaled models may hide the true complexity of a layer, but are visually more appealing.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

visualkeras-0.0.2.tar.gz (13.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

visualkeras-0.0.2-py3-none-any.whl (13.0 kB view details)

Uploaded Python 3

File details

Details for the file visualkeras-0.0.2.tar.gz.

File metadata

  • Download URL: visualkeras-0.0.2.tar.gz
  • Upload date:
  • Size: 13.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.6

File hashes

Hashes for visualkeras-0.0.2.tar.gz
Algorithm Hash digest
SHA256 8cd900884da9f39e8de1e431fa3a42686af435cceab83d013b1c17a5ac9c1b97
MD5 7490325cf34ee4253ae3350ae392db11
BLAKE2b-256 e5e6ae948673eaed8515e4c1dd24d8ca40df85f14493914fc5d2e9c09b82fa33

See more details on using hashes here.

File details

Details for the file visualkeras-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: visualkeras-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 13.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.6

File hashes

Hashes for visualkeras-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 83526e80faa4ae9c39cceee9158714f3166e91e2a8df7272569362ebc1cc827e
MD5 c088f31106285cba118b7379b0acf2a2
BLAKE2b-256 a593abd375b37add77d58a8c48a506ae39bb77c4380e2507ea444325ff1b9971

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page