Exemplo n.º 1
0
def dcgan_encoder(name,
                  inputs,
                  n_channels,
                  latent_dim,
                  is_training,
                  mode=None,
                  nonlinearity=LeakyReLU):
    conv2d.set_weights_stdev(0.02)
    deconv2d.set_weights_stdev(0.02)
    linear.set_weights_stdev(0.02)

    output = tf.reshape(inputs, [-1, n_channels, DIM, DIM])
    output = Conv2D(name + '.1', 3, DIM, 5, output, stride=2)
    output = nonlinearity(output)

    output = Conv2D(name + '.2', DIM, 2 * DIM, 5, output, stride=2)
    output = Normalize(name + '.BN2', [0, 2, 3], output, is_training, mode)
    output = nonlinearity(output)

    output = Conv2D(name + '.3', 2 * DIM, 4 * DIM, 5, output, stride=2)
    output = Normalize(name + '.BN3', [0, 2, 3], output, is_training, mode)
    output = nonlinearity(output)

    output = Conv2D(name + '.4', 4 * DIM, 8 * DIM, 5, output, stride=2)
    output = Normalize(name + '.BN4', [0, 2, 3], output, is_training, mode)
    output = nonlinearity(output)

    output = tf.reshape(output, [-1, 4 * 4 * 8 * DIM])
    output = Linear(name + '.Output', 4 * 4 * 8 * DIM, latent_dim, output)

    conv2d.unset_weights_stdev()
    deconv2d.unset_weights_stdev()
    linear.unset_weights_stdev()

    return output
Exemplo n.º 2
0
def high_capacity_encoder(name,
                          inputs,
                          n_channels,
                          latent_dim,
                          is_training,
                          mode=None,
                          nonlinearity=tf.nn.relu):
    output = tf.reshape(inputs, [-1, n_channels, DIM, DIM])
    output = Conv2D(name + '.0', n_channels, DIM, 5, output, stride=2)
    output = Normalize(name + '.BN0', [0, 2, 3], output, is_training, mode)
    output = nonlinearity(output)

    output = Conv2D(name + '.1', DIM, DIM * 2, 5, output, stride=2)
    output = Normalize(name + '.BN1', [0, 2, 3], output, is_training, mode)
    output = nonlinearity(output)

    output = Conv2D(name + '.2', DIM * 2, DIM * 4, 5, output, stride=2)
    output = Normalize(name + '.BN2', [0, 2, 3], output, is_training, mode)
    output = nonlinearity(output)

    output = tf.reshape(output, [-1, DIM * 4 * 8 * 8])
    output = Linear(name + '.FC', DIM * 4 * 8 * 8, DIM * 4 * 8, output)
    output = Normalize(name + '.BNFC', [0], output, is_training, mode)
    output = nonlinearity(output)

    output = Linear(name + '.Output', DIM * 4 * 8, latent_dim, output)
    return output
