LoginSignup
4
1

More than 1 year has passed since last update.

tf.keras.utils の plot_model で Failed to import pydot. You must install pydot and graphviz for pydotprint to work と言われた時【TensorFlow】

Last updated at Posted at 2021-07-02

はじめに

タイトルの通り、tensorflow.keras.utilsplot_model を使った際、ImportError: Failed to import pydot. You must install pydot and graphviz for pydotprint to work と言われたときの解決策を書きます。

簡単なメモですが参考になれば嬉しいです。

環境

項目 version
OS Ubuntu 18.04
Python 3.7
TensorFlow 2.2

これらは Anaconda 上の Jupyter Notebook での話です。

エラーが起きたコード

from tensorflow.keras.utils import plot_model

# モデルは適当です
model = tf.keras.models.Sequential([
  tf.keras.layers.Flatten(input_shape=(28, 28)),
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Dense(10, activation='softmax')
])

plot_model(model)  # これ

このコードを実行すると、以下のようなエラーが出ます。

ImportError: Failed to import pydot. You must install pydot and graphviz for pydotprint to work

解決策

結論として、以下の3個のライブラリを順番にインストールすると解決しました。

conda install pydot
conda install pydotplus
conda install graphviz

anaconda環境でない場合はpipでも大丈夫だと思いますが未検証です。

余談集

ややこしい点や解決までにハマった点のメモです。

余談1: graphvizをインストールしなかった場合のエラー

graphvizのみインストールしていない場合は以下のエラーが出ました。きちんと3個インストールしましょう。

InvocationException: GraphViz's executables not found

余談2: plot_modelのパラメータについて

plot_modelにはいくつかパラメータがあります。
お好みに合わせて調整してみてください。

plot_model(model, show_layer_names=False, show_shapes=True, to_file="../fig/model.png")

余談3: to_fileで指定できる拡張子について

to_fileで指定できる拡張子は限られているようで、.pdf.jpgは以下のようなエラーが出てしまいました(自分だけ?)。

ValueError: Cannot embed the 'pdf' image format

しかし、フォルダを確認したところきちんと保存されていたので、皆さんも一度フォルダを確認してみてください。

おわりに

短い記事でしたがご覧頂きありがとうございました。
ご意見等ございましたらコメントお願いします。

4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1