| 40 | |
| 41 | |
| 42 | class Model(GANModelDesc): |
| 43 | def inputs(self): |
| 44 | return [tf.TensorSpec((None, 28, 28), tf.float32, 'input'), |
| 45 | tf.TensorSpec((None,), tf.int32, 'label')] |
| 46 | |
| 47 | def generator(self, z, y): |
| 48 | l = FullyConnected('fc0', tf.concat([z, y], 1), 1024, activation=BNReLU) |
| 49 | l = FullyConnected('fc1', tf.concat([l, y], 1), 64 * 2 * 7 * 7, activation=BNReLU) |
| 50 | l = tf.reshape(l, [-1, 7, 7, 64 * 2]) |
| 51 | |
| 52 | y = tf.reshape(y, [-1, 1, 1, 10]) |
| 53 | l = tf.concat([l, tf.tile(y, [1, 7, 7, 1])], 3) |
| 54 | l = Conv2DTranspose('deconv1', l, 64 * 2, 5, 2, activation=BNReLU) |
| 55 | |
| 56 | l = tf.concat([l, tf.tile(y, [1, 14, 14, 1])], 3) |
| 57 | l = Conv2DTranspose('deconv2', l, 1, 5, 2, activation=tf.identity) |
| 58 | l = tf.nn.tanh(l, name='gen') |
| 59 | return l |
| 60 | |
| 61 | @auto_reuse_variable_scope |
| 62 | def discriminator(self, imgs, y): |
| 63 | """ return a (b, 1) logits""" |
| 64 | yv = y |
| 65 | y = tf.reshape(y, [-1, 1, 1, 10]) |
| 66 | with argscope(Conv2D, kernel_size=5, strides=1): |
| 67 | l = (LinearWrap(imgs) |
| 68 | .ConcatWith(tf.tile(y, [1, 28, 28, 1]), 3) |
| 69 | .Conv2D('conv0', 11) |
| 70 | .tf.nn.leaky_relu() |
| 71 | |
| 72 | .ConcatWith(tf.tile(y, [1, 14, 14, 1]), 3) |
| 73 | .Conv2D('conv1', 74) |
| 74 | .BatchNorm('bn1') |
| 75 | .tf.nn.leaky_relu() |
| 76 | |
| 77 | .apply(batch_flatten) |
| 78 | .ConcatWith(yv, 1) |
| 79 | .FullyConnected('fc1', 1024, activation=tf.identity) |
| 80 | .BatchNorm('bn2') |
| 81 | .tf.nn.leaky_relu() |
| 82 | |
| 83 | .ConcatWith(yv, 1) |
| 84 | .FullyConnected('fct', 1, activation=tf.identity)()) |
| 85 | return l |
| 86 | |
| 87 | def build_graph(self, image_pos, y): |
| 88 | image_pos = tf.expand_dims(image_pos * 2.0 - 1, -1) |
| 89 | y = tf.one_hot(y, 10, name='label_onehot') |
| 90 | |
| 91 | z = tf.random_uniform([BATCH, 100], -1, 1, name='z_train') |
| 92 | z = tf.placeholder_with_default(z, [None, 100], name='z') # clear the static shape |
| 93 | |
| 94 | with argscope([Conv2D, Conv2DTranspose, FullyConnected], |
| 95 | kernel_initializer=tf.truncated_normal_initializer(stddev=0.02)): |
| 96 | with tf.variable_scope('gen'): |
| 97 | image_gen = self.generator(z, y) |
| 98 | tf.summary.image('gen', image_gen, 30) |
| 99 | with tf.variable_scope('discrim'): |
no outgoing calls
no test coverage detected
searching dependent graphs…