解决 Windows OSError - pydot failed to call GraphViz.Please install GraphViz 报错

Windows操作系统下,运行pydot相关程序时(我的是keras.utils.plot_model)报错,提示没有安装GraphViz,事实上并不都是因为GraphViz没有安装,本文记录错误解决方法。

问题复现

操作系统:Win10

keras版本:2.2.4

在Win10系统下(Windows系列都可能出这个问题)keras建立简单的模型,执行 plot_model,报错:

import keras 
from keras.models import Model
from keras.layers import Input
from keras.layers import Conv2D
from keras.layers import GlobalAveragePooling2D
from keras.layers import Dense

import numpy as np

from keras.utils import plot_model

import os
os.environ["PATH"] += os.pathsep + r'E:\Program Files (x86)\Graphviz2.38\bin'


A = Input(shape=(16,16,3))
x = Conv2D(filters=10, kernel_size=(3,3), padding='same', activation='relu')(A)
x = Conv2D(filters=10, kernel_size=(3,3), padding='same', activation='relu')(x)
x = GlobalAveragePooling2D()(x)
x = Dense(units = 5, activation='softmax')(x)

model = Model(A,x)

model.summary()

test_input = np.random.rand(1,16,16,3)

results = model.predict(test_input)

plot_model(model)
复制代码

错误信息:

builtins.OSError: `pydot` failed to call GraphViz.Please install GraphViz (https://www.graphviz.org/) and ensure that its executables are in the $PATH.
复制代码

问题原因与解决方案

情况 1

  • 原因 :真的没有安装GraphViz
  • 解决方案:
    • 安装相应模块
pip install pydot-ng 
pip install graphviz 
pip install pydot 
复制代码

如果问题没有排除,可能是GraphViz程序没有加入到系统路径,考虑情况2

情况 2

  • 原因:GraphViz程序没有加入到系统路径
  • 解决方案:
    • 下载graphviz-2.38.msi ,我是在这里下载的 www.5down.net/soft/graphv…
    • 我安装在了E盘:E:\Program Files (x86)\Graphviz2.38\bin
    • 将路径加入到系统变量

目前为止是网上大多数存在的解决方案,相信大部分的同学到此为止已经解决了问题。

如果错误继续,那么我和你一样,进入情况3。

情况 3

  • 原因:依赖模块已经安装、程序已经加入系统变量,仍然出现上述提示,是因为pydot在建立Dot类时查找的dot程序的名字是 ’dot‘ 而不是我们 Windows 里的可执行程序文件名 'dot.exe'

  • 解决方案:改过来就好了,具体方法如下

    • 在报错的位置找到pydot

    • 找到Dot类

    • 类的开头代码是这样的:

class Dot(Graph):
    """A container for handling a dot language file.

    This class implements methods to write and process
    a dot language file. It is a derived class of
    the base class 'Graph'.
    """
复制代码
  • 找到其中的 self.prog = 'dot'

  • 讲这句话替换为:

import platform
system = platform.system()
            
if system == 'Windows':
    self.prog = 'dot.exe'
else:
    self.prog = 'dot'
复制代码
  • 保存再次运行程序即可

猜你喜欢

转载自juejin.im/post/7018372456335278117