Пример #1
0
def block_unconv(name,
                 bottom,
                 top,
                 z_label='',
                 n_filter=64,
                 offset=(1, 0, 0),
                 size=(32, 32, 3.5),
                 opacity=0.5):
    layers = tikz.unpool(name='unpool_{}'.format(name),
                         offset=offset,
                         to='({}-east)'.format(bottom),
                         width=1,
                         height=size[0],
                         depth=size[1],
                         opacity=opacity)
    layers += tikz.conv_res(name='ccr_res_{}'.format(name),
                            offset='(0,0,0)',
                            to='(unpool_{}-east)'.format(name),
                            z_label=z_label,
                            n_filter=str(n_filter),
                            width=size[2],
                            height=size[0],
                            depth=size[1],
                            opacity=opacity)
    layers += tikz.conv(name='ccr_{}'.format(name),
                        offset='(0,0,0)',
                        to='(ccr_res_{}-east)'.format(name),
                        z_label=z_label,
                        n_filter=str(n_filter),
                        width=size[2],
                        height=size[0],
                        depth=size[1])
    layers += tikz.conv_res(name='ccr_res_c_{}'.format(name),
                            offset='(0,0,0)',
                            to='(ccr_{}-east)'.format(name),
                            z_label=z_label,
                            n_filter=str(n_filter),
                            width=size[2],
                            height=size[0],
                            depth=size[1],
                            opacity=opacity)
    layers += tikz.conv(name='{}'.format(top),
                        offset='(0,0,0)',
                        to='(ccr_res_c_{}-east)'.format(name),
                        z_label=z_label,
                        n_filter=str(n_filter),
                        width=size[2],
                        height=size[0],
                        depth=size[1])
    layers += tikz.short_connection('{}'.format(bottom),
                                    'unpool_{}'.format(name))
    return layers
Пример #2
0
def res(num,
        name,
        bottom,
        top,
        start_no=0,
        z_label='',
        n_filter=64,
        offset=(0, 0, 0),
        size=(32, 32, 3.5),
        opacity=0.5):
    layers = []
    layer_names = [*['{}_{}'.format(name, i) for i in range(num - 1)], top]
    for name in layers:
        layer = [
            tikz.conv(name='{}'.format(name),
                      offset=offset,
                      to='{}-east'.format(bottom),
                      z_label=z_label,
                      n_filter=str(n_filter),
                      width=size[2],
                      height=size[0],
                      depth=size[1]),
            tikz.short_connection('{}'.format(bottom), '{}'.format(name))
        ]
        bottom = name
        layers += layer

    layers += [
        tikz.skip(of=layer_names[1], to=layer_names[-2], pos=1.25),
    ]
    return layers
Пример #3
0
def conv(name,
         prev='',
         z_label='',
         n_filter=64,
         offset=(1, 0, 0),
         size=(32, 32),
         width=0,
         caption=' ',
         conn=True,
         anchor='-east'):
    """
    Generate a convolution layer

    Arguments:
        name {str} -- layer name

   Keyword Arguments:
        prev {str} -- name of previous layer (default: {''})
        z_label {str} -- label along the z axis of the layer (default: '')
        n_filter {int} -- number of filters (default: {64})
        offset {tuple} -- offset to previous layer (default: {(1,0,0)})
        size {tuple} -- size (default: {(32,32)})
        width {str} -- width of layers in graph (default: {'0'})
        caption {str} -- layer caption (default: {''})
        conn {bool} -- draw short connection from prev layer (default: {False})
        anchor {str} -- position of anchor (default: {'-east'})

    Returns:
        layer {list} -- list of graph elements
    """
    if not isinstance(size, tuple):
        size = (size, size)
    if not prev:
        # assuming that layer names are given in a uniform way with incrementing numbers
        prev_s = name.split('_')
        prev = '{}_{}'.format(prev_s[0], str(int(prev_s[1]) - 1))
    if not width:
        width = log(n_filter, 4)
    layer = tikz.conv(
        name='{}'.format(name),
        z_label=z_label,
        n_filter=(n_filter),
        offset=offset,
        caption=caption,
        to='{}{}'.format(prev, anchor),
        width=width,
        size=size,
    )
    if conn:
        layer += tikz.short_connection('{}'.format(prev), '{}'.format(name))
    return layer
