本文最后更新于:2023年12月5日 下午

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

问题复现

操作系统:Win10

keras版本:2.2.4

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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)

错误信息:

1
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
  • 解决方案:
    • 安装相应模块
1
2
3
pip install pydot-ng 
pip install graphviz
pip install pydot

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

情况 2

  • 原因:GraphViz程序没有加入到系统路径
  • 解决方案:

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

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

情况 3

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

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

    • 在报错的位置找到pydot

    • 找到Dot类

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

1
2
3
4
5
6
7
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'

  • 讲这句话替换为:

1
2
3
4
5
6
7
import platform
system = platform.system()

if system == 'Windows':
self.prog = 'dot.exe'
else:
self.prog = 'dot'
  • 保存再次运行程序即可



文章链接:
https://www.zywvvd.com/notes/study/deep-learning/keras/call-GraphViz-failed/call-GraphViz-failed/


“觉得不错的话,给点打赏吧 ୧(๑•̀⌄•́๑)૭”

微信二维码

微信支付

支付宝二维码

支付宝支付

解决 Windows OSError - pydot failed to call GraphViz.Please install GraphViz 报错
https://www.zywvvd.com/notes/study/deep-learning/keras/call-GraphViz-failed/call-GraphViz-failed/
作者
Yiwei Zhang
发布于
2020年5月20日
许可协议