Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Residual block example #102

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
*.aux
*.log
*.gz
*.pdf
*.png
*.DS_Store

__pycache__
books
Expand Down Expand Up @@ -82,4 +85,4 @@ venv.bak/
/site

# mypy
.mypy_cache/
.mypy_cache/
Binary file modified pycore/__init__.pyc
Binary file not shown.
32 changes: 32 additions & 0 deletions pycore/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,38 @@ def block_2ConvPool( name, botton, top, s_filer=256, n_filer=64, offset="(1,0,0)
)
]

def block_2Conv( name, botton, top, s_filer=256, n_filer=64, offset="(1,0,0)", size=(32,32,3.5), opacity=0.5 ):
return [
to_ConvConvRelu(
name="{}".format( top ),
s_filer=str(s_filer),
n_filer=(n_filer,n_filer),
offset=offset,
to="({}-east)".format( botton ),
width=(size[2],size[2]),
height=size[0],
depth=size[1],
),
to_connection(
"{}".format( botton ),
"{}".format( top )
)
]


def block_2DeConv( name, botton, top, s_filer=256, n_filer=64, offset="(1,0,0)", size=(32,32,3.5), opacity=0.5 ):
return [
# to_UnPool( name='unpool_{}'.format(name), offset=offset, to="({}-east)".format(botton), width=1, height=size[0] - int(size[0]/4), depth=size[1]- int(size[0]/4), opacity=opacity ),
to_ConvRes( name='ccr_res_{}'.format(name), offset=offset, to="({}-east)".format(botton), s_filer=str(s_filer), n_filer=str(n_filer), width=size[2], height=size[0], depth=size[1], opacity=opacity ),
# to_Conv( name='ccr_{}'.format(name), offset="(0,0,0)", to="(ccr_res_{}-east)".format(name), s_filer=str(s_filer), n_filer=str(n_filer), width=size[2], height=size[0], depth=size[1] ),
to_ConvRes( name='{}'.format(top), offset="(0,0,0)", to="(ccr_res_{}-east)".format(name), s_filer=str(s_filer), n_filer=str(n_filer), width=size[2], height=size[0], depth=size[1], opacity=opacity ),
# to_Conv( name='{}'.format(top), offset="(0,0,0)", to="(ccr_res_c_{}-east)".format(name), s_filer=str(s_filer), n_filer=str(n_filer), width=size[2], height=size[0], depth=size[1] ),
to_connection(
"{}".format( botton ),
"ccr_res_{}".format(name)
)
]


