Issue with plot_model in keras and pydot

user1033745 picture user1033745 · Feb 17, 2018 · Viewed 14.9k times · Source

I have read similar questions - my error appears to be different since the solutions proposed do not solve my problem.

I am having trouble plotting graphs of keras models.

I have installed graphviz binaries using homebrew

I have installed graphviz python wrapper and pydot using pip (also tried with conda, since this seems to have been an issue in the past).

Using python 3.5

Running:

from keras.utils import plot_model plot_model(cnn_model, to_file='cnn_model.png')

I get the error:

ImportError: Failed to import pydot. You must install pydot and graphviz for pydotprint to work.

with the trace:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/Users/jusjosgra/anaconda/lib/python3.5/site-packages/keras/utils/vis_utils.py in _check_pydot()
     26         # so no specific class can be caught.
---> 27         raise ImportError('Failed to import pydot. You must install pydot'
     28                           ' and graphviz for `pydotprint` to work.')

AttributeError: 'NoneType' object has no attribute 'Dot'

During handling of the above exception, another exception occurred:

ImportError                               Traceback (most recent call last)
<ipython-input-450-82ff54d9260b> in <module>()
      1 from keras.utils import plot_model
----> 2 plot_model(cnn_model, to_file='cnn_model.png')

/Users/jusjosgra/anaconda/lib/python3.5/site-packages/keras/utils/vis_utils.py in plot_model(model, to_file, show_shapes, show_layer_names, rankdir)
    133     if not extension:
    134         extension = 'png'
--> 135     else:
    136         extension = extension[1:]
    137     dot.write(to_file, format=extension)

/Users/jusjosgra/anaconda/lib/python3.5/site-packages/keras/utils/vis_utils.py in model_to_dot(model, show_shapes, show_layer_names, rankdir)
     54     dot.set('rankdir', rankdir)
     55     dot.set('concentrate', True)
---> 56     dot.set_node_defaults(shape='record')
     57 
     58     if isinstance(model, Sequential):

/Users/jusjosgra/anaconda/lib/python3.5/site-packages/keras/utils/vis_utils.py in _check_pydot()
     29 
     30 
---> 31 def model_to_dot(model,
     32                  show_shapes=False,
     33                  show_layer_names=True,

I can successfully import pydot and graphviz independently.

There appears to be a history of bugs between keras and graphviz. Any ideas on a solution?

Answer

iun1x picture iun1x · Apr 3, 2019

I solved it with

sudo apt-get install graphviz