Пример #4
0
def multi_conv_z(num,
                 name,
                 prev,
                 layer_num=0,
                 z_label='',
                 n_filter=64,
                 name_start=0,
                 offset=(1, 0, 0),
                 width='0',
                 size=(32, 32),
                 opacity=0.5,
                 conn=True,
                 anchor='-east'):
    """
    Generate a block of multiple convolution layers along the z axis

    Arguments:
        num {int} -- number of layers
        name {string} -- block name
        prev {string} -- name of previous layer

    Keyword Arguments:
        layer_num {int} -- layer number in the network (default: {0})
        z_label {str} -- label along the z axis of the layer (default: '')
        n_filter {int} -- number of filters (default: {64})
        name_start {int} -- number of the first layer, succeeding layers get incrementing numbers (default: {0})
        offset {tuple} -- offset between layers (default: {'(1,0,0)'})
        width {str} -- width of layers in graph (default: {'0'})
        size {tuple} -- [description] (default: {(32,32)})
        opacity {float} -- [description] (default: {0.5})
        conn {bool} -- [description] (default: {False})
        anchor {str} -- [description] (default: {'-east'})

    Returns:
        layers {list} -- list of graph elements
    """

    layers = []
    j = 0
    layer_names = [
        *[
            '{}_{}'.format(name, i)
            for i in range(name_start, num + name_start)
        ]
    ]
    if not isinstance(n_filter, list):
        n_filter = [n_filter] * num
    if not isinstance(size, tuple):
        size = (size, size)
    # first layer
    layer = [
        tikz.conv(name='{}'.format(layer_names[0]),
                  caption=str(layer_num + j),
                  offset=offset,
                  to='{}{}'.format(prev, anchor),
                  n_filter=n_filter[j],
                  size=size,
                  width=log(n_filter[j], 4))
    ]
    j += 1
    layers = layer
    if conn:
        layers += tikz.long_connection(prev, layer_names[0])
    prev = layer_names[0]

    # middle layers
    for l_name in layer_names[1:-1]:
        layer = [
            tikz.conv(name='{}'.format(l_name),
                      caption=str(layer_num + j),
                      offset=offset,
                      to='{}{}'.format(prev, anchor),
                      n_filter=n_filter[j],
                      size=size,
                      width=log(n_filter[j], 4))
        ]
        prev = l_name
        layers += layer
        j += 1
    # last layer
    layer = [
        tikz.conv(name='{}'.format(layer_names[-1]),
                  caption=str(layer_num + j),
                  offset=offset,
                  to='{}{}'.format(prev, anchor),
                  z_label=z_label,
                  n_filter=n_filter[j],
                  size=size,
                  width=log(n_filter[j], 4))
    ]
    layers += layer
    return layers
Пример #5
0
def multi_conv(num,
               name,
               prev,
               layer_num=0,
               z_label='',
               n_filter=64,
               scale=32,
               name_start=0,
               offset=(1, 0, 0),
               width='0',
               size=(32, 32),
               opacity=0.5,
               conn=True,
               anchor='-east'):
    """
    Generate a block of multiple convolution layers

    Arguments:
        num {[type]} -- [description]
        name {[type]} -- [description]
        prev {[type]} -- [description]

    Keyword Arguments:
        layer_num {int} -- [description] (default: {0})
        z_label {str} -- [description] (default: {256})
        n_filter {int} -- [description] (default: {64})
        scale {int} -- [description] (default: {32})
        name_start {int} -- [description] (default: {0})
        offset {tuple} -- [description] (default: {(1, 0, 0)})
        width {str} -- [description] (default: {'0'})
        size {tuple} -- [description] (default: {(32, 32)})
        opacity {float} -- [description] (default: {0.5})
        conn {bool} -- [description] (default: {False})
        anchor {str} -- [description] (default: {'-east'})

    Returns:
        [type] -- [description]
    """

    layers = []
    j = 0
    layer_names = [
        *[
            '{}_{}'.format(name, i)
            for i in range(name_start, num + name_start)
        ]
    ]
    if not isinstance(n_filter, list):
        n_filter = [n_filter] * num
    if not isinstance(size, tuple):
        size = (size, size)
    # first layer
    layer = [
        tikz.conv(name='{}'.format(layer_names[0]),
                  caption=str(layer_num + j),
                  offset=offset,
                  to='{}{}'.format(prev, anchor),
                  n_filter=n_filter[j],
                  size=size,
                  width=log(n_filter[j], 4))
    ]
    j += 1
    layers = layer
    if conn:
        layers += tikz.short_connection(prev, layer_names[0])
    prev = layer_names[0]

    # middle layers
    for l_name in layer_names[1:-1]:
        layer = [
            tikz.conv(name='{}'.format(l_name),
                      caption=str(layer_num + j),
                      offset='(0,0,0)',
                      to='{}{}'.format(prev, anchor),
                      n_filter=n_filter[j],
                      size=size,
                      width=log(n_filter[j], 4))
        ]
        prev = l_name
        layers += layer
        j += 1

    # last layer
    layer = [
        tikz.conv(name='{}'.format(layer_names[-1]),
                  caption=str(layer_num + j),
                  offset='(0,0,0)',
                  to='{}{}'.format(prev, anchor),
                  z_label=z_label,
                  n_filter=n_filter[j],
                  size=size,
                  width=log(n_filter[j], 4))
    ]
    layers += layer
    return layers