Exemplo n.º 3
0
def high_capacity_decoder(name,
                          z,
                          n_channels,
                          is_training,
                          mode=None,
                          nonlinearity=tf.nn.relu):
    output = Linear(name + '.Input',
                    z.get_shape().as_list()[1], DIM * 4 * 8 * 8, z)
    output = Normalize(name + '.BN0', [0], output, is_training, mode)
    output = nonlinearity(output)
    output = tf.reshape(output, [-1, DIM * 4, 8, 8])

    output = Deconv2D(name + '.1', DIM * 4, DIM * 4, 5, output)
    output = Normalize(name + '.BN1', [0, 2, 3], output, is_training, mode)
    output = nonlinearity(output)

    output = Deconv2D(name + '.2', DIM * 4, DIM * 2, 5, output)
    output = Normalize(name + '.BN2', [0, 2, 3], output, is_training, mode)
    output = nonlinearity(output)

    output = Deconv2D(name + '.3', DIM * 2, DIM // 2, 5, output)
    output = Normalize(name + '.BN3', [0, 2, 3], output, is_training, mode)
    output = nonlinearity(output)

    output = Conv2D(name + '.4', DIM // 2, n_channels, 5, output)
    output = tf.reshape(output, [-1, n_channels * DIM * DIM])
    return output
Exemplo n.º 4
0
def resnet_encoder_new(name,
                       inputs,
                       n_channels,
                       latent_dim,
                       is_training,
                       mode=None,
                       nonlinearity=tf.nn.relu):
    output = tf.reshape(inputs, [-1, n_channels, DIM, DIM])
    output = Conv2D(name + '.Input',
                    n_channels,
                    DIM,
                    3,
                    output,
                    he_init=False,
                    cpu=CPU)

    output = ResidualBlock(name + '.Res1',
                           DIM,
                           2 * DIM,
                           3,
                           output,
                           is_training,
                           mode,
                           resample='down')
    output = ResidualBlock(name + '.Res2',
                           2 * DIM,
                           4 * DIM,
                           3,
                           output,
                           is_training,
                           mode,
                           resample='down')
    output = ResidualBlock(name + '.Res3',
                           4 * DIM,
                           8 * DIM,
                           3,
                           output,
                           is_training,
                           mode,
                           resample='down')
    output = ResidualBlock(name + '.Res4',
                           8 * DIM,
                           8 * DIM,
                           3,
                           output,
                           is_training,
                           mode,
                           resample='down')
    output = Normalize(name + '.BN.5', [0, 2, 3], output, is_training, mode)
    output = nonlinearity(output)
    output = tf.reshape(output, [-1, 4 * 4 * 8 * DIM])
    output = Linear(name + '.Output', 4 * 4 * 8 * DIM, latent_dim, output)

    return output
Exemplo n.º 5
0
def resnet_decoder(name,
                   z,
                   n_channels,
                   is_training,
                   mode=None,
                   nonlinearity=tf.nn.relu):
    output = Linear(name + '.Input',
                    z.get_shape().as_list()[1], 4 * 4 * 8 * DIM, z)
    output = Normalize(name + '.BN0', [0], output, is_training, mode)
    output = nonlinearity(output)
    output = tf.reshape(output, [-1, 8 * DIM, 4, 4])

    output = ResidualBlock(name + '.Res1',
                           8 * DIM,
                           8 * DIM,
                           3,
                           output,
                           is_training,
                           mode,
                           resample='up',
                           norm_inputs=True)
    output = ResidualBlock(name + '.Res2',
                           8 * DIM,
                           4 * DIM,
                           3,
                           output,
                           is_training,
                           mode,
                           resample='up')
    output = ResidualBlock(name + '.Res3',
                           4 * DIM,
                           2 * DIM,
                           3,
                           output,
                           is_training,
                           mode,
                           resample='up')
    output = ResidualBlock(name + '.Res4',
                           2 * DIM,
                           1 * DIM,
                           3,
                           output,
                           is_training,
                           mode,
                           resample='up')

    output = Normalize(name + '.BN5', [0, 2, 3], output, is_training, mode)
    output = nonlinearity(output)
    output = Conv2D(name + '.Output', DIM, n_channels, 3, output)
    output = tf.reshape(output, [-1, n_channels * DIM * DIM])
    return output
Exemplo n.º 6
0
def ConvMeanPool(name,
                 input_dim,
                 output_dim,
                 filter_size,
                 inputs,
                 he_init=True,
                 biases=True):
    output = Conv2D(name,
                    input_dim,
                    output_dim,
                    filter_size,
                    inputs,
                    he_init=he_init,
                    biases=biases,
                    cpu=CPU)
    output = tf.add_n([
        output[:, :, ::2, ::2], output[:, :, 1::2, ::2],
        output[:, :, ::2, 1::2], output[:, :, 1::2, 1::2]
    ]) / 4.
    return output
Exemplo n.º 7
0
def UpsampleConv(name,
                 input_dim,
                 output_dim,
                 filter_size,
                 inputs,
                 he_init=True,
                 biases=True):
    output = inputs
    output = tf.concat([output, output, output, output], axis=1)
    output = tf.transpose(output, [0, 2, 3, 1])
    output = tf.depth_to_space(output, 2)
    output = tf.transpose(output, [0, 3, 1, 2])
    output = Conv2D(name,
                    input_dim,
                    output_dim,
                    filter_size,
                    output,
                    he_init=he_init,
                    biases=biases,
                    cpu=CPU)
    return output