6

I am trying to make a plot/graph of deep learning model in Python using Keras package but unfortunately it keeps giving me an error which is not very informative.

I run python on Linux with Python 3.5.2, Anaconda 4.2.0, Keras 2.1.6 and I use tensorflow-gpu 1.7.0 Backend.

Here is the error message:

keras.utils.plot_model(unet, to_file='model.png', show_shapes=False, show_layer_names=True, rankdir='TB')

['dot', '-Tps', '/tmp/tmphesl1j0c'] return code: 127

stdout, stderr:
 b''
b'dot: error while loading shared libraries: libexpat.so.0: cannot open shared object file: No such file or directory\n'

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-9-60bb0e3b97bd> in <module>()
----> 1 keras.utils.plot_model(unet, to_file='model.png', show_shapes=False, show_layer_names=True, rankdir='TB')

/.../anaconda3-4.2.0/lib/python3.5/site-packages/keras/utils/vis_utils.py in plot_model(model, to_file, show_shapes, show_layer_names, rankdir)
    132             'LR' creates a horizontal plot.
    133     """
--> 134     dot = model_to_dot(model, show_shapes, show_layer_names, rankdir)
    135     _, extension = os.path.splitext(to_file)
    136     if not extension:

/.../anaconda3-4.2.0/lib/python3.5/site-packages/keras/utils/vis_utils.py in model_to_dot(model, show_shapes, show_layer_names, rankdir)
     53     from ..models import Sequential
     54 
---> 55     _check_pydot()
     56     dot = pydot.Dot()
     57     dot.set('rankdir', rankdir)

/.../anaconda3-4.2.0/lib/python3.5/site-packages/keras/utils/vis_utils.py in _check_pydot()
     24         # Attempt to create an image of a blank graph
     25         # to check the pydot/graphviz installation.
---> 26         pydot.Dot.create(pydot.Dot())
     27     except OSError:
     28         raise OSError(

/.../anaconda3-4.2.0/lib/python3.5/site-packages/pydot.py in create(self, prog, format, encoding)
   1882                      out=stdout_data,
   1883                      err=stderr_data))
-> 1884         assert p.returncode == 0, p.returncode
   1885         return stdout_data

AssertionError: 127

I would really appreciate if somebody could help me with this error.

Note: both pydot and graphviz are intalled

2
  • have u got the answer as I'm having same problem ? Commented May 25, 2021 at 18:56
  • 1
    @AshishSaini yes it was some compatibility issue. I can't recall exactly what it was right now. But try some of the suggestions from this thread Commented May 26, 2021 at 19:09

13 Answers 13

7

For me solution was to import like this:

from keras.utils.vis_utils import plot_model
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! I am surprised that this was answered just 19 hours before I faced the problem. You saved my day!
It doesn't work for me. AttributeError: module 'keras.api._v2.keras.utils' has no attribute 'vis_utils' I use from tensorflow.keras.utils import plot_model instead.
6

For me the solution was:

  • conda install pydotplus (pydot-ng was not installable with tensorflow-gpu it said).
  • Search for viz_utils.py in anaconda directory and open all of them. Make sure everywhere pydot is imported, it is done the following way:
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

There was one of the files where it just said import pyplot. After changing that, it worked for me.

Comments

6

I changed keras.utils to tensorflow.keras.utils and it helped me

1 Comment

and is now: tensorflow.python.keras.utils
1

It seems like that there are some compatibility issue ! (Link)

Installing Graphviz and adding it to paths works for me.

Comments

1

If you are running an IDE like PyCharm, after installing pydot and installing Graphviz(also adding it to the environment PATH variable. eg C:\Program Files\Graphviz\bin. see here https://www.architectryan.com/2018/03/17/add-to-the-path-on-windows-10/), you should restart the IDE.

If you are working in a virtual environment, I would suggest deactivate and restart the terminal and activate the virtual environment again.

Reason - the package is trying to locate graphviz from os.environ['PATH'], and somehow it was not updated to show graphviz in the path. After restarting Pycharm, I found os.environ['PATH'] was properly updated and the plot_model function worked correctly.

Comments

1

For me, there was an error where I didn't put '' over model.png in.

plot_model(model, to_file= 'model.png' , show_shapes=True)

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

I solved the issue commenting line 117 program += extension in pydot.py

Comments

0
from keras.utils.vis_utils import plot_model
keras.utils.vis_utils.plot_model(
csf1, to_file='model.png', show_shapes=True, show_dtype=True,
show_layer_names=True, rankdir='TB', expand_nested=True, dpi=96
)

You can use this code, it works with my project correctly.

Comments

0

The following code works for me:

conda install -c anaconda graphviz
conda install pydotplus

Just open your terminal and run the commands. Then you should be fine.

Comments

0

Even I was facing this error since past 2 days.

I did the following:

conda uninstall pyplot
conda uninstall pyplotplus
conda uninstall graphviz

Then, I once again installed all these three packages.

conda install pyplot
conda install pyplotplus
conda install graphviz

Hope this helps!

Comments

0

Try this way

import keras
keras.utils.plot_model(Your_Parameters)

or

from keras.utils import plot_model
plot_model(Your_Parameters)

Comments

0

I faced the same issue, and it was resolved by changing the rankdir parameter.

Graphviz can have trouble rendering large vertical graphs. Try changing the orientation:

keras.utils.plot_model(model, to_file="model.png", show_shapes=True, rankdir="LR")

Comments

-1

the comment says

 24         # Attempt to create an image of a blank graph
 25         # to check the pydot/graphviz installation.

so I suppose you need to install graphviz and pydot

assuming you are on ubuntu or similar:

sudo apt install graphviz

and in your anaconda env:

pip install pydot

4 Comments

Both pydot and graphviz are installed
can you run pydot.Dot.create(pydot.Dot()) in a python shell? I did the above 2 and it works for me, I am not using anaconda though
I am not use but I think there is a mistake with (). I tried different combinations of () but every time it says Badly placed ()'s
idk, I did exactly the above (apt, pip, and the python line) and it works, both with python 2.7 and 3.5

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.