Wednesday, May 3, 2017

Visualizing Keras Models - Updated

About 2 months back, I did a post on how you can visualize the structure of a Keras model. As I mentioned, when the machine learning (or deep learning) model you're building is complex, then it may be easier to understand it if you can see a visual representation of it.

I showed you how to use the Visualization utility in Keras in order to draw the structure of a model in Keras, such as this visualization of the LeNet model,

Visualizing the LeNet model
Visualizing the LeNet model

But a few days back, several people had got some errors when following the steps I explained. I digged a bit to find why the errors are happening, and found that with the latest version of Keras (v 2.*) they have changed the API of the visualization utility.

The following are the main changes,
  • The module has been renamed, from visualize_util to vis_utils.
  • The function name for plotting has been renamed, from plot to plot_model.
So, here's the updated guide on how to visualize a Keras model.


Note: As with all my guides, I'm using Anaconda Python here as well for Python environment and package management.

First, we install the graphviz Anaconda package,
 conda install graphviz  

The graphviz Anaconda package only contains the graphviz binaries, which gets placed in <path to your anaconda environment>\Library\bin\graphviz\ directory. So, you need to add that path to your system PATH.

We need the graphviz Python package as well, not just the binaries. So, we'll install it from pip,
 pip install graphviz  

Then, we need the pydot package. The original pydot package has some issues. So, we'll be using the pydot-ng package,
 pip install pydot-ng  

Now we are ready to visualize.

Add the following lines to your code,
 from keras.utils import plot_model  
   
 ### Build, Load, and Compile your model  
   
 plot_model(model, to_file='model.png', show_shapes=True, show_layer_names=True)  

The two optional parameters, show_shapes and show_layer_names works as follows,
  • show_shapes - default 'False' - controls whether output shapes are shown in the graph
  • show_layer_names - defaults 'True' - controls whether layer names are shown in the graph
In the LeNel model visualization I've shown at the start of this post, I have set  show_shapes=True and show_layer_names=False.

How about we visualize a bit more complex model?

Here's what VGG16 looks like,

Visualization of VGG16, with Output Shapes and Layer Names
Visualization of VGG16, with Output Shapes and Layer Names

I've set both  show_shape and show_layer_names to True here.

Hope these updated steps helps you get through any issues that has arised with the earlier guide.

Related links:
https://keras.io/visualization/

Build Deeper: Deep Learning Beginners' Guide is the ultimate guide for anyone taking their first step into Deep Learning.

Get your copy now!

1 comment:

  1. Hello, Thank for the article! I am having the following issue when installing 'graphviz' using conda. How to solve this?
    ------------------------------------------------
    UnsatisfiableError: The following specifications were found to be in conflict:
    - graphviz
    - poppler
    ----------------------------------------------------------

    ReplyDelete