Пример #1
0
def generator(z, train_mode):
    with tf.variable_scope("generator") as scope:
        W_0 = utils.weight_variable([FLAGS.z_dim, 64 * GEN_DIMENSION / 2 * IMAGE_SIZE / 16 * IMAGE_SIZE / 16],
                                    name="W_0")
        b_0 = utils.bias_variable([64 * GEN_DIMENSION / 2 * IMAGE_SIZE / 16 * IMAGE_SIZE / 16], name="b_0")
        z_0 = tf.matmul(z, W_0) + b_0
        h_0 = tf.reshape(z_0, [-1, IMAGE_SIZE / 16, IMAGE_SIZE / 16, 64 * GEN_DIMENSION / 2])
        h_bn0 = utils.batch_norm(h_0, 64 * GEN_DIMENSION / 2, train_mode, scope="gen_bn0")
        h_relu0 = tf.nn.relu(h_bn0, name='relu0')
        utils.add_activation_summary(h_relu0)

        # W_1 = utils.weight_variable([5, 5, 64 * GEN_DIMENSION/2, 64 * GEN_DIMENSION], name="W_1")
        # b_1 = utils.bias_variable([64 * GEN_DIMENSION/2], name="b_1")
        # deconv_shape = tf.pack([tf.shape(h_relu0)[0], IMAGE_SIZE / 16, IMAGE_SIZE / 16, 64 * GEN_DIMENSION/2])
        # h_conv_t1 = utils.conv2d_transpose_strided(h_relu0, W_1, b_1, output_shape=deconv_shape)
        # h_bn1 = utils.batch_norm(h_conv_t1, 64 * GEN_DIMENSION/2, train_mode, scope="gen_bn1")
        # h_relu1 = tf.nn.relu(h_bn1, name='relu1')
        # utils.add_activation_summary(h_relu1)

        W_2 = utils.weight_variable([5, 5, 64 * GEN_DIMENSION / 4, 64 * GEN_DIMENSION / 2],
                                    name="W_2")
        b_2 = utils.bias_variable([64 * GEN_DIMENSION / 4], name="b_2")
        deconv_shape = tf.pack([tf.shape(h_relu0)[0], IMAGE_SIZE / 8, IMAGE_SIZE / 8, 64 * GEN_DIMENSION / 4])
        h_conv_t2 = utils.conv2d_transpose_strided(h_relu0, W_2, b_2, output_shape=deconv_shape)
        h_bn2 = utils.batch_norm(h_conv_t2, 64 * GEN_DIMENSION / 4, train_mode, scope="gen_bn2")
        h_relu2 = tf.nn.relu(h_bn2, name='relu2')
        utils.add_activation_summary(h_relu2)

        W_3 = utils.weight_variable([5, 5, 64 * GEN_DIMENSION / 8, 64 * GEN_DIMENSION / 4],
                                    name="W_3")
        b_3 = utils.bias_variable([64 * GEN_DIMENSION / 8], name="b_3")
        deconv_shape = tf.pack([tf.shape(h_relu2)[0], IMAGE_SIZE / 4, IMAGE_SIZE / 4, 64 * GEN_DIMENSION / 8])
        h_conv_t3 = utils.conv2d_transpose_strided(h_relu2, W_3, b_3, output_shape=deconv_shape)
        h_bn3 = utils.batch_norm(h_conv_t3, 64 * GEN_DIMENSION / 8, train_mode, scope="gen_bn3")
        h_relu3 = tf.nn.relu(h_bn3, name='relu3')
        utils.add_activation_summary(h_relu3)

        W_4 = utils.weight_variable([5, 5, 64 * GEN_DIMENSION / 16, 64 * GEN_DIMENSION / 8],
                                    name="W_4")
        b_4 = utils.bias_variable([64 * GEN_DIMENSION / 16], name="b_4")
        deconv_shape = tf.pack([tf.shape(h_relu3)[0], IMAGE_SIZE / 2, IMAGE_SIZE / 2, 64 * GEN_DIMENSION / 16])
        h_conv_t4 = utils.conv2d_transpose_strided(h_relu3, W_4, b_4, output_shape=deconv_shape)
        h_bn4 = utils.batch_norm(h_conv_t4, 64 * GEN_DIMENSION / 16, train_mode, scope="gen_bn4")
        h_relu4 = tf.nn.relu(h_bn4, name='relu4')
        utils.add_activation_summary(h_relu4)

        W_5 = utils.weight_variable([5, 5, NUM_OF_CHANNELS, 64 * GEN_DIMENSION / 16], name="W_5")
        b_5 = utils.bias_variable([NUM_OF_CHANNELS], name="b_5")
        deconv_shape = tf.pack([tf.shape(h_relu4)[0], IMAGE_SIZE, IMAGE_SIZE, NUM_OF_CHANNELS])
        h_conv_t5 = utils.conv2d_transpose_strided(h_relu4, W_5, b_5, output_shape=deconv_shape)
        pred_image = tf.nn.tanh(h_conv_t5, name='pred_image')
        utils.add_activation_summary(pred_image)

    return pred_image
