5

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

Sabrina
  • 103
  • 1
  • 1
  • 7

11 Answers11

7

For me solution was to import like this:

from keras.utils.vis_utils import plot_model
Josef
  • 2,869
  • 2
  • 22
  • 23
DomagojM
  • 101
  • 1
  • 3
  • 1
    Thank you! I am surprised that this was answered just 19 hours before I faced the problem. You saved my day! –  Jul 01 '21 at 08:42
  • 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. – Michael Chao Feb 16 '22 at 05:06
6

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

4

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.

smoothware
  • 898
  • 7
  • 19
1

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

Installing Graphviz and adding it to paths works for me.

M at
  • 1,020
  • 1
  • 12
  • 26
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.

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)

  • 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](/help/how-to-answer). – Community Jul 04 '22 at 18:35
0

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

Luca
  • 1,610
  • 1
  • 19
  • 30
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.

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.

Michael Chao
  • 570
  • 5
  • 14
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!

-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
f4.
  • 3,814
  • 1
  • 23
  • 30
  • Both pydot and graphviz are installed – Sabrina Jun 22 '18 at 23:53
  • 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 – f4. Jun 23 '18 at 00:24
  • 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` – Sabrina Jun 25 '18 at 16:42
  • idk, I did exactly the above (apt, pip, and the python line) and it works, both with python 2.7 and 3.5 – f4. Jun 25 '18 at 16:45