keras 和 pydot 中的 plot_model 问题

use*_*745 3 python graphviz pydot keras

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

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

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

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

使用 python 3.5

跑步:

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

我收到错误:

导入错误:无法导入 pydot。您必须安装 pydot 和 graphvizpydotprint才能工作。

跟踪:

---------------------------------------------------------------------------
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,
Run Code Online (Sandbox Code Playgroud)

我可以独立成功导入pydot和graphviz。

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

iun*_*n1x 7

我解决了

sudo apt-get 安装 graphviz


小智 5

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


Ioa*_*dis 4

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.')
Run Code Online (Sandbox Code Playgroud)

该方法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 包graphviz与 无关pydot,并且不需要通过 GraphViz 使用pydot。有关此问题的更多信息,请参阅:

/sf/answers/3304681691/