Пример #2
0
def generator(z, train_mode):
    with tf.variable_scope("generator") as scope:
        W_0 = utils.weight_variable([FLAGS.z_dim, 64 * GEN_DIMENSION / 2 * IMAGE_SIZE / 16 * IMAGE_SIZE / 16],
                                    name="W_0")
        b_0 = utils.bias_variable([64 * GEN_DIMENSION / 2 * IMAGE_SIZE / 16 * IMAGE_SIZE / 16], name="b_0")
        z_0 = tf.matmul(z, W_0) + b_0
        h_0 = tf.reshape(z_0, [-1, IMAGE_SIZE / 16, IMAGE_SIZE / 16, 64 * GEN_DIMENSION / 2])
        h_bn0 = utils.batch_norm(h_0, 64 * GEN_DIMENSION / 2, train_mode, scope="gen_bn0")
        h_relu0 = tf.nn.relu(h_bn0, name='relu0')
        utils.add_activation_summary(h_relu0)

        # W_1 = utils.weight_variable([5, 5, 64 * GEN_DIMENSION/2, 64 * GEN_DIMENSION], name="W_1")
        # b_1 = utils.bias_variable([64 * GEN_DIMENSION/2], name="b_1")
        # deconv_shape = tf.pack([tf.shape(h_relu0)[0], IMAGE_SIZE / 16, IMAGE_SIZE / 16, 64 * GEN_DIMENSION/2])
        # h_conv_t1 = utils.conv2d_transpose_strided(h_relu0, W_1, b_1, output_shape=deconv_shape)
        # h_bn1 = utils.batch_norm(h_conv_t1, 64 * GEN_DIMENSION/2, train_mode, scope="gen_bn1")
        # h_relu1 = tf.nn.relu(h_bn1, name='relu1')
        # utils.add_activation_summary(h_relu1)

        W_2 = utils.weight_variable([5, 5, 64 * GEN_DIMENSION / 4, 64 * GEN_DIMENSION / 2],
                                    name="W_2")
        b_2 = utils.bias_variable([64 * GEN_DIMENSION / 4], name="b_2")
        deconv_shape = tf.pack([tf.shape(h_relu0)[0], IMAGE_SIZE / 8, IMAGE_SIZE / 8, 64 * GEN_DIMENSION / 4])
        h_conv_t2 = utils.conv2d_transpose_strided(h_relu0, W_2, b_2, output_shape=deconv_shape)
        h_bn2 = utils.batch_norm(h_conv_t2, 64 * GEN_DIMENSION / 4, train_mode, scope="gen_bn2")
        h_relu2 = tf.nn.relu(h_bn2, name='relu2')
        utils.add_activation_summary(h_relu2)

        W_3 = utils.weight_variable([5, 5, 64 * GEN_DIMENSION / 8, 64 * GEN_DIMENSION / 4],
                                    name="W_3")
        b_3 = utils.bias_variable([64 * GEN_DIMENSION / 8], name="b_3")
        deconv_shape = tf.pack([tf.shape(h_relu2)[0], IMAGE_SIZE / 4, IMAGE_SIZE / 4, 64 * GEN_DIMENSION / 8])
        h_conv_t3 = utils.conv2d_transpose_strided(h_relu2, W_3, b_3, output_shape=deconv_shape)
        h_bn3 = utils.batch_norm(h_conv_t3, 64 * GEN_DIMENSION / 8, train_mode, scope="gen_bn3")
        h_relu3 = tf.nn.relu(h_bn3, name='relu3')
        utils.add_activation_summary(h_relu3)

        W_4 = utils.weight_variable([5, 5, 64 * GEN_DIMENSION / 16, 64 * GEN_DIMENSION / 8],
                                    name="W_4")
        b_4 = utils.bias_variable([64 * GEN_DIMENSION / 16], name="b_4")
        deconv_shape = tf.pack([tf.shape(h_relu3)[0], IMAGE_SIZE / 2, IMAGE_SIZE / 2, 64 * GEN_DIMENSION / 16])
        h_conv_t4 = utils.conv2d_transpose_strided(h_relu3, W_4, b_4, output_shape=deconv_shape)
        h_bn4 = utils.batch_norm(h_conv_t4, 64 * GEN_DIMENSION / 16, train_mode, scope="gen_bn4")
        h_relu4 = tf.nn.relu(h_bn4, name='relu4')
        utils.add_activation_summary(h_relu4)

        W_5 = utils.weight_variable([5, 5, NUM_OF_CHANNELS, 64 * GEN_DIMENSION / 16], name="W_5")
        b_5 = utils.bias_variable([NUM_OF_CHANNELS], name="b_5")
        deconv_shape = tf.pack([tf.shape(h_relu4)[0], IMAGE_SIZE, IMAGE_SIZE, NUM_OF_CHANNELS])
        h_conv_t5 = utils.conv2d_transpose_strided(h_relu4, W_5, b_5, output_shape=deconv_shape)
        pred_image = tf.nn.tanh(h_conv_t5, name='pred_image')
        utils.add_activation_summary(pred_image)

    return pred_image
