예제 #1
0
def old_discriminator(inputs, cfg):
    output = tf.reshape(inputs, [-1, 3, 32, 32])
    output = optimized_res_block_disc1(output, cfg=cfg)
    output = residual_block('discriminator.2',
                            cfg.MODEL.DIM_D,
                            cfg.MODEL.DIM_D,
                            3,
                            output,
                            resample='down')
    output = residual_block('discriminator.3',
                            cfg.MODEL.DIM_D,
                            cfg.MODEL.DIM_D,
                            3,
                            output,
                            resample=None)
    output = residual_block('discriminator.4',
                            cfg.MODEL.DIM_D,
                            cfg.MODEL.DIM_D,
                            3,
                            output,
                            resample=None)
    output = tf.nn.relu(output)
    output = tf.reduce_mean(output, axis=[2, 3])
    output_wgan = linear('discriminator.Output', cfg.MODEL.DIM_D, 1, output)
    output_wgan = tf.reshape(output_wgan, [-1])
    output_acgan = linear('discriminator.ACGANOutput', cfg.MODEL.DIM_D,
                          cfg.MODEL.HASH_DIM, output)
    output_acgan = tf.nn.tanh(output_acgan)
    return output_wgan, output_acgan
예제 #2
0
        def build_feat_noise(x):
            for i in range(self.common_length):
                self.res_cnt += 1
                name = 'Res%d' % self.res_cnt
                x = residual_block(name, x, residual_dim)

            x = tf.reshape(x, [-1, np.prod(x.get_shape()[1:])])

            rec_c = ops.linear("fc_c",
                               x,
                               self.c_len,
                               activation_fn=tf.nn.sigmoid,
                               normalizer_mode=None,
                               training=self.training,
                               reuse=tf.AUTO_REUSE)

            rec_z = ops.linear("fc_z",
                               x,
                               self.z_len,
                               activation_fn=tf.nn.tanh,
                               normalizer_mode=None,
                               training=self.training,
                               reuse=tf.AUTO_REUSE)

            return tf.concat([rec_z, rec_c], axis=1)
예제 #3
0
def good_discriminator(inputs, cfg):
    output = tf.reshape(inputs, [-1, 3, 64, 64])
    output = conv2D('discriminator.Input',
                    3,
                    cfg.MODEL.DIM,
                    3,
                    output,
                    he_init=False)

    output = residual_block('discriminator.Res1',
                            cfg.MODEL.DIM,
                            2 * cfg.MODEL.DIM,
                            3,
                            output,
                            resample='down')
    output = residual_block('discriminator.Res2',
                            2 * cfg.MODEL.DIM,
                            4 * cfg.MODEL.DIM,
                            3,
                            output,
                            resample='down')
    output = residual_block('discriminator.Res3',
                            4 * cfg.MODEL.DIM,
                            8 * cfg.MODEL.DIM,
                            3,
                            output,
                            resample='down')
    output = residual_block('discriminator.Res4',
                            8 * cfg.MODEL.DIM,
                            8 * cfg.MODEL.DIM,
                            3,
                            output,
                            resample='down')

    output = tf.reshape(output, [-1, 4 * 4 * 8 * cfg.MODEL.DIM])
    output_wgan = linear('discriminator.Output', 4 * 4 * 8 * cfg.MODEL.DIM, 1,
                         output)

    output_acgan = linear('discriminator.ACGANOutput',
                          4 * 4 * 8 * cfg.MODEL.DIM, cfg.MODEL.HASH_DIM,
                          output)
    output_acgan = tf.nn.tanh(output_acgan)
    return output_wgan, output_acgan
