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 ,我是在這裏下載的 https://www.5down.net/soft/graphviz.html
- 我安裝在了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'
-
保存再次運行程序即可
- 在報錯的位置找到