LeNet
In [1]:
Copied!
from PIL import Image as PILImage
from chalk import *
from colour import Color
from chalk import BoundingBox
from PIL import Image as PILImage from chalk import * from colour import Color from chalk import BoundingBox
In [2]:
Copied!
# Colors
papaya = Color("#ff9700")
blue = Color("#005FDB")
black = Color("#000000")
white = Color("#ffffff")
grey = Color("#bbbbbb")
# Colors papaya = Color("#ff9700") blue = Color("#005FDB") black = Color("#000000") white = Color("#ffffff") grey = Color("#bbbbbb")
Some general functions
In [3]:
Copied!
def label(te):
"Create text."
return text(te, 2).fill_color(black).line_width(0)
def label(te): "Create text." return text(te, 2).fill_color(black).line_width(0)
In [4]:
Copied!
def cover(d, a, b):
"Draw a bounding_box around a subdiagram"
b1 = d.get_subdiagram_envelope(a)
b2 = d.get_subdiagram_envelope(b)
new_bb = b1 + b2
return rectangle(new_bb.width, new_bb.height) \
.translate(new_bb.center.x, new_bb.center.y)
def cover(d, a, b): "Draw a bounding_box around a subdiagram" b1 = d.get_subdiagram_envelope(a) b2 = d.get_subdiagram_envelope(b) new_bb = b1 + b2 return rectangle(new_bb.width, new_bb.height) \ .translate(new_bb.center.x, new_bb.center.y)
In [5]:
Copied!
def tile(d, m, n, name = ""):
"Tile a digram with names"
return hcat(vcat(d.named((name, j, i)) for j in range(n)) for i in range(m)).center_xy()
def tile(d, m, n, name = ""): "Tile a digram with names" return hcat(vcat(d.named((name, j, i)) for j in range(n)) for i in range(m)).center_xy()
In [6]:
Copied!
def connect_all(d, a, b):
"Connect all corners of two diagrams"
for x_border in [-unit_x, unit_x]:
for y_border in [-unit_y, unit_y]:
p = x_border + y_border
d = d.connect_perim(a, b, p, p, ArrowOpts(head_arrow=empty()))
return d
def connect_all(d, a, b): "Connect all corners of two diagrams" for x_border in [-unit_x, unit_x]: for y_border in [-unit_y, unit_y]: p = x_border + y_border d = d.connect_perim(a, b, p, p, ArrowOpts(head_arrow=empty())) return d
In [7]:
Copied!
# NN drawing
def cell():
return rectangle(1, 1).line_width(0.01)
# NN drawing def cell(): return rectangle(1, 1).line_width(0.01)
In [8]:
Copied!
def matrix(n, r, c):
return tile(cell(), c, r, n)
def matrix(n, r, c): return tile(cell(), c, r, n)
In [9]:
Copied!
def back(r, n):
"Backing stack"
return concat((r.translate(-i/2, -i/2).fill_opacity((n - i + n /2) / n)
for i in range(n-1, -1, -1)))
def back(r, n): "Backing stack" return concat((r.translate(-i/2, -i/2).fill_opacity((n - i + n /2) / n) for i in range(n-1, -1, -1)))
In [10]:
Copied!
lw = 0.05
def stack(n, size, l, top, bot):
"Feature map stack"
m = matrix(n, size, size).fill_color(Color("#dddddd"))
r = rectangle(size, size).fill_color(grey).line_width(lw)
return (label(top) / (back(r, l) + m) / label(bot)).center_xy()
stack("a", 32, 0, "", "")
lw = 0.05 def stack(n, size, l, top, bot): "Feature map stack" m = matrix(n, size, size).fill_color(Color("#dddddd")) r = rectangle(size, size).fill_color(grey).line_width(lw) return (label(top) / (back(r, l) + m) / label(bot)).center_xy() stack("a", 32, 0, "", "")
Out[10]:
In [11]:
Copied!
def network(n, size, top, bot):
"Draw a network layer"
return (label(top) / rectangle(2, size).fill_color(grey).line_width(lw).named(n) / label(bot)).center_xy()
def network(n, size, top, bot): "Draw a network layer" return (label(top) / rectangle(2, size).fill_color(grey).line_width(lw).named(n) / label(bot)).center_xy()
In [12]:
Copied!
# The number 7
draw = make_path([(-10, -10), (10, -10 ), (-10, 10)]).line_width(0.09).line_color(blue).fill_opacity(0)
# The number 7 draw = make_path([(-10, -10), (10, -10 ), (-10, 10)]).line_width(0.09).line_color(blue).fill_opacity(0)
In [13]:
Copied!
# Draw the main diagram.
h = hstrut(6.5)
d = ((stack("a", 32, 0, "", "") + draw) | (label("conv") / h) |
stack("b", 28, 6, "", "C1") | (label("pool") / h) |
stack("c", 14, 6, "", "S2") | (label("conv") / h) |
stack("d", 10, 16, "", "C3") | (label("pool") / h) | hstrut(-0.5) |
stack("e", 5, 16, "", "S4") | (label("dense") / h) |
network("dense1", 12, "", "") | (label("dense") / (h)) |
network("dense2", 8.4, "", "") | (label("dense") / h) |
network("dense3", 1, "", ""))
# Draw the main diagram. h = hstrut(6.5) d = ((stack("a", 32, 0, "", "") + draw) | (label("conv") / h) | stack("b", 28, 6, "", "C1") | (label("pool") / h) | stack("c", 14, 6, "", "S2") | (label("conv") / h) | stack("d", 10, 16, "", "C3") | (label("pool") / h) | hstrut(-0.5) | stack("e", 5, 16, "", "S4") | (label("dense") / h) | network("dense1", 12, "", "") | (label("dense") / (h)) | network("dense2", 8.4, "", "") | (label("dense") / h) | network("dense3", 1, "", ""))
In [14]:
Copied!
d = d.scale_uniform_to_x(5)
d = d.scale_uniform_to_x(5)
In [15]:
Copied!
# Draw the orange boxes
boxes = [(("a", 2, 2), ("a", 6, 6)),
(("b", 2, 2), ("b", 2, 2)),
(("b", 20, 2), ("b", 23, 5)),
(("c", 10, 2), ("c", 11, 3)),
(("c", 4, 6), ("c", 8, 10)),
(("d", 4, 6), ("d", 4, 6)),
(("d", 6, 4), ("d", 9, 7)),
(("e", 3, 2), ("e", 4, 3))]
# Draw the orange boxes boxes = [(("a", 2, 2), ("a", 6, 6)), (("b", 2, 2), ("b", 2, 2)), (("b", 20, 2), ("b", 23, 5)), (("c", 10, 2), ("c", 11, 3)), (("c", 4, 6), ("c", 8, 10)), (("d", 4, 6), ("d", 4, 6)), (("d", 6, 4), ("d", 9, 7)), (("e", 3, 2), ("e", 4, 3))]
In [16]:
Copied!
d += concat([cover(d, *b).fill_color(papaya).fill_opacity(0.3).named(("box", i))
for i, b in enumerate(boxes)])
d += concat([cover(d, *b).fill_color(papaya).fill_opacity(0.3).named(("box", i)) for i, b in enumerate(boxes)])
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Input In [16], in <cell line: 1>() ----> 1 d += concat([cover(d, *b).fill_color(papaya).fill_opacity(0.3).named(("box", i)) 2 for i, b in enumerate(boxes)]) Input In [16], in <listcomp>(.0) ----> 1 d += concat([cover(d, *b).fill_color(papaya).fill_opacity(0.3).named(("box", i)) 2 for i, b in enumerate(boxes)]) Input In [4], in cover(d, a, b) 1 def cover(d, a, b): 2 "Draw a bounding_box around a subdiagram" ----> 3 b1 = d.get_subdiagram_envelope(a) 4 b2 = d.get_subdiagram_envelope(b) 5 new_bb = b1 + b2 AttributeError: 'ApplyTransform' object has no attribute 'get_subdiagram_envelope'
In [17]:
Copied!
connect = [(("box", i), ("box", i + 1)) for i in range(0, 6, 2)]
connect = [(("box", i), ("box", i + 1)) for i in range(0, 6, 2)]
In [18]:
Copied!
for b in connect:
d = connect_all(d, *b)
for b in connect: d = connect_all(d, *b)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Input In [18], in <cell line: 1>() 1 for b in connect: ----> 2 d = connect_all(d, *b) Input In [6], in connect_all(d, a, b) 5 for y_border in [-unit_y, unit_y]: 6 p = x_border + y_border ----> 7 d = d.connect_perim(a, b, p, p, ArrowOpts(head_arrow=empty())) 8 return d File ~/work/pydiagrams/chalk/arrow.py:84, in connect_perim(self, name1, name2, v1, v2, style) 81 sub2 = self.get_subdiagram(name2) 83 if not sub1 or not sub2: ---> 84 raise ValueError("Cannot connect") 86 loc1 = sub1.get_location() 87 loc2 = sub2.get_location() ValueError: Cannot connect
In [19]:
Copied!
set_svg_height(300)
d
set_svg_height(300) d
Out[19]:
Last update: 2022-08-21