在使用keras.utils 的 polt_model 方法时,遇到如下报错:
'`pydot` failed to call GraphViz.'
OSError: `pydot` failed to call GraphViz.Please install GraphViz (https://www.graphviz.org/) and ensure that its executables are in the $PATH.
我是一个守规矩的人,在这个报错出现前,我已经做了如下工作:
pip install pydot-ng
pip install graphviz
pip install pydot
下载graphviz-2.38.msi,安装至C盘,然后把安装文件夹下的bin文件夹加入PATH环境变量。
也许是准备工作做得太充分,在遇到问题后百度一番,发现别人因为不严谨而漏做的事情,严谨的我都已经做过了,不严谨的他们把他们的不严谨造成的漏洞弥补下,问题结决了,而我,头痛。
好不容易找到一篇,说是即使完成上述操作,依旧可能报这个错的,犹如抓住救命稻草,他说是装在C盘可能需要管理员权限运行,导致python调用需要的exe的时候权限不够,我看了自己的用户权限,就是管理员,尽管如此,我依旧一试,依旧报错。
走投无路情况下,我决定自己看看代码,根据报错的堆栈,找到了报错文件,C:\User\用户名\.conda\envs\deeplearning\lib\site-packages\keras\utils\vis_utils.py line 29,
抛出异常的代码是:
if pydot is None:
raise ImportError(
'Failed to import `pydot`. '
'Please install `pydot`. '
'For example with `pip install pydot`.')
try:
# Attempt to create an image of a blank graph
# to check the pydot/graphviz installation.
pydot.Dot.create(pydot.Dot())
except OSError:
raise OSError(
'`pydot` failed to call GraphViz.'
'Please install GraphViz (https://www.graphviz.org/) '
'and ensure that its executables are in the $PATH.')
于是先定位到pydot.Dot()方法,该方法代码如下:
def __init__(self, *argsl, **argsd):
Graph.__init__(self, *argsl, **argsd)
self.shape_files = list()
self.formats = [
'canon', 'cmap', 'cmapx',
'cmapx_np', 'dia', 'dot',
'fig', 'gd', 'gd2', 'gif',
'hpgl', 'imap', 'imap_np', 'ismap',
'jpe', 'jpeg', 'jpg', 'mif',
'mp', 'pcl', 'pdf', 'pic', 'plain',
'plain-ext', 'png', 'ps', 'ps2',
'svg', 'svgz', 'vml', 'vmlz',
'vrml', 'vtx', 'wbmp', 'xdot', 'xlib']
self.prog = 'dot'
# Automatically creates all
# the methods enabling the creation
# of output in any of the supported formats.
for frmt in self.formats:
def new_method(
f=frmt, prog=self.prog,
encoding=None):
"""Refer to docstring of method `create`."""
return self.create(
format=f, prog=prog, encoding=encoding)
name = 'create_{fmt}'.format(fmt=frmt)
self.__setattr__(name, new_method)
for frmt in self.formats+['raw']:
def new_method(
path, f=frmt, prog=self.prog,
encoding=None):
"""Refer to docstring of method `write.`"""
self.write(
path, format=f, prog=prog,
encoding=encoding)
name = 'write_{fmt}'.format(fmt=frmt)
self.__setattr__(name, new_method)
我一眼看到了 self.prog = 'dot' 这行代码,然后仅仅抱着试试看的心态,改成了这样:
self.prog = 'dot.exe'
然后就跑成功了......纠结一个小时的问题就这么解决了啊.......心里一万只草泥马奔腾而过啊......
于是写下来,希望相同遭遇的人能受益~