Пример #1
0
def cifar10_shufflenet(x, n_groups=2, n_filters=200, ratio=1.0, seed=42):
    layers = []
    variables = []

    training = tf.placeholder(tf.bool, name="training")
    variables.append(("training", training))

    conv = conv2d_bn_relu(x,
                          size=3,
                          n_filters=24,
                          is_training=training,
                          kernel_init=He_normal(seed=seed + 1),
                          name="initial_conv")
    layers.append(("initial_conv", conv))

    slayer1 = shufflenet.shufflenet_layer(conv,
                                          n_filters=n_filters,
                                          n_repeat=3,
                                          n_groups=n_groups,
                                          reduction_ratio=ratio,
                                          is_training=training,
                                          kernel_init=He_normal(seed=seed + 2),
                                          name="shufflenet_layer_1")
    layers.append(("shufflenet_layer_1", slayer1))

    slayer2 = shufflenet.shufflenet_layer(slayer1,
                                          n_filters=n_filters * 2,
                                          n_repeat=7,
                                          n_groups=n_groups,
                                          reduction_ratio=ratio,
                                          is_training=training,
                                          kernel_init=He_normal(seed=seed + 3),
                                          name="shufflenet_layer_2")
    layers.append(("shufflenet_layer_2", slayer2))

    slayer3 = shufflenet.shufflenet_layer(slayer2,
                                          n_filters=n_filters * 4,
                                          n_repeat=3,
                                          n_groups=n_groups,
                                          reduction_ratio=ratio,
                                          is_training=training,
                                          kernel_init=He_normal(seed=seed + 4),
                                          name="shufflenet_layer_3")
    layers.append(("shufflenet_layer_3", slayer3))

    pool = global_avg_pool2d(slayer3)
    layers.append(("pool", pool))

    dense1 = dense(pool,
                   n_units=10,
                   kernel_init=Kumar_normal(activation=None,
                                            mode="FAN_IN",
                                            seed=seed + 5),
                   name="dense_1")
    layers.append(("logit", dense1))

    prob = tf.nn.softmax(dense1, name="prob")
    layers.append(("prob", prob))

    return layers, variables
Пример #2
0
def auxiliary_classifier_bn(inputs,
                            pool=avg_pool2d,
                            pool_size=5,
                            pool_stride=3,
                            n_filters_1x1=128,
                            n_units=1024,
                            is_training=False,
                            regularizer_conv=None,
                            regularizer_dense=None,
                            kernel_init_conv=He_normal(seed=42),
                            kernel_init_dense=He_normal(seed=42),
                            name="inception_auxiliary_classifier_batchnorm"):

    with tf.variable_scope(name):
        # pool
        pool = pool(inputs,
                    size=pool_size,
                    stride=pool_stride,
                    padding="VALID",
                    name="pool")

        # 1x1
        x_1x1 = conv2d_bn_relu(inputs,
                               size=1,
                               n_filters=n_filters_1x1,
                               stride=1,
                               is_training=is_training,
                               regularizer=regularizer_conv,
                               kernel_init=kernel_init_conv,
                               name="conv_1x1")

        # dense
        flat = flatten(x_1x1, name="flatten")
        dense = dense_bn_relu(flat,
                              n_units=n_units,
                              is_training=is_training,
                              regularizer=regularizer_dense,
                              kernel_init=kernel_init_dense,
                              name="dense")

        return dense