예제 #4
0
        def highway(input_,
                    size,
                    num_layers=1,
                    bias=-2.0,
                    f=tf.nn.relu,
                    scope='Highway'):
            """Highway Network (cf. http://arxiv.org/abs/1505.00387).
            t = sigmoid(Wy + b)
            z = t * g(Wy + b) + (1 - t) * y
            where g is nonlinearity, t is transform gate, and (1 - t) is carry gate.
            """

            with tf.variable_scope(scope):
                for idx in range(num_layers):
                    g = f(linear(input_, size, scope='highway_lin_%d' % idx))

                    t = tf.sigmoid(
                        linear(input_, size, scope='highway_gate_%d' % idx) +
                        bias)

                    output = t * g + (1. - t) * input_
                    input_ = output

            return output
예제 #5
0
def build_simpleQN(s_t, action_size, target_q_t, action, learning_rate_step):
    min_delta = -1
    max_delta = 1
    learning_rate_initial = 0.0025
    learning_rate_minimum = 0.0025
    learning_rate_decay = 0.96
    learning_rate_decay_step = 100
    w = {}
    activation_fn = tf.nn.relu
    with tf.variable_scope('Q_network'):
        shape = s_t.get_shape().as_list()
        s_t_flat = tf.reshape(s_t, [-1, reduce(lambda x, y: x * y, shape[1:])])
        #l, w['l_w'], w['l_b'] = linear(s_t_flat, action_size+s_t_flat.get_shape().as_list()[-1], activation_fn=activation_fn, name='l')
        q, w['q_w'], w['q_b'] = linear(s_t_flat, action_size, name='q')
        q_summary = []
        avg_q = tf.reduce_mean(q, 0)
        for idx in range(action_size):
            q_summary.append(tf.histogram_summary('q/%s' % idx, avg_q[idx]))
        q_summary = tf.merge_summary(q_summary, 'q_summary')
    with tf.variable_scope('optimzier'):
        action_one_hot = tf.one_hot(action,
                                    action_size,
                                    1.0,
                                    0.0,
                                    name='action_one_hot')
        q_acted = tf.reduce_sum(q * action_one_hot,
                                reduction_indices=1,
                                name='q_acted')
        delta = target_q_t - q_acted
        clipped_delta = tf.clip_by_value(delta,
                                         min_delta,
                                         max_delta,
                                         name='clipped_delta')
        loss = tf.reduce_mean(tf.square(clipped_delta), name='loss')
        learning_rate = tf.maximum(
            learning_rate_minimum,
            tf.train.exponential_decay(learning_rate_initial,
                                       learning_rate_step,
                                       learning_rate_decay_step,
                                       learning_rate_decay,
                                       staircase=True))
        #optim = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)
        optim = tf.train.RMSPropOptimizer(learning_rate,
                                          momentum=0.95,
                                          epsilon=0.01).minimize(loss)
    return w, q, q_summary, optim, loss
예제 #6
0
        def build_noise_feat(x, map_size=8):
            self.map_size = map_size
            self.map_depth = 128
            # assume 64x64 target
            x = ops.linear("fc1",
                           x, (self.map_size**2) * self.map_depth,
                           activation_fn=None,
                           normalizer_mode=None,
                           training=self.training,
                           reuse=tf.AUTO_REUSE)

            x = tf.reshape(
                x, shape=[-1, self.map_size, self.map_size, self.map_depth])

            x = ops.get_norm(x,
                             name="fc1/" + self.norm_mtd,
                             training=self.training,
                             reuse=tf.AUTO_REUSE)

            x = tf.nn.relu(x)

            # upsample to 64x64x128:
            map_size = self.map_size
            map_depth = self.map_depth
            for i in range(2):
                self.conv_cnt += 1
                map_size *= 2

                lx = tf.image.resize_images(x, [map_size, map_size])
                x = ops.deconv2d("conv%d" % self.conv_cnt,
                                 x,
                                 map_depth,
                                 3,
                                 2,
                                 activation_fn=tf.nn.relu,
                                 normalizer_mode=None,
                                 training=self.training,
                                 reuse=tf.AUTO_REUSE)
                x = x + lx
                x = ops.get_norm(x,
                                 name="conv%d/%s" %
                                 (self.conv_cnt, self.norm_mtd),
                                 training=self.training,
                                 reuse=tf.AUTO_REUSE)

            return x
