Exemplo n.º 1
0
def fc_battery(x,
               global_step,
               n=1024,
               bias=0.1,
               is_training=False,
               dropout=0.0,
               winit=None,
               verbose=False,
               name="FC"):
    # BATCH NORM SETTINGS
    bn_offset = 0.0
    bn_scale = 1.0

    # DROPOUT
    dropout = tf.cond(is_training, lambda: tf.constant(dropout),
                      lambda: tf.constant(0.0))

    # DEFAULT WEIGHTS INITIALIZATION
    if winit is None:
        winit = he_weights_initializer()  # identity_weights_initializer()

    # FC STACK
    with tf.name_scope(name) as scope:
        x = fc_layer(x, n=n, bias=bias, winit=winit, name="FC")
        x = batchnormC(x,
                       is_training=is_training,
                       iteration=global_step,
                       conv=False,
                       offset=bn_offset,
                       scale=bn_scale)
        x = tf.nn.dropout(x, keep_prob=1 - dropout)
        x = leaky_relu(x, rate=0.01, name="relu")
        print_tensor_shape(x, name=scope, verbose=verbose)

    return x
Exemplo n.º 2
0
def conv_battery(x,
                 global_step,
                 convk=3,
                 n=32,
                 mpk=2,
                 mpstride=1,
                 dropout=0.0,
                 is_training=False,
                 name="C",
                 verbose=False):
    # BATCH NORM SETTINGS
    bn_offset = 0.0
    bn_scale = 1.0

    # DROPOUT
    conv_dropout = tf.cond(is_training, lambda: tf.constant(dropout),
                           lambda: tf.constant(0.0))
    # CONV STACK
    with tf.name_scope(name) as scope:
        x = conv_layer(x, k=convk, n=n, bias=None, stride=1, name="conv")
        x = batchnormC(x,
                       is_training=is_training,
                       iteration=global_step,
                       conv=True,
                       offset=bn_offset,
                       scale=bn_scale)
        x = tf.nn.dropout(x, keep_prob=1 - conv_dropout)
        x = max_pool_layer(x, k=mpk, stride=mpstride, name="maxpool")
        x = leaky_relu(x, name="relu")
        print_tensor_shape(x, name=scope, verbose=verbose)

    return x
Exemplo n.º 3
0
def conv_layer(x,
               k=3,
               n=8,
               stride=1,
               bias=0.1,
               winit=he_weights_initializer(),
               pad='SAME',
               name="conv_layer"):
    num_filters_in = int(x.get_shape()[-1])  # The number of chanels coming in

    with tf.name_scope(name) as scope:
        weights = tf.Variable(winit([k, k, num_filters_in, n]),
                              name="weights",
                              dtype=tf.float32)
        if bias is not None:
            conv = tf.nn.conv2d(x,
                                weights,
                                strides=[1, stride, stride, 1],
                                padding=pad,
                                name="conv")
            bias = tf.Variable(tf.constant(bias, shape=[n]), name="bias")
            preactivation = tf.add(conv, bias, name=scope)
        else:
            preactivation = tf.nn.conv2d(x,
                                         weights,
                                         strides=[1, stride, stride, 1],
                                         padding=pad,
                                         name=scope)

    return preactivation
Exemplo n.º 4
0
def fc_layer(x,
             n=32,
             bias=0.1,
             winit=he_weights_initializer(),
             dtype=tf.float32,
             name="fc_layer"):
    """Fully Connected Layer
    Args:
        x:          The tensor from the previous layer.
        n:          The number of nodes for this layer (width).
        bias:       (float or None)(default=0.1)
                    Initial value for bias units.
                    If None, then no Biases will be used.
        winit:      weights initializer
        name:       Name for this operation
    """
    in_nodes = int(x.get_shape()[-1])  # The number of nodes from input x
    with tf.name_scope(name) as scope:
        weights = tf.Variable(winit([in_nodes, n]),
                              name="weights",
                              dtype=dtype)
        if bias is not None:
            bias = tf.Variable(tf.constant(bias, shape=[n], dtype=dtype),
                               name="bias")
            preactivation = tf.add(tf.matmul(x, weights), bias, name=scope)
        else:
            preactivation = tf.matmul(x, weights, name=scope)
    return preactivation