def cifar10_mobilenet_v2(x, expand_ratio=6, seed=42):
    layers = []
    variables = []

    training = tf.placeholder(tf.bool, name="training")
    variables.append(("training", training))

    conv1 = conv2d_bn_relu(x,
                           size=3,
                           n_filters=32,
                           kernel_init=He_normal(seed=seed + 1),
                           is_training=training,
                           name="initial_conv")
    layers.append(("initial_conv", conv1))

    # 32x32x32 -> 32x32x16
    invres1 = mobilenet_v2.inverted_residual_block(
        conv1,
        n_repeat=1,
        n_filters=16,
        stride=1,
        expand_ratio=1,
        kernel_init=He_normal(seed=seed + 2),
        is_training=training,
        name="inverted_residual_block_1")
    layers.append(("inverted_residual_block_1", invres1))

    # 32x32x16 -> 32x32x24
    invres2 = mobilenet_v2.inverted_residual_block(
        invres1,
        n_repeat=2,
        n_filters=24,
        stride=1,
        expand_ratio=expand_ratio,
        kernel_init=He_normal(seed=seed + 3),
        is_training=training,
        name="inverted_residual_block_2")
    layers.append(("inverted_residual_block_2", invres2))

    #32x32x24 -> 16x16x32
    invres3 = mobilenet_v2.inverted_residual_block(
        invres2,
        n_repeat=3,
        n_filters=32,
        stride=2,
        expand_ratio=expand_ratio,
        kernel_init=He_normal(seed=seed + 4),
        is_training=training,
        name="inverted_residual_block_3")
    layers.append(("inverted_residual_block_3", invres3))

    #16x16x32 -> 8x8x64
    invres4 = mobilenet_v2.inverted_residual_block(
        invres3,
        n_repeat=4,
        n_filters=64,
        stride=2,
        expand_ratio=expand_ratio,
        kernel_init=He_normal(seed=seed + 5),
        is_training=training,
        name="inverted_residual_block_4")
    layers.append(("inverted_residual_block_4", invres4))

    #8x8x64 -> 8x8x96
    invres5 = mobilenet_v2.inverted_residual_block(
        invres4,
        n_repeat=3,
        n_filters=96,
        stride=1,
        expand_ratio=expand_ratio,
        kernel_init=He_normal(seed=seed + 6),
        is_training=training,
        name="inverted_residual_block_5")
    layers.append(("inverted_residual_block_5", invres5))

    #8x8x96 -> 4x4x160
    invres6 = mobilenet_v2.inverted_residual_block(
        invres5,
        n_repeat=3,
        n_filters=160,
        stride=2,
        expand_ratio=expand_ratio,
        kernel_init=He_normal(seed=seed + 7),
        is_training=training,
        name="inverted_residual_block_6")
    layers.append(("inverted_residual_block_6", invres6))

    #4x4x160 -> 4x4x320
    invres7 = mobilenet_v2.inverted_residual_block(
        invres6,
        n_repeat=1,
        n_filters=320,
        stride=1,
        expand_ratio=expand_ratio,
        kernel_init=He_normal(seed=seed + 8),
        is_training=training,
        name="inverted_residual_block_7")
    layers.append(("inverted_residual_block_7", invres7))

    conv2 = conv2d_bn_relu(invres7,
                           size=1,
                           n_filters=1280,
                           kernel_init=He_normal(seed=seed + 9),
                           is_training=training,
                           name="final_conv")
    layers.append(("final_conv", conv2))

    pool = global_avg_pool2d(conv2)
    layers.append(("pool", pool))

    dense1 = dense(pool,
                   n_units=10,
                   kernel_init=Kumar_normal(activation=None,
                                            mode="FAN_IN",
                                            seed=seed + 10),
                   name="dense_1")
    layers.append(("logit", dense1))

    prob = tf.nn.softmax(dense1, name="prob")
    layers.append(("prob", prob))

    return layers, variables
Пример #4
0
def reduction_bn_v4_2(inputs,
                      n_reduce_3x3=192,
                      n_filters_3x3=192,
                      n_reduce_7x7=256,
                      n_filters_1x7=256,
                      n_filters_7x1=320,
                      n_filters_7x7_3x3=320,
                      pool=max_pool2d,
                      pool_size=3,
                      padding="VALID",
                      is_training=False,
                      regularizer=None,
                      kernel_init=He_normal(seed=42),
                      kernel_init_reduce=He_normal(seed=42),
                      name="inception_v4_reduction_2"):
    with tf.variable_scope(name):
        # 3x3 stride 2
        reduce_3x3 = conv2d_bn_relu(inputs,
                                    size=1,
                                    n_filters=n_reduce_3x3,
                                    stride=1,
                                    is_training=is_training,
                                    regularizer=regularizer,
                                    kernel_init=kernel_init_reduce,
                                    name="conv_reduce_3x3")
        x_3x3 = conv2d_bn_relu(reduce_3x3,
                               size=3,
                               n_filters=n_filters_3x3,
                               stride=2,
                               padding=padding,
                               is_training=is_training,
                               regularizer=regularizer,
                               kernel_init=kernel_init,
                               name="conv_3x3")

        # 1x7 + 7x1 + 3x3 stride 2
        reduce_7x7 = conv2d_bn_relu(inputs,
                                    size=1,
                                    n_filters=n_reduce_7x7,
                                    stride=1,
                                    is_training=is_training,
                                    regularizer=regularizer,
                                    kernel_init=kernel_init_reduce,
                                    name="conv_reduce_7x7")
        x_1x7 = conv2d_bn_relu(reduce_7x7,
                               size=[1, 7],
                               n_filters=n_filters_1x7,
                               stride=1,
                               is_training=is_training,
                               regularizer=regularizer,
                               kernel_init=kernel_init,
                               name="conv_1x7")
        x_7x1 = conv2d_bn_relu(x_1x7,
                               size=[7, 1],
                               n_filters=n_filters_7x1,
                               stride=1,
                               is_training=is_training,
                               regularizer=regularizer,
                               kernel_init=kernel_init,
                               name="conv_7x1")
        x_7x7_3x3 = conv2d_bn_relu(x_7x1,
                                   size=3,
                                   n_filters=n_filters_7x7_3x3,
                                   stride=2,
                                   padding=padding,
                                   is_training=is_training,
                                   regularizer=regularizer,
                                   kernel_init=kernel_init,
                                   name="conv_7x7_3x3")

        # pool
        pool = pool(inputs,
                    size=pool_size,
                    stride=2,
                    padding=padding,
                    name="pool")

        inception = tf.concat([x_3x3, x_7x7_3x3, pool], axis=3)

    return inception
