Skip to content

Commit

Permalink
Update readme and setup code to be pushed to pip repository.
Browse files Browse the repository at this point in the history
  • Loading branch information
helblazer811 committed Dec 31, 2022
1 parent 313d1aa commit 334662e
Show file tree
Hide file tree
Showing 11 changed files with 341 additions and 154 deletions.
6 changes: 6 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ class VariationalAutoencoderScene(Scene):
self.play(neural_network.make_forward_pass_animation(run_time=15))
```

### Convolutional Neural Network

This is a visualization of a Convolutional Neural Network.

<img src="examples/media/CNNScene.gif">

### Generative Adversarial Network

This is a visualization of a Generative Adversarial Network made using ManimML.
Expand Down
25 changes: 0 additions & 25 deletions examples/cnn/cnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,26 @@
from manim import *
from PIL import Image

<<<<<<< HEAD
from manim_ml.neural_network.layers.convolutional3d import Convolutional3DLayer
=======
from manim_ml.neural_network.layers import Convolutional3DLayer
>>>>>>> 0bc3ad561ba224f3d33e9f843665c1d50d64a68b
from manim_ml.neural_network.layers.feed_forward import FeedForwardLayer
from manim_ml.neural_network.layers.image import ImageLayer
from manim_ml.neural_network.neural_network import NeuralNetwork

<<<<<<< HEAD
# Make the specific scene
config.pixel_height = 1200
config.pixel_width = 1900
config.frame_height = 7.0
config.frame_width = 7.0
=======
ROOT_DIR = Path(__file__).parents[2]
>>>>>>> 0bc3ad561ba224f3d33e9f843665c1d50d64a68b

def make_code_snippet():
code_str = """
# Make nn
nn = NeuralNetwork([
<<<<<<< HEAD
ImageLayer(numpy_image, height=1.5),
Convolutional3DLayer(1, 7, 7, 3, 3),
Convolutional3DLayer(3, 5, 5, 3, 3),
Convolutional3DLayer(5, 3, 3, 1, 1),
=======
ImageLayer(numpy_image),
Convolutional3DLayer(3, 3, 3),
Convolutional3DLayer(5, 2, 2),
Convolutional3DLayer(10, 2, 1),
>>>>>>> 0bc3ad561ba224f3d33e9f843665c1d50d64a68b
FeedForwardLayer(3),
FeedForwardLayer(3),
])
Expand Down Expand Up @@ -64,7 +50,6 @@ def construct(self):
numpy_image = np.asarray(image)
# Make nn
nn = NeuralNetwork([
<<<<<<< HEAD
ImageLayer(numpy_image, height=1.5),
Convolutional3DLayer(1, 7, 7, 3, 3, filter_spacing=0.32),
Convolutional3DLayer(3, 5, 5, 3, 3, filter_spacing=0.32),
Expand All @@ -75,16 +60,6 @@ def construct(self):
layer_spacing=0.25,
)
# Center the nn
=======
ImageLayer(numpy_image, height=3.5),
Convolutional3DLayer(3, 3, 3, filter_spacing=0.2),
Convolutional3DLayer(5, 2, 2, filter_spacing=0.2),
Convolutional3DLayer(10, 2, 1, filter_spacing=0.2),
FeedForwardLayer(3, rectangle_stroke_width=4, node_stroke_width=4).scale(2),
FeedForwardLayer(1, rectangle_stroke_width=4, node_stroke_width=4).scale(2)
], layer_spacing=0.2)
nn.scale(0.9)
>>>>>>> 0bc3ad561ba224f3d33e9f843665c1d50d64a68b
nn.move_to(ORIGIN)
self.add(nn)
# Make code snippet
Expand Down
85 changes: 85 additions & 0 deletions examples/code_snippet/image_nn_code_snippet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
from manim import *
from manim_ml.neural_network.layers import FeedForwardLayer, ImageLayer
from manim_ml.neural_network.neural_network import NeuralNetwork
from PIL import Image
import numpy as np

config.pixel_height = 720
config.pixel_width = 1280
config.frame_height = 6.0
config.frame_width = 6.0

class ImageNeuralNetworkScene(Scene):

def make_code_snippet(self):
code_str = """
# Make image object
image = Image.open('images/image.jpeg')
numpy_image = np.asarray(image)
# Make Neural Network
layers = [
ImageLayer(numpy_image, height=1.4),
FeedForwardLayer(3),
FeedForwardLayer(5),
FeedForwardLayer(3)
]
nn = NeuralNetwork(layers)
self.add(nn)
# Play animation
self.play(
nn.make_forward_pass_animation()
)
"""

code = Code(
code = code_str,
tab_width=4,
background_stroke_width=1,
background_stroke_color=WHITE,
insert_line_no=False,
style='monokai',
#background="window",
language="py",
)
code.scale(0.2)

return code

def construct(self):
image = Image.open('../../tests/images/image.jpeg')
numpy_image = np.asarray(image)
# Make nn
layers = [
ImageLayer(numpy_image, height=1.4),
FeedForwardLayer(3),
FeedForwardLayer(5),
FeedForwardLayer(3),
FeedForwardLayer(6)
]
nn = NeuralNetwork(layers)
nn.scale(0.9)
# Center the nn
nn.move_to(ORIGIN)
nn.rotate(-PI/2)
nn.layers[0].image_mobject.rotate(PI/2)
nn.layers[0].image_mobject.shift([0, -0.4, 0])
nn.shift([1.5, 0.3, 0])
self.add(nn)
# Make code snippet
code_snippet = self.make_code_snippet()
code_snippet.scale(1.9)
code_snippet.shift([-1.25, 0, 0])
self.add(code_snippet)
# Play animation
self.play(
nn.make_forward_pass_animation(run_time=10)
)

if __name__ == "__main__":
"""Render all scenes"""
# Feed Forward Neural Network
ffnn_scene = FeedForwardNeuralNetworkScene()
ffnn_scene.render()
# Neural Network
nn_scene = NeuralNetworkScene()
nn_scene.render()
85 changes: 85 additions & 0 deletions examples/code_snippet/vae_code_landscape.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
from manim import *
from manim_ml.neural_network.layers import FeedForwardLayer, ImageLayer, EmbeddingLayer
from manim_ml.neural_network.neural_network import NeuralNetwork
from PIL import Image
import numpy as np

config.pixel_height = 720
config.pixel_width = 720
config.frame_height = 6.0
config.frame_width = 6.0

class VAECodeSnippetScene(Scene):

def make_code_snippet(self):
code_str = """
# Make Neural Network
nn = NeuralNetwork([
ImageLayer(numpy_image, height=1.2),
FeedForwardLayer(5),
FeedForwardLayer(3),
EmbeddingLayer(),
FeedForwardLayer(3),
FeedForwardLayer(5),
ImageLayer(numpy_image, height=1.2),
], layer_spacing=0.1)
# Play animation
self.play(nn.make_forward_pass_animation())
"""

code = Code(
code = code_str,
tab_width=4,
background_stroke_width=1,
# background_stroke_color=WHITE,
insert_line_no=False,
background="window",
# font="Monospace",
style='one-dark',
language="py",
)

return code

def construct(self):
image = Image.open('../../tests/images/image.jpeg')
numpy_image = np.asarray(image)
embedding_layer = EmbeddingLayer(dist_theme="ellipse", point_radius=0.04).scale(1.0)
# Make nn
nn = NeuralNetwork([
ImageLayer(numpy_image, height=1.2),
FeedForwardLayer(5),
FeedForwardLayer(3),
embedding_layer,
FeedForwardLayer(3),
FeedForwardLayer(5),
ImageLayer(numpy_image, height=1.2),
], layer_spacing=0.1)

nn.scale(1.1)
# Center the nn
nn.move_to(ORIGIN)
# nn.rotate(-PI/2)
# nn.all_layers[0].image_mobject.rotate(PI/2)
# nn.all_layers[0].image_mobject.shift([0, -0.4, 0])
# nn.all_layers[-1].image_mobject.rotate(PI/2)
# nn.all_layers[-1].image_mobject.shift([0, -0.4, 0])
nn.shift([0, -1.4, 0])
self.add(nn)
# Make code snippet
code_snippet = self.make_code_snippet()
code_snippet.scale(0.52)
code_snippet.shift([0, 1.25, 0])
# code_snippet.shift([-1.25, 0, 0])
self.add(code_snippet)
# Play animation
self.play(
nn.make_forward_pass_animation(),
run_time=10
)

if __name__ == "__main__":
"""Render all scenes"""
# Neural Network
nn_scene = VAECodeSnippetScene()
nn_scene.render()
92 changes: 92 additions & 0 deletions examples/code_snippet/vae_nn_code_snippet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
from manim import *
from manim_ml.neural_network.layers import FeedForwardLayer, ImageLayer, EmbeddingLayer
from manim_ml.neural_network.neural_network import NeuralNetwork
from PIL import Image
import numpy as np

config.pixel_height = 720
config.pixel_width = 1280
config.frame_height = 6.0
config.frame_width = 6.0

class VAECodeSnippetScene(Scene):

def make_code_snippet(self):
code_str = """
# Make image object
image = Image.open('images/image.jpeg')
numpy_image = np.asarray(image)
# Make Neural Network
nn = NeuralNetwork([
ImageLayer(numpy_image, height=1.2),
FeedForwardLayer(5),
FeedForwardLayer(3),
EmbeddingLayer(),
FeedForwardLayer(3),
FeedForwardLayer(5),
ImageLayer(numpy_image, height=1.2),
], layer_spacing=0.1)
self.add(nn)
# Play animation
self.play(
nn.make_forward_pass_animation()
)
"""

code = Code(
code = code_str,
tab_width=4,
background_stroke_width=1,
# background_stroke_color=WHITE,
insert_line_no=False,
background="window",
# font="Monospace",
style='one-dark',
language="py",
)
code.scale(0.2)

return code

def construct(self):
image = Image.open('../../tests/images/image.jpeg')
numpy_image = np.asarray(image)
embedding_layer = EmbeddingLayer(dist_theme="ellipse", point_radius=0.04).scale(1.0)
# Make nn
nn = NeuralNetwork([
ImageLayer(numpy_image, height=1.0),
FeedForwardLayer(5),
FeedForwardLayer(3),
embedding_layer,
FeedForwardLayer(3),
FeedForwardLayer(5),
ImageLayer(numpy_image, height=1.0),
], layer_spacing=0.1)

nn.scale(0.65)
# Center the nn
nn.move_to(ORIGIN)
nn.rotate(-PI/2)
nn.all_layers[0].image_mobject.rotate(PI/2)
# nn.all_layers[0].image_mobject.shift([0, -0.4, 0])
nn.all_layers[-1].image_mobject.rotate(PI/2)
# nn.all_layers[-1].image_mobject.shift([0, -0.4, 0])
nn.shift([1.5, 0.0, 0])
self.add(nn)
# Make code snippet
code_snippet = self.make_code_snippet()
code_snippet.scale(1.9)
code_snippet.shift([-1.25, 0, 0])
self.add(code_snippet)
# Play animation
self.play(
nn.make_forward_pass_animation(),
run_time=10
)


if __name__ == "__main__":
"""Render all scenes"""
# Neural Network
nn_scene = VAECodeSnippetScene()
nn_scene.render()
41 changes: 41 additions & 0 deletions examples/logo/logo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""
Logo for Manim Machine Learning
"""
from manim import *
from manim_ml.neural_network.neural_network import FeedForwardNeuralNetwork

config.pixel_height = 500
config.pixel_width = 500
config.frame_height = 4.0
config.frame_width = 4.0

class ManimMLLogo(Scene):

def construct(self):
self.text = Text("ManimML")
self.text.scale(1.0)
self.neural_network = FeedForwardNeuralNetwork([3, 5, 3, 6, 3], layer_spacing=0.3, node_color=BLUE)
self.neural_network.scale(1.0)
self.neural_network.move_to(self.text.get_bottom())
self.neural_network.shift(1.25 * DOWN)
self.logo_group = Group(self.text, self.neural_network)
self.logo_group.scale(1.0)
self.logo_group.move_to(ORIGIN)
self.play(Write(self.text))
self.play(Create(self.neural_network))
# self.surrounding_rectangle = SurroundingRectangle(self.logo_group, buff=0.3, color=BLUE)
underline = Underline(self.text, color=BLUE)
animation_group = AnimationGroup(
self.neural_network.make_forward_pass_animation(run_time=5),
Create(underline),
# Create(self.surrounding_rectangle)
)
# self.surrounding_rectangle = SurroundingRectangle(self.logo_group, buff=0.3, color=BLUE)
underline = Underline(self.text, color=BLUE)
animation_group = AnimationGroup(
self.neural_network.make_forward_pass_animation(run_time=5),
Create(underline),
# Create(self.surrounding_rectangle)
)
self.play(animation_group)
self.wait(5)
Loading

0 comments on commit 334662e

Please sign in to comment.