Ejemplo n.º 1
0
def lenet5(images, nclass):
    # conv 1
    net = layers.conv_2d_layer(images, [5, 5, images.get_shape()[3], 20], nonlinearity=None,
                               padding='SAME', name='conv_1', with_biases=False)
    biases = layers.variable_on_cpu('biases_1', net.get_shape()[3], tf.constant_initializer(0.0), dtype=tf.float32)
    net = tf.nn.bias_add(net, biases)
    net = tf.nn.relu(net)
    # max pool
    net = tf.nn.max_pool(net, ksize=[1,2,2,1], strides=[1,2,2,1], padding='VALID')
    # conv 2
    net = layers.conv_2d_layer(net, [5, 5, net.get_shape()[3], 50], nonlinearity=None,
                               padding='SAME', name='conv_2', with_biases=False)
    biases = layers.variable_on_cpu('biases_2', net.get_shape()[3], tf.constant_initializer(0.0), dtype=tf.float32)
    net = tf.nn.bias_add(net, biases)
    net = tf.nn.relu(net)
    # max pool
    net = tf.nn.max_pool(net, ksize=[1,2,2,1], strides=[1,2,2,1], padding='VALID')
    # reshape
    net = tf.reshape(net, [-1, (net.get_shape()[1]*net.get_shape()[2]*net.get_shape()[3]).value])
    # dense 1
    net = layers.dense_layer(net, net.get_shape()[1], 500,
                             nonlinearity=None, name='dense_1')
    net = tf.nn.relu(net)
    # dense 2
    net = layers.dense_layer(net, net.get_shape()[1], nclass,
                             nonlinearity=None, name='dense_2')
    return net
Ejemplo n.º 2
0
def conv_bn_rectify(net, num_filters, name, is_training, reuse, kl_weight=1.0):
    with tf.variable_scope(name):
        net = layers.conv_2d_layer(net, [3,3, net.get_shape()[3], num_filters], nonlinearity=None,
                                   padding='SAME', name='conv', with_biases=False, reuse=reuse)
        net = layers.sbp_dropout(net, 3, is_training, 'sbp', reuse, kl_weight=kl_weight)
        biases = layers.variable_on_cpu('biases', net.get_shape()[3], tf.constant_initializer(0.0), dtype=tf.float32)
        net = tf.nn.bias_add(net, biases)
        net = tf.contrib.layers.batch_norm(net, scope=tf.get_variable_scope(), reuse=reuse, is_training=False,
                                           center=True, scale=True)
        net = tf.nn.relu(net)
    return net
Ejemplo n.º 3
0
def net_vgglike(images, nclass, scale, is_training, reuse):
    net = conv_bn_rectify(images, int(64*scale), 'conv_1', is_training, reuse, 10.0)
    net = conv_bn_rectify(net, int(64*scale), 'conv_2', is_training, reuse, 10.0)
    net = tf.nn.max_pool(net, ksize=[1,2,2,1], strides=[1,2,2,1], padding='VALID')

    net = conv_bn_rectify(net, int(128*scale), 'conv_3', is_training, reuse, 5.0)
    net = conv_bn_rectify(net, int(128*scale), 'conv_4', is_training, reuse, 5.0)
    net = tf.nn.max_pool(net, ksize=[1,2,2,1], strides=[1,2,2,1], padding='VALID')

    net = conv_bn_rectify(net, int(256*scale), 'conv_5', is_training, reuse)
    net = conv_bn_rectify(net, int(256*scale), 'conv_6', is_training, reuse)
    net = conv_bn_rectify(net, int(256*scale), 'conv_7', is_training, reuse)
    net = tf.nn.max_pool(net, ksize=[1,2,2,1], strides=[1,2,2,1], padding='VALID')

    net = conv_bn_rectify(net, int(512*scale), 'conv_8', is_training, reuse)
    net = conv_bn_rectify(net, int(512*scale), 'conv_9', is_training, reuse)
    net = conv_bn_rectify(net, int(512*scale), 'conv_10', is_training, reuse)
    net = tf.nn.max_pool(net, ksize=[1,2,2,1], strides=[1,2,2,1], padding='VALID')

    net = conv_bn_rectify(net, int(512*scale), 'conv_11', is_training, reuse)
    net = conv_bn_rectify(net, int(512*scale), 'conv_12', is_training, reuse)
    net = conv_bn_rectify(net, int(512*scale), 'conv_13', is_training, reuse)
    net = tf.nn.max_pool(net, ksize=[1,2,2,1], strides=[1,2,2,1], padding='VALID')

    net = tf.reshape(net, [-1, (net.get_shape()[1]*net.get_shape()[2]*net.get_shape()[3]).value])
    net = layers.sbp_dropout(net, 1, is_training, 'sbp_dense_1', reuse)
    net = layers.dense_layer(net, net.get_shape()[1], int(512*scale),
                             nonlinearity=None, name='dense_1', with_biases=False)
    biases = layers.variable_on_cpu('biases_dense_1', net.get_shape()[1], tf.constant_initializer(0.0),
                                    dtype=tf.float32)
    net = tf.nn.bias_add(net, biases)
    net = tf.contrib.layers.batch_norm(net, scope=tf.get_variable_scope(), reuse=reuse, is_training=False,
                                       center=True, scale=True)
    net = tf.nn.relu(net)
    net = layers.sbp_dropout(net, 1, is_training, 'sbp_dense_2', reuse)
    net = layers.dense_layer(net, net.get_shape()[1], nclass,
                             nonlinearity=None, name='dense_2', with_biases=False)
    biases = layers.variable_on_cpu('biases_dense_2', net.get_shape()[1], tf.constant_initializer(0.0),
                                    dtype=tf.float32)
    net = tf.nn.bias_add(net, biases)
    return net
