def LeakyReLULayer(name, n_in, n_out, inputs): output = linear.Linear(name + '.Linear', n_in, n_out, inputs, initialization='he') return LeakyReLU(output)
def MultiplicativeDCGANGenerator(n_samples, noise=None, dim=config.DIM, bn=True): if noise is None: noise = tf.random_normal([n_samples, 128]) output = linear.Linear('Generator.Input', 128, 4 * 4 * 8 * dim * 2, noise) output = tf.reshape(output, [-1, 8 * dim * 2, 4, 4]) if bn: output = Batchnorm('Generator.BN1', [0, 2, 3], output) output = pixcnn_gated_nonlinearity(output[:, ::2], output[:, 1::2]) output = deconv2d.Deconv2D('Generator.2', 8 * dim, 4 * dim * 2, 5, output) if bn: output = Batchnorm('Generator.BN2', [0, 2, 3], output) output = pixcnn_gated_nonlinearity(output[:, ::2], output[:, 1::2]) output = deconv2d.Deconv2D('Generator.3', 4 * dim, 2 * dim * 2, 5, output) if bn: output = Batchnorm('Generator.BN3', [0, 2, 3], output) output = pixcnn_gated_nonlinearity(output[:, ::2], output[:, 1::2]) output = deconv2d.Deconv2D('Generator.4', 2 * dim, dim * 2, 5, output) if bn: output = Batchnorm('Generator.BN4', [0, 2, 3], output) output = pixcnn_gated_nonlinearity(output[:, ::2], output[:, 1::2]) output = deconv2d.Deconv2D('Generator.5', dim, 3, 5, output) output = tf.tanh(output) return tf.reshape(output, [-1, config.OUTPUT_DIM])
def ReLULayer(name, n_in, n_out, inputs): output = linear.Linear(name + '.Linear', n_in, n_out, inputs, initialization='he') return tf.nn.relu(output)
def FCDiscriminator(inputs, FC_DIM=512, n_layers=3): output = LeakyReLULayer('Discriminator.Input', config.OUTPUT_DIM, FC_DIM, inputs) for i in xrange(n_layers): output = LeakyReLULayer('Discriminator.{}'.format(i), FC_DIM, FC_DIM, output) output = linear.Linear('Discriminator.Out', FC_DIM, 1, output) return tf.reshape(output, [-1])
def FCGenerator(n_samples, noise=None, FC_DIM=512): if noise is None: noise = tf.random_normal([n_samples, 128]) output = ReLULayer('Generator.1', 128, FC_DIM, noise) output = ReLULayer('Generator.2', FC_DIM, FC_DIM, output) output = ReLULayer('Generator.3', FC_DIM, FC_DIM, output) output = ReLULayer('Generator.4', FC_DIM, FC_DIM, output) output = linear.Linear('Generator.Out', FC_DIM, config.OUTPUT_DIM, output) output = tf.tanh(output) return output
def DCGANDiscriminator(inputs, dim=config.DIM, bn=True, nonlinearity=LeakyReLU): output = tf.reshape(inputs, [-1, 3, 64, 64]) # 3 * 64 * 64 conv2d.set_weights_stdev(0.02) deconv2d.set_weights_stdev(0.02) linear.set_weights_stdev(0.02) output = conv2d.Conv2D('Discriminator.1', 3, dim, 5, output, stride=2) # 3 --> 64 output = nonlinearity(output) output = conv2d.Conv2D('Discriminator.2', dim, 2 * dim, 5, output, stride=2) # 64 --> 128 if bn: output = Batchnorm('Discriminator.BN2', [0, 2, 3], output) output = nonlinearity(output) output = conv2d.Conv2D('Discriminator.3', 2 * dim, 4 * dim, 5, output, stride=2) if bn: output = Batchnorm('Discriminator.BN3', [0, 2, 3], output) output = nonlinearity(output) output = conv2d.Conv2D('Discriminator.4', 4 * dim, 8 * dim, 5, output, stride=2) if bn: output = Batchnorm('Discriminator.BN4', [0, 2, 3], output) output = nonlinearity(output) output = tf.reshape(output, [-1, 4 * 4 * 8 * dim]) output = linear.Linear('Discriminator.Output', 4 * 4 * 8 * dim, 1, output) conv2d.unset_weights_stdev() deconv2d.unset_weights_stdev() linear.unset_weights_stdev() return tf.reshape(output, [-1])
def DCGANGenerator(n_samples, noise=None, dim=config.DIM, bn=True, nonlinearity=tf.nn.relu): conv2d.set_weights_stdev(0.02) deconv2d.set_weights_stdev(0.02) linear.set_weights_stdev(0.02) if noise is None: noise = tf.random_normal([n_samples, 128]) output = linear.Linear('Generator.Input', 128, 4 * 4 * 8 * dim, noise) # 128 --> 8192 output = tf.reshape(output, [-1, 8 * dim, 4, 4]) # 8192 --> 512 if bn: output = Batchnorm('Generator.BN1', [0, 2, 3], output) output = nonlinearity(output) output = deconv2d.Deconv2D('Generator.2', 8 * dim, 4 * dim, 5, output) # 512 --> 256 if bn: output = Batchnorm('Generator.BN2', [0, 2, 3], output) output = nonlinearity(output) output = deconv2d.Deconv2D('Generator.3', 4 * dim, 2 * dim, 5, output) # 256 --> 128 if bn: output = Batchnorm('Generator.BN3', [0, 2, 3], output) output = nonlinearity(output) output = deconv2d.Deconv2D('Generator.4', 2 * dim, dim, 5, output) # 128 --> 64 if bn: output = Batchnorm('Generator.BN4', [0, 2, 3], output) output = nonlinearity(output) output = deconv2d.Deconv2D('Generator.5', dim, 3, 5, output) # 64 --> 3 output = tf.tanh(output) conv2d.unset_weights_stdev() deconv2d.unset_weights_stdev() linear.unset_weights_stdev() return tf.reshape(output, [-1, config.OUTPUT_DIM]) # 64 * 64 * 3
def MultiplicativeDCGANDiscriminator(inputs, dim=config.DIM, bn=True): output = tf.reshape(inputs, [-1, 3, 64, 64]) output = conv2d.Conv2D('Discriminator.1', 3, dim * 2, 5, output, stride=2) # 3 --> 128 output = pixcnn_gated_nonlinearity(output[:, ::2], output[:, 1::2]) output = conv2d.Conv2D('Discriminator.2', dim, 2 * dim * 2, 5, output, stride=2) # 64 --> 256 if bn: output = Batchnorm('Discriminator.BN2', [0, 2, 3], output) output = pixcnn_gated_nonlinearity(output[:, ::2], output[:, 1::2]) output = conv2d.Conv2D('Discriminator.3', 2 * dim, 4 * dim * 2, 5, output, stride=2) if bn: output = Batchnorm('Discriminator.BN3', [0, 2, 3], output) output = pixcnn_gated_nonlinearity(output[:, ::2], output[:, 1::2]) output = conv2d.Conv2D('Discriminator.4', 4 * dim, 8 * dim * 2, 5, output, stride=2) if bn: output = Batchnorm('Discriminator.BN4', [0, 2, 3], output) output = pixcnn_gated_nonlinearity(output[:, ::2], output[:, 1::2]) output = tf.reshape(output, [-1, 4 * 4 * 8 * dim]) output = linear.Linear('Discriminator.Output', 4 * 4 * 8 * dim, 1, output) return tf.reshape(output, [-1])
def WGANPaper_CrippledDCGANGenerator(n_samples, noise=None, dim=config.DIM): if noise is None: noise = tf.random_normal([n_samples, 128]) output = linear.Linear('Generator.Input', 128, 4 * 4 * dim, noise) # 128 --> 4*4*64 output = tf.nn.relu(output) output = tf.reshape(output, [-1, dim, 4, 4]) # 4*4*64 --> 64*4*4 output = deconv2d.Deconv2D('Generator.2', dim, dim, 5, output) output = tf.nn.relu(output) output = deconv2d.Deconv2D('Generator.3', dim, dim, 5, output) output = tf.nn.relu(output) output = deconv2d.Deconv2D('Generator.4', dim, dim, 5, output) output = tf.nn.relu(output) output = deconv2d.Deconv2D('Generator.5', dim, 3, 5, output) output = tf.tanh(output) return tf.reshape(output, [-1, config.OUTPUT_DIM])
def ResnetDiscriminator(inputs, dim=config.DIM): output = tf.reshape(inputs, [-1, 3, 64, 64]) output = conv2d.Conv2D('Discriminator.In', 3, dim / 2, 1, output, he_init=False) for i in xrange(5): output = ResidualBlock('Discriminator.64x64_{}'.format(i), dim / 2, dim / 2, 3, output, resample=None) output = ResidualBlock('Discriminator.Down1', dim / 2, dim * 1, 3, output, resample='down') for i in xrange(6): output = ResidualBlock('Discriminator.32x32_{}'.format(i), dim * 1, dim * 1, 3, output, resample=None) output = ResidualBlock('Discriminator.Down2', dim * 1, dim * 2, 3, output, resample='down') for i in xrange(6): output = ResidualBlock('Discriminator.16x16_{}'.format(i), dim * 2, dim * 2, 3, output, resample=None) output = ResidualBlock('Discriminator.Down3', dim * 2, dim * 4, 3, output, resample='down') for i in xrange(6): output = ResidualBlock('Discriminator.8x8_{}'.format(i), dim * 4, dim * 4, 3, output, resample=None) output = ResidualBlock('Discriminator.Down4', dim * 4, dim * 8, 3, output, resample='down') for i in xrange(6): output = ResidualBlock('Discriminator.4x4_{}'.format(i), dim * 8, dim * 8, 3, output, resample=None) output = tf.reshape(output, [-1, 4 * 4 * 8 * dim]) output = linear.Linear('Discriminator.Output', 4 * 4 * 8 * dim, 1, output) return tf.reshape(output / 5., [-1])
def ResnetGenerator(n_samples, noise=None, dim=config.DIM): if noise is None: noise = tf.random_normal([n_samples, 128]) output = linear.Linear('Generator.Input', 128, 4 * 4 * 8 * dim, noise) output = tf.reshape(output, [-1, 8 * dim, 4, 4]) # 4*4*8*64 --> 8*64 for i in range(6): output = ResidualBlock('Generator.4x4_{}'.format(i), 8 * dim, 8 * dim, 3, output, resample=None) output = ResidualBlock('Generator.Up1', 8 * dim, 4 * dim, 3, output, resample='up') # 8*64 --> 4*64 for i in range(6): output = ResidualBlock('Generator.8x8_{}'.format(i), 4 * dim, 4 * dim, 3, output, resample=None) output = ResidualBlock('Generator.Up2', 4 * dim, 2 * dim, 3, output, resample='up') # 4*64 --> 2*64 for i in range(6): output = ResidualBlock('Generator.16x16_{}'.format(i), 2 * dim, 2 * dim, 3, output, resample=None) output = ResidualBlock('Generator.Up3', 2 * dim, 1 * dim, 3, output, resample='up') # 2*64 --> 1*64 for i in range(6): output = ResidualBlock('Generator.32x32_{}'.format(i), 1 * dim, 1 * dim, 3, output, resample=None) output = ResidualBlock('Generator.Up4', 1 * dim, dim / 2, 3, output, resample='up') # 1*64 --> 1/2*64 for i in range(5): output = ResidualBlock('Generator.64x64_{}'.format(i), dim / 2, dim / 2, 3, output, resample=None) output = conv2d.Conv2D('Generator.Out', dim / 2, 3, 1, output, he_init=False) # 1/2*64 --> 3 output = tf.tanh(output / 5.) return tf.reshape(output, [-1, config.OUTPUT_DIM]) # 3 --> 64*64*3