Commit cf2012dd authored by Yuxin Wu's avatar Yuxin Wu

better EMA scope under existing name_scope.

parent 7c6d9b4b
...@@ -86,15 +86,17 @@ class Model(GANModelDesc): ...@@ -86,15 +86,17 @@ class Model(GANModelDesc):
def _build_graph(self, inputs): def _build_graph(self, inputs):
A, B = inputs A, B = inputs
with tf.name_scope('preprocess'):
A = tf.transpose(A / 128.0 - 1.0, [0, 3, 1, 2]) A = tf.transpose(A / 128.0 - 1.0, [0, 3, 1, 2])
B = tf.transpose(B / 128.0 - 1.0, [0, 3, 1, 2]) B = tf.transpose(B / 128.0 - 1.0, [0, 3, 1, 2])
def viz3(name, a, b, c): def viz3(name, a, b, c):
with tf.name_scope(name):
im = tf.concat([a, b, c], axis=3) im = tf.concat([a, b, c], axis=3)
im = tf.transpose(im, [0, 2, 3, 1]) im = tf.transpose(im, [0, 2, 3, 1])
im = (im + 1.0) * 128 im = (im + 1.0) * 128
im = tf.clip_by_value(im, 0, 255) im = tf.clip_by_value(im, 0, 255)
im = tf.cast(im, tf.uint8, name='viz_' + name) im = tf.cast(im, tf.uint8, name='viz')
tf.summary.image(name, im, max_outputs=50) tf.summary.image(name, im, max_outputs=50)
# use the initializers from torch # use the initializers from torch
...@@ -124,7 +126,6 @@ class Model(GANModelDesc): ...@@ -124,7 +126,6 @@ class Model(GANModelDesc):
B_dis_fake = self.discriminator(AB) B_dis_fake = self.discriminator(AB)
def LSGAN_losses(real, fake): def LSGAN_losses(real, fake):
with tf.name_scope('LSGAN_losses'):
d_real = tf.reduce_mean(tf.squared_difference(real, 0.9), name='d_real') d_real = tf.reduce_mean(tf.squared_difference(real, 0.9), name='d_real')
d_fake = tf.reduce_mean(tf.square(fake), name='d_fake') d_fake = tf.reduce_mean(tf.square(fake), name='d_fake')
d_loss = tf.multiply(d_real + d_fake, 0.5, name='d_loss') d_loss = tf.multiply(d_real + d_fake, 0.5, name='d_loss')
...@@ -133,6 +134,7 @@ class Model(GANModelDesc): ...@@ -133,6 +134,7 @@ class Model(GANModelDesc):
add_moving_summary(g_loss, d_loss) add_moving_summary(g_loss, d_loss)
return g_loss, d_loss return g_loss, d_loss
with tf.name_scope('losses'):
with tf.name_scope('LossA'): with tf.name_scope('LossA'):
# reconstruction loss # reconstruction loss
recon_loss_A = tf.reduce_mean(tf.abs(A - ABA), name='recon_loss') recon_loss_A = tf.reduce_mean(tf.abs(A - ABA), name='recon_loss')
...@@ -183,7 +185,7 @@ def get_data(datadir, isTrain=True): ...@@ -183,7 +185,7 @@ def get_data(datadir, isTrain=True):
class VisualizeTestSet(Callback): class VisualizeTestSet(Callback):
def _setup_graph(self): def _setup_graph(self):
self.pred = self.trainer.get_predictor( self.pred = self.trainer.get_predictor(
['inputA', 'inputB'], ['viz_A_recon', 'viz_B_recon']) ['inputA', 'inputB'], ['A_recon/viz', 'B_recon/viz'])
def _before_train(self): def _before_train(self):
global args global args
......
...@@ -179,10 +179,12 @@ def add_moving_summary(v, *args, **kwargs): ...@@ -179,10 +179,12 @@ def add_moving_summary(v, *args, **kwargs):
ema_var = tf.get_variable(name, shape=c.shape, dtype=c.dtype, ema_var = tf.get_variable(name, shape=c.shape, dtype=c.dtype,
initializer=tf.constant_initializer(), trainable=False) initializer=tf.constant_initializer(), trainable=False)
ns = vs.original_name_scope ns = vs.original_name_scope
with tf.name_scope(ns): # first clear NS to avoid duplicated name in variables
with tf.name_scope(None), tf.name_scope(ns):
ema_op = moving_averages.assign_moving_average( ema_op = moving_averages.assign_moving_average(
ema_var, c, decay, ema_var, c, decay,
zero_debias=True, name=name + '_EMA_apply') zero_debias=True, name=name + '_EMA_apply')
with tf.name_scope(None):
tf.summary.scalar(name + '-summary', ema_op) tf.summary.scalar(name + '-summary', ema_op)
tf.add_to_collection(coll, ema_op) tf.add_to_collection(coll, ema_op)
# TODO a new collection to summary every step? # TODO a new collection to summary every step?
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment