diff --git a/.gitignore b/.gitignore index 9c18754..0cfb744 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,9 @@ *.aux *.log *.gz +*.pdf +*.png +*.DS_Store __pycache__ books @@ -82,4 +85,4 @@ venv.bak/ /site # mypy -.mypy_cache/ \ No newline at end of file +.mypy_cache/ diff --git a/pycore/__init__.pyc b/pycore/__init__.pyc index e9f05b6..46ffe32 100644 Binary files a/pycore/__init__.pyc and b/pycore/__init__.pyc differ diff --git a/pycore/blocks.py b/pycore/blocks.py index 82c2c2a..c1b1c78 100644 --- a/pycore/blocks.py +++ b/pycore/blocks.py @@ -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 [ diff --git a/pycore/tikzeng.py b/pycore/tikzeng.py index 8e988c4..da7180c 100644 --- a/pycore/tikzeng.py +++ b/pycore/tikzeng.py @@ -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""" @@ -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 @@ -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) diff --git a/pycore/tikzeng.pyc b/pycore/tikzeng.pyc index ed1ff6e..2d7ce75 100644 Binary files a/pycore/tikzeng.pyc and b/pycore/tikzeng.pyc differ diff --git a/pyexamples/autoencoder.py b/pyexamples/autoencoder.py new file mode 100644 index 0000000..c1ac1ed --- /dev/null +++ b/pyexamples/autoencoder.py @@ -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() + diff --git a/pyexamples/autoencoder_res.py b/pyexamples/autoencoder_res.py new file mode 100644 index 0000000..c6d3e84 --- /dev/null +++ b/pyexamples/autoencoder_res.py @@ -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() + diff --git a/pyexamples/residual_block.py b/pyexamples/residual_block.py new file mode 100644 index 0000000..4480ef7 --- /dev/null +++ b/pyexamples/residual_block.py @@ -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() + diff --git a/pyexamples/unet.py b/pyexamples/unet.py index 25ddd3f..e03bd9a 100644 --- a/pyexamples/unet.py +++ b/pyexamples/unet.py @@ -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"), diff --git a/tikzmake.sh b/tikzmake.sh index 560ca43..0588811 100644 --- a/tikzmake.sh +++ b/tikzmake.sh @@ -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