Exemplo n.º 5
0
def model_a(x, is_training, global_step, settings=None, verbose=True):
    """
    """
    # BATCH NORM SETTINGS
    bn_offset = 0.0
    bn_scale = 1.0

    # MISC SETTINGS
    bval = 0.01  # Bias value
    leak = 0.01  # leakiness of leaky relus

    # WEIGHTS INITIALIZERS
    # st_winit = zero_weights_initializer()
    conv_winit = he_weights_initializer()
    fc_winit = he_weights_initializer()  # identity_weights_initializer()

    # DROPOUT SETTINGS
    conv_dropout = tf.cond(is_training,
                           lambda: tf.constant(settings.conv_dropout),
                           lambda: tf.constant(0.0))
    fc_dropout = tf.cond(is_training, lambda: tf.constant(settings.fc_dropout),
                         lambda: tf.constant(0.0))

    # --------------------------------------------------------------------------
    #                                                                      TRUNK
    # --------------------------------------------------------------------------
    # CONV LAYERS
    x = conv_battery(x,
                     global_step=global_step,
                     convk=5,
                     n=48,
                     mpk=2,
                     mpstride=2,
                     is_training=is_training,
                     verbose=verbose)

    x = conv_battery(x,
                     global_step=global_step,
                     convk=5,
                     n=64,
                     mpk=2,
                     mpstride=1,
                     is_training=is_training,
                     verbose=verbose)

    x = conv_battery(x,
                     global_step=global_step,
                     convk=5,
                     n=128,
                     mpk=2,
                     mpstride=2,
                     is_training=is_training,
                     verbose=verbose)

    x = conv_battery(x,
                     global_step=global_step,
                     convk=5,
                     n=160,
                     mpk=2,
                     mpstride=1,
                     is_training=is_training,
                     verbose=verbose)

    x = conv_battery(x,
                     global_step=global_step,
                     convk=3,
                     n=192,
                     mpk=2,
                     mpstride=2,
                     is_training=is_training,
                     verbose=verbose)

    x = conv_battery(x,
                     global_step=global_step,
                     convk=3,
                     n=192,
                     mpk=2,
                     mpstride=1,
                     is_training=is_training,
                     verbose=verbose)

    x = conv_battery(x,
                     global_step=global_step,
                     convk=3,
                     n=192,
                     mpk=2,
                     mpstride=2,
                     is_training=is_training,
                     verbose=verbose)

    x = conv_battery(x,
                     global_step=global_step,
                     convk=2,
                     n=192,
                     mpk=2,
                     mpstride=1,
                     is_training=is_training,
                     verbose=verbose)

    # FC LAYER
    x = flatten(x)
    print_tensor_shape(x, verbose=verbose)
    x = fc_battery(x,
                   global_step=global_step,
                   n=1024,
                   bias=None,
                   is_training=is_training,
                   dropout=settings.fc_dropout,
                   winit=fc_winit,
                   verbose=verbose,
                   name="FC")

    # --------------------------------------------------------------------------
    #                                                             DIGIT BRANCHES
    # --------------------------------------------------------------------------
    max_digits = 5
    d = [None] * max_digits
    for i in range(max_digits):
        d[i] = fc_layer(x,
                        n=11,
                        bias=0.1,
                        winit=fc_winit,
                        name="branch_{}".format(i + 1))
        print_tensor_shape(d[i], verbose=verbose)

    digits = tf.stack(d, axis=0, name="digit_logits")
    print_tensor_shape(digits, verbose=verbose)

    # --------------------------------------------------------------------------
    #                                                              BBOX BRANCHES
    # --------------------------------------------------------------------------
    bboxes = fc_layer(x, n=24, bias=0.1, winit=fc_winit, name="bbox_logits")
    print_tensor_shape(bboxes, verbose=verbose)

    return digits, bboxes
Exemplo n.º 6
0
 def _initializer(shape, dtype=dtype, partition_info=None):
     # print("initializing with IDENTITY_WEIGHTS")
     shape = list(shape)
     return tf.constant(np.eye(shape[0], shape[1]), dtype=dtype)