Exemplo n.º 1
0
    def _build_graph(self, input_vars):
        input, output = input_vars
        input, output = input / 128.0 - 1, output / 128.0 - 1

        with argscope([Conv2D, Deconv2D],
                W_init=tf.truncated_normal_initializer(stddev=0.02)), \
                argscope(LeakyReLU, alpha=0.2):
            with tf.variable_scope('gen'):
                fake_output = self.generator(input)
            with tf.variable_scope('discrim'):
                real_pred = self.discriminator(input, output)
            with tf.variable_scope('discrim', reuse=True):
                fake_pred = self.discriminator(input, fake_output)

        self.g_loss, self.d_loss = build_GAN_losses(real_pred, fake_pred)
        errL1 = tf.reduce_mean(tf.abs(fake_output - output), name='L1_loss')
        self.g_loss = tf.add(self.g_loss, LAMBDA * errL1, name='total_g_loss')
        add_moving_summary(errL1, self.g_loss)

        # tensorboard visualization
        if IN_CH == 1:
            input = tf.image.grayscale_to_rgb(input)
        if OUT_CH == 1:
            output = tf.image.grayscale_to_rgb(output)
            fake_output = tf.image.grayscale_to_rgb(fake_output)
        viz = (tf.concat(2, [input, output, fake_output]) + 1.0) * 128.0
        viz = tf.cast(viz, tf.uint8, name='viz')
        tf.image_summary('gen', viz, max_images=max(30, BATCH))

        all_vars = tf.trainable_variables()
        self.g_vars = [v for v in all_vars if v.name.startswith('gen/')]
        self.d_vars = [v for v in all_vars if v.name.startswith('discrim/')]
Exemplo n.º 2
0
    def _build_graph(self, input_vars):
        image_pos = input_vars[0]
        image_pos = tf.expand_dims(image_pos * 2.0 - 1, -1)

        prior_prob = tf.constant([0.1] * 10, name='prior_prob')
        # assume first 10 is categorical
        ids = tf.multinomial(tf.zeros([BATCH, 10]), num_samples=1)[:, 0]
        zc = tf.one_hot(ids, 10, name='zc_train')
        zc = tf.placeholder_with_default(zc, [None, 10], name='zc')

        z = tf.random_uniform(tf.pack([tf.shape(zc)[0], 90]),
                              -1,
                              1,
                              name='z_train')
        z = tf.placeholder_with_default(z, [None, 90], name='z')
        z = tf.concat(1, [zc, z], name='fullz')

        with argscope([Conv2D, Deconv2D, FullyConnected],
                      W_init=tf.truncated_normal_initializer(stddev=0.02)):
            with tf.variable_scope('gen'):
                image_gen = self.generator(z)
                tf.image_summary('gen', image_gen, max_images=30)
            with tf.variable_scope('discrim'):
                vecpos, _ = self.discriminator(image_pos)
            with tf.variable_scope('discrim', reuse=True):
                vecneg, dist_param = self.discriminator(image_gen)
                logprob = tf.nn.log_softmax(
                    dist_param)  # log prob of each category

        # Q(c|x) = Q(zc | image_gen)
        log_qc_given_x = tf.reduce_sum(logprob * zc, 1, name='logQc_x')  # bx1
        log_qc = tf.reduce_sum(prior_prob * zc, 1, name='logQc')
        Elog_qc_given_x = tf.reduce_mean(log_qc_given_x, name='ElogQc_x')
        Hc = tf.reduce_mean(-log_qc, name='Hc')
        MIloss = tf.mul(Hc + Elog_qc_given_x, -1.0, name='neg_MI')

        self.g_loss, self.d_loss = build_GAN_losses(vecpos, vecneg)
        self.g_loss = tf.add(self.g_loss, MIloss, name='total_g_loss')
        self.d_loss = tf.add(self.d_loss, MIloss, name='total_g_loss')
        summary.add_moving_summary(MIloss, self.g_loss, self.d_loss, Hc,
                                   Elog_qc_given_x)

        all_vars = tf.trainable_variables()
        self.g_vars = [v for v in all_vars if v.name.startswith('gen/')]
        self.d_vars = [v for v in all_vars if v.name.startswith('discrim/')]
Exemplo n.º 3
0
    def _build_graph(self, input_vars):
        image_pos = input_vars[0]
        image_pos = image_pos / 128.0 - 1

        z = tf.random_uniform([CFG.BATCH, CFG.Z_DIM], -1, 1, name='z_train')
        z = tf.placeholder_with_default(z, [None, CFG.Z_DIM], name='z')

        with argscope([Conv2D, Deconv2D, FullyConnected],
                      W_init=tf.truncated_normal_initializer(stddev=0.02)):
            with tf.variable_scope('gen'):
                image_gen = self.generator(z)
                tf.image_summary('gen', image_gen, max_images=30)
            with tf.variable_scope('discrim'):
                vecpos = self.discriminator(image_pos)
            with tf.variable_scope('discrim', reuse=True):
                vecneg = self.discriminator(image_gen)

        self.g_loss, self.d_loss = build_GAN_losses(vecpos, vecneg)
        all_vars = tf.trainable_variables()
        self.g_vars = [v for v in all_vars if v.name.startswith('gen/')]
        self.d_vars = [v for v in all_vars if v.name.startswith('discrim/')]