Visualize Keras Neural Networks with Netron
Have you ever felt the pain of explaining your deep learning model to somebody else? As a machine learning scholar or a practitioner, you will have to communicate to others about an architecture you’re working on or you might be trying to make sense of a model that you’ve got your hands on. Visualizations can be a powerful tool in explaining something complex like a deep learning model. Sure, you could always get a detailed summary of any Keras model but nothing beats a good visualization. That’s where Netron comes in. I was exploring tools that can be used to create detailed visualizations of Keras models and when I was introduced to Netron, I was immediately intrigued.
Why Netron?
Netron works great for me, due to multiple reasons.
- Netron is a cross-platform tool. It works on Linux, macOS and Windows. Also, it has a web version (netron.app).
- Netron is easy to install and use.
- Netron supports a vast range of deep learning/machine learning model formats. You can see the full list of model formats Netron is currently supporting by visiting Netron Github repository documentation.
- Netron is Open Source! This is a huge plus point for me since I’m a big fan of Open Source Software. You can visit Netron Github repository to learn more and even make contributions to the project!
Installing Netron
As mentioned earlier, Netron is a cross-platform and therefore can be installed in Linux, macOS and Windows by commands shown below.
Linux: snap install netron
macOS: brew install netron
Windows: winget install netron
Also, Netron is available as a web version and as a Python package. To use the web version, simply visit https://netron.app/.
To install Netron as a Python package, run pip install netron.
Using Netron to visualize
You can generate a visualization for your saved model file using the following command.
netron <path_to_model_file>
If you’re using Python, it can be done as follows,
That’s it! It’s that simple! Now we’ll go through an example.
An Example
I will be using a very simple image classification model I created using Keras to demonstrate creating visualizations using Netron.
The following is the code for declaring and saving the Keras model.
Running above code will save the model as keras_model.h5 and it can be used as input to Netron.
To gain a better idea about the structure of the model, let me share the summary of the model, before we move on to analyze the visualization.
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 244, 244, 16) 448
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 122, 122, 16) 0
_________________________________________________________________
conv2d_1 (Conv2D) (None, 122, 122, 32) 4640
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 61, 61, 32) 0
_________________________________________________________________
flatten (Flatten) (None, 119072) 0
_________________________________________________________________
dense (Dense) (None, 2) 238146
=================================================================
Total params: 243,234
Trainable params: 243,234
Non-trainable params: 0
_________________________________________________________________
In order to create the visualization, I will run the following command on my terminal.
netron keras_model.h5
This command will start Netron server and the visualization will be rendered afterwards.
The visualization Netron rendered looks like this:
When you click on a layer, details of that layer will pop up on the screen.
It shows type, name, attributes, inputs and outputs of the selected layer. Netron is able to show information that are unique to a certain type of layers. For an example, for a convolutional layer, information like kernel size, padding and strides are shown.
In conclusion, Netron is a valuable tool which lets you create detailed diagrams of deep/machine learning models with ease. With its support for various model types out there, Netron is a must-have tool for any machine learning practitioner.