Пример #5
0
def reduction_bn_v4_1(
        inputs,
        n_filters_3x3_1=384,  #n
        n_reduce_3x3_2=192,  #k
        n_filters_3x3_2_1=224,  #l
        n_filters_3x3_2_2=256,  #m
        pool=max_pool2d,
        pool_size=3,
        padding="VALID",
        is_training=False,
        regularizer=None,
        kernel_init=He_normal(seed=42),
        kernel_init_reduce=He_normal(seed=42),
        name="inception_v4_reduction_1"):
    with tf.variable_scope(name):
        # 3x3 stride 2
        x_3x3_1 = conv2d_bn_relu(inputs,
                                 size=3,
                                 n_filters=n_filters_3x3_1,
                                 stride=2,
                                 padding=padding,
                                 is_training=is_training,
                                 regularizer=regularizer,
                                 kernel_init=kernel_init,
                                 name="conv_3x3_1")

        # 3x3 stride 1 + 3x3 stride 2
        reduce_3x3_2 = conv2d_bn_relu(inputs,
                                      size=1,
                                      n_filters=n_reduce_3x3_2,
                                      stride=1,
                                      is_training=is_training,
                                      regularizer=regularizer,
                                      kernel_init=kernel_init_reduce,
                                      name="conv_reduce_3x3_2")
        x_3x3_2_1 = conv2d_bn_relu(reduce_3x3_2,
                                   size=3,
                                   n_filters=n_filters_3x3_2_1,
                                   stride=1,
                                   is_training=is_training,
                                   regularizer=regularizer,
                                   kernel_init=kernel_init,
                                   name="conv_3x3_2_1")
        x_3x3_2_2 = conv2d_bn_relu(x_3x3_2_1,
                                   size=3,
                                   n_filters=n_filters_3x3_2_2,
                                   stride=2,
                                   padding=padding,
                                   is_training=is_training,
                                   regularizer=regularizer,
                                   kernel_init=kernel_init,
                                   name="conv_3x3_2_2")

        # pool
        pool = pool(inputs,
                    size=pool_size,
                    stride=2,
                    padding=padding,
                    name="pool")

        inception = tf.concat([x_3x3_1, x_3x3_2_2, pool], axis=3)

    return inception
