• Stars
    star
    546
  • Rank 81,396 (Top 2 %)
  • Language
    Python
  • License
    MIT License
  • Created about 4 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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.

visualkeras for Keras / TensorFlow

Latest Version Build Status Download Count

âš  Maintainer Wanted âš 

When I released this package a few years ago just before I started my PhD I was almost certain that I'd continue to use TensorFlow and would require this library daily. However, nowadays I never use TensorFlow... As such maintaining a package for a framework that I do not use while doing a PhD has become very cumbersome. That's why I need you! Yes, you! If you have at least some experience with OSS and know how to create good code please contact me to become a maintainer. There is a lot of potential in this framwork and it has gained quite some traction!

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.

FAQ

Feature X documented here does not work

The main branch may be ahead of pypi. Consider upgrading to the latest (perhaps unstable) build as discussed in Installation.

Installing aggdraw fails

This is most likely due to missing gcc / g++ components (e.g. on Elementary OS). Try installing them via your package manager, e.g.:

sudo apt-get install gcc
sudo apt-get install g++
.show() doesn't open a window

You have probably not configured your default image viewer. You can install imagemagick via most package managers:

sudo apt-get install imagemagick

Citation

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}},
}

More Repositories

1

xiaomi-miband-android

Access Xiaomi MiBand from Android (4.3+) via Bluetooth LE.
Java
280
star
2

xiaomi-miband-cocoa

Access Xiaomi MiBand from OS X via Bluetooth LE.
Swift
168
star
3

CNN-Filter-DB

A database of over 1.4 billion 3x3 convolution filters extracted from hundreds of diverse CNN models with relevant meta information (CVPR 2022 ORAL)
Jupyter Notebook
29
star
4

parallel-matplotlib-grid

This Python 3 module helps you speedup generation of subplots in pseudo-parallel mode using matplotlib and multiprocessing. This can be useful if you are dealing with expensive preprocessing or plotting tasks such as violin plots per subplot.
Python
15
star
5

vlm_shapebias

Official code for "Are Vision Language Models Texture or Shape Biased and Can We Steer Them?".
Python
10
star
6

biases_vs_generalization

Official code for the CVPR 2024 Paper "Can Biases in ImageNet Models Explain Generalization?".
Jupyter Notebook
7
star
7

torchconvquality

A library for PyTorch model convolution quality analysis.
Python
7
star
8

cvpr22w_RobustnessThroughTheLens

Official repository of our submission "Adversarial Robustness through the Lens of Convolutional Filters" for the CVPR2022 Workshop "The Art of Robustness: Devil and Angel in Adversarial Machine Learning Workshop"
Jupyter Notebook
7
star
9

tinytorchtrainer

A small framework to train image classifiers in PyTorch
Python
4
star
10

adversarial_solarization

Official implementation of Don't Look into the Sun: Adversarial Solarization Attacks on Image Classifiers
Jupyter Notebook
3
star
11

pytorch-pretrained-cnns

A framework and model zoo for CNN training.
Python
3
star
12

padding_robustness

Official repository for the ICCVW2023 paper "On the Interplay of Convolutional Padding and Adversarial Robustness".
2
star
13

adversarial_training_vs_humans

Official repository for the CVPRW2023 paper "An Extended Study of Human-like Behavior under Adversarial Training".
2
star
14

torchconvview

A library for PyTorch convolution layer visualizations.
Python
2
star
15

diffusion-noise-cls-pytorch

This is an unofficial implementation of the diffusion-style noise frontend in "Intriguing properties of generative classifiers" by Priyank Jaini, Kevin Clark, Robert Geirhos to improve the shape-bias of vision models.
Python
2
star
16

onnxzoo2filterdb

Python
1
star