| 165 | |
| 166 | |
| 167 | class DCGAN: |
| 168 | def __init__(self, img_length, num_colors, d_sizes, g_sizes): |
| 169 | |
| 170 | # save for later |
| 171 | self.img_length = img_length |
| 172 | self.num_colors = num_colors |
| 173 | self.latent_dims = g_sizes['z'] |
| 174 | |
| 175 | # define the input data |
| 176 | self.X = tf.placeholder( |
| 177 | tf.float32, |
| 178 | shape=(None, img_length, img_length, num_colors), |
| 179 | name='X' |
| 180 | ) |
| 181 | self.Z = tf.placeholder( |
| 182 | tf.float32, |
| 183 | shape=(None, self.latent_dims), |
| 184 | name='Z' |
| 185 | ) |
| 186 | |
| 187 | # note: by making batch_sz a placeholder, we can specify a variable |
| 188 | # number of samples in the FS-conv operation where we are required |
| 189 | # to pass in output_shape |
| 190 | # we need only pass in the batch size via feed_dict |
| 191 | self.batch_sz = tf.placeholder(tf.int32, shape=(), name='batch_sz') |
| 192 | |
| 193 | # build the discriminator |
| 194 | logits = self.build_discriminator(self.X, d_sizes) |
| 195 | |
| 196 | # build generator |
| 197 | self.sample_images = self.build_generator(self.Z, g_sizes) |
| 198 | |
| 199 | # get sample logits |
| 200 | with tf.variable_scope("discriminator") as scope: |
| 201 | scope.reuse_variables() |
| 202 | sample_logits = self.d_forward(self.sample_images, True) |
| 203 | |
| 204 | # get sample images for test time (batch norm is different) |
| 205 | with tf.variable_scope("generator") as scope: |
| 206 | scope.reuse_variables() |
| 207 | self.sample_images_test = self.g_forward( |
| 208 | self.Z, reuse=True, is_training=False |
| 209 | ) |
| 210 | |
| 211 | # build costs |
| 212 | self.d_cost_real = tf.nn.sigmoid_cross_entropy_with_logits( |
| 213 | logits=logits, |
| 214 | labels=tf.ones_like(logits) |
| 215 | ) |
| 216 | self.d_cost_fake = tf.nn.sigmoid_cross_entropy_with_logits( |
| 217 | logits=sample_logits, |
| 218 | labels=tf.zeros_like(sample_logits) |
| 219 | ) |
| 220 | self.d_cost = tf.reduce_mean(self.d_cost_real) + tf.reduce_mean(self.d_cost_fake) |
| 221 | self.g_cost = tf.reduce_mean( |
| 222 | tf.nn.sigmoid_cross_entropy_with_logits( |
| 223 | logits=sample_logits, |
| 224 | labels=tf.ones_like(sample_logits) |