Пример #6
0
def expanded_filterbank_fact1d_bn(
        inputs,
        n_filters_1x1=256,
        n_reduce_1x3_3x1=384,
        n_filters_1x3_3x1=256,
        n_reduce_1x3=384,
        n_filters_1x3=448,
        n_filters_3x1=512,
        n_filters_3x3_1x3_3x1=256,
        n_filters_pool=256,
        pool=avg_pool2d,
        pool_size=3,
        is_training=False,
        regularizer=None,
        kernel_init=He_normal(seed=42),
        kernel_init_reduce=He_normal(seed=42),
        name="inception_expanded_filterbank_batchnorm"):
    with tf.variable_scope(name):
        # 1x1
        x_1x1 = conv2d_bn_relu(inputs,
                               size=1,
                               n_filters=n_filters_1x1,
                               stride=1,
                               is_training=is_training,
                               regularizer=regularizer,
                               kernel_init=kernel_init,
                               name="conv_1x1")

        # 1x3 + 3x1
        reduce_1x3_3x1 = conv2d_bn_relu(inputs,
                                        size=1,
                                        n_filters=n_reduce_1x3_3x1,
                                        stride=1,
                                        is_training=is_training,
                                        regularizer=regularizer,
                                        kernel_init=kernel_init_reduce,
                                        name="conv_reduce_1x3_3x1")
        x_1x3 = conv2d_bn_relu(reduce_1x3_3x1,
                               size=[1, 3],
                               n_filters=n_filters_1x3_3x1,
                               stride=1,
                               is_training=is_training,
                               regularizer=regularizer,
                               kernel_init=kernel_init,
                               name="conv_1x3_1")
        x_3x1 = conv2d_bn_relu(reduce_1x3_3x1,
                               size=[3, 1],
                               n_filters=n_filters_1x3_3x1,
                               stride=1,
                               is_training=is_training,
                               regularizer=regularizer,
                               kernel_init=kernel_init,
                               name="conv_3x1_1")

        # 1x3 + 3x1 + (1x3 + 3x1)
        reduce_1x3 = conv2d_bn_relu(inputs,
                                    size=1,
                                    n_filters=n_reduce_1x3,
                                    stride=1,
                                    is_training=is_training,
                                    regularizer=regularizer,
                                    kernel_init=kernel_init_reduce,
                                    name="conv_reduce_1x3")
        x_1x3_2 = conv2d_bn_relu(reduce_1x3,
                                 size=[1, 3],
                                 n_filters=n_filters_1x3,
                                 stride=1,
                                 is_training=is_training,
                                 regularizer=regularizer,
                                 kernel_init=kernel_init,
                                 name="conv_1x3_2")
        x_3x1_2 = conv2d_bn_relu(x_1x3_2,
                                 size=[3, 1],
                                 n_filters=n_filters_3x1,
                                 stride=1,
                                 is_training=is_training,
                                 regularizer=regularizer,
                                 kernel_init=kernel_init,
                                 name="conv_3x1_2")
        x_3x3_1x3 = conv2d_relu(x_3x1_2,
                                size=[1, 3],
                                n_filters=n_filters_3x3_1x3_3x1,
                                stride=1,
                                regularizer=regularizer,
                                kernel_init=kernel_init,
                                name="conv_3x3_1x3")
        x_3x3_3x1 = conv2d_bn_relu(x_3x1_2,
                                   size=[3, 1],
                                   n_filters=n_filters_3x3_1x3_3x1,
                                   stride=1,
                                   is_training=is_training,
                                   regularizer=regularizer,
                                   kernel_init=kernel_init,
                                   name="conv_3x3_3x1")

        # pool projection
        pool = pool(inputs, size=pool_size, stride=1, name="pool")
        proj_pool = conv2d_bn_relu(pool,
                                   size=1,
                                   n_filters=n_filters_pool,
                                   stride=1,
                                   is_training=is_training,
                                   regularizer=regularizer,
                                   kernel_init=kernel_init_reduce,
                                   name="conv_pool_proj")

        inception = tf.concat(
            [x_1x1, x_1x3, x_3x1, x_3x3_1x3, x_3x3_3x1, proj_pool], axis=3)

    return inception
Пример #7
0
def grid_fact1d_bn(inputs,
                   n_filters_1x1=384,
                   n_reduce_7x7_1=192,
                   n_filters_7x7_1=[(224, 256)],
                   n_reduce_7x7_2=192,
                   n_filters_7x7_2=[(192, 224), (224, 256)],
                   n_filters_pool=128,
                   pool=max_pool2d,
                   pool_size=3,
                   is_training=False,
                   regularizer=None,
                   kernel_init=He_normal(seed=42),
                   kernel_init_reduce=He_normal(seed=42),
                   name="inception_grid_1d_factorized_batchnorm"):
    with tf.variable_scope(name):
        # 1x1
        x_1x1 = conv2d_bn_relu(inputs,
                               size=1,
                               n_filters=n_filters_1x1,
                               stride=1,
                               is_training=is_training,
                               regularizer=regularizer,
                               kernel_init=kernel_init,
                               name="conv_1x1")

        # 1x7 + 7x1
        reduce_7x7_1 = conv2d_bn_relu(inputs,
                                      size=1,
                                      n_filters=n_reduce_7x7_1,
                                      stride=1,
                                      is_training=is_training,
                                      regularizer=regularizer,
                                      kernel_init=kernel_init_reduce,
                                      name="conv_reduce_7x7_1")

        x_7x7_1 = factorized_conv2d_bn_relu(reduce_7x7_1,
                                            size=7,
                                            n_repeat=1,
                                            n_filters=n_filters_7x7_1,
                                            stride=1,
                                            is_training=is_training,
                                            regularizer=regularizer,
                                            kernel_init=kernel_init,
                                            name="conv_fact_7x7_1")

        # 1x7 + 7x1 + 1x7 + 7x1
        reduce_7x7_2 = conv2d_bn_relu(inputs,
                                      size=1,
                                      n_filters=n_reduce_7x7_2,
                                      stride=1,
                                      is_training=is_training,
                                      regularizer=regularizer,
                                      kernel_init=kernel_init_reduce,
                                      name="conv_reduce_7x7_2")

        x_7x7_2 = factorized_conv2d_bn_relu(reduce_7x7_2,
                                            size=7,
                                            n_repeat=2,
                                            n_filters=n_filters_7x7_2,
                                            stride=1,
                                            is_training=is_training,
                                            regularizer=regularizer,
                                            kernel_init=kernel_init,
                                            name="conv_fact_7x7_2")

        # pool projection
        pool = pool(inputs, size=pool_size, stride=1, name="pool")
        proj_pool = conv2d_bn_relu(pool,
                                   size=1,
                                   n_filters=n_filters_pool,
                                   stride=1,
                                   is_training=is_training,
                                   regularizer=regularizer,
                                   kernel_init=kernel_init_reduce,
                                   name="conv_pool_proj")

        inception = tf.concat([x_1x1, x_7x7_1, x_7x7_2, proj_pool], axis=3)

    return inception
