【问题标题】:Issue with plot_model in keras and pydotkeras 和 pydot 中的 plot_model 问题
【发布时间】:2018-07-28 06:42:33
【问题描述】:

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

我无法绘制 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')

我得到错误:

ImportError: 无法导入 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 之间似乎有一段错误的历史。关于解决方案的任何想法?

【问题讨论】:

    标签: python keras graphviz pydot


    【解决方案1】:

    错误消息不明确:当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。有关此问题的更多信息,请参阅:

    https://stackoverflow.com/a/47209738/1959808

    【讨论】:

      【解决方案2】:

      我用

      解决了

      sudo apt-get install graphviz

      【讨论】:

      • 您的回答为我指明了如何解决该问题。我在我的 Mac 上,首先我尝试了 brew install graphviz 但没有运气。然后我执行了brew link --overwrite graphviz,一切正常!
      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2018-04-21
        • 2018-04-22
        • 2020-01-01
        • 2019-05-05
        • 2020-06-10
        • 2016-08-21
        • 1970-01-01
        • 1970-01-01
        • 2019-02-02
        相关资源
        最近更新 更多