Пример #1
0
def semantic2visual_2d(semantic_feat,
                       semantic_size,
                       visual_size,
                       name='default',
                       reuse=False,
                       dropout=False,
                       keep_prob=1.0,
                       activation='leaky_relu'):
    assert activation == 'leaky_relu'
    with tf.variable_scope('semantic2visual_2d_%s' % (name),
                           reuse=reuse) as vs:
        assert semantic_feat.get_shape().as_list()[-2:] == [
            semantic_size, semantic_size
        ]
        reshape_semantic_feat = tf.reshape(semantic_feat,
                                           [-1, semantic_size * semantic_size],
                                           name='reshape_semantic_feat')
        if dropout:
            reshape_semantic_feat = tf.nn.dropout(
                reshape_semantic_feat,
                keep_prob,
                name='reshape_semantic_feat_dropout')
        map2 = leaky_relu(
            fc(reshape_semantic_feat, semantic_size, biased=True, name='map2'))
        if dropout:
            map2 = tf.nn.dropout(map2, keep_prob, name='map2_dropout')
        visual_feat = leaky_relu(
            fc(map2, visual_size, biased=True, name='map1'))
    variables = tf.contrib.framework.get_variables(vs)
    return visual_feat, variables
def discriminator_cycle(vector, name='vector', reuse=False):
    semantic_size = vector.get_shape().as_list()[-1]
    with tf.variable_scope('discriminator_%s' % (name), reuse=reuse) as vs:
        fc1 = leaky_relu(
            fc(vector, semantic_size, name='fc1', biased=True,
               trainable=False))
        output = fc(fc1, 1, name='output', biased=True, trainable=False)
    variables = tf.contrib.framework.get_variables(vs)
    return output, variables
Пример #3
0
def comparator_caffenet_fc7(input_image, reuse=False, trainable=False):
    with tf.variable_scope('comparator', reuse=reuse) as vs:
        # input_image = tf.placeholder(tf.float32, shape=(None, 227, 227, 3), name='input_image')
        assert input_image.get_shape().as_list()[1:] == [227, 227, 3]
        relu1 = relu(
            conv(input_image,
                 11,
                 11,
                 96,
                 4,
                 4,
                 pad='VALID',
                 name='conv1',
                 trainable=trainable))
        pool1 = max_pool(relu1, 3, 3, 2, 2, pad='VALID', name='pool1')
        norm1 = lrn(pool1, 2, 2e-05, 0.75, name='norm1')
        relu2 = relu(
            conv(norm1,
                 5,
                 5,
                 256,
                 1,
                 1,
                 group=2,
                 name='conv2',
                 trainable=trainable))
        pool2 = max_pool(relu2, 3, 3, 2, 2, pad='VALID', name='pool2')
        norm2 = lrn(pool2, 2, 2e-05, 0.75, name='norm2')
        relu3 = relu(
            conv(norm2, 3, 3, 384, 1, 1, name='conv3', trainable=trainable))
        relu4 = relu(
            conv(relu3,
                 3,
                 3,
                 384,
                 1,
                 1,
                 group=2,
                 name='conv4',
                 trainable=trainable))
        relu5 = relu(
            conv(relu4,
                 3,
                 3,
                 256,
                 1,
                 1,
                 group=2,
                 name='conv5',
                 trainable=trainable))
        pool5 = max_pool(relu5, 3, 3, 2, 2, pad='VALID', name='pool5')
        fc6 = relu(fc(pool5, 4096, name='fc6', trainable=trainable))
        fc7 = relu(fc(fc6, 4096, name='fc7', trainable=trainable))
    variables = tf.contrib.framework.get_variables(vs)

    return fc7, variables
def discriminator_image(input_image,
                        real_input_label,
                        fake_input_label,
                        num_train_classes,
                        name='image',
                        reuse=False):
    with tf.variable_scope('discriminator_%s' % (name), reuse=reuse) as vs:
        dconv1 = relu(
            conv(input_image, 7, 7, 32, 4, 4, pad='VALID', name='dconv1'))
        dconv2 = relu(conv(dconv1, 5, 5, 64, 1, 1, pad='VALID', name='dconv2'))
        dconv3 = relu(conv(dconv2, 3, 3, 128, 2, 2, pad='VALID',
                           name='dconv3'))
        dconv4 = relu(conv(dconv3, 3, 3, 256, 1, 1, pad='VALID',
                           name='dconv4'))
        dconv5 = relu(conv(dconv4, 3, 3, 256, 2, 2, pad='VALID',
                           name='dconv5'))

        dpool5 = avg_pool(dconv5, 11, 11, 11, 11, name='dpool5')
        dpool5_reshape = tf.reshape(dpool5, [-1, 256], name='dpool5_reshape')

        # label information
        real_label_feat = tf.one_hot(real_input_label, depth=num_train_classes)
        fake_label_feat = tf.one_hot(fake_input_label, depth=num_train_classes)
        label_feat = tf.concat([real_label_feat, fake_label_feat], axis=0)

        # SAVE GPU MEMORY
        Ffc1 = relu(fc(label_feat, 128, name='Ffc1'))
        Ffc2 = relu(fc(Ffc1, 128, name='Ffc2'))

        concat5 = tf.concat([dpool5_reshape, Ffc2], axis=1, name='concat5')
        drop5 = tf.nn.dropout(concat5, keep_prob=0.5, name='drop5')
        # SAVE GPU MEMORY
        dfc6 = relu(fc(drop5, 256, name='dfc6'))
        dfc7 = fc(dfc6, 1, name='dfc7')

    variables = tf.contrib.framework.get_variables(vs)

    return dfc7, variables
Пример #5
0
def semantic2semantic_2layer(semantic_feat,
                             semantic_size,
                             name='default',
                             reuse=False,
                             dropout=False,
                             keep_prob=1.0,
                             activation='leaky_relu'):
    assert activation == 'leaky_relu'
    with tf.variable_scope('semantic2semantic_2layer_%s' % (name),
                           reuse=reuse) as vs:
        # assert semantic_feat.get_shape().as_list()[-1] == semantic_size
        if dropout:
            semantic_feat = tf.nn.dropout(semantic_feat,
                                          keep_prob,
                                          name='semantic_feat_dropout')
        map1 = leaky_relu(
            fc(semantic_feat, semantic_size, biased=True, name='map1'))
        if dropout:
            map1 = tf.nn.dropout(map1, keep_prob, name='map1_dropout')
        ret_semantic_feat = leaky_relu(
            fc(map1, semantic_size, biased=True, name='ret_semantic_feat'))

    variables = tf.contrib.framework.get_variables(vs)
    return ret_semantic_feat, variables
Пример #6
0
def classifier_caffenet(input_image,
                        output,
                        reuse=False,
                        trainable=False,
                        dropout=False,
                        keep_prob=1.0):
    with tf.variable_scope('classifier', reuse=reuse) as vs:
        # input_image = tf.placeholder(tf.float32, shape=(None, 227, 227, 3), name='input_image')
        # assert input_image.get_shape().as_list()[1:] == [227, 227, 3]
        relu1 = relu(
            conv(input_image,
                 11,
                 11,
                 96,
                 4,
                 4,
                 pad='VALID',
                 name='conv1',
                 trainable=trainable))
        pool1 = max_pool(relu1, 3, 3, 2, 2, pad='VALID', name='pool1')
        norm1 = lrn(pool1, 2, 2e-05, 0.75, name='norm1')
        relu2 = relu(
            conv(norm1,
                 5,
                 5,
                 256,
                 1,
                 1,
                 group=2,
                 name='conv2',
                 trainable=trainable))
        pool2 = max_pool(relu2, 3, 3, 2, 2, pad='VALID', name='pool2')
        norm2 = lrn(pool2, 2, 2e-05, 0.75, name='norm2')
        relu3 = relu(
            conv(norm2, 3, 3, 384, 1, 1, name='conv3', trainable=trainable))
        relu4 = relu(
            conv(relu3,
                 3,
                 3,
                 384,
                 1,
                 1,
                 group=2,
                 name='conv4',
                 trainable=trainable))
        relu5 = relu(
            conv(relu4,
                 3,
                 3,
                 256,
                 1,
                 1,
                 group=2,
                 name='conv5',
                 trainable=trainable))
        pool5 = max_pool(relu5, 3, 3, 2, 2, pad='VALID', name='pool5')
        fc6 = relu(fc(pool5, 4096, name='fc6', trainable=trainable))
        if dropout:
            fc6 = tf.nn.dropout(fc6, keep_prob=keep_prob)
        fc7 = relu(fc(fc6, 4096, name='fc7', trainable=trainable))
        if dropout:
            fc7 = tf.nn.dropout(fc7, keep_prob=keep_prob)
        fc8 = fc(fc7, output, name='output', trainable=trainable)

    variables = tf.contrib.framework.get_variables(vs)
    return fc8, variables
Пример #7
0
def generator_caffenet_fc6(input_feat, reuse=False, trainable=False):
    with tf.variable_scope('generator', reuse=reuse) as vs:
        assert input_feat.get_shape().as_list()[-1] == 4096
        # input_feat = tf.placeholder(tf.float32, shape=(None, 4096), name='feat')

        relu_defc7 = leaky_relu(
            fc(input_feat, 4096, name='defc7', trainable=trainable))
        relu_defc6 = leaky_relu(
            fc(relu_defc7, 4096, name='defc6', trainable=trainable))
        relu_defc5 = leaky_relu(
            fc(relu_defc6, 4096, name='defc5', trainable=trainable))
        reshaped_defc5 = tf.reshape(relu_defc5, [-1, 256, 4, 4])
        relu_deconv5 = leaky_relu(
            upconv(tf.transpose(reshaped_defc5, perm=[0, 2, 3, 1]),
                   256,
                   4,
                   2,
                   'deconv5',
                   biased=True,
                   trainable=trainable))
        relu_conv5_1 = leaky_relu(
            upconv(relu_deconv5,
                   512,
                   3,
                   1,
                   'conv5_1',
                   biased=True,
                   trainable=trainable))
        relu_deconv4 = leaky_relu(
            upconv(relu_conv5_1,
                   256,
                   4,
                   2,
                   'deconv4',
                   biased=True,
                   trainable=trainable))
        relu_conv4_1 = leaky_relu(
            upconv(relu_deconv4,
                   256,
                   3,
                   1,
                   'conv4_1',
                   biased=True,
                   trainable=trainable))
        relu_deconv3 = leaky_relu(
            upconv(relu_conv4_1,
                   128,
                   4,
                   2,
                   'deconv3',
                   biased=True,
                   trainable=trainable))
        relu_conv3_1 = leaky_relu(
            upconv(relu_deconv3,
                   128,
                   3,
                   1,
                   'conv3_1',
                   biased=True,
                   trainable=trainable))
        deconv2 = leaky_relu(
            upconv(relu_conv3_1,
                   64,
                   4,
                   2,
                   'deconv2',
                   biased=True,
                   trainable=trainable))
        deconv1 = leaky_relu(
            upconv(deconv2,
                   32,
                   4,
                   2,
                   'deconv1',
                   biased=True,
                   trainable=trainable))
        deconv0 = upconv(deconv1,
                         3,
                         4,
                         2,
                         'deconv0',
                         biased=True,
                         trainable=trainable)

    variables = tf.contrib.framework.get_variables(vs)

    return deconv0, variables