Пример #8
0
def grid_fact2d_bn(inputs,
                   n_filters_1x1=64,
                   n_reduce_3x3=96,
                   n_filters_3x3=128,
                   n_reduce_5x5=16,
                   n_filters_5x5=32,
                   n_filters_pool=32,
                   pool=max_pool2d,
                   pool_size=3,
                   is_training=False,
                   regularizer=None,
                   kernel_init=He_normal(seed=42),
                   kernel_init_reduce=He_normal(seed=42),
                   name="inception_grid_2d_factorized_batchnorm"):
    with tf.variable_scope(name):
        # 1x1
        x_1x1 = conv2d_bn_relu(inputs,
                               size=1,
                               n_filters=n_filters_1x1,
                               stride=1,
                               is_training=is_training,
                               regularizer=regularizer,
                               kernel_init=kernel_init,
                               name="conv_1x1")

        # 3x3
        reduce_3x3 = conv2d_bn_relu(inputs,
                                    size=1,
                                    n_filters=n_reduce_3x3,
                                    stride=1,
                                    is_training=is_training,
                                    regularizer=regularizer,
                                    kernel_init=kernel_init_reduce,
                                    name="conv_reduce_3x3")
        x_3x3 = conv2d_bn_relu(reduce_3x3,
                               size=3,
                               n_filters=n_filters_3x3,
                               stride=1,
                               is_training=is_training,
                               regularizer=regularizer,
                               kernel_init=kernel_init,
                               name="conv_3x3")

        # factorized 5x5
        reduce_5x5 = conv2d_bn_relu(inputs,
                                    size=1,
                                    n_filters=n_reduce_5x5,
                                    stride=1,
                                    is_training=is_training,
                                    regularizer=regularizer,
                                    kernel_init=kernel_init_reduce,
                                    name="conv_reduce_5x5")
        fact_5x5_1 = conv2d_bn_relu(reduce_5x5,
                                    size=3,
                                    n_filters=n_filters_5x5,
                                    stride=1,
                                    is_training=is_training,
                                    regularizer=regularizer,
                                    kernel_init=kernel_init,
                                    name="fact_conv_5x5_1")
        fact_5x5_2 = conv2d_bn_relu(fact_5x5_1,
                                    size=3,
                                    n_filters=n_filters_5x5,
                                    stride=1,
                                    is_training=is_training,
                                    regularizer=regularizer,
                                    kernel_init=kernel_init,
                                    name="fact_conv_5x5_2")

        # pool projection
        pool = pool(inputs, size=pool_size, stride=1, name="pool")
        proj_pool = conv2d_bn_relu(pool,
                                   size=1,
                                   n_filters=n_filters_pool,
                                   stride=1,
                                   is_training=is_training,
                                   regularizer=regularizer,
                                   kernel_init=kernel_init_reduce,
                                   name="conv_pool_proj")

        inception = tf.concat([x_1x1, x_3x3, fact_5x5_2, proj_pool], axis=3)

    return inception
