MCPcopy Create free account
hub / github.com/SystemErrorWang/White-box-Cartoonization / wgan_loss

Function wgan_loss

train_code/loss.py:124–146  ·  view source on GitHub ↗
(discriminator, real, fake, patch=True, 
              channel=32, name='discriminator', lambda_=2)

Source from the content-addressed store, hash-verified

122
123
124def wgan_loss(discriminator, real, fake, patch=True,
125 channel=32, name='discriminator', lambda_=2):
126 real_logits = discriminator(real, patch=patch, channel=channel, name=name, reuse=False)
127 fake_logits = discriminator(fake, patch=patch, channel=channel, name=name, reuse=True)
128
129 d_loss_real = - tf.reduce_mean(real_logits)
130 d_loss_fake = tf.reduce_mean(fake_logits)
131
132 d_loss = d_loss_real + d_loss_fake
133 g_loss = - d_loss_fake
134
135 """ Gradient Penalty """
136 # This is borrowed from https://github.com/kodalinaveen3/DRAGAN/blob/master/DRAGAN.ipynb
137 alpha = tf.random_uniform([tf.shape(real)[0], 1, 1, 1], minval=0.,maxval=1.)
138 differences = fake - real # This is different from MAGAN
139 interpolates = real + (alpha * differences)
140 inter_logit = discriminator(interpolates, channel=channel, name=name, reuse=True)
141 gradients = tf.gradients(inter_logit, [interpolates])[0]
142 slopes = tf.sqrt(tf.reduce_sum(tf.square(gradients), reduction_indices=[1]))
143 gradient_penalty = tf.reduce_mean((slopes - 1.) ** 2)
144 d_loss += lambda_ * gradient_penalty
145
146 return d_loss, g_loss
147
148
149def gan_loss(discriminator, real, fake, scale=1,channel=32, patch=False, name='discriminator'):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected