SolvedCapsNet Keras pydot issue

Hi, I got the following error message (copied below) when I tried the code, although I do have both pydot and graphviz. I don't think this is an issue with your code. It seems to be a general long-standing issue with pydot. The solutions are discussed here:
1: Theano/Theano#1801
2: pydot/pydot#126

Traceback (most recent call last):
File "/Users/Qihong/anaconda/envs/brainiak/lib/python3.6/site-packages/keras/utils/vis_utils.py", line 23, in _check_pydot
pydot.Dot.create(pydot.Dot())
File "/Users/Qihong/anaconda/envs/brainiak/lib/python3.6/site-packages/pydot_ng/init.py", line 1890, in create
'GraphViz's executables not found')
pydot_ng.InvocationException: GraphViz's executables not found

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "capsulenet.py", line 195, in
plot_model(model, to_file=args.save_dir+'/model.png', show_shapes=True)
File "/Users/Qihong/anaconda/envs/brainiak/lib/python3.6/site-packages/keras/utils/vis_utils.py", line 131, in plot_model
dot = model_to_dot(model, show_shapes, show_layer_names, rankdir)
File "/Users/Qihong/anaconda/envs/brainiak/lib/python3.6/site-packages/keras/utils/vis_utils.py", line 52, in model_to_dot
_check_pydot()
File "/Users/Qihong/anaconda/envs/brainiak/lib/python3.6/site-packages/keras/utils/vis_utils.py", line 27, in _check_pydot
raise ImportError('Failed to import pydot. You must install pydot'
ImportError: Failed to import pydot. You must install pydot and graphviz for pydotprint to work.

28 Answers

✔️Accepted Answer

I'm writing this here in case anyone finds this again.

Checking the code for keras/utils/vis_utils.py:

"""Utilities related to model visualization."""
import os

try:
    # pydot-ng is a fork of pydot that is better maintained.
    import pydot_ng as pydot
except ImportError:
    # pydotplus is an improved version of pydot
    try:
        import pydotplus as pydot
    except ImportError:
        # Fall back on pydot if necessary.
        try:
            import pydot
        except ImportError:
            pydot = None


def _check_pydot():
    try:
        # Attempt to create an image of a blank graph
        # to check the pydot/graphviz installation.
        pydot.Dot.create(pydot.Dot())
    except Exception:
        # pydot raises a generic Exception here,
        # so no specific class can be caught.
        raise ImportError('Failed to import pydot. You must install pydot'
                          ' and graphviz for `pydotprint` to work.')

we can see the error raised by the try except clause above. This tries to run a dummy pydot command in to confirm that pydot is installed correctly.
However the first lines how that the code tries to install initially the package pydot_ng then pydotplus and finally if it's not found the pydot package.

So I installed pydot_ng and it worked for me. However I had another issue. The system couldn't find the GraphViz executables:

InvocationException: GraphViz's executables not found

Under Ubuntu 16.04 I ran:

sudo apt-get install graphviz

and it worked.

Other Answers:

For future reference: If you're using a Mac using Pip to install graphviz is not enough. You need to use brew
brew install graphviz

FWIW pip install pydot==1.2.3 didn't work for me. Commenting out the lines worked.

This is a bug of Keras. You maybe solve this by removing the plot_model function and from keras.utils.vis_utils import plot_model.

Or you can install pydot 1.2.3 by pip.
pip install pydot==1.2.3

I have not re-tested these solutions, if they work, please let me know. Thanks. @QihongL

This is a bug, keras should fix it.