예제 #1
0
    def generator(self, input):
        if self.arch:
            arch = self.arch
            dim_z = arch['dim_z']
            dim_c = arch['dim_c_G']
            if arch['activation_G'] == 'relu':
                activation_fn = tf.nn.relu
            elif arch['activation_G'] == 'leakyRelu':
                activation_fn = tf.nn.leaky_relu
            else:
                activation_fn = tf.nn.tanh
            batchnorm = arch['batchnorm_G']
        else:
            dim_z = self.config.dim_z
            dim_c = self.config.dim_c
            activation_fn = tf.nn.relu
            batchnorm = True

        with tf.variable_scope('Generator'):
            output = layers.linear(input, 4 * 4 * 4 * dim_c, name='LN1')
            if batchnorm:
                output = layers.batchnorm(output,
                                          is_training=self.is_training,
                                          name='BN1')
            output = activation_fn(output)
            output = tf.reshape(output, [-1, 4, 4, 4 * dim_c])

            output_shape = [-1, 8, 8, 2 * dim_c]
            output = layers.deconv2d(output, output_shape, name='Deconv2')
            if batchnorm:
                output = layers.batchnorm(output,
                                          is_training=self.is_training,
                                          name='BN2')
            output = activation_fn(output)

            output_shape = [-1, 16, 16, dim_c]
            output = layers.deconv2d(output, output_shape, name='Decovn3')
            if batchnorm:
                output = layers.batchnorm(output,
                                          is_training=self.is_training,
                                          name='BN3')
            output = activation_fn(output)

            output_shape = [-1, 32, 32, 3]
            output = layers.deconv2d(output, output_shape, name='Deconv4')
            output = tf.nn.tanh(output)

            return tf.reshape(output, [-1, 32 * 32 * 3])
예제 #2
0
    def discriminator(self, inputs, reuse=False):
        if self.arch:
            arch = self.arch
            dim_c = arch['dim_c_D']
            if arch['activation_D'] == 'relu':
                activation_fn = tf.nn.relu
            elif arch['activation_D'] == 'leakyRelu':
                activation_fn = tf.nn.leaky_relu
            else:
                activation_fn = tf.nn.tanh
            batchnorm = arch['batchnorm_D']
        else:
            dim_c = self.config.dim_c
            activation_fn = tf.nn.leaky_relu
            batchnorm = False

        with tf.variable_scope('Discriminator') as scope:
            if reuse:
                scope.reuse_variables()

            output = tf.reshape(inputs, [-1, 28, 28, 1])

            output = layers.conv2d(output, dim_c, name='Conv1')
            output = activation_fn(output)

            output = layers.conv2d(output, 2*dim_c, name='Conv2')
            if batchnorm:
                output = layers.batchnorm(output, is_training=self.is_training,
                                          name='BN2')
            output = activation_fn(output)

            output = tf.reshape(output, [-1, 7*7*2*dim_c])
            output = layers.linear(output, 1, name='LN3')

            return tf.reshape(output, [-1])
예제 #3
0
 def generator(self, input):
     dim_x = self.config.dim_x
     n_hidden = self.config.n_hidden_gen
     with tf.variable_scope('Generator'):
         output = layers.linear(input, n_hidden, name='LN1', stdev=0.2)
         output = layers.batchnorm(output,
                                   is_training=self.is_training,
                                   name='BN1')
         output = tf.nn.relu(output)
         output = layers.linear(output, n_hidden, name='LN2', stdev=0.2)
         output = layers.batchnorm(output,
                                   is_training=self.is_training,
                                   name='BN2')
         output = tf.nn.relu(output)
         output = layers.linear(output, dim_x, name='LN3', stdev=0.2)
         #output = slim.fully_connected(input, n_hidden,
         #                              activation_fn=tf.nn.relu)
         #output = slim.fully_connected(output, n_hidden,
         #                              activation_fn=tf.nn.relu)
         #output = slim.fully_connected(output, dim_x, activation_fn=None)
         return output
