keras 和 pydot 中 plot_model 的问题

Issue with plot_model in keras and pydot

我读过类似的问题 - 我的错误似乎有所不同,因为提出的解决方案没有解决我的问题。

我在绘制 keras 模型图时遇到问题。

我已经使用自制软件安装了 graphviz 二进制文件

我已经使用 pip 安装了 graphviz python wrapper 和 pydot(也尝试使用 conda,因为这在过去似乎是个问题)。

使用 python 3.5

运行:

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

我收到错误:

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

跟踪:

---------------------------------------------------------------------------
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,

我可以独立导入pydot和graphviz了

keras 和 graphviz 之间似乎有错误的历史。关于解决方案的任何想法?

错误消息不明确:当成功导入 pydot(或模块 vis_utils 中提到的任何分支)但调用 pydot.Dot.create 时也可能引发异常失败。来自 https://github.com/keras-team/keras/blob/4eab0556d29f11ff41758d80c15d6457263f6a93/keras/utils/vis_utils.py:

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.')

并且方法 pydot.Dot.create 尝试调用可执行文件 dot(由 GraphViz 安装):

https://github.com/erocarrera/pydot/blob/d6ac9e9244d1a882103422ac2b35ceef96f5dfe3/pydot.py#L1856

如果 dot 不在环境的 PATH 变量中,那么它对 pydot 是不可见的,尽管它存在于机器上。

在 Python 解释器中导入包意味着它们在 site-packages 下可用,或者从它们以开发模式安装的任何地方(例如,使用 python setup.py develop,或使用 pip install -e .). GraphViz 的可执行文件是否在路径上是一个单独的问题。

此外,Python 包 graphvizpydot 无关,并且不需要通过 pydot 使用 GraphViz。有关此问题的更多信息,请参阅:

我用

解决了

sudo apt-get install graphviz

我用了“conda install graphviz”,它解决了问题。