Skip to main content

A Python Library for Visualizing Keras Model that covers a variety of Layers

Project description

LayerViz

License

logo (2)

A Python Library for Visualizing Keras Models covering a variety of Layers.

Table of Contents

Installation

Install

Use python package manager (pip) to install LayerViz.

pip install LayerViz

Upgrade

Use python package manager (pip) to upgrade LayerViz.

pip install LayerViz --upgrade

Usage

from libname imoprt layerviz
# create your model here
# model = ...

layerviz(model, file_format='png')

Parameters

layerviz(model, file_name='graph', file_format=None, view=False, settings=None)
  • model : a Keras model instance.
  • file_name : where to save the visualization.
  • file_format : file format to save 'pdf', 'png'.
  • view : open file after process if True.
  • settings : a dictionary of available settings.

Note :

  • set file_format='png' or file_format='pdf' to save visualization file.
  • use view=True to open visualization file.
  • use settings to customize output image.

Settings

you can customize settings for your output image. here is the default settings dictionary:

 recurrent_layers = ['LSTM', 'GRU']
    main_settings = {
        # ALL LAYERS
        'MAX_NEURONS': 10,
        'ARROW_COLOR': '#707070',
        # INPUT LAYERS
        'INPUT_DENSE_COLOR': '#2ecc71',
        'INPUT_EMBEDDING_COLOR': 'black',
        'INPUT_EMBEDDING_FONT': 'white',
        'INPUT_GRAYSCALE_COLOR': 'black:white',
        'INPUT_GRAYSCALE_FONT': 'white',
        'INPUT_RGB_COLOR': '#e74c3c:#3498db',
        'INPUT_RGB_FONT': 'white',
        'INPUT_LAYER_COLOR': 'black',
        'INPUT_LAYER_FONT': 'white',
        # HIDDEN LAYERS
        'HIDDEN_DENSE_COLOR': '#3498db',
        'HIDDEN_CONV_COLOR': '#5faad0',
        'HIDDEN_CONV_FONT': 'black',
        'HIDDEN_POOLING_COLOR': '#8e44ad',
        'HIDDEN_POOLING_FONT': 'white',
        'HIDDEN_FLATTEN_COLOR': '#2c3e50',
        'HIDDEN_FLATTEN_FONT': 'white',
        'HIDDEN_DROPOUT_COLOR': '#f39c12',
        'HIDDEN_DROPOUT_FONT': 'black',
        'HIDDEN_ACTIVATION_COLOR': '#00b894',
        'HIDDEN_ACTIVATION_FONT': 'black',
        'HIDDEN_LAYER_COLOR': 'black',
        'HIDDEN_LAYER_FONT': 'white',
        # RECURRENT LAYERS
        'RECURRENT_LAYER_COLOR': '#9b59b6',
        'RECURRENT_LAYER_FONT': 'white',
        # OUTPUT LAYER
        'OUTPUT_DENSE_COLOR': '#e74c3c',
        'OUTPUT_LAYER_COLOR': 'black',
        'OUTPUT_LAYER_FONT': 'white',
    }


    for layer_type in recurrent_layers:
      main_settings[layer_type + '_COLOR'] = '#9b59b6'
    settings = {**main_settings, **settings} if settings is not None else {**main_settings}
    max_neurons = settings['MAX_NEURONS']

Note:

  • set 'MAX_NEURONS': None to disable max neurons constraint.
  • see list of color names here.
my_settings = {
    'MAX_NEURONS': None,
    'INPUT_DENSE_COLOR': 'teal',
    'HIDDEN_DENSE_COLOR': 'gray',
    'OUTPUT_DENSE_COLOR': 'crimson'
}

# model = ...

layerviz(model, file_format='png', settings=my_settings)

Sample Usage

📖 Resource: The architecture we're using below is a scaled-down version of VGG-16, a convolutional neural network which came 2nd in the 2014 ImageNet classification competition.

For reference, the model we're using replicates TinyVGG, the computer vision architecture which fuels the CNN explainer webpage.

from keras import models, layers
from libname import layerviz
import tensorflow as tf
model_1 = tf.keras.models.Sequential([
  tf.keras.layers.Conv2D(filters=10, 
                         kernel_size=3, # can also be (3, 3)
                         activation="relu", 
                         input_shape=(224, 224, 3)), # first layer specifies input shape (height, width, colour channels)
  tf.keras.layers.Conv2D(10, 3, activation="relu"),
  tf.keras.layers.MaxPool2D(pool_size=2, # pool_size can also be (2, 2)
                            padding="valid"), # padding can also be 'same'
  tf.keras.layers.Conv2D(10, 3, activation="relu"),
  tf.keras.layers.Conv2D(10, 3, activation="relu"), # activation='relu' == tf.keras.layers.Activations(tf.nn.relu)
  tf.keras.layers.MaxPool2D(2),
  tf.keras.layers.Flatten(),
  tf.keras.layers.Dense(1, activation="sigmoid") # binary activation output
])

layerviz(model_1, file_name='sample1', file_format='png')

from IPython.display import Image
Image('sample1.png')

download

Supported layers

Explore list of keras layers

  1. Core layers

    • Input object
    • Dense layer
    • Activation layer
    • Embedding layer
    • Masking layer
    • Lambda layer
  2. Convolution layers

    • Conv1D layer
    • Conv2D layer
    • Conv3D layer
    • SeparableConv1D layer
    • SeparableConv2D layer
    • DepthwiseConv2D layer
    • Conv1DTranspose layer
    • Conv2DTranspose layer
    • Conv3DTranspose layer
  3. Pooling layers

    • MaxPooling1D layer
    • MaxPooling2D layer
    • MaxPooling3D layer
    • AveragePooling1D layer
    • AveragePooling2D layer
    • AveragePooling3D layer
    • GlobalMaxPooling1D layer
    • GlobalMaxPooling2D layer
    • GlobalMaxPooling3D layer
    • GlobalAveragePooling1D layer
    • GlobalAveragePooling2D layer
    • GlobalAveragePooling3D layer
  4. Reshaping layers

    • Reshape layer
    • Flatten layer
    • RepeatVector layer
    • Permute layer
    • Cropping1D layer
    • Cropping2D layer
    • Cropping3D layer
    • UpSampling1D layer
    • UpSampling2D layer
    • UpSampling3D layer
    • ZeroPadding1D layer
    • ZeroPadding2D layer
    • ZeroPadding3D layer
  5. Regularization layers

    • Dropout layer
    • SpatialDropout1D layer
    • SpatialDropout2D layer
    • SpatialDropout3D layer
    • GaussianDropout layer
    • GaussianNoise layer
    • ActivityRegularization layer
    • AlphaDropout layer
  6. Recurrent Layers

    • LSTM
    • GRU

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

LayerViz-0.1.1.tar.gz (6.1 kB view hashes)

Uploaded Source

Built Distribution

LayerViz-0.1.1-py3-none-any.whl (6.6 kB view hashes)

Uploaded Python 3

Supported by

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