예제 #4
0
    def _create_model(self, input_shape):
        droprate = 0.20
        enc_cfg, dec_cfg = configs[self.cfg_idx]

        # ================== encoder ==================
        with tf.name_scope('encoder_input'):  # (N, M, 1)
            encoder_input = Input(shape=input_shape, name="encoder_input")

        with tf.name_scope('encoder_input_noise'):  # (N, M, 1)
            encoder = GaussianNoise(stddev=const.NOISE_STDDEV)(encoder_input)

        with tf.name_scope('encoder_conv_1'):  # (N/2, M/2, 32)
            encoder = conv(enc_cfg[0], strides=2)(encoder)
            encoder = batchnorm()(encoder)
            encoder = dropout(droprate)(encoder)

        with tf.name_scope('encoder_conv_2'):  # (N/4, M/4, 32)
            encoder = conv(enc_cfg[1], strides=2)(encoder)
            encoder = batchnorm()(encoder)
            encoder = dropout(droprate)(encoder)

        with tf.name_scope('encoder_conv_3'):  # (N/8, M/8, 32)
            encoder = conv(enc_cfg[2], strides=2)(encoder)
            encoder = batchnorm()(encoder)
            encoder = dropout(droprate)(encoder)

        if enc_cfg[3] > 0:
            with tf.name_scope('encoder_conv_4'):  # (N/10, M/10, 32)
                encoder = conv(enc_cfg[3], strides=2)(encoder)
                encoder = batchnorm()(encoder)
                encoder = dropout(droprate)(encoder)

        if enc_cfg[4] > 0:
            with tf.name_scope('encoder_conv_4'):  # (N/10, M/10, 32)
                encoder = conv(enc_cfg[4], strides=2)(encoder)
                encoder = batchnorm()(encoder)
                encoder = dropout(droprate)(encoder)

        with tf.name_scope('encoder_fully_connected_1'):  # (512)
            encoder = Flatten()(encoder)
            encoder = dense(enc_cfg[5], activation=activations.lrelu)(encoder)
            encoder = batchnorm()(encoder)
            encoder = dropout(droprate)(encoder)

        with tf.name_scope('encoder_fully_connected_2'):  # (256)
            encoder = dense(enc_cfg[6], activation=activations.relu)(encoder)

        encoder_model = Model(inputs=encoder_input, outputs=encoder, name="encoder")

        # ================== decoder ==================
        with tf.name_scope('decoder_input'):  # (N)
            decoder_input = Input(shape=encoder_model.output_shape[1:], name="decoder_input")
            # decoder_input = encoder

        with tf.name_scope('decoder_fully_connected_1'):  # (256)
            decoder = dense(dec_cfg[0])(decoder_input)

        with tf.name_scope('decoder_reshape_1'):  # (2, 2, 64)
            decoder = Reshape((2, 2, dec_cfg[0] // 4))(decoder)

        with tf.name_scope('decoder_deconv_1'):  # (4, 4, 32)
            decoder = deconv(dec_cfg[1], strides=2)(decoder)
            decoder = batchnorm()(decoder)
            decoder = dropout(droprate)(decoder)

        with tf.name_scope('decoder_deconv_2'):  # (8, 8, 32)
            decoder = deconv(dec_cfg[2], strides=2)(decoder)
            decoder = batchnorm()(decoder)
            decoder = dropout(droprate)(decoder)

        with tf.name_scope('decoder_deconv_3'):  # (16, 16, 32)
            decoder = deconv(dec_cfg[3], strides=2)(decoder)
            decoder = batchnorm()(decoder)
            decoder = dropout(droprate)(decoder)

        with tf.name_scope('decoder_deconv_4'):  # (32, 32, 32)
            decoder = deconv(dec_cfg[4], strides=2)(decoder)
            decoder = batchnorm()(decoder)
            decoder = dropout(droprate)(decoder)

        with tf.name_scope('decoder_deconv_5'):  # (64, 64, 3)
            decoder = deconv(3, strides=2, activation=activations.relu)(decoder)

        decoder_model = Model(inputs=decoder_input, outputs=decoder, name='decoder')

        # ================== CAE ==================
        model = Model(inputs=encoder_input, outputs=decoder_model(encoder_model(encoder_input)), name='cae')

        self.encoder_model = encoder_model
        self.decoder_model = decoder_model

        return model