def block_Unconv( name, botton, top, s_filer=256, n_filer=64, offset="(1,0,0)", size=(32,32,3.5), opacity=0.5 ):
return [
Expand Down
27 changes: 24 additions & 3 deletions pycore/tikzeng.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ def to_input( pathfile, to='(-3,0,0)', width=8, height=8, name="temp" ):
\node[canvas is zy plane at x=0] (""" + name + """) at """+ to +""" {\includegraphics[width="""+ str(width)+"cm"+""",height="""+ str(height)+"cm"+"""]{"""+ pathfile +"""}};
"""

def to_output( pathfile, to='(-3,0,0)', width=8, height=8, name="temp",xshift=3):
return r"""
\node[canvas is zy plane at x=0] (""" + name + """) at """+ "([xshift=" + str(xshift) + "cm]{}-east)".format(to) + """ {\includegraphics[width="""+ str(width)+"cm"+""",height="""+ str(height)+"cm"+"""]{"""+ pathfile +"""}};
"""


# Conv
def to_Conv( name, s_filer=256, n_filer=64, offset="(0,0,0)", to="(0,0,0)", width=1, height=40, depth=40, caption=" " ):
return r"""
Expand Down Expand Up @@ -76,6 +82,21 @@ def to_ConvConvRelu( name, s_filer=256, n_filer=(64,64), offset="(0,0,0)", to="(
};
"""

# ReLu
def to_ReLU(name, offset="(0,0,0)", to="(0,0,0)", width=1, height=32, depth=32, opacity=0.5, caption=" "):
return r"""
\pic[shift={ """+ offset +""" }] at """+ to +"""
{Box={
name="""+name+""",
caption="""+ caption +r""",
fill=\ConvReluColor,
opacity="""+ str(opacity) +""",
height="""+ str(height) +""",
width="""+ str(width) +""",
depth="""+ str(depth) +"""
}
};
"""


# Pool
Expand Down Expand Up @@ -184,10 +205,10 @@ def to_connection( of, to):
\draw [connection] ("""+of+"""-east) -- node {\midarrow} ("""+to+"""-west);
"""

def to_skip( of, to, pos=1.25):
def to_skip( of, to, pos_of=1.25, pos_to=1.25):
return r"""
\path ("""+ of +"""-southeast) -- ("""+ of +"""-northeast) coordinate[pos="""+ str(pos) +"""] ("""+ of +"""-top) ;
\path ("""+ to +"""-south) -- ("""+ to +"""-north) coordinate[pos="""+ str(pos) +"""] ("""+ to +"""-top) ;
\path ("""+ of +"""-southeast) -- ("""+ of +"""-northeast) coordinate[pos="""+ str(pos_of) +"""] ("""+ of +"""-top) ;
\path ("""+ to +"""-south) -- ("""+ to +"""-north) coordinate[pos="""+ str(pos_to) +"""] ("""+ to +"""-top) ;
\draw [copyconnection] ("""+of+"""-northeast)
-- node {\copymidarrow}("""+of+"""-top)
-- node {\copymidarrow}("""+to+"""-top)
Expand Down
Binary file modified pycore/tikzeng.pyc
Binary file not shown.
54 changes: 54 additions & 0 deletions pyexamples/autoencoder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

import sys
sys.path.append('../')
from pycore.tikzeng import *
from pycore.blocks import *

arch = [
to_head('..'),
to_cor(),
to_begin(),

#input
to_input( '../examples/fcn8s/A.png' ),

#block-001
to_ConvConvRelu( name='ccr_b1', s_filer=500, n_filer=(64,64), offset="(0,0,0)", to="(0,0,0)", width=(2,2), height=40, depth=40 ),
# to_Pool(name="pool_b1", offset="(0,0,0)", to="(ccr_b1-east)", width=1, height=32, depth=32, opacity=0.5),

*block_2Conv( name='b2', botton='ccr_b1', top='pool_b2', s_filer=256, n_filer=128, offset="(1,0,0)", size=(32,32,3.5), opacity=0.5 ),
*block_2Conv( name='b3', botton='pool_b2', top='pool_b3', s_filer=128, n_filer=256, offset="(1,0,0)", size=(25,25,4.5), opacity=0.5 ),
*block_2Conv( name='b4', botton='pool_b3', top='pool_b4', s_filer=64, n_filer=512, offset="(1,0,0)", size=(16,16,5.5), opacity=0.5 ),

#Bottleneck
#block-005
# to_ConvConvRelu( name='ccr_b5', s_filer=32, n_filer=(1024,1024), offset="(2,0,0)", to="(pool_b4-east)", width=(8,8), height=8, depth=8, caption="Latent space" ),
# to_connection( "pool_b4", "ccr_b5"),

#Decoder
*block_2DeConv( name="b6", botton="pool_b4", top='end_b6', s_filer=64, n_filer=512, offset="(2.1,0,0)", size=(16,16,5.0), opacity=0.5 ),
# to_skip( of='ccr_b4', to='ccr_res_b6', pos_of=1.25),
*block_2DeConv( name="b7", botton="end_b6", top='end_b7', s_filer=128, n_filer=256, offset="(2.1,0,0)", size=(25,25,4.5), opacity=0.5 ),
# to_skip( of='pool_b3', to='ccr_res_b7', pos_of=1.25),
*block_2DeConv( name="b8", botton="end_b7", top='end_b8', s_filer=256, n_filer=128, offset="(2.1,0,0)", size=(32,32,3.5), opacity=0.5 ),
# to_skip( of='pool_b2', to='ccr_res_b8', pos_of=1.25),

*block_2DeConv( name="b9", botton="end_b8", top='end_b9', s_filer=512, n_filer=64, offset="(2.1,0,0)", size=(40,40,2.5), opacity=0.5 ),
# to_skip( of='ccr_b1', to='ccr_res_b9', pos_of=1.25),

# to_ConvSoftMax( name="soft1", s_filer=512, offset="(0.75,0,0)", to="(end_b9-east)", width=1, height=40, depth=40, caption="SOFT" ),
# to_connection( "end_b9", "soft1"),

to_output( '../examples/fcn8s/A.png', to='end_b9', xshift=2.5),

to_end()
]


def main():
namefile = str(sys.argv[0]).split('.')[0]
to_generate(arch, namefile + '.tex' )

if __name__ == '__main__':
main()

54 changes: 54 additions & 0 deletions pyexamples/autoencoder_res.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

import sys
sys.path.append('../')
from pycore.tikzeng import *
from pycore.blocks import *

arch = [
to_head('..'),
to_cor(),
to_begin(),

#input
to_input( '../examples/fcn8s/A_noise.png' ),

#block-001
to_ConvConvRelu( name='ccr_b1', s_filer=500, n_filer=(64,64), offset="(0,0,0)", to="(0,0,0)", width=(2,2), height=40, depth=40 ),
# to_Pool(name="pool_b1", offset="(0,0,0)", to="(ccr_b1-east)", width=1, height=32, depth=32, opacity=0.5),

*block_2Conv( name='b2', botton='ccr_b1', top='pool_b2', s_filer=256, n_filer=128, offset="(1,0,0)", size=(32,32,3.5), opacity=0.5 ),
*block_2Conv( name='b3', botton='pool_b2', top='pool_b3', s_filer=128, n_filer=256, offset="(1,0,0)", size=(25,25,4.5), opacity=0.5 ),
*block_2Conv( name='b4', botton='pool_b3', top='pool_b4', s_filer=64, n_filer=512, offset="(1,0,0)", size=(16,16,5.5), opacity=0.5 ),

#Bottleneck
#block-005
# to_ConvConvRelu( name='ccr_b5', s_filer=32, n_filer=(1024,1024), offset="(2,0,0)", to="(pool_b4-east)", width=(8,8), height=8, depth=8, caption="Latent space" ),
# to_connection( "pool_b4", "ccr_b5"),

#Decoder
*block_2DeConv( name="b6", botton="pool_b4", top='end_b6', s_filer=64, n_filer=512, offset="(2.1,0,0)", size=(16,16,5.0), opacity=0.5 ),
# to_skip( of='ccr_b4', to='ccr_res_b6', pos_of=1.25),
*block_2DeConv( name="b7", botton="end_b6", top='end_b7', s_filer=128, n_filer=256, offset="(2.1,0,0)", size=(25,25,4.5), opacity=0.5 ),
to_skip( of='pool_b3', to='ccr_res_b7', pos_of=1.25),
*block_2DeConv( name="b8", botton="end_b7", top='end_b8', s_filer=256, n_filer=128, offset="(2.1,0,0)", size=(32,32,3.5), opacity=0.5 ),
to_skip( of='pool_b2', to='ccr_res_b8', pos_of=1.25),

*block_2DeConv( name="b9", botton="end_b8", top='end_b9', s_filer=512, n_filer=64, offset="(2.1,0,0)", size=(40,40,2.5), opacity=0.5 ),
to_skip( of='ccr_b1', to='ccr_res_b9', pos_of=1.25),

# to_ConvSoftMax( name="soft1", s_filer=512, offset="(0.75,0,0)", to="(end_b9-east)", width=1, height=40, depth=40, caption="SOFT" ),
# to_connection( "end_b9", "soft1"),

to_output( '../examples/fcn8s/A.png', to='end_b9', xshift=2.5),

to_end()
]


def main():
namefile = str(sys.argv[0]).split('.')[0]
to_generate(arch, namefile + '.tex' )

if __name__ == '__main__':
main()

31 changes: 31 additions & 0 deletions pyexamples/residual_block.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

import sys
sys.path.append('../')
from pycore.tikzeng import *
from pycore.blocks import *

arch = [
to_head( '..' ),
to_cor(),
to_begin(),
to_input( '../examples/fcn8s/A.png', to="(-.5,0,0)", height=1.5, width=1.5),
to_Conv("conv1", 32, 1, offset="(0,0,0)", to="(0,0,0)", height=8, depth=8, width=2 ),
to_ReLU("relu1", offset="(0,0,0)", to="(conv1-east)", height=8, depth=8, width=2),
to_Conv("conv2", 32, 1, offset="(1.5,0,0)", to="(relu1-east)", height=8, depth=8, width=2 ),
to_connection( "relu1", "conv2"),
to_Sum("sum1", offset="(1.5,0,0)", to="(conv2-east)", radius=2.5, opacity=0.6),
to_connection("conv2", "sum1"),
to_skip( of='relu1', to='sum1', pos_of=2, pos_to=2.85),
to_ReLU("relu2", offset="(1.5,0,0)", to="(sum1-east)", height=8, depth=8, width=2),
to_connection("sum1", "relu2"),
to_end()
]


def main():
namefile = str(sys.argv[0]).split('.')[0]
to_generate(arch, namefile + '.tex' )

if __name__ == '__main__':
main()

8 changes: 4 additions & 4 deletions pyexamples/unet.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@

#Decoder
*block_Unconv( name="b6", botton="ccr_b5", top='end_b6', s_filer=64, n_filer=512, offset="(2.1,0,0)", size=(16,16,5.0), opacity=0.5 ),
to_skip( of='ccr_b4', to='ccr_res_b6', pos=1.25),
to_skip( of='ccr_b4', to='ccr_res_b6', pos_of=1.25),
*block_Unconv( name="b7", botton="end_b6", top='end_b7', s_filer=128, n_filer=256, offset="(2.1,0,0)", size=(25,25,4.5), opacity=0.5 ),
to_skip( of='ccr_b3', to='ccr_res_b7', pos=1.25),
to_skip( of='ccr_b3', to='ccr_res_b7', pos_of=1.25),
*block_Unconv( name="b8", botton="end_b7", top='end_b8', s_filer=256, n_filer=128, offset="(2.1,0,0)", size=(32,32,3.5), opacity=0.5 ),
to_skip( of='ccr_b2', to='ccr_res_b8', pos=1.25),
to_skip( of='ccr_b2', to='ccr_res_b8', pos_of=1.25),

*block_Unconv( name="b9", botton="end_b8", top='end_b9', s_filer=512, n_filer=64, offset="(2.1,0,0)", size=(40,40,2.5), opacity=0.5 ),
to_skip( of='ccr_b1', to='ccr_res_b9', pos=1.25),
to_skip( of='ccr_b1', to='ccr_res_b9', pos_of=1.25),

to_ConvSoftMax( name="soft1", s_filer=512, offset="(0.75,0,0)", to="(end_b9-east)", width=1, height=40, depth=40, caption="SOFT" ),
to_connection( "end_b9", "soft1"),
Expand Down
2 changes: 1 addition & 1 deletion tikzmake.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ python $1.py
pdflatex $1.tex

rm *.aux *.log *.vscodeLog
rm *.tex
#rm *.tex

if [[ "$OSTYPE" == "darwin"* ]]; then
open $1.pdf
Expand Down