matplotlibでjupyter notebookカーネルが落ちる

事象

jupyter notebookでmatplotlibで以下のようなコードで動画描画を実行するとカーネルが停止する。

from JSAnimation.IPython_display import display_animation
from matplotlib import animation
from IPython.display import display

def display_frames_as_gif(frames):
    plt.figure(figsize=(frames[0].shape[1]/72.0, frames[0].shape[0]/72.0), dpi=72)
    patch = plt.imshow(frames[0])
    plt.axis('off')

    def animate(i):
        patch.set_data(frames[i])
        
    anim = animation.FuncAnimation(plt.gcf(), animate, frames=len(frames), interval=50)
    anim.save('movie_cartpole.mp4')
    display(display_animation(anim, default_mode='loop'))

(display_frames_as_gifを呼び出すコードを実行)

原因

問題のコードをコマンドプロンプトからPythonを対話的実行して、デバッグ文を確認する。

$ Python
(略_問題のコードを1行ずつ実行)
OMP: Error #15: Initializing libiomp5md.dll, but found libiomp5md.dll already initialized.
OMP: Hint This means that multiple copies of the OpenMP runtime have been linked into the program. That is dangerous, since it can degrade performance or cause incorrect results. The best thing to do is to ensure that only a single OpenMP runtime is linked into the process, e.g. by avoiding static linking of the OpenMP runtime in any library. As an unsafe, unsupported, undocumented workaround you can set the environment variable KMP_DUPLICATE_LIB_OK=TRUE to allow the program to continue to execute, but that may cause crashes or silently produce incorrect results. For more information, please see http://www.intel.com/software/products/support/.

 要するに動的リンクの「libiomp5md.dll」が複数あり、どれを読み込めばいいか分からないことが原因の模様。
(OpenMP実行環境とかは、エラー発生とは直接関係ないので記載しない)

.dll拡張子

 .dll拡張子は、Windows用にコンパイルされたバイナリファイルで、「動的リンクファイル」というらしい。プログラムを実行中に読み込まれるファイル。つまりは、普通のプログラム言語でいうところのライブラリのWindows用バイナリファイル版。

解決

仮想環境のPathを調べる。仮想環境をオンにし、以下のコマンド。

(venv) $>PATH

Pathを調べたところ、仮想環境オン時には、主に以下のパスが追加される模様。
(私はanacondaを利用しています。)

  1. anaconda直下のパス
  2. オンにした仮想環境配下の様々なパス

つまり1、2でエクスプローラーなどでファイル検索で、「libiomp5md.dll」が重複していないか調べ、重複している場合は1,2のどちらかを消す操作をする。
※不都合あった際に戻せるように、削除ではなくPathの通ってないフォルダに移動させるようにしましょう。
※1,2をどちらを消す操作をすべきかよくわかりません。1はすべての仮想環境で共通のパスなので、全仮想環境に影響が波及します。

感想

 動的リンクファイルが重複することが原因。.dllファイルなど勉強になりました。ただし、なぜ重複した.dllファイルインストールされたのか。。。

コメント

タイトルとURLをコピーしました