Visualizing the structure of your neural network is quite useful for publications, such as papers and blogs.
Today, various tools exist for generating these visualizations - allowing engineers and researchers to generate them either by hand, or even (partially) automated.
Net2Vis is one such tool: recognizing that current tools have certain flaws, scholars at a German university designed a web application which allows you to visualize Keras-based neural networks automatically.
In this blog post, we'll take a look at Net2Vis. Firstly, we'll inspect the challenges of current tools in more detail, followed by the introduction of Net2Vis. We then suggest a different way of installing it, by using our Docker-based installation process, saving you quite some time on installing dependencies. Subsequently, we'll talk you through our experience with Net2Vis - and show you what it's capable of.
Are you ready? All right! Let's go :)
Let's briefly take a look at what Net2Vis is precisely first :)
In the scientific field of deep learning, many scholars are writing academic papers about their findings. The same is true for practical findings in industry journals and on industry blogs. Visualizing neural networks is a key element in those reports, as people often appreciate visual structures over large amounts of text.
However, when looking at the available tools and techniques for visualizing neural networks, Bäuerle & Ropinski (2019) found some key insights about the state of the art of neural network visualization:
Hence, convinced that the current tool landscape is suboptimal, they set out and created Net2Vis, a web application for automatically visualizing your Keras neural networks.
If you don't want to install Net2Vis at all, you can also use it online at the authors' website: https://viscom.net2vis.uni-ulm.de :)
Installing Net2Vis is quite easy if you use a Mac or a Linux based machine, but it's more difficult when using Windows. However, don't worry, because I created a Docker based variant which also makes using Net2Vis on Windows easy. So, for installing Net2Vis, you can choose from one of two options:
If you wish to install Net2Vis with Docker, keep on reading.
Docker's slogan is as follows:
Securely build, share and run any application, anywhere
This sounds good :)
Docker is software which allows you to run apps in a virtualized-ish fashion, but not in the style of traditional Virtual Machines as we know them. Rather, Docker runs containerized apps directly in Docker's container engine, on your host machine. This allows you to still use containerized apps, still run apps in an isolated and atomic fashion, still benefit from the benefits of Linux - even on Windows, without having the need to install massive operating systems in virtual machines, consuming a lot of disk space.
There's a couple of things that you must install if you wish to install Net2Vis with Docker:
As per the GitHub repository, this is the installation procedure:
git clone https://github.com/christianversloot/net2vis-docker.git
cd
into the folder where net2vis-docker
has been unpacked, and run it with Docker compose:docker-compose up
if you wish to run it in the front so that you can see messages easily, with the downside that it shuts down when you close the terminal;docker-compose up -d
if you wish to run it in the background so that it keeps running when you close the terminal, with the downside that you'll have to run docker logs <container id>
if you wish to see what happens inside.If you choose to run it in the frontend, the backend will start first, followed by the frontend. The first time, it will also build the Docker containers :) Startup looks like this:
Creating backend ... done Recreating frontend ... done Attaching to backend, frontend
backend | * Serving Flask app "server" (lazy loading)
backend | * Environment: production
backend | WARNING: This is a development server. Do not use it in a production deployment.
backend | Use a production WSGI server instead.
backend | * Debug mode: on
backend | * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
backend | * Restarting with stat
backend | * Debugger is active!
backend | * Debugger PIN: 181-171-933
frontend |
frontend | > netviz@0.1.0 start /Net2Vis/net2vis
frontend | > npm-run-all -p watch-css start-js
frontend |
frontend |
frontend | > netviz@0.1.0 start-js /Net2Vis/net2vis
frontend | > react-scripts start
frontend |
frontend |
frontend | > netviz@0.1.0 watch-css /Net2Vis/net2vis
frontend | > npm run build-css && node-sass-chokidar src/ -o src/ --watch --recursive
frontend |
frontend |
frontend | > netviz@0.1.0 build-css /Net2Vis/net2vis
frontend | > node-sass-chokidar src/ -o src/
frontend |
frontend | Rendering Complete, saving .css file...
frontend | Wrote CSS to /Net2Vis/net2vis/src/styles/index.css
frontend | Wrote 1 CSS files to /Net2Vis/net2vis/src/
frontend | => changed: /Net2Vis/net2vis/src/styles/index.scss
frontend | Rendering Complete, saving .css file...
frontend | Wrote CSS to /Net2Vis/net2vis/src/styles/index.css
frontend | [HPM] Proxy created: /api -> http://host.docker.internal:5000
frontend | Starting the development server...
frontend |
frontend | Browserslist: caniuse-lite is outdated. Please run next command `npm update`
frontend | Compiled successfully!
frontend |
frontend | You can now view netviz in the browser.
frontend |
frontend | Local: http://localhost:3000/
frontend | On Your Network: http://192.168.96.3:3000/
frontend |
frontend | Note that the development build is not optimized.
frontend | To create a production build, use npm run build.
frontend |
frontend | Compiling...
frontend | Compiled successfully!
Of course, you might also wish to omit installing Net2Vis with Docker - for example, because you have a Mac or Linux based system, which enables you to install e.g. Cairo quite easily. In that case, please go to the original Net2Vis repository and follow its installation instructions.
When Net2Vis is running, you can access it from http://localhost:3000. When navigating here with your web browser, a blue and white web application will open with a code editor on the left, a settings panel on the right and a visualization in the middle of your screen.
Let's now walk through each of these :)
But first - the model we'll be visualizing today. It's a simple ConvNet with two Conv2D layers, Max Pooling and Dropout. Do note that Net2Vis has a particular structure, which in essence boils down to this:
def get_model():
model = ....
.....
return model
That is, it will look in your code for a definition called get_model
which should return a Keras model. Both the Sequential and the Functional API are supported. This also means that in essence, Net2Vis doesn't really care about data preprocessing, and you can skip these parts of your code.
You really only have to add the model
definition, the layers, the necessary variables as used in the layers, and finally return the model
.
Take this example of the ConvNet to be visualized as follows:
# You can freely modify this file.
# However, you need to have a function that is named get_model and returns a Keras Model.
import keras as k
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense
def get_model():
input_shape = (28, 28, 1)
no_classes = 10
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=input_shape))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Conv2D(64, kernel_size=(3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dense(no_classes, activation='softmax'))
return model
If you add it (do note that the code editor can be sensitive at times; it's best to write your code in a regular editor, after which you select the code in Net2Vis, delete it entirely, and ctrl+c/ctrl+v your own code in the empty code editor), the default visualization should immediately change into something that resembles this:
That's good, because this is the ConvNet we just defined đ
However, the default visualization doesn't provide you with all the details - for example, it doesn't show you which layer is being visualized.
Fortunately, Net2Vis shows a legend immediately below the visualization:
Indeed, the colors match the model code we just defined, so we've got the correct one visualized :)
On the right side of the web application, there is a large amount of configuration options. Let's take a look at these.
By default, Net2Vis determines the height of a particular box by the spatial resolution of the layer, whereas the width is determined by the number of feature channels (Bäuerle & Ropinski, 2019). Note that the resolution and number of feature channels of Conv and Dense layers do not influence each other, given the fact that they are incomparable in terms of these determinants.
It's possible to change these minimum and maximum visualization heights. By default, the neural network is 30 pixels high at minimum and 100 at maximum, but let's take a look at what the model looks like when we change these values.
For example, let's set the maximum height to 350 and the minimum height to 100:
It's also possible to do this with minimum and maximum visualization widths of the individual layers - say, we change them from 20/80 into 50/100:
Increasing or decreasing the horizontal and vertical spacing between individual layers is also possible. For example, here, the horizontal spacing was changed from 20 into 50:
(note that vertical spacing becomes useful when you have models that branch at one point in time and, possibly, join together later)
These options give you full control over the shape of your visualization.
But there is more. Net2Vis also allows you to adapt the color of the visualizations, and even provide a colorless version for people with monochromatic vision, which means that people can only see in grayscale (Bäuerle & Ropinski, 2019).
By selecting the color selector on the right, it's possible to select one out of three options:
Color blindness mode, which also provides a palette, but instead provides only 8 colors to ensure that color blind people can sufficiently distinguish between layer colors (Bäuerle & Ropinski, 2019).
Palette (top), interpolation (middle) and color blindness mode (bottom).
By selecting 'Disable color', you activate monochromatic mode:
However, it doesn't end here: users can select their custom colors as well :)
By clicking on a layer, the palette for that particular layer type can be changed. That is, when selecting a color (one can even set a hex value manually!), it's possible to change the layer style - the other layers of the type change automatically when the color is changed.
So, beyond space, the user can also fully control the colors of their neural network visualizations with Net2Vis. I love it :)
And we're still not there yet :)
Net2Vis has additional functionality with respect to labels and additional details. As explained by Bäuerle & Ropinski in their paper (Bäuerle & Ropinski, 2019), the goal of Net2Vis is to produce models that are as sparsely and as understandably as possible. As a result, the authors have decided to keep as much as possible away from the default visualization - which essentially just distinguishes between layers and shows spatial resolutions and number of kernels used.
However, it's possible to:
For example, by selecting the dimensions label, the spatial dimensions of the layer inputs start popping up. For example, in our ConvNet, we have a 28 x 28 pixels input which is downsampled to 5 x 5 before it's flattened (green) and fed to the Dense layers (red). This is reminiscent of the MNIST dataset, and indeed, this was the dataset which we trained our model with :)
By selecting the features label, the number of features learnt per layer is shown. As we can see, the first Conv2D learns 32 filters, and the second one learns 64. The first Dense layer learns 256 features, while the second one learns 10 - and hey, this is exactly the number of classes present within our MNIST dataset, which suggests a multiclass / categorical data classification problem and a Softmax activation function.
Before configuring layer shape and colors, however, you may wish to take a look at the functionality for grouping layers together.
For example, in the visualizations above, it's clear that there's a block that repeats twice: the Conv2D-MaxPooling2D-Dropout block. I always tend to call these 'convolutional blocks', as it's a nice summary, and it's possible to visualize these with Net2Vis as well.
This is especially useful if you have to visualize very large networks, and don't have so much space available in e.g. your paper or on your website.
By clicking "automatically group", Net2Vis will attempt to find blocks by checking the sequences available within the graph. For example, it will find the convolutional blocks we identified in the plot manually:
What's best is that it automatically adapts the legend, and adds the group to it: purple = blue + yellow + brown, i.e. group = Conv2D + MaxPooling2D + Dropout. Great :)
By clicking the download button in the blue top bar, your visualization will be downloaded to your computer:
It'll download a ZIP file containing:
In this blog post, we've shown you what Net2Vis is and how it can be used to visualize Keras models easily and consistently. We took a look at the features available within this web application, which was designed by Bäuerle & Ropinski (2019).
What's more, given the relative complexity of installing Net2Vis on a Windows machine, we made available a Docker based version called net2vis-docker
, which takes away the complex installation steps and installs Net2Vis for you, granted that you have Docker and Docker Compose installed on your system, as well as Git.
I hope this blog post has been useful to your machine learning projects. If it has, I'd love to know how, so in that case please leave a comment in the comments box below! đ Please do too if you have questions or remarks :)
Thanks for reading MachineCurve today and happy engineering! đ
Bäuerle, A., & Ropinski, T. (2019). Net2Vis: Transforming Deep Convolutional Networks into Publication-Ready Visualizations. arXiv preprint arXiv:1902.04394.
Visual Computing Group (Ulm University). (n.d.). Net2Vis. Retrieved from https://github.com/viscom-ulm/Net2Vis
MachineCurve. (n.d.). net2vis-docker. Retrieved from https://github.com/christianversloot/net2vis-docker
Docker. (n.d.). Enterprise Container Platform. Retrieved from https://www.docker.com/
Learn how large language models and other foundation models are working and how you can train open source ones yourself.
Keras is a high-level API for TensorFlow. It is one of the most popular deep learning frameworks.
Read about the fundamentals of machine learning, deep learning and artificial intelligence.
To get in touch with me, please connect with me on LinkedIn. Make sure to write me a message saying hi!
The content on this website is written for educational purposes. In writing the articles, I have attempted to be as correct and precise as possible. Should you find any errors, please let me know by creating an issue or pull request in this GitHub repository.
All text on this website written by me is copyrighted and may not be used without prior permission. Creating citations using content from this website is allowed if a reference is added, including an URL reference to the referenced article.
If you have any questions or remarks, feel free to get in touch.
TensorFlow, the TensorFlow logo and any related marks are trademarks of Google Inc.
PyTorch, the PyTorch logo and any related marks are trademarks of The Linux Foundation.
Montserrat and Source Sans are fonts licensed under the SIL Open Font License version 1.1.
Mathjax is licensed under the Apache License, Version 2.0.