Пример #3
0
def vgg_net(weights, image, is_train):
    layers = ('conv1_1', 'relu1_1', 'conv1_2', 'relu1_2', 'pool1', 'conv2_1',
              'relu2_1', 'conv2_2', 'relu2_2', 'pool2', 'conv3_1', 'relu3_1',
              'conv3_2', 'relu3_2', 'conv3_3', 'relu3_3', 'conv3_4', 'relu3_4',
              'pool3', 'conv4_1', 'relu4_1', 'conv4_2', 'relu4_2', 'conv4_3',
              'relu4_3', 'conv4_4', 'relu4_4', 'pool4', 'conv5_1', 'relu5_1',
              'conv5_2', 'relu5_2', 'conv5_3', 'relu5_3', 'conv5_4', 'relu5_4')

    net = {}
    current = image
    for i, name in enumerate(layers):
        kind = name[:4]
        if kind == 'conv':
            kernels, bias = weights[i][0][0][0][0]
            # matconvnet: weights are [width, height, in_channels, out_channels]
            # tensorflow: weights are [height, width, in_channels, out_channels]
            kernels = utils.get_variable(np.transpose(kernels, (1, 0, 2, 3)),
                                         name='%s_w' % name)
            current = utils.conv2d_basic(current, kernels, None)
            current = utils.batch_norm(current,
                                       kernels.get_shape()[3], is_train,
                                       '%s_bn' % name)
        elif kind == 'relu':
            current = tf.nn.relu(current, name='%s' % name)
            if FLAGS.debug:
                utils.add_activation_summary(current)
        elif kind == 'pool':
            current = utils.avg_pool_2x2(current)
        net[name] = current

    return net
def encoder(dataset, train_mode):
    with tf.variable_scope("Encoder"):
        with tf.name_scope("enc_conv1") as scope:
            W_conv1 = utils.weight_variable_xavier_initialized([3, 3, 3, 32], name="W_conv1")
            b_conv1 = utils.bias_variable([32], name="b_conv1")
            h_conv1 = utils.conv2d_strided(dataset, W_conv1, b_conv1)
            h_bn1 = utils.batch_norm(h_conv1, 32, train_mode, scope="conv1_bn")
            h_relu1 = tf.nn.relu(h_bn1)

        with tf.name_scope("enc_conv2") as scope:
            W_conv2 = utils.weight_variable_xavier_initialized([3, 3, 32, 64], name="W_conv2")
            b_conv2 = utils.bias_variable([64], name="b_conv2")
            h_conv2 = utils.conv2d_strided(h_relu1, W_conv2, b_conv2)
            h_bn2 = utils.batch_norm(h_conv2, 64, train_mode, scope="conv2_bn")
            h_relu2 = tf.nn.relu(h_bn2)

        with tf.name_scope("enc_conv3") as scope:
            W_conv3 = utils.weight_variable_xavier_initialized([3, 3, 64, 128], name="W_conv3")
            b_conv3 = utils.bias_variable([128], name="b_conv3")
            h_conv3 = utils.conv2d_strided(h_relu2, W_conv3, b_conv3)
            h_bn3 = utils.batch_norm(h_conv3, 128, train_mode, scope="conv3_bn")
            h_relu3 = tf.nn.relu(h_bn3)

        with tf.name_scope("enc_conv4") as scope:
            W_conv4 = utils.weight_variable_xavier_initialized([3, 3, 128, 256], name="W_conv4")
            b_conv4 = utils.bias_variable([256], name="b_conv4")
            h_conv4 = utils.conv2d_strided(h_relu3, W_conv4, b_conv4)
            h_bn4 = utils.batch_norm(h_conv4, 256, train_mode, scope="conv4_bn")
            h_relu4 = tf.nn.relu(h_bn4)

        with tf.name_scope("enc_conv5") as scope:
            W_conv5 = utils.weight_variable_xavier_initialized([3, 3, 256, 512], name="W_conv5")
            b_conv5 = utils.bias_variable([512], name="b_conv5")
            h_conv5 = utils.conv2d_strided(h_relu4, W_conv5, b_conv5)
            h_bn5 = utils.batch_norm(h_conv5, 512, train_mode, scope="conv5_bn")
            h_relu5 = tf.nn.relu(h_bn5)

        with tf.name_scope("enc_fc") as scope:
            image_size = IMAGE_SIZE // 32
            h_relu5_flatten = tf.reshape(h_relu5, [-1, image_size * image_size * 512])
            W_fc = utils.weight_variable([image_size * image_size * 512, 1024], name="W_fc")
            b_fc = utils.bias_variable([1024], name="b_fc")
            encoder_val = tf.matmul(h_relu5_flatten, W_fc) + b_fc

    return encoder_val
def discriminator(input_images, train_mode):
    # dropout_prob = 1.0
    # if train_mode:
    #     dropout_prob = 0.5
    W_conv0 = utils.weight_variable([5, 5, NUM_OF_CHANNELS, 64 * 1],
                                    name="W_conv0")
    b_conv0 = utils.bias_variable([64 * 1], name="b_conv0")
    h_conv0 = utils.conv2d_strided(input_images, W_conv0, b_conv0)
    h_bn0 = h_conv0  # utils.batch_norm(h_conv0, 64 * 1, train_mode, scope="disc_bn0")
    h_relu0 = utils.leaky_relu(h_bn0, 0.2, name="h_relu0")
    utils.add_activation_summary(h_relu0)

    W_conv1 = utils.weight_variable([5, 5, 64 * 1, 64 * 2], name="W_conv1")
    b_conv1 = utils.bias_variable([64 * 2], name="b_conv1")
    h_conv1 = utils.conv2d_strided(h_relu0, W_conv1, b_conv1)
    h_bn1 = utils.batch_norm(h_conv1, 64 * 2, train_mode, scope="disc_bn1")
    h_relu1 = utils.leaky_relu(h_bn1, 0.2, name="h_relu1")
    utils.add_activation_summary(h_relu1)

    W_conv2 = utils.weight_variable([5, 5, 64 * 2, 64 * 4], name="W_conv2")
    b_conv2 = utils.bias_variable([64 * 4], name="b_conv2")
    h_conv2 = utils.conv2d_strided(h_relu1, W_conv2, b_conv2)
    h_bn2 = utils.batch_norm(h_conv2, 64 * 4, train_mode, scope="disc_bn2")
    h_relu2 = utils.leaky_relu(h_bn2, 0.2, name="h_relu2")
    utils.add_activation_summary(h_relu2)

    W_conv3 = utils.weight_variable([5, 5, 64 * 4, 64 * 8], name="W_conv3")
    b_conv3 = utils.bias_variable([64 * 8], name="b_conv3")
    h_conv3 = utils.conv2d_strided(h_relu2, W_conv3, b_conv3)
    h_bn3 = utils.batch_norm(h_conv3, 64 * 8, train_mode, scope="disc_bn3")
    h_relu3 = utils.leaky_relu(h_bn3, 0.2, name="h_relu3")
    utils.add_activation_summary(h_relu3)

    shape = h_relu3.get_shape().as_list()
    h_3 = tf.reshape(
        h_relu3,
        [FLAGS.batch_size, (IMAGE_SIZE // 16) * (IMAGE_SIZE // 16) * shape[3]])
    W_4 = utils.weight_variable([h_3.get_shape().as_list()[1], 1], name="W_4")
    b_4 = utils.bias_variable([1], name="b_4")
    h_4 = tf.matmul(h_3, W_4) + b_4

    return tf.nn.sigmoid(h_4), h_4, h_relu3
def inpainter(embedding, train_mode):
    with tf.variable_scope("context_inpainter"):
        image_size = IMAGE_SIZE // 32
        with tf.name_scope("dec_fc") as scope:
            W_fc = utils.weight_variable([1024, image_size * image_size * 512], name="W_fc")
            b_fc = utils.bias_variable([image_size * image_size * 512], name="b_fc")
            h_fc = tf.nn.relu(tf.matmul(embedding, W_fc) + b_fc)

        with tf.name_scope("dec_conv1") as scope:
            h_reshaped = tf.reshape(h_fc, tf.pack([tf.shape(h_fc)[0], image_size, image_size, 512]))
            W_conv_t1 = utils.weight_variable_xavier_initialized([3, 3, 256, 512], name="W_conv_t1")
            b_conv_t1 = utils.bias_variable([256], name="b_conv_t1")
            deconv_shape = tf.pack([tf.shape(h_reshaped)[0], 2 * image_size, 2 * image_size, 256])
            h_conv_t1 = utils.conv2d_transpose_strided(h_reshaped, W_conv_t1, b_conv_t1, output_shape=deconv_shape)
            h_bn_t1 = utils.batch_norm(h_conv_t1, 256, train_mode, scope="conv_t1_bn")
            h_relu_t1 = tf.nn.relu(h_bn_t1)

        with tf.name_scope("dec_conv2") as scope:
            W_conv_t2 = utils.weight_variable_xavier_initialized([3, 3, 128, 256], name="W_conv_t2")
            b_conv_t2 = utils.bias_variable([128], name="b_conv_t2")
            deconv_shape = tf.pack([tf.shape(h_relu_t1)[0], 4 * image_size, 4 * image_size, 128])
            h_conv_t2 = utils.conv2d_transpose_strided(h_relu_t1, W_conv_t2, b_conv_t2, output_shape=deconv_shape)
            h_bn_t2 = utils.batch_norm(h_conv_t2, 128, train_mode, scope="conv_t2_bn")
            h_relu_t2 = tf.nn.relu(h_bn_t2)

        with tf.name_scope("dec_conv3") as scope:
            W_conv_t3 = utils.weight_variable_xavier_initialized([3, 3, 64, 128], name="W_conv_t3")
            b_conv_t3 = utils.bias_variable([64], name="b_conv_t3")
            deconv_shape = tf.pack([tf.shape(h_relu_t2)[0], 8 * image_size, 8 * image_size, 64])
            h_conv_t3 = utils.conv2d_transpose_strided(h_relu_t2, W_conv_t3, b_conv_t3, output_shape=deconv_shape)
            h_bn_t3 = utils.batch_norm(h_conv_t3, 64, train_mode, scope="conv_t3_bn")
            h_relu_t3 = tf.nn.relu(h_bn_t3)

        with tf.name_scope("dec_conv4") as scope:
            W_conv_t4 = utils.weight_variable_xavier_initialized([3, 3, 3, 64], name="W_conv_t4")
            b_conv_t4 = utils.bias_variable([3], name="b_conv_t4")
            deconv_shape = tf.pack([tf.shape(h_relu_t3)[0], 16 * image_size, 16 * image_size, 3])
            pred_image = utils.conv2d_transpose_strided(h_relu_t3, W_conv_t4, b_conv_t4, output_shape=deconv_shape)
    return pred_image
Пример #7
0
def discriminator(input_images, train_mode):
    # dropout_prob = 1.0
    # if train_mode:
    #     dropout_prob = 0.5
    W_conv0 = utils.weight_variable([5, 5, NUM_OF_CHANNELS, 64 * 1], name="W_conv0")
    b_conv0 = utils.bias_variable([64 * 1], name="b_conv0")
    h_conv0 = utils.conv2d_strided(input_images, W_conv0, b_conv0)
    h_bn0 = h_conv0  # utils.batch_norm(h_conv0, 64 * 1, train_mode, scope="disc_bn0")
    h_relu0 = utils.leaky_relu(h_bn0, 0.2, name="h_relu0")
    utils.add_activation_summary(h_relu0)

    W_conv1 = utils.weight_variable([5, 5, 64 * 1, 64 * 2], name="W_conv1")
    b_conv1 = utils.bias_variable([64 * 2], name="b_conv1")
    h_conv1 = utils.conv2d_strided(h_relu0, W_conv1, b_conv1)
    h_bn1 = utils.batch_norm(h_conv1, 64 * 2, train_mode, scope="disc_bn1")
    h_relu1 = utils.leaky_relu(h_bn1, 0.2, name="h_relu1")
    utils.add_activation_summary(h_relu1)

    W_conv2 = utils.weight_variable([5, 5, 64 * 2, 64 * 4], name="W_conv2")
    b_conv2 = utils.bias_variable([64 * 4], name="b_conv2")
    h_conv2 = utils.conv2d_strided(h_relu1, W_conv2, b_conv2)
    h_bn2 = utils.batch_norm(h_conv2, 64 * 4, train_mode, scope="disc_bn2")
    h_relu2 = utils.leaky_relu(h_bn2, 0.2, name="h_relu2")
    utils.add_activation_summary(h_relu2)

    W_conv3 = utils.weight_variable([5, 5, 64 * 4, 64 * 8], name="W_conv3")
    b_conv3 = utils.bias_variable([64 * 8], name="b_conv3")
    h_conv3 = utils.conv2d_strided(h_relu2, W_conv3, b_conv3)
    h_bn3 = utils.batch_norm(h_conv3, 64 * 8, train_mode, scope="disc_bn3")
    h_relu3 = utils.leaky_relu(h_bn3, 0.2, name="h_relu3")
    utils.add_activation_summary(h_relu3)

    shape = h_relu3.get_shape().as_list()
    h_3 = tf.reshape(h_relu3, [FLAGS.batch_size, (IMAGE_SIZE // 16) * (IMAGE_SIZE // 16) * shape[3]])
    W_4 = utils.weight_variable([h_3.get_shape().as_list()[1], 1], name="W_4")
    b_4 = utils.bias_variable([1], name="b_4")
    h_4 = tf.matmul(h_3, W_4) + b_4

    return tf.nn.sigmoid(h_4), h_4, h_relu3
Пример #8
0
def u_net(image, phase_train):
    with tf.variable_scope("u_net"):
        w1_1 = utils.weight_variable([3, 3, int(image.shape[3]), 32],
                                     name="w1_1")
        b1_1 = utils.bias_variable([32], name="b1_1")
        conv1_1 = utils.conv2d_basic(image, w1_1, b1_1)
        relu1_1 = tf.nn.relu(conv1_1, name="relu1_1")
        w1_2 = utils.weight_variable([3, 3, 32, 32], name="w1_2")
        b1_2 = utils.bias_variable([32], name="b1_2")
        conv1_2 = utils.conv2d_basic(relu1_1, w1_2, b1_2)
        relu1_2 = tf.nn.relu(conv1_2, name="relu1_2")
        pool1 = utils.max_pool_2x2(relu1_2)
        bn1 = utils.batch_norm(pool1,
                               pool1.get_shape()[3],
                               phase_train,
                               scope="bn1")

        w2_1 = utils.weight_variable([3, 3, 32, 64], name="w2_1")
        b2_1 = utils.bias_variable([64], name="b2_1")
        conv2_1 = utils.conv2d_basic(bn1, w2_1, b2_1)
        relu2_1 = tf.nn.relu(conv2_1, name="relu2_1")
        w2_2 = utils.weight_variable([3, 3, 64, 64], name="w2_2")
        b2_2 = utils.bias_variable([64], name="b2_2")
        conv2_2 = utils.conv2d_basic(relu2_1, w2_2, b2_2)
        relu2_2 = tf.nn.relu(conv2_2, name="relu2_2")
        pool2 = utils.max_pool_2x2(relu2_2)
        bn2 = utils.batch_norm(pool2,
                               pool2.get_shape()[3],
                               phase_train,
                               scope="bn2")

        w3_1 = utils.weight_variable([3, 3, 64, 128], name="w3_1")
        b3_1 = utils.bias_variable([128], name="b3_1")
        conv3_1 = utils.conv2d_basic(bn2, w3_1, b3_1)
        relu3_1 = tf.nn.relu(conv3_1, name="relu3_1")
        w3_2 = utils.weight_variable([3, 3, 128, 128], name="w3_2")
        b3_2 = utils.bias_variable([128], name="b3_2")
        conv3_2 = utils.conv2d_basic(relu3_1, w3_2, b3_2)
        relu3_2 = tf.nn.relu(conv3_2, name="relu3_2")
        pool3 = utils.max_pool_2x2(relu3_2)
        bn3 = utils.batch_norm(pool3,
                               pool3.get_shape()[3],
                               phase_train,
                               scope="bn3")

        w4_1 = utils.weight_variable([3, 3, 128, 256], name="w4_1")
        b4_1 = utils.bias_variable([256], name="b4_1")
        conv4_1 = utils.conv2d_basic(bn3, w4_1, b4_1)
        relu4_1 = tf.nn.relu(conv4_1, name="relu4_1")
        w4_2 = utils.weight_variable([3, 3, 256, 256], name="w4_2")
        b4_2 = utils.bias_variable([256], name="b4_2")
        conv4_2 = utils.conv2d_basic(relu4_1, w4_2, b4_2)
        relu4_2 = tf.nn.relu(conv4_2, name="relu4_2")
        pool4 = utils.max_pool_2x2(relu4_2)
        bn4 = utils.batch_norm(pool4,
                               pool4.get_shape()[3],
                               phase_train,
                               scope="bn4")

        w5_1 = utils.weight_variable([3, 3, 256, 512], name="w5_1")
        b5_1 = utils.bias_variable([512], name="b5_1")
        conv5_1 = utils.conv2d_basic(bn4, w5_1, b5_1)
        relu5_1 = tf.nn.relu(conv5_1, name="relu5_1")
        w5_2 = utils.weight_variable([3, 3, 512, 512], name="w5_2")
        b5_2 = utils.bias_variable([512], name="b5_2")
        conv5_2 = utils.conv2d_basic(relu5_1, w5_2, b5_2)
        relu5_2 = tf.nn.relu(conv5_2, name="relu5_2")
        bn5 = utils.batch_norm(relu5_2,
                               relu5_2.get_shape()[3],
                               phase_train,
                               scope="bn5")
        ###up6
        W_t1 = utils.weight_variable([2, 2, 256, 512], name="W_t1")
        b_t1 = utils.bias_variable([256], name="b_t1")
        conv_t1 = utils.conv2d_transpose_strided(
            bn5, W_t1, b_t1, output_shape=tf.shape(relu4_2))
        merge1 = tf.concat([conv_t1, relu4_2], 3)
        w6_1 = utils.weight_variable([3, 3, 512, 256], name="w6_1")
        b6_1 = utils.bias_variable([256], name="b6_1")
        conv6_1 = utils.conv2d_basic(merge1, w6_1, b6_1)
        relu6_1 = tf.nn.relu(conv6_1, name="relu6_1")
        w6_2 = utils.weight_variable([3, 3, 256, 256], name="w6_2")
        b6_2 = utils.bias_variable([256], name="b6_2")
        conv6_2 = utils.conv2d_basic(relu6_1, w6_2, b6_2)
        relu6_2 = tf.nn.relu(conv6_2, name="relu6_2")
        bn6 = utils.batch_norm(relu6_2,
                               relu6_2.get_shape()[3],
                               phase_train,
                               scope="bn6")
        ###up7
        W_t2 = utils.weight_variable([2, 2, 128, 256], name="W_t2")
        b_t2 = utils.bias_variable([128], name="b_t2")
        conv_t2 = utils.conv2d_transpose_strided(
            bn6, W_t2, b_t2, output_shape=tf.shape(relu3_2))
        merge2 = tf.concat([conv_t2, relu3_2], 3)
        w7_1 = utils.weight_variable([3, 3, 256, 128], name="w7_1")
        b7_1 = utils.bias_variable([128], name="b7_1")
        conv7_1 = utils.conv2d_basic(merge2, w7_1, b7_1)
        relu7_1 = tf.nn.relu(conv7_1, name="relu7_1")
        w7_2 = utils.weight_variable([3, 3, 128, 128], name="w7_2")
        b7_2 = utils.bias_variable([128], name="b7_2")
        conv7_2 = utils.conv2d_basic(relu7_1, w7_2, b7_2)
        relu7_2 = tf.nn.relu(conv7_2, name="relu7_2")
        bn7 = utils.batch_norm(relu7_2,
                               relu7_2.get_shape()[3],
                               phase_train,
                               scope="bn7")
        ###up8
        W_t3 = utils.weight_variable([2, 2, 64, 128], name="W_t3")
        b_t3 = utils.bias_variable([64], name="b_t3")
        conv_t3 = utils.conv2d_transpose_strided(
            bn7, W_t3, b_t3, output_shape=tf.shape(relu2_2))
        merge3 = tf.concat([conv_t3, relu2_2], 3)
        w8_1 = utils.weight_variable([3, 3, 128, 64], name="w8_1")
        b8_1 = utils.bias_variable([64], name="b8_1")
        conv8_1 = utils.conv2d_basic(merge3, w8_1, b8_1)
        relu8_1 = tf.nn.relu(conv8_1, name="relu8_1")
        w8_2 = utils.weight_variable([3, 3, 64, 64], name="w8_2")
        b8_2 = utils.bias_variable([64], name="b8_2")
        conv8_2 = utils.conv2d_basic(relu8_1, w8_2, b8_2)
        relu8_2 = tf.nn.relu(conv8_2, name="relu8_2")
        bn8 = utils.batch_norm(relu8_2,
                               relu8_2.get_shape()[3],
                               phase_train,
                               scope="bn8")
        ###up9
        W_t4 = utils.weight_variable([2, 2, 32, 64], name="W_t4")
        b_t4 = utils.bias_variable([32], name="b_t4")
        conv_t4 = utils.conv2d_transpose_strided(
            bn8, W_t4, b_t4, output_shape=tf.shape(relu1_2))
        merge4 = tf.concat([conv_t4, relu1_2], 3)
        w9_1 = utils.weight_variable([3, 3, 64, 32], name="w9_1")
        b9_1 = utils.bias_variable([32], name="b9_1")
        conv9_1 = utils.conv2d_basic(merge4, w9_1, b9_1)
        relu9_1 = tf.nn.relu(conv9_1, name="relu9_1")
        w9_2 = utils.weight_variable([3, 3, 32, 32], name="w9_2")
        b9_2 = utils.bias_variable([32], name="b9_2")
        conv9_2 = utils.conv2d_basic(relu9_1, w9_2, b9_2)
        relu9_2 = tf.nn.relu(conv9_2, name="relu9_2")
        bn9 = utils.batch_norm(relu9_2,
                               relu9_2.get_shape()[3],
                               phase_train,
                               scope="bn9")

        ###output scoreMap
        w10 = utils.weight_variable([1, 1, 32, NUM_OF_CLASSESS], name="w10")
        b10 = utils.bias_variable([NUM_OF_CLASSESS], name="b10")
        conv10 = utils.conv2d_basic(bn9, w10, b10)
        annotation_pred = tf.argmax(conv10, dimension=3, name="prediction")
        return annotation_pred, conv10
def inpainter(embedding, train_mode):
    with tf.variable_scope("context_inpainter"):
        image_size = IMAGE_SIZE // 32
        with tf.name_scope("dec_fc") as scope:
            W_fc = utils.weight_variable([1024, image_size * image_size * 512],
                                         name="W_fc")
            b_fc = utils.bias_variable([image_size * image_size * 512],
                                       name="b_fc")
            h_fc = tf.nn.relu(tf.matmul(embedding, W_fc) + b_fc)

        with tf.name_scope("dec_conv1") as scope:
            h_reshaped = tf.reshape(
                h_fc, tf.pack([tf.shape(h_fc)[0], image_size, image_size,
                               512]))
            W_conv_t1 = utils.weight_variable_xavier_initialized(
                [3, 3, 256, 512], name="W_conv_t1")
            b_conv_t1 = utils.bias_variable([256], name="b_conv_t1")
            deconv_shape = tf.pack(
                [tf.shape(h_reshaped)[0], 2 * image_size, 2 * image_size, 256])
            h_conv_t1 = utils.conv2d_transpose_strided(
                h_reshaped, W_conv_t1, b_conv_t1, output_shape=deconv_shape)
            h_bn_t1 = utils.batch_norm(h_conv_t1,
                                       256,
                                       train_mode,
                                       scope="conv_t1_bn")
            h_relu_t1 = tf.nn.relu(h_bn_t1)

        with tf.name_scope("dec_conv2") as scope:
            W_conv_t2 = utils.weight_variable_xavier_initialized(
                [3, 3, 128, 256], name="W_conv_t2")
            b_conv_t2 = utils.bias_variable([128], name="b_conv_t2")
            deconv_shape = tf.pack(
                [tf.shape(h_relu_t1)[0], 4 * image_size, 4 * image_size, 128])
            h_conv_t2 = utils.conv2d_transpose_strided(
                h_relu_t1, W_conv_t2, b_conv_t2, output_shape=deconv_shape)
            h_bn_t2 = utils.batch_norm(h_conv_t2,
                                       128,
                                       train_mode,
                                       scope="conv_t2_bn")
            h_relu_t2 = tf.nn.relu(h_bn_t2)

        with tf.name_scope("dec_conv3") as scope:
            W_conv_t3 = utils.weight_variable_xavier_initialized(
                [3, 3, 64, 128], name="W_conv_t3")
            b_conv_t3 = utils.bias_variable([64], name="b_conv_t3")
            deconv_shape = tf.pack(
                [tf.shape(h_relu_t2)[0], 8 * image_size, 8 * image_size, 64])
            h_conv_t3 = utils.conv2d_transpose_strided(
                h_relu_t2, W_conv_t3, b_conv_t3, output_shape=deconv_shape)
            h_bn_t3 = utils.batch_norm(h_conv_t3,
                                       64,
                                       train_mode,
                                       scope="conv_t3_bn")
            h_relu_t3 = tf.nn.relu(h_bn_t3)

        with tf.name_scope("dec_conv4") as scope:
            W_conv_t4 = utils.weight_variable_xavier_initialized(
                [3, 3, 3, 64], name="W_conv_t4")
            b_conv_t4 = utils.bias_variable([3], name="b_conv_t4")
            deconv_shape = tf.pack(
                [tf.shape(h_relu_t3)[0], 16 * image_size, 16 * image_size, 3])
            pred_image = utils.conv2d_transpose_strided(
                h_relu_t3, W_conv_t4, b_conv_t4, output_shape=deconv_shape)
    return pred_image
def encoder(dataset, train_mode):
    with tf.variable_scope("Encoder"):
        with tf.name_scope("enc_conv1") as scope:
            W_conv1 = utils.weight_variable_xavier_initialized([3, 3, 3, 32],
                                                               name="W_conv1")
            b_conv1 = utils.bias_variable([32], name="b_conv1")
            h_conv1 = utils.conv2d_strided(dataset, W_conv1, b_conv1)
            h_bn1 = utils.batch_norm(h_conv1, 32, train_mode, scope="conv1_bn")
            h_relu1 = tf.nn.relu(h_bn1)

        with tf.name_scope("enc_conv2") as scope:
            W_conv2 = utils.weight_variable_xavier_initialized([3, 3, 32, 64],
                                                               name="W_conv2")
            b_conv2 = utils.bias_variable([64], name="b_conv2")
            h_conv2 = utils.conv2d_strided(h_relu1, W_conv2, b_conv2)
            h_bn2 = utils.batch_norm(h_conv2, 64, train_mode, scope="conv2_bn")
            h_relu2 = tf.nn.relu(h_bn2)

        with tf.name_scope("enc_conv3") as scope:
            W_conv3 = utils.weight_variable_xavier_initialized([3, 3, 64, 128],
                                                               name="W_conv3")
            b_conv3 = utils.bias_variable([128], name="b_conv3")
            h_conv3 = utils.conv2d_strided(h_relu2, W_conv3, b_conv3)
            h_bn3 = utils.batch_norm(h_conv3,
                                     128,
                                     train_mode,
                                     scope="conv3_bn")
            h_relu3 = tf.nn.relu(h_bn3)

        with tf.name_scope("enc_conv4") as scope:
            W_conv4 = utils.weight_variable_xavier_initialized(
                [3, 3, 128, 256], name="W_conv4")
            b_conv4 = utils.bias_variable([256], name="b_conv4")
            h_conv4 = utils.conv2d_strided(h_relu3, W_conv4, b_conv4)
            h_bn4 = utils.batch_norm(h_conv4,
                                     256,
                                     train_mode,
                                     scope="conv4_bn")
            h_relu4 = tf.nn.relu(h_bn4)

        with tf.name_scope("enc_conv5") as scope:
            W_conv5 = utils.weight_variable_xavier_initialized(
                [3, 3, 256, 512], name="W_conv5")
            b_conv5 = utils.bias_variable([512], name="b_conv5")
            h_conv5 = utils.conv2d_strided(h_relu4, W_conv5, b_conv5)
            h_bn5 = utils.batch_norm(h_conv5,
                                     512,
                                     train_mode,
                                     scope="conv5_bn")
            h_relu5 = tf.nn.relu(h_bn5)

        with tf.name_scope("enc_fc") as scope:
            image_size = IMAGE_SIZE // 32
            h_relu5_flatten = tf.reshape(h_relu5,
                                         [-1, image_size * image_size * 512])
            W_fc = utils.weight_variable([image_size * image_size * 512, 1024],
                                         name="W_fc")
            b_fc = utils.bias_variable([1024], name="b_fc")
            encoder_val = tf.matmul(h_relu5_flatten, W_fc) + b_fc

    return encoder_val