예제 #7
0
def good_generator(n_samples, labels, cfg, noise=None):
    if noise is None:
        noise = tf.random_normal([n_samples, 128])

    noise = tf.concat([
        tf.cast(labels, tf.float32),
        tf.slice(noise, [0, cfg.DATA.LABEL_DIM], [-1, -1])
    ], 1)
    output = linear('generator.Input', 128, 4 * 4 * 8 * cfg.MODEL.DIM, noise)
    output = tf.reshape(output, [-1, 8 * cfg.MODEL.DIM, 4, 4])

    output = residual_block('generator.Res1',
                            8 * cfg.MODEL.DIM,
                            8 * cfg.MODEL.DIM,
                            3,
                            output,
                            resample='up')
    output = residual_block('generator.Res2',
                            8 * cfg.MODEL.DIM,
                            4 * cfg.MODEL.DIM,
                            3,
                            output,
                            resample='up')
    output = residual_block('generator.Res3',
                            4 * cfg.MODEL.DIM,
                            2 * cfg.MODEL.DIM,
                            3,
                            output,
                            resample='up')
    output = residual_block('generator.Res4',
                            2 * cfg.MODEL.DIM,
                            1 * cfg.MODEL.DIM,
                            3,
                            output,
                            resample='up')

    output = normalize('generator.OutputN', output)
    output = tf.nn.relu(output)
    output = conv2D('generator.Output', 1 * cfg.MODEL.DIM, 3, 3, output)
    output = tf.tanh(output)

    return tf.reshape(output, [-1, cfg.DATA.OUTPUT_DIM])
예제 #8
0
def old_generator(n_samples, labels, cfg, noise=None):
    if noise is None:
        noise = tf.random_normal([n_samples, 256])

    # concat noise with label
    noise = tf.concat([
        tf.cast(labels, tf.float32),
        tf.slice(noise, [0, cfg.DATA.LABEL_DIM], [-1, -1])
    ], 1)
    output = linear('generator.Input', 256, 4 * 4 * cfg.MODEL.DIM_G, noise)
    output = tf.reshape(output, [-1, cfg.MODEL.DIM_G, 4, 4])
    output = residual_block('generator.1',
                            cfg.MODEL.DIM_G,
                            cfg.MODEL.DIM_G,
                            3,
                            output,
                            resample='up')
    output = residual_block('generator.2',
                            cfg.MODEL.DIM_G,
                            cfg.MODEL.DIM_G,
                            3,
                            output,
                            resample='up')
    output = residual_block('generator.3',
                            cfg.MODEL.DIM_G,
                            cfg.MODEL.DIM_G,
                            3,
                            output,
                            resample='up')
    output = normalize('generator.OutputN', output)
    output = tf.nn.relu(output)
    output = conv2D('generator.Output',
                    cfg.MODEL.DIM_G,
                    3,
                    3,
                    output,
                    he_init=False)
    output = tf.tanh(output)
    return tf.reshape(output, [-1, cfg.DATA.OUTPUT_DIM])
