Beispiel #1
0
def conv_bn_rectify(net, num_filters, wd, name, is_training, reuse):
    with tf.variable_scope(name):
        net = layers.conv_2d_layer(net, [3,3], net.get_shape()[3], num_filters, nonlinearity=None, wd=wd,
                                   padding='SAME', name='conv', with_biases=False)
        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(), decay=0.9, reuse=reuse, is_training=is_training)
        net = tf.nn.relu(net)
    return net
def net_vgglike(images, nclass, scale, is_training, reuse, threshold):
    net = conv_bn_rectify(images, int(64 * scale), 0.0, 'conv_1', is_training,
                          reuse, threshold)
    net = conv_bn_rectify(net, int(64 * scale), 0.0, 'conv_2', is_training,
                          reuse, threshold)
    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), 0.0, 'conv_3', is_training,
                          reuse, threshold)
    net = conv_bn_rectify(net, int(128 * scale), 0.0, 'conv_4', is_training,
                          reuse, threshold)
    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), 0.0, 'conv_5', is_training,
                          reuse, threshold)
    net = conv_bn_rectify(net, int(256 * scale), 0.0, 'conv_6', is_training,
                          reuse, threshold)
    net = conv_bn_rectify(net, int(256 * scale), 0.0, 'conv_7', is_training,
                          reuse, threshold)
    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), 0.0, 'conv_8', is_training,
                          reuse, threshold)
    net = conv_bn_rectify(net, int(512 * scale), 0.0, 'conv_9', is_training,
                          reuse, threshold)
    net = conv_bn_rectify(net, int(512 * scale), 0.0, 'conv_10', is_training,
                          reuse, threshold)
    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), 0.0, 'conv_11', is_training,
                          reuse, threshold)
    net = conv_bn_rectify(net, int(512 * scale), 0.0, 'conv_12', is_training,
                          reuse, threshold)
    net = conv_bn_rectify(net, int(512 * scale), 0.0, 'conv_13', is_training,
                          reuse, threshold)
    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,
                             threshold)
    net = layers.dense_layer(net,
                             net.get_shape()[1],
                             int(512 * scale),
                             nonlinearity=None,
                             wd=0.0,
                             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,
                             threshold)
    net = layers.dense_layer(net,
                             net.get_shape()[1],
                             nclass,
                             nonlinearity=None,
                             wd=0.0,
                             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