Пример #9
0
def cifar10_sequential_cbn5d3_wd(x,
                                 drop_rate=0.5,
                                 weight_decay=0.001,
                                 seed=42):
    layers = []
    variables = []

    training = tf.placeholder(tf.bool, name="training")
    variables.append(("training", training))

    # 2x conv3x3 selu + pool
    conv1 = conv2d_bn_relu(
        x,
        size=5,
        n_filters=64,
        is_training=training,
        regularizer=tf.contrib.layers.l2_regularizer(weight_decay),
        kernel_init=He_normal(seed=seed + 1),
        name="conv_1")
    layers.append(("conv_1", conv1))
    pool1 = max_pool2d(conv1, size=3, stride=2, name="pool_1")
    layers.append(("pool_1", pool1))

    # 2x conv3x3 selu + pool
    conv2 = conv2d_bn_relu(
        pool1,
        size=5,
        n_filters=64,
        is_training=training,
        regularizer=tf.contrib.layers.l2_regularizer(weight_decay),
        kernel_init=He_normal(seed=seed + 2),
        name="conv_2")
    layers.append(("conv_2", conv2))
    pool2 = max_pool2d(conv2, size=3, stride=2, name="pool_2")
    layers.append(("pool_2", pool2))

    # 2x conv3x3 selu + pool
    conv3 = conv2d_bn_relu(
        pool2,
        size=3,
        n_filters=128,
        is_training=training,
        regularizer=tf.contrib.layers.l2_regularizer(weight_decay),
        kernel_init=He_normal(seed=seed + 3),
        name="conv_3")
    layers.append(("conv_3", conv3))
    conv4 = conv2d_bn_relu(
        conv3,
        size=3,
        n_filters=128,
        is_training=training,
        regularizer=tf.contrib.layers.l2_regularizer(weight_decay),
        kernel_init=He_normal(seed=seed + 4),
        name="conv_4")
    layers.append(("conv_4", conv4))
    conv5 = conv2d_bn_relu(
        conv4,
        size=3,
        n_filters=128,
        is_training=training,
        regularizer=tf.contrib.layers.l2_regularizer(weight_decay),
        kernel_init=He_normal(seed=seed + 5),
        name="conv_5")
    layers.append(("conv_5", conv5))
    pool3 = max_pool2d(conv5, size=2, stride=2, name="pool_3")
    layers.append(("pool_3", pool3))

    flat = flatten(pool3, name="flatten")
    layers.append(("flatten", flat))

    dense1 = dense_relu(
        flat,
        n_units=384,
        regularizer=tf.contrib.layers.l2_regularizer(weight_decay),
        kernel_init=He_normal(seed=seed + 6),
        name="dense_1")
    layers.append(("dense_1", dense1))

    if drop_rate > 0.0:
        dense1 = tf.layers.dropout(dense1,
                                   rate=drop_rate,
                                   training=training,
                                   seed=seed + 6,
                                   name="dropout_1")

    dense2 = dense_relu(
        dense1,
        n_units=192,
        regularizer=tf.contrib.layers.l2_regularizer(weight_decay),
        kernel_init=He_normal(seed=seed + 7),
        name="dense_2")
    layers.append(("dense_2", dense2))

    if drop_rate > 0.0:
        dense2 = tf.layers.dropout(dense2,
                                   rate=drop_rate,
                                   training=training,
                                   seed=seed + 7,
                                   name="dropout_2")

    # dense softmax
    dense3 = dense(dense2,
                   n_units=10,
                   regularizer=tf.contrib.layers.l2_regularizer(weight_decay),
                   kernel_init=Kumar_normal(activation=None,
                                            mode="FAN_IN",
                                            seed=seed + 8),
                   name="dense_3")
    layers.append(("logit", dense3))
    prob = tf.nn.softmax(dense3, name="prob")
    layers.append(("prob", prob))

    return layers, variables