예제 #9
0
def build_DQN(s_t,
              action_size,
              target_q_t,
              action,
              learning_rate_step,
              cnn_format='NHWC'):

    min_delta = -1
    max_delta = 1
    learning_rate_initial = 0.00025
    learning_rate_minimum = 0.00025
    learning_rate_decay = 0.96
    learning_rate_decay_step = 50

    w = {}
    #initializer = tf.contrib.layers.xavier_initializer()
    initializer = tf.truncated_normal_initializer(0, 0.02)
    activation_fn = tf.nn.relu

    with tf.variable_scope('Q_network'):

        l1, w['l1_w'], w['l1_b'] = conv2d(s_t,
                                          32, [8, 8], [4, 4],
                                          initializer,
                                          activation_fn,
                                          cnn_format,
                                          name='l1')
        l2, w['l2_w'], w['l2_b'] = conv2d(l1,
                                          64, [4, 4], [2, 2],
                                          initializer,
                                          activation_fn,
                                          cnn_format,
                                          name='l2')
        l3, w['l3_w'], w['l3_b'] = conv2d(l2,
                                          64, [3, 3], [1, 1],
                                          initializer,
                                          activation_fn,
                                          cnn_format,
                                          name='l3')

        shape = l3.get_shape().as_list()
        l3_flat = tf.reshape(l3, [-1, reduce(lambda x, y: x * y, shape[1:])])

        l4, w['l4_w'], w['l4_b'] = linear(l3_flat,
                                          512,
                                          activation_fn=activation_fn,
                                          name='l4')
        q, w['q_w'], w['q_b'] = linear(l4, action_size, name='q')

        q_summary = []
        avg_q = tf.reduce_mean(q, 0)
        for idx in range(action_size):
            q_summary.append(tf.histogram_summary('q/%s' % idx, avg_q[idx]))
        q_summary = tf.merge_summary(q_summary, 'q_summary')

    with tf.variable_scope('optimzier'):

        action_one_hot = tf.one_hot(action,
                                    action_size,
                                    1.0,
                                    0.0,
                                    name='action_one_hot')
        q_acted = tf.reduce_sum(q * action_one_hot,
                                reduction_indices=1,
                                name='q_acted')

        delta = target_q_t - q_acted
        clipped_delta = tf.clip_by_value(delta,
                                         min_delta,
                                         max_delta,
                                         name='clipped_delta')

        loss = tf.reduce_mean(tf.square(clipped_delta), name='loss')
        learning_rate = tf.maximum(
            learning_rate_minimum,
            tf.train.exponential_decay(learning_rate_initial,
                                       learning_rate_step,
                                       learning_rate_decay_step,
                                       learning_rate_decay,
                                       staircase=True))

        optim = tf.train.RMSPropOptimizer(learning_rate,
                                          momentum=0.95,
                                          epsilon=0.01).minimize(loss)

    return w, q, q_summary, optim, loss