Ejemplo n.º 4
0
def net_vgglike(images, nclass, num_filters, is_training, reuse):
    net = conv_bn_rectify(images, num_filters[0], 'conv_1', is_training, reuse)
    net = tf.contrib.layers.dropout(net,
                                    keep_prob=0.7,
                                    is_training=is_training)
    net = conv_bn_rectify(net, num_filters[1], 'conv_2', is_training, reuse)
    net = tf.nn.max_pool(net,
                         ksize=[1, 2, 2, 1],
                         strides=[1, 2, 2, 1],
                         padding='VALID')

    net = conv_bn_rectify(net, num_filters[2], 'conv_3', is_training, reuse)
    net = tf.contrib.layers.dropout(net,
                                    keep_prob=0.6,
                                    is_training=is_training)
    net = conv_bn_rectify(net, num_filters[3], 'conv_4', is_training, reuse)
    net = tf.nn.max_pool(net,
                         ksize=[1, 2, 2, 1],
                         strides=[1, 2, 2, 1],
                         padding='VALID')

    net = conv_bn_rectify(net, num_filters[4], 'conv_5', is_training, reuse)
    net = tf.contrib.layers.dropout(net,
                                    keep_prob=0.6,
                                    is_training=is_training)
    net = conv_bn_rectify(net, num_filters[5], 'conv_6', is_training, reuse)
    net = tf.contrib.layers.dropout(net,
                                    keep_prob=0.6,
                                    is_training=is_training)
    net = conv_bn_rectify(net, num_filters[6], 'conv_7', is_training, reuse)
    net = tf.nn.max_pool(net,
                         ksize=[1, 2, 2, 1],
                         strides=[1, 2, 2, 1],
                         padding='VALID')

    net = conv_bn_rectify(net, num_filters[7], 'conv_8', is_training, reuse)
    net = tf.contrib.layers.dropout(net,
                                    keep_prob=0.6,
                                    is_training=is_training)
    net = conv_bn_rectify(net, num_filters[8], 'conv_9', is_training, reuse)
    net = tf.contrib.layers.dropout(net,
                                    keep_prob=0.6,
                                    is_training=is_training)
    net = conv_bn_rectify(net, num_filters[9], 'conv_10', is_training, reuse)
    net = tf.nn.max_pool(net,
                         ksize=[1, 2, 2, 1],
                         strides=[1, 2, 2, 1],
                         padding='VALID')

    net = conv_bn_rectify(net, num_filters[10], 'conv_11', is_training, reuse)
    net = conv_bn_rectify(net, num_filters[11], 'conv_12', is_training, reuse)
    net = conv_bn_rectify(net, num_filters[12], 'conv_13', is_training, reuse)
    net = tf.nn.max_pool(net,
                         ksize=[1, 2, 2, 1],
                         strides=[1, 2, 2, 1],
                         padding='VALID')
    net = tf.reshape(net, [
        -1,
        (net.get_shape()[1] * net.get_shape()[2] * net.get_shape()[3]).value
    ])

    net = tf.contrib.layers.dropout(net,
                                    keep_prob=0.5,
                                    is_training=is_training)
    net = layers.dense_layer(net,
                             net.get_shape()[1],
                             512,
                             nonlinearity=None,
                             name='dense_1',
                             with_biases=False)
    biases = layers.variable_on_cpu('biases_dense_1',
                                    net.get_shape()[1],
                                    tf.constant_initializer(0.0),
                                    dtype=tf.float32)
    net = tf.nn.bias_add(net, biases)
    net = tf.contrib.layers.batch_norm(net,
                                       scope=tf.get_variable_scope(),
                                       reuse=reuse,
                                       is_training=is_training,
                                       center=True,
                                       scale=True)
    net = tf.nn.relu(net)
    net = tf.contrib.layers.dropout(net,
                                    keep_prob=0.5,
                                    is_training=is_training)
    net = layers.dense_layer(net,
                             net.get_shape()[1],
                             nclass,
                             nonlinearity=None,
                             name='dense_2',
                             with_biases=False)
    biases = layers.variable_on_cpu('biases_dense_2',
                                    net.get_shape()[1],
                                    tf.constant_initializer(0.0),
                                    dtype=tf.float32)
    net = tf.nn.bias_add(net, biases)
    tf.add_to_collection('logits', net)
    return net