簡體   English   中英

嘗試使用 keras.utils.plot_model 時出錯

[英]error when trying to use keras.utils.plot_model

我正在嘗試使用 Keras package 在 Python 中繪制深度學習 model 的圖/圖,但不幸的是,它一直給我一個信息不多的錯誤。

我在 Linux 和 Python 3.5.2、Anaconda 4.2.0、Keras 2.1.6 上運行 python,我使用 tensorflow-gpu 1.7.0 后端。

這是錯誤消息:

keras.utils.plot_model(unet, to_file='model.png', show_shapes=False, show_layer_names=True, rankdir='TB')

['dot', '-Tps', '/tmp/tmphesl1j0c'] return code: 127

stdout, stderr:
 b''
b'dot: error while loading shared libraries: libexpat.so.0: cannot open shared object file: No such file or directory\n'

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-9-60bb0e3b97bd> in <module>()
----> 1 keras.utils.plot_model(unet, to_file='model.png', show_shapes=False, show_layer_names=True, rankdir='TB')

/.../anaconda3-4.2.0/lib/python3.5/site-packages/keras/utils/vis_utils.py in plot_model(model, to_file, show_shapes, show_layer_names, rankdir)
    132             'LR' creates a horizontal plot.
    133     """
--> 134     dot = model_to_dot(model, show_shapes, show_layer_names, rankdir)
    135     _, extension = os.path.splitext(to_file)
    136     if not extension:

/.../anaconda3-4.2.0/lib/python3.5/site-packages/keras/utils/vis_utils.py in model_to_dot(model, show_shapes, show_layer_names, rankdir)
     53     from ..models import Sequential
     54 
---> 55     _check_pydot()
     56     dot = pydot.Dot()
     57     dot.set('rankdir', rankdir)

/.../anaconda3-4.2.0/lib/python3.5/site-packages/keras/utils/vis_utils.py in _check_pydot()
     24         # Attempt to create an image of a blank graph
     25         # to check the pydot/graphviz installation.
---> 26         pydot.Dot.create(pydot.Dot())
     27     except OSError:
     28         raise OSError(

/.../anaconda3-4.2.0/lib/python3.5/site-packages/pydot.py in create(self, prog, format, encoding)
   1882                      out=stdout_data,
   1883                      err=stderr_data))
-> 1884         assert p.returncode == 0, p.returncode
   1885         return stdout_data

AssertionError: 127

如果有人可以幫助我解決這個錯誤,我將不勝感激。

注意:pydot 和 graphviz 都安裝了

對我來說,解決方案是像這樣導入:

from keras.utils.vis_utils import plot_model

我將 keras.utils 更改為 tensorflow.keras.utils,它幫助了我

似乎存在一些兼容性問題! (鏈接)

安裝Graphviz並將其添加到路徑對我有用

對我來說,解決方案是:

  • conda install pydotplus (它說pydot-ng不能用tensorflow-gpu安裝)。
  • 在 anaconda 目錄中搜索 viz_utils.py 並打開它們。 確保在任何地方都導入了 pydot,它是通過以下方式完成的:
try:
  # pydot-ng is a fork of pydot that is better maintained.
  import pydot_ng as pydot
except ImportError:
  # pydotplus is an improved version of pydot
  try:
    import pydotplus as pydot
  except ImportError:
    # Fall back on pydot if necessary.
    try:
      import pydot
    except ImportError:
      pydot = None

有一個文件只是說import pyplot 更改后,它對我有用。

如果您運行的是 PyCharm 之類的 IDE,則在安裝 pydot 並安裝 Graphviz 之后(還將其添加到環境PATH 變量中。例如 C:\\Program Files\\Graphviz\\bin。請參閱此處https://www.architectryan.com/2018/ 03/17/add-to-the-path-on-windows-10/ ),您應該重新啟動 IDE

如果您在虛擬環境中工作,我建議停用並重新啟動終端並再次激活虛擬環境。

原因- 包試圖從 os.environ['PATH'] 中找到 graphviz,但不知何故它沒有更新以在路徑中顯示 graphviz。 重新啟動 Pycharm 后,我發現 os.environ['PATH'] 已正確更新並且 plot_model 函數正常工作。

對我來說,有一個錯誤,我沒有在 model.png 中輸入 ''。

plot_model(模型,to_file='model.png',show_shapes=True)

我解決了在pydot.py評論第 117 行program += extensionpydot.py

from keras.utils.vis_utils import plot_model
keras.utils.vis_utils.plot_model(
csf1, to_file='model.png', show_shapes=True, show_dtype=True,
show_layer_names=True, rankdir='TB', expand_nested=True, dpi=96
)

您可以使用此代碼,它可以正確地與我的項目一起使用。

以下代碼對我有用:

conda install -c anaconda graphviz
conda install pydotplus

只需打開終端並運行命令即可。 那你應該沒問題。

自從過去 2 天以來,我什至都面臨着這個錯誤。

我做了以下事情:

conda uninstall pyplot
conda uninstall pyplotplus
conda uninstall graphviz

然后,我再次安裝了這三個包。

conda install pyplot
conda install pyplotplus
conda install graphviz

希望這可以幫助!

評論說

 24         # Attempt to create an image of a blank graph
 25         # to check the pydot/graphviz installation.

所以我想你需要安裝graphviz和pydot

假設您使用的是 ubuntu 或類似版本:

sudo apt install graphviz

在你的 anaconda 環境中:

pip install pydot

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM