Sabrina
Sabrina

Reputation: 103

error when trying to use keras.utils.plot_model

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

Upvotes: 5

Views: 23457

Answers (12)

Gundyabhau Dandekar
Gundyabhau Dandekar

Reputation: 1

Try this way

import keras
keras.utils.plot_model(Your_Parameters)

or

from keras.utils import plot_model
plot_model(Your_Parameters)

Upvotes: 0

Bogdan Ivchenko
Bogdan Ivchenko

Reputation: 11

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)

Upvotes: 1

Kinnari Bhalerao
Kinnari Bhalerao

Reputation: 1

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!

Upvotes: 0

Michael Chao
Michael Chao

Reputation: 883

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.

Upvotes: 0

ahmed almindelawy
ahmed almindelawy

Reputation: 3

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.

Upvotes: 0

DomagojM
DomagojM

Reputation: 101

For me solution was to import like this:

from keras.utils.vis_utils import plot_model

Upvotes: 7

Sparsh Singhal
Sparsh Singhal

Reputation: 11

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.

Upvotes: 1

Kymbat Taalaibekova
Kymbat Taalaibekova

Reputation: 71

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

Upvotes: 6

Luca
Luca

Reputation: 1810

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

Upvotes: 0

smoothware
smoothware

Reputation: 948

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.

Upvotes: 6

Mahdi Rafatjah
Mahdi Rafatjah

Reputation: 1072

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

Installing Graphviz and adding it to paths works for me.

Upvotes: 1

f4.
f4.

Reputation: 3852

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

Upvotes: -1

Related Questions