예제 #10
0
def alexnet_discriminator(inputs, cfg, stage="train"):
    # noinspection PyTypeChecker
    # TODO: don't load imagenet pretrained model when D_PRETRAINED_MODEL_PATH is given
    net_data = dict(
        np.load(cfg.MODEL.ALEXNET_PRETRAINED_MODEL_PATH,
                encoding='latin1').item())

    if inputs.shape[1] != 256:
        reshaped_image = preprocess_resize_scale_img(inputs,
                                                     cfg.DATA.WIDTH_HEIGHT)
    else:
        reshaped_image = inputs

    height = width = 227

    # Randomly crop a [height, width] section of each image
    if stage == "train":
        distorted_image = tf.stack([
            tf.random_crop(tf.image.random_flip_left_right(each_image),
                           [height, width, 3])
            for each_image in tf.unstack(reshaped_image)
        ])
    else:
        # Randomly crop a [height, width] section of each image
        distorted_image1 = tf.stack([
            tf.image.crop_to_bounding_box(tf.image.flip_left_right(each_image),
                                          0, 0, height, width)
            for each_image in tf.unstack(reshaped_image)
        ])
        distorted_image2 = tf.stack([
            tf.image.crop_to_bounding_box(tf.image.flip_left_right(each_image),
                                          28, 28, height, width)
            for each_image in tf.unstack(reshaped_image)
        ])
        distorted_image3 = tf.stack([
            tf.image.crop_to_bounding_box(tf.image.flip_left_right(each_image),
                                          28, 0, height, width)
            for each_image in tf.unstack(reshaped_image)
        ])
        distorted_image4 = tf.stack([
            tf.image.crop_to_bounding_box(tf.image.flip_left_right(each_image),
                                          0, 28, height, width)
            for each_image in tf.unstack(reshaped_image)
        ])
        distorted_image5 = tf.stack([
            tf.image.crop_to_bounding_box(tf.image.flip_left_right(each_image),
                                          14, 14, height, width)
            for each_image in tf.unstack(reshaped_image)
        ])

        distorted_image6 = tf.stack([
            tf.image.crop_to_bounding_box(each_image, 0, 0, height, width)
            for each_image in tf.unstack(reshaped_image)
        ])
        distorted_image7 = tf.stack([
            tf.image.crop_to_bounding_box(each_image, 28, 28, height, width)
            for each_image in tf.unstack(reshaped_image)
        ])
        distorted_image8 = tf.stack([
            tf.image.crop_to_bounding_box(each_image, 28, 0, height, width)
            for each_image in tf.unstack(reshaped_image)
        ])
        distorted_image9 = tf.stack([
            tf.image.crop_to_bounding_box(each_image, 0, 28, height, width)
            for each_image in tf.unstack(reshaped_image)
        ])
        distorted_image0 = tf.stack([
            tf.image.crop_to_bounding_box(each_image, 14, 14, height, width)
            for each_image in tf.unstack(reshaped_image)
        ])

        distorted_image = tf.concat([
            distorted_image1, distorted_image2, distorted_image3,
            distorted_image4, distorted_image5, distorted_image6,
            distorted_image7, distorted_image8, distorted_image9,
            distorted_image0
        ], 0)

    # Zero-mean input
    mean = tf.constant([103.939, 116.779, 123.68],
                       dtype=tf.float32,
                       shape=[1, 1, 1, 3],
                       name='img-mean')
    distorted_image = distorted_image - mean

    # Conv1
    # Output 96, kernel 11, stride 4
    scope = 'discriminator.conv1.'
    kernel = param(scope + 'weights', net_data['conv1'][0])
    biases = param(scope + 'biases', net_data['conv1'][1])
    conv = tf.nn.conv2d(distorted_image, kernel, [1, 4, 4, 1], padding='VALID')
    out = tf.nn.bias_add(conv, biases)
    conv1 = tf.nn.relu(out, name=scope)

    # Pool1
    pool1 = tf.nn.max_pool(conv1,
                           ksize=[1, 3, 3, 1],
                           strides=[1, 2, 2, 1],
                           padding='VALID',
                           name='pool1')

    # LRN1
    if cfg.TRAIN.WGAN_SCALE == 0:
        lrn1 = tf.nn.local_response_normalization(pool1,
                                                  depth_radius=2,
                                                  alpha=2e-05,
                                                  beta=0.75,
                                                  bias=1.0)
    else:
        lrn1 = pool1

    # Conv2
    # Output 256, pad 2, kernel 5, group 2
    scope = 'discriminator.conv2.'
    kernel = param(scope + 'weights', net_data['conv2'][0])
    biases = param(scope + 'biases', net_data['conv2'][1])
    group = 2

    def convolve(i, k):
        return tf.nn.conv2d(i, k, [1, 1, 1, 1], padding='SAME')

    input_groups = tf.split(lrn1, group, 3)
    kernel_groups = tf.split(kernel, group, 3)
    output_groups = [
        convolve(i, k) for i, k in zip(input_groups, kernel_groups)
    ]
    # Concatenate the groups
    conv = tf.concat(output_groups, 3)
    out = tf.nn.bias_add(conv, biases)
    conv2 = tf.nn.relu(out, name=scope)

    # Pool2
    pool2 = tf.nn.max_pool(conv2,
                           ksize=[1, 3, 3, 1],
                           strides=[1, 2, 2, 1],
                           padding='VALID',
                           name='pool2')

    # LRN2
    if cfg.TRAIN.WGAN_SCALE == 0:
        radius = 2
        alpha = 2e-05
        beta = 0.75
        bias = 1.0
        lrn2 = tf.nn.local_response_normalization(pool2,
                                                  depth_radius=radius,
                                                  alpha=alpha,
                                                  beta=beta,
                                                  bias=bias)
    else:
        lrn2 = pool2

    # Conv3
    # Output 384, pad 1, kernel 3
    scope = 'discriminator.conv3.'
    kernel = param(scope + 'weights', net_data['conv3'][0])
    biases = param(scope + 'biases', net_data['conv3'][1])
    conv = tf.nn.conv2d(lrn2, kernel, [1, 1, 1, 1], padding='SAME')
    out = tf.nn.bias_add(conv, biases)
    conv3 = tf.nn.relu(out, name=scope)

    # Conv4
    # Output 384, pad 1, kernel 3, group 2
    scope = 'discriminator.conv4.'
    kernel = param(scope + 'weights', net_data['conv4'][0])
    biases = param(scope + 'biases', net_data['conv4'][1])
    group = 2

    def convolve(i, k):
        return tf.nn.conv2d(i, k, [1, 1, 1, 1], padding='SAME')

    input_groups = tf.split(conv3, group, 3)
    kernel_groups = tf.split(kernel, group, 3)
    output_groups = [
        convolve(i, k) for i, k in zip(input_groups, kernel_groups)
    ]
    # Concatenate the groups
    conv = tf.concat(output_groups, 3)
    out = tf.nn.bias_add(conv, biases)
    conv4 = tf.nn.relu(out, name=scope)

    # Conv5
    # Output 256, pad 1, kernel 3, group 2
    scope = 'discriminator.conv5.'
    kernel = param(scope + 'weights', net_data['conv5'][0])
    biases = param(scope + 'biases', net_data['conv5'][1])
    group = 2

    def convolve(i, k):
        return tf.nn.conv2d(i, k, [1, 1, 1, 1], padding='SAME')

    input_groups = tf.split(conv4, group, 3)
    kernel_groups = tf.split(kernel, group, 3)
    output_groups = [
        convolve(i, k) for i, k in zip(input_groups, kernel_groups)
    ]
    # Concatenate the groups
    conv = tf.concat(output_groups, 3)
    out = tf.nn.bias_add(conv, biases)
    conv5 = tf.nn.relu(out, name=scope)

    # Pool5
    pool5 = tf.nn.max_pool(conv5,
                           ksize=[1, 3, 3, 1],
                           strides=[1, 2, 2, 1],
                           padding='VALID',
                           name='pool5')

    # FC6
    # Output 4096
    shape = int(np.prod(pool5.get_shape()[1:]))
    scope = 'discriminator.fc6.'
    fc6w = param(scope + 'weights', net_data['fc6'][0])
    fc6b = param(scope + 'biases', net_data['fc6'][1])
    pool5_flat = tf.reshape(pool5, [-1, shape])
    fc6l = tf.nn.bias_add(tf.matmul(pool5_flat, fc6w), fc6b)
    fc6 = tf.nn.dropout(tf.nn.relu(fc6l), 0.5)

    # FC7
    # Output 4096
    scope = 'discriminator.fc7.'
    fc7w = param(scope + 'weights', net_data['fc7'][0])
    fc7b = param(scope + 'biases', net_data['fc7'][1])
    fc7l = tf.nn.bias_add(tf.matmul(fc6, fc7w), fc7b)
    fc7 = tf.nn.dropout(tf.nn.relu(fc7l), 0.5)

    # FC8
    # Output output_dim
    fc8 = linear('discriminator.ACGANOutput', 4096, cfg.MODEL.HASH_DIM, fc7)
    if stage == "train":
        output = tf.nn.tanh(fc8)
    else:
        fc8_t = tf.nn.tanh(fc8)
        fc8_t = tf.concat(
            [tf.expand_dims(i, 0) for i in tf.split(fc8_t, 10, 0)], 0)
        output = tf.reduce_mean(fc8_t, 0)
    output_wgan = linear('discriminator.Output', 4096, 1, fc7)

    return output_wgan, output