Keras和Pydot中plot_model存在问题

6

我已经阅读了类似的问题-我的错误似乎不同,因为提出的解决方案不能解决我的问题。

我在绘制keras模型的图表时遇到了麻烦。

我使用homebrew安装了graphviz二进制文件

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

使用python 3.5

运行:

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

我收到以下错误:

导入pydot失败。您必须安装pydot和graphviz才能使pydotprint正常工作。

带有跟踪:

---------------------------------------------------------------------------
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之间存在一些历史性的错误。有没有解决方案?

3个回答

8
我用以下命令解决了这个问题:sudo apt-get install graphviz,它会安装一个名为“graphviz”的程序。

1
你的回答给了我解决问题的方向。我正在使用我的Mac电脑,最初尝试了brew install graphviz但没有成功。然后我执行了brew link --overwrite graphviz,一切都按预期工作了! - Qiushi Huang

6
我使用了 "conda install graphviz" 命令,问题得到了解决。

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

方法 pydot.Dot.create 试图调用由 GraphViz 安装的可执行文件 dot

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

如果 dot 不在环境变量 PATH 中,则对于 pydot 而言它是不可见的,即使它存在于计算机上。

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

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

https://dev59.com/-FYN5IYBdhLWcg3wm5EL#47209738


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接