鰤切手

旧初めての理系編入。編入から無職まであなたのお供をします。

【スポンサーリンク】

深層学習を可視化できるライブラリManimMLを試す 深層学習編

先日

www.itmedia.co.jp

上記の記事を見かけました。

深層学習って何よってよく聞かれるのですが、「えー層があってそこに画像などのデータを数値化したものが・・・」って話そうもんなら相手を置いてってしまうし、どうしたものかとよく悩んでいたんですよね。

そんな時にアニメーション簡単に作れる上記のライブラリ使えば説明簡単にできるじゃん!と思い、ためにしにいじろうと今に至る。

参考になれば幸いです。

 

お品書き

 

ManimMLとは

ざっくりいうとPython使って深層学習の様子をアニメーションにできるライブラリ、詳しい説明は以下を見てください。

github.com

 

試しに自分も作ってみたのですがこんなのが深層学習のモデルをコードで作るようにできるので本当に助かります。

 

 

環境構築

今回はgoogle colaboratoryを使ってやりました。

colabのインストールの仕方も下記に書いてあって助かります。

docs.manim.community

ほかの環境構築は上記公式サイトに詳しく載ってますので参考にしてください。

 

 

必要なライブラリのインストール

ってなわけでライブラリ周りをインストールします。

 

!sudo apt update
!sudo apt install libcairo2-dev ffmpeg \
    texlive texlive-latex-extra texlive-fonts-extra \
    texlive-latex-recommended texlive-science \
    tipa libpango1.0-dev
!pip install manim
!pip install IPython --upgrade

 

このコマンドを打って実行。

すると、ランタイムを再起動しろって言われるので上の「ランタイム」タブから「ランタイムを再起動」を選択。

 

次に新しいセルを作って以下を実行

!pip install manim_ml

 

最後に

from manim import *

のインポート文を書いて下準備は完成。

 

 

試しにアニメーションを作る

下準備ができたので新しいセルでアニメーションを作ってみましょう。

github.com

 

にある最初のコードは20分ぐらいかかったのでもう少し簡単なやつで行うといいです。

 
%%manim -qm -v WARNING BasicScene
from manim_ml.neural_network import FeedForwardLayer, NeuralNetwork

# This changes the resolution of our rendered videos
config.pixel_height = 700
config.pixel_width = 1900
config.frame_height = 7.0
config.frame_width = 7.0

# Here we define our basic scene
class BasicScene(ThreeDScene):

    # The code for generating our scene goes here
    def construct(self):
        # Make the neural network
        nn = NeuralNetwork([
                FeedForwardLayer(3),
                FeedForwardLayer(7),
                FeedForwardLayer(5)
            ],
            layer_spacing=0.25,
        )
        # Center the neural network
        nn.move_to(ORIGIN)
        self.add(nn)
        # Make a forward pass animation
        forward_pass = nn.make_forward_pass_animation()
        # Play animation
        self.play(forward_pass)

実行結果

 

完成。PyTorchで深層学習をするときのコードを書いているようでびっくりですね。

 

入力画像を追加する

もう少し伝わりやすくなるように入力画像を入れてみます。

 

%%manim -qm -v WARNING BasicScene
import numpy as np
from PIL import Image
from manim_ml.neural_network import NeuralNetwork, FeedForwardLayer, ImageLayer

image = Image.open("data") # You will need to download an image of a digit.
numpy_image = np.asarray(image)

# This changes the resolution of our rendered videos
config.pixel_height = 700
config.pixel_width = 1900
config.frame_height = 7.0
config.frame_width = 7.0

# Here we define our basic scene
class BasicScene(ThreeDScene):

    # The code for generating our scene goes here
    def construct(self):
        # Make the neural network
        nn = NeuralNetwork([
                ImageLayer(numpy_image, height=1.5),
                FeedForwardLayer(784),
                FeedForwardLayer(256),
                FeedForwardLayer(1)
            ],
            layer_spacing=0.25,
        )
        # Center the neural network
        nn.move_to(ORIGIN)
        self.add(nn)
        # Make a forward pass animation
        forward_pass = nn.make_forward_pass_animation()
        # Play animation
        self.play(forward_pass)

 

image = Image.open("data") # You will need to download an image of a digit.
numpy_image = np.asarray(image)
 
の箇所で画像をダウンロードして
 
ImageLayer(numpy_image, height=1.5),
 
の箇所で層に画像を入れています。
 
とやろうとしたら二時間ぐらいかかっても回りきらず・・・
ここから動かなくなってしまいました。

層のノード(丸の部分)が多すぎるのかもしれません。
 
 

ノードを少なくする

FeedForwardLayerの中の値を小さくしてみました。するとめっちゃ早く終わりました。

%%manim -qm -v WARNING BasicScene
import numpy as np
from PIL import Image
from manim_ml.neural_network import NeuralNetwork, FeedForwardLayer, ImageLayer

image = Image.open("data.png") # You will need to download an image of a digit.
numpy_image = np.asarray(image)

# This changes the resolution of our rendered videos
config.pixel_height = 700
config.pixel_width = 1900
config.frame_height = 7.0
config.frame_width = 7.0

# Here we define our basic scene
class BasicScene(ThreeDScene):

    # The code for generating our scene goes here
    def construct(self):
        # Make the neural network
        nn = NeuralNetwork([
                ImageLayer(numpy_image, height=1.5),
                FeedForwardLayer(7), # 数を減らした
                FeedForwardLayer(3),
                FeedForwardLayer(1)
            ],
            layer_spacing=0.25,
        )
        # Center the neural network
        nn.move_to(ORIGIN)
        self.add(nn)
        # Make a forward pass animation
        forward_pass = nn.make_forward_pass_animation()
        # Play animation
        self.play(forward_pass)
出力結果

これで値の伝搬のイメージが伝わりやすくなると思います。
画像のピクセルごとから値出ていると最高でしたね。
 
こんなかんじで深層学習モデルの学習の様子を可視化できました。
もう少しいじってみたいと思います。