Пример #10
0
def cifar10_sequential_cbn3d(x,
                             drop_rate_1=0.2,
                             drop_rate_2=0.3,
                             drop_rate_3=0.4,
                             seed=42):
    layers = []
    variables = []

    training = tf.placeholder(tf.bool, name="training")
    variables.append(("training", training))

    # conv5x5 batch-norm relu + pool
    conv1 = conv2d_bn_relu(x,
                           size=5,
                           n_filters=32,
                           is_training=training,
                           kernel_init=He_normal(seed=seed + 1),
                           name="conv_1")
    layers.append(("conv_1", conv1))
    pool1 = max_pool2d(conv1, size=3, stride=2, name="pool_1")
    layers.append(("pool_1", pool1))
    dropout1 = tf.layers.dropout(pool1,
                                 rate=drop_rate_1,
                                 training=training,
                                 seed=seed + 1,
                                 name="dropout_1")
    layers.append(("dropout_1", dropout1))

    # conv5x5 batch-norm relu + pool
    conv2 = conv2d_bn_relu(dropout1,
                           size=5,
                           n_filters=64,
                           is_training=training,
                           kernel_init=He_normal(seed=seed + 2),
                           name="conv_2")
    layers.append(("conv_2", conv2))
    pool2 = max_pool2d(conv2, size=3, stride=2, name="pool_2")
    layers.append(("pool_2", pool2))
    dropout2 = tf.layers.dropout(pool2,
                                 rate=drop_rate_2,
                                 training=training,
                                 seed=seed + 2,
                                 name="dropout_2")
    layers.append(("dropout_2", dropout2))

    # conv3x3 batch-norm relu + pool
    conv3 = conv2d_bn_relu(dropout2,
                           size=3,
                           n_filters=128,
                           is_training=training,
                           kernel_init=He_normal(seed=seed + 3),
                           name="conv_3")
    layers.append(("conv_3", conv3))
    pool3 = max_pool2d(conv3, size=2, stride=2, name="pool_3")
    layers.append(("pool_3", pool3))
    dropout3 = tf.layers.dropout(pool3,
                                 rate=drop_rate_3,
                                 training=training,
                                 seed=seed + 3,
                                 name="dropout_3")
    layers.append(("dropout_3", dropout3))

    flat = flatten(dropout3, name="flatten")
    layers.append(("flatten", flat))

    # dense softmax
    dense1 = dense(flat,
                   n_units=10,
                   kernel_init=Kumar_normal(activation=None,
                                            mode="FAN_IN",
                                            seed=seed + 4),
                   name="dense_1")
    layers.append(("logit", dense1))
    prob = tf.nn.softmax(dense1, name="prob")
    layers.append(("prob", prob))

    return layers, variables
Пример #11
0
def cifar10_xception_wd(x, drop_rate=0.5, weight_decay=0.0001, seed=42):
    layers = []
    variables = []

    training = tf.placeholder(tf.bool, name="training")
    variables.append(("training", training))

    # 32x32
    conv = conv2d_bn_relu(
        x,
        size=3,
        n_filters=64,
        is_training=training,
        regularizer=tf.contrib.layers.l2_regularizer(weight_decay),
        kernel_init=He_normal(seed=seed + 1),
        name="initial_conv")
    layers.append(("initial_conv", conv))

    entry = xception.entry_module(
        conv,
        n_filters=[128, 256, 728],
        is_training=training,
        regularizer=tf.contrib.layers.l2_regularizer(weight_decay),
        kernel_init=He_normal(seed=seed + 2),
        name="entry")
    layers.append(("entry", entry))

    mid = xception.middle_module(
        entry,
        n_filters=728,
        n_repeat=3,
        is_training=training,
        regularizer=tf.contrib.layers.l2_regularizer(weight_decay),
        kernel_init=He_normal(seed=seed + 3),
        name="middle")
    layers.append(("middle", mid))

    exits = xception.exit_module(
        mid,
        n_filters_1=[728, 1024],
        n_filters_2=[1536, 2048],
        is_training=training,
        regularizer=tf.contrib.layers.l2_regularizer(weight_decay),
        kernel_init=He_normal(seed=seed + 4),
        name="exit")
    layers.append(("exit", exits))

    pool1 = global_avg_pool2d(exits, name="pool1")
    layers.append(("pool1", pool1))

    dropout1 = tf.layers.dropout(pool1,
                                 rate=drop_rate,
                                 training=training,
                                 seed=seed + 5,
                                 name="dropout")
    layers.append(("dropout1", dropout1))

    dense1 = dense(dropout1,
                   n_units=10,
                   regularizer=tf.contrib.layers.l2_regularizer(weight_decay),
                   kernel_init=Kumar_normal(activation=None,
                                            mode="FAN_IN",
                                            seed=seed + 6),
                   name="dense1")
    layers.append(("logit", dense1))

    prob = tf.nn.softmax(dense1, name="prob")
    layers.append(("prob", prob))

    return layers, variables
