| 62 | |
| 63 | |
| 64 | class Model(GANModelDesc): |
| 65 | def inputs(self): |
| 66 | SHAPE = 256 |
| 67 | return [tf.TensorSpec((None, SHAPE, SHAPE, IN_CH), tf.float32, 'input'), |
| 68 | tf.TensorSpec((None, SHAPE, SHAPE, OUT_CH), tf.float32, 'output')] |
| 69 | |
| 70 | def generator(self, imgs): |
| 71 | # imgs: input: 256x256xch |
| 72 | # U-Net structure, it's slightly different from the original on the location of relu/lrelu |
| 73 | with argscope(BatchNorm, training=True), \ |
| 74 | argscope(Dropout, is_training=True): |
| 75 | # always use local stat for BN, and apply dropout even in testing |
| 76 | with argscope(Conv2D, kernel_size=4, strides=2, activation=BNLReLU): |
| 77 | e1 = Conv2D('conv1', imgs, NF, activation=tf.nn.leaky_relu) |
| 78 | e2 = Conv2D('conv2', e1, NF * 2) |
| 79 | e3 = Conv2D('conv3', e2, NF * 4) |
| 80 | e4 = Conv2D('conv4', e3, NF * 8) |
| 81 | e5 = Conv2D('conv5', e4, NF * 8) |
| 82 | e6 = Conv2D('conv6', e5, NF * 8) |
| 83 | e7 = Conv2D('conv7', e6, NF * 8) |
| 84 | e8 = Conv2D('conv8', e7, NF * 8, activation=BNReLU) # 1x1 |
| 85 | with argscope(Conv2DTranspose, activation=BNReLU, kernel_size=4, strides=2): |
| 86 | return (LinearWrap(e8) |
| 87 | .Conv2DTranspose('deconv1', NF * 8) |
| 88 | .Dropout() |
| 89 | .ConcatWith(e7, 3) |
| 90 | .Conv2DTranspose('deconv2', NF * 8) |
| 91 | .Dropout() |
| 92 | .ConcatWith(e6, 3) |
| 93 | .Conv2DTranspose('deconv3', NF * 8) |
| 94 | .Dropout() |
| 95 | .ConcatWith(e5, 3) |
| 96 | .Conv2DTranspose('deconv4', NF * 8) |
| 97 | .ConcatWith(e4, 3) |
| 98 | .Conv2DTranspose('deconv5', NF * 4) |
| 99 | .ConcatWith(e3, 3) |
| 100 | .Conv2DTranspose('deconv6', NF * 2) |
| 101 | .ConcatWith(e2, 3) |
| 102 | .Conv2DTranspose('deconv7', NF * 1) |
| 103 | .ConcatWith(e1, 3) |
| 104 | .Conv2DTranspose('deconv8', OUT_CH, activation=tf.tanh)()) |
| 105 | |
| 106 | @auto_reuse_variable_scope |
| 107 | def discriminator(self, inputs, outputs): |
| 108 | """ return a (b, 1) logits""" |
| 109 | l = tf.concat([inputs, outputs], 3) |
| 110 | with argscope(Conv2D, kernel_size=4, strides=2, activation=BNLReLU): |
| 111 | l = (LinearWrap(l) |
| 112 | .Conv2D('conv0', NF, activation=tf.nn.leaky_relu) |
| 113 | .Conv2D('conv1', NF * 2) |
| 114 | .Conv2D('conv2', NF * 4) |
| 115 | .Conv2D('conv3', NF * 8, strides=1, padding='VALID') |
| 116 | .Conv2D('convlast', 1, strides=1, padding='VALID', activation=tf.identity)()) |
| 117 | return l |
| 118 | |
| 119 | def build_graph(self, input, output): |
| 120 | input, output = input / 128.0 - 1, output / 128.0 - 1 |
| 121 |
no outgoing calls
no test coverage detected