MCPcopy Create free account
hub / github.com/tensorpack/tensorpack / build_graph

Method build_graph

examples/GAN/CycleGAN.py:88–153  ·  view source on GitHub ↗
(self, A, B)

Source from the content-addressed store, hash-verified

86 return l
87
88 def build_graph(self, A, B):
89 with tf.name_scope('preprocess'):
90 A = tf.transpose(A / 128.0 - 1.0, [0, 3, 1, 2])
91 B = tf.transpose(B / 128.0 - 1.0, [0, 3, 1, 2])
92
93 def viz3(name, a, b, c):
94 with tf.name_scope(name):
95 im = tf.concat([a, b, c], axis=3)
96 im = tf.transpose(im, [0, 2, 3, 1])
97 im = (im + 1.0) * 128
98 im = tf.clip_by_value(im, 0, 255)
99 im = tf.cast(im, tf.uint8, name='viz')
100 tf.summary.image(name, im, max_outputs=50)
101
102 # use the initializers from torch
103 with argscope([Conv2D, Conv2DTranspose], use_bias=False,
104 kernel_initializer=tf.random_normal_initializer(stddev=0.02)), \
105 argscope([Conv2D, Conv2DTranspose, InstanceNorm], data_format='channels_first'):
106 with tf.variable_scope('gen'):
107 with tf.variable_scope('B'):
108 AB = self.generator(A)
109 with tf.variable_scope('A'):
110 BA = self.generator(B)
111 ABA = self.generator(AB)
112 with tf.variable_scope('B'):
113 BAB = self.generator(BA)
114
115 viz3('A_recon', A, AB, ABA)
116 viz3('B_recon', B, BA, BAB)
117
118 with tf.variable_scope('discrim'):
119 with tf.variable_scope('A'):
120 A_dis_real = self.discriminator(A)
121 A_dis_fake = self.discriminator(BA)
122
123 with tf.variable_scope('B'):
124 B_dis_real = self.discriminator(B)
125 B_dis_fake = self.discriminator(AB)
126
127 def LSGAN_losses(real, fake):
128 d_real = tf.reduce_mean(tf.squared_difference(real, 1), name='d_real')
129 d_fake = tf.reduce_mean(tf.square(fake), name='d_fake')
130 d_loss = tf.multiply(d_real + d_fake, 0.5, name='d_loss')
131
132 g_loss = tf.reduce_mean(tf.squared_difference(fake, 1), name='g_loss')
133 add_moving_summary(g_loss, d_loss)
134 return g_loss, d_loss
135
136 with tf.name_scope('losses'):
137 with tf.name_scope('LossA'):
138 # reconstruction loss
139 recon_loss_A = tf.reduce_mean(tf.abs(A - ABA), name='recon_loss')
140 # gan loss
141 G_loss_A, D_loss_A = LSGAN_losses(A_dis_real, A_dis_fake)
142
143 with tf.name_scope('LossB'):
144 recon_loss_B = tf.reduce_mean(tf.abs(B - BAB), name='recon_loss')
145 G_loss_B, D_loss_B = LSGAN_losses(B_dis_real, B_dis_fake)

Callers

nothing calls this directly

Calls 6

generatorMethod · 0.95
discriminatorMethod · 0.95
add_moving_summaryFunction · 0.90
argscopeFunction · 0.85
addMethod · 0.45
collect_variablesMethod · 0.45

Tested by

no test coverage detected