Пример #12
0
def cifar10_mobilenet(x, seed=42):
    layers = []
    variables = []

    training = tf.placeholder(tf.bool, name="training")
    variables.append(("training", training))

    conv = conv2d_bn_relu(x,
                          size=3,
                          n_filters=32,
                          kernel_init=He_normal(seed=seed + 1),
                          is_training=training,
                          name="initial_conv")
    layers.append(("initial_conv", conv))

    mblock1 = mobilenet.mobilenet_block(conv,
                                        n_filters=64,
                                        stride=1,
                                        kernel_init=He_normal(seed=seed + 2),
                                        is_training=training,
                                        name="mobilenet_block_1")
    layers.append(("mobilenet_block_1", mblock1))

    # 16x16
    mblock2 = mobilenet.mobilenet_block(mblock1,
                                        n_filters=128,
                                        stride=2,
                                        kernel_init=He_normal(seed=seed + 3),
                                        is_training=training,
                                        name="mobilenet_block_2")
    layers.append(("mobilenet_block_2", mblock2))

    mblock3 = mobilenet.mobilenet_block(mblock2,
                                        n_filters=128,
                                        stride=1,
                                        kernel_init=He_normal(seed=seed + 4),
                                        is_training=training,
                                        name="mobilenet_block_3")
    layers.append(("mobilenet_block_3", mblock3))

    # 8x8
    mblock4 = mobilenet.mobilenet_block(mblock3,
                                        n_filters=256,
                                        stride=2,
                                        kernel_init=He_normal(seed=seed + 5),
                                        is_training=training,
                                        name="mobilenet_block_4")
    layers.append(("mobilenet_block_4", mblock4))

    mblock5 = mobilenet.mobilenet_block(mblock4,
                                        n_filters=256,
                                        stride=1,
                                        kernel_init=He_normal(seed=seed + 6),
                                        is_training=training,
                                        name="mobilenet_block_5")
    layers.append(("mobilenet_block_5", mblock5))

    # 4x4
    mblock6 = mobilenet.mobilenet_block(mblock5,
                                        n_filters=512,
                                        stride=2,
                                        kernel_init=He_normal(seed=seed + 7),
                                        is_training=training,
                                        name="mobilenet_block_6")
    layers.append(("mobilenet_block_6", mblock6))

    mblock7 = mobilenet.mobilenet_block(mblock6,
                                        n_filters=512,
                                        stride=1,
                                        kernel_init=He_normal(seed=seed + 8),
                                        is_training=training,
                                        name="mobilenet_block_7")
    layers.append(("mobilenet_block_7", mblock7))

    mblock8 = mobilenet.mobilenet_block(mblock7,
                                        n_filters=512,
                                        stride=1,
                                        kernel_init=He_normal(seed=seed + 9),
                                        is_training=training,
                                        name="mobilenet_block_8")
    layers.append(("mobilenet_block_8", mblock8))

    mblock9 = mobilenet.mobilenet_block(mblock8,
                                        n_filters=512,
                                        stride=1,
                                        kernel_init=He_normal(seed=seed + 10),
                                        is_training=training,
                                        name="mobilenet_block_9")
    layers.append(("mobilenet_block_9", mblock9))

    mblock10 = mobilenet.mobilenet_block(mblock9,
                                         n_filters=512,
                                         stride=1,
                                         kernel_init=He_normal(seed=seed + 11),
                                         is_training=training,
                                         name="mobilenet_block_10")
    layers.append(("mobilenet_block_10", mblock10))

    mblock11 = mobilenet.mobilenet_block(mblock10,
                                         n_filters=512,
                                         stride=1,
                                         kernel_init=He_normal(seed=seed + 12),
                                         is_training=training,
                                         name="mobilenet_block_11")
    layers.append(("mobilenet_block_11", mblock11))

    # 2x2
    mblock12 = mobilenet.mobilenet_block(mblock11,
                                         n_filters=1024,
                                         stride=2,
                                         kernel_init=He_normal(seed=seed + 13),
                                         is_training=training,
                                         name="mobilenet_block_12")
    layers.append(("mobilenet_block_12", mblock12))

    mblock13 = mobilenet.mobilenet_block(mblock12,
                                         n_filters=1024,
                                         stride=1,
                                         kernel_init=He_normal(seed=seed + 14),
                                         is_training=training,
                                         name="mobilenet_block_13")
    layers.append(("mobilenet_block_13", mblock13))

    pool = global_avg_pool2d(mblock13)
    layers.append(("pool", pool))

    dense1 = dense(pool,
                   n_units=10,
                   kernel_init=Kumar_normal(activation=None,
                                            mode="FAN_IN",
                                            seed=seed + 15),
                   name="dense1")
    layers.append(("logit", dense1))

    prob = tf.nn.softmax(dense1, name="prob")
    layers.append(("prob", prob))

    return layers, variables