示例#1
0
def _encoder(layer,
             nHidden,
             nLayers,
             activation,
             nOutput=2,
             summaries=False,
             kernel_initializer='glorot_uniform',
             dropout=False,
             dropout_fun=Dropout,
             keep_prob=1.,
             name='encoder'):
    for i in range(nLayers):
        layer_name = name + str(i)
        layer = Dense(nHidden,
                      activation=activation,
                      kernel_initializer=kernel_initializer,
                      _reuse=tf.compat.v1.AUTO_REUSE,
                      name=layer_name)(layer)

        if summaries:
            _layer_summary(layer_name, layer.dtype)

        if dropout:
            layer = dropout_fun(keep_prob)(layer)

    layer_name = name + '_out'
    layer = Dense(nOutput, _reuse=tf.compat.v1.AUTO_REUSE,
                  name=layer_name)(layer)

    if summaries:
        _layer_summary(layer_name, layer.dtype)

    return layer
示例#2
0
文件: autoencoder.py 项目: irfixq/AE
def autoencoder(x, dropout_rate, dropout, config):
    outputs = {}

    with tf.variable_scope('Encoder'):
        encoder = build_unified_encoder(x.get_shape().as_list(), config.intermediateResolutions)

        temp_out = x
        for layer in encoder:
            temp_out = layer(temp_out)

    with tf.variable_scope("Bottleneck"):
        intermediate_conv = Conv2D(temp_out.get_shape().as_list()[3] // 8, 1, padding='same')
        intermediate_conv_reverse = Conv2D(temp_out.get_shape().as_list()[3], 1, padding='same')
        dropout_layer = Dropout(dropout_rate)
        temp_out = intermediate_conv(temp_out)

        reshape = temp_out.get_shape().as_list()[1:]
        z_layer = Dense(config.zDim)
        dec_dense = Dense(np.prod(reshape))

        outputs['z'] = z = dropout_layer(z_layer(Flatten()(temp_out)), dropout)
        temp_out = intermediate_conv_reverse(tf.reshape(dropout_layer(dec_dense(z)), [-1, *reshape]))

    with tf.variable_scope('Decoder'):
        decoder = build_unified_decoder(config.outputWidth, config.intermediateResolutions, config.numChannels)
        # Decode: z -> x_hat
        for layer in decoder:
            temp_out = layer(temp_out)

        outputs['x_hat'] = temp_out

    return outputs
示例#3
0
def variational_autoencoder_Zimmerer(x, dropout_rate, dropout, config):
    outputs = {}
    enc_conv2D_1 = Conv2D(filters=16, kernel_size=4, strides=2, padding='same', name='enc_conv2D_1', activation=leaky_relu)
    enc_conv2D_2 = Conv2D(filters=64, kernel_size=4, strides=2, padding='same', name='enc_conv2D_2', activation=leaky_relu)
    enc_conv2D_3 = Conv2D(filters=256, kernel_size=4, strides=2, padding='same', name='enc_conv2D_3', activation=leaky_relu)
    enc_conv2D_4 = Conv2D(filters=1024, kernel_size=4, strides=2, padding='same', name='enc_conv2D_4', activation=leaky_relu)
    flatten_layer = Flatten()
    mu_layer = Dense(config.zDim)
    sigma_layer = Dense(config.zDim)

    dec_dense = Dense(config.intermediateResolutions[0] * config.intermediateResolutions[1] * 1024)
    dec_Conv2DT_1 = Conv2DTranspose(filters=1024, kernel_size=4, strides=2, padding='same', name='dec_Conv2DT_1', activation=leaky_relu)
    dec_Conv2DT_2 = Conv2DTranspose(filters=256, kernel_size=4, strides=2, padding='same', name='dec_Conv2DT_2', activation=leaky_relu)
    dec_Conv2DT_3 = Conv2DTranspose(filters=64, kernel_size=4, strides=2, padding='same', name='dec_Conv2DT_3', activation=leaky_relu)
    dec_Conv2DT_4 = Conv2DTranspose(filters=16, kernel_size=4, strides=2, padding='same', name='dec_Conv2DT_4', activation=leaky_relu)
    dec_Conv2D_final = Conv2D(filters=1, kernel_size=4, strides=1, padding='same', name='dec_Conv2D_final')

    flatten = flatten_layer(enc_conv2D_4(enc_conv2D_3(enc_conv2D_2(enc_conv2D_1(x)))))
    outputs['z_mu'] = z_mu = mu_layer(flatten)
    outputs['z_log_sigma'] = z_log_sigma = sigma_layer(flatten)
    outputs['z_sigma'] = z_sigma = tf.exp(z_log_sigma)
    z_vae = z_mu + tf.random_normal(tf.shape(z_sigma)) * z_sigma
    # Decode: z -> x_hat
    reshaped = tf.reshape(dec_dense(z_vae), [-1, config.intermediateResolutions[0], config.intermediateResolutions[1], 1024])
    outputs['x_hat'] = dec_Conv2D_final(dec_Conv2DT_4(dec_Conv2DT_3(dec_Conv2DT_2(dec_Conv2DT_1(reshaped)))))

    return outputs
示例#4
0
def _decoder(layer,
             nHidden,
             nLayers,
             activation,
             M,
             summaries=False,
             dropout=False,
             dropout_fun=Dropout,
             keep_prob=1.,
             name='decoder'):
    for i in range(nLayers):
        layer_name = name + str(i)
        layer = Dense(nHidden,
                      activation=activation,
                      _reuse=tf.compat.v1.AUTO_REUSE,
                      name=layer_name)(layer)

        if summaries:
            _layer_summary(layer_name, layer.dtype)

        if dropout:
            layer = dropout_fun(layer, keep_prob)

    layer_name = name + '_out'
    layer = Dense(M, _reuse=tf.compat.v1.AUTO_REUSE, name=layer_name)(layer)

    if summaries:
        _layer_summary(layer_name, layer.dtype)

    return layer
示例#5
0
 def setup_dist_params(self, latent_policy):
     mean = Dense(units=1,
                  activation="sigmoid",
                  kernel_initializer="he_uniform")(latent_policy)
     neg_log_std = Dense(units=1,
                         activation="sigmoid",
                         kernel_initializer="he_uniform")(latent_policy)
     return tf1.concat([mean, -neg_log_std], axis=1)
示例#6
0
def net_core(observation_ph, net_arch, initializer, activation):
    input_layer = observation_ph
    for layer_size in net_arch:
        input_layer = Dense(units=layer_size,
                            activation=activation,
                            kernel_initializer=initializer)(input_layer)
    return input_layer
示例#7
0
 def setup_dist_params(self, latent_policy):
     mean = Dense(units=1,
                  activation=None,
                  kernel_initializer="he_uniform",
                  name="gaussian_mean")(latent_policy)
     log_std = tf1.get_variable("log_std",
                                initializer=[[0.]],
                                trainable=True)
     # broadcast and concat
     dist_params = tf1.concat([mean, mean * 0 + log_std], axis=1)
     return dist_params
示例#8
0
def variational_autoencoder(x, dropout_rate, dropout, config):
    outputs = {}

    with tf.variable_scope('Encoder'):
        encoder = build_unified_encoder(x.get_shape().as_list(), config.intermediateResolutions)

        temp_out = x
        for layer in encoder:
            temp_out = layer(temp_out)

    with tf.variable_scope("Bottleneck"):
        intermediate_conv = Conv2D(temp_out.get_shape().as_list()[3] // 8, 1, padding='same')
        intermediate_conv_reverse = Conv2D(temp_out.get_shape().as_list()[3], 1, padding='same')
        dropout_layer = Dropout(dropout_rate)
        temp_out = intermediate_conv(temp_out)
        reshape = temp_out.get_shape().as_list()[1:]

        mu_layer = Dense(config.zDim)
        sigma_layer = Dense(config.zDim)
        dec_dense = Dense(np.prod(reshape))

        flatten = Flatten()(temp_out)
        outputs['z_mu'] = z_mu = dropout_layer(mu_layer(flatten), dropout)
        outputs['z_log_sigma'] = z_log_sigma = dropout_layer(sigma_layer(flatten), dropout)
        outputs['z_sigma'] = z_sigma = tf.exp(z_log_sigma)
        z_vae = z_mu + tf.random_normal(tf.shape(z_sigma)) * z_sigma
        reshaped = tf.reshape(dropout_layer(dec_dense(z_vae), dropout), [-1, *reshape])
        temp_out = intermediate_conv_reverse(reshaped)

    with tf.variable_scope('Decoder'):
        decoder = build_unified_decoder(config.outputWidth, config.intermediateResolutions, config.numChannels)

        # Decode: z -> x_hat
        for layer in decoder:
            temp_out = layer(temp_out)

        outputs['x_hat'] = temp_out

    return outputs
示例#9
0
    def __init__(self, observation_size, net_arch, initializer, activation,
                 clip_range, value_coef, entropy_coef, learning_rate,
                 pre_training_learning_rate, action_bounds, policy):
        """
        :param observation_size:
        :param net_arch:
        :param initializer:
        :param activation:
        :param clip_range:
        :param value_coef:
        :param entropy_coef:
        :param learning_rate:
        :param pre_training_learning_rate:
        :param action_bounds:
        :param policy:
        """
        """Set class constants"""
        self.observation_size = observation_size
        self.net_arch = net_arch
        self.initializer = initializer
        self.activation = activation
        self.clip_range = clip_range
        self.value_coef = value_coef
        self.entropy_coef = entropy_coef

        if action_bounds is None:
            action_bounds = [0.0, 1.5]
        self.action_bounds = action_bounds
        self.learning_rate = learning_rate
        self.pre_training_learning_rate = pre_training_learning_rate

        if policy is None:
            policy = GaussFull()
        self.policy = policy
        """Set up the tensorflow graph"""
        self.graph = Graph()

        with self.graph.as_default():
            self.sess = Session(graph=self.graph)
            """ core """
            # place holders
            self.observation_string_ph = placeholder(
                shape=(None, 1), dtype=string, name="observation_string_ph")
            self.action_ph = placeholder(dtype=float32,
                                         shape=(None, 1),
                                         name="action_ph")
            self.old_neg_logits = placeholder(dtype=float32,
                                              shape=(None, 1),
                                              name="old_neg_logits")
            self.advantage_ph = placeholder(dtype=float32,
                                            shape=(None, 1),
                                            name="advantage_ph")
            self.value_target_ph = placeholder(dtype=float32,
                                               shape=(None, 1),
                                               name="value_target_ph")
            # learning rate tensors
            self.learning_rate_ph = placeholder_with_default(
                input=self.learning_rate, shape=())
            self.pre_training_learning_rate_ph = placeholder_with_default(
                input=self.pre_training_learning_rate, shape=())

            # observation tensor
            replaced1 = regex_replace(self.observation_string_ph, "/", "_")
            replaced2 = regex_replace(replaced1, r"\+", "-")
            byte_tensor = decode_base64(replaced2)
            decoded = decode_raw(byte_tensor, out_type=float32)
            squeezed = squeeze(decoded, axis=1)
            self.observation_input = ensure_shape(
                squeezed,
                shape=(None, self.observation_size),
                name="observation_input")

            # policy net
            latent_policy = net_core(self.observation_input, self.net_arch,
                                     self.initializer, self.activation)
            self.policy.construct(latent_policy=latent_policy)

            self.clipped_action = clip_by_value(
                cast(self.policy.action, float32), self.action_bounds[0],
                self.action_bounds[1], "clipped_action")

            # value net
            latent_value = net_core(self.observation_input, self.net_arch,
                                    self.initializer, self.activation)
            self.value = identity(
                input=Dense(units=1,
                            activation=None,
                            kernel_initializer=self.initializer)(latent_value),
                name="value")
            """loss calculation"""
            # policy loss
            self.neg_logits = self.policy.neg_logits_from_actions(
                self.action_ph)
            ratio = exp(self.old_neg_logits - self.neg_logits)

            standardized_adv = (self.advantage_ph - reduce_mean(
                self.advantage_ph)) / (reduce_std(self.advantage_ph) + 1e-8)
            raw_policy_loss = -standardized_adv * ratio
            clipped_policy_loss = -standardized_adv * clip_by_value(
                ratio, 1 - self.clip_range, 1 + self.clip_range)
            self.policy_loss = reduce_mean(
                maximum(raw_policy_loss, clipped_policy_loss))

            self.value_loss = mean_squared_error(self.value_target_ph,
                                                 self.value)

            # entropy loss
            self.entropy_loss = -reduce_mean(self.policy.entropy)

            # total loss
            self.total_loss = self.policy_loss + self.value_coef * self.value_loss + self.entropy_coef * self.entropy_loss

            # optimizer
            optimizer = AdamOptimizer(learning_rate=self.learning_rate_ph)

            # training ops
            self.training_op = optimizer.minimize(self.total_loss)

            # pre training
            self.dist_param_target_ph = placeholder(
                dtype=float32,
                shape=(None, self.policy.dist_params.shape[1]),
                name="dist_param_label_ph")
            self.pre_training_loss = mean_squared_error(
                self.dist_param_target_ph, self.policy.dist_params)
            pre_training_optimizer = GradientDescentOptimizer(
                learning_rate=self.pre_training_learning_rate_ph)
            self.pre_training_op = pre_training_optimizer.minimize(
                self.pre_training_loss)
            """utility nodes"""
            # inspect model weights
            self.trainable_variables = trainable_variables()

            # saviour
            self.saver = Saver()

            # tensorboard summaries
            self.summary = merge([
                histogram("values", self.value),
                histogram("advantages", standardized_adv),
                histogram("actions", self.clipped_action),
                histogram("det_actions",
                          replace_nan(self.policy.det_action, 0.0)),
                histogram("value_targets", self.value_target_ph),
                scalar("policy_loss", self.policy_loss),
                scalar("value_loss", self.value_loss),
                scalar("entropy_loss", self.entropy_loss)
            ])

            self.pre_summary = merge([
                histogram("pretraining_actions", self.clipped_action),
                scalar("pretraining_loss", self.pre_training_loss)
            ])

            # initialization
            init = global_variables_initializer()
            self.sess.run(init)
示例#10
0
 def setup_dist_params(self, latent_policy):
     proba = Dense(units=1,
                   activation=tf1.sigmoid,
                   kernel_initializer="zeros")(latent_policy)
     return proba
示例#11
0
文件: fanogan.py 项目: irfixq/AE
def fanogan(z, x, dropout_rate, dropout, config):
    outputs = {}

    # Encoder
    with tf.variable_scope('Encoder'):
        encoder = build_unified_encoder(x.get_shape().as_list(),
                                        config.intermediateResolutions)

        temp_out = x
        for layer in encoder:
            temp_out = layer(temp_out)

        temp_temp_out = temp_out
        intermediate_conv = Conv2D(temp_temp_out.get_shape().as_list()[3] // 8,
                                   1,
                                   padding='same')
        dropout_layer = Dropout(dropout_rate)
        temp_out = intermediate_conv(temp_out)

        reshape = temp_out.get_shape().as_list()[1:]
        z_layer = Dense(config.zDim)
        outputs['z_enc'] = z_enc = tf.nn.tanh(
            dropout_layer(z_layer(Flatten()(temp_out)), dropout))

    # Generator
    with tf.variable_scope('Generator'):
        intermediate_conv_reverse = Conv2D(
            temp_temp_out.get_shape().as_list()[3], 1, padding='same')
        dec_dense = Dense(np.prod(reshape))
        generator = build_unified_decoder(config.outputWidth,
                                          config.intermediateResolutions,
                                          config.numChannels,
                                          use_batchnorm=False)

        temp_out_z_enc = intermediate_conv_reverse(
            tf.reshape(dropout_layer(dec_dense(z_enc), dropout),
                       [-1, *reshape]))
        # encoder training:
        for layer in generator:
            temp_out_z_enc = layer(temp_out_z_enc)
        outputs['x_enc'] = x_enc = sigmoid(temp_out_z_enc)  # recon_img
        # generator training
        temp_out = intermediate_conv_reverse(
            tf.reshape(dropout_layer(dec_dense(z), dropout), [-1, *reshape]))
        for layer in generator:
            temp_out = layer(temp_out)
        outputs['x_'] = x_ = sigmoid(temp_out)

    # Discriminator
    with tf.variable_scope('Discriminator'):
        discriminator = build_unified_encoder(x_.get_shape().as_list(),
                                              config.intermediateResolutions,
                                              use_batchnorm=False)
        discriminator_dense = Dense(1)

        # fake:
        temp_out = x_
        for layer in discriminator:
            temp_out = layer(temp_out)
        outputs['d_fake_features'] = temp_out
        outputs['d_'] = discriminator_dense(temp_out)

        # real:
        temp_out = x
        for layer in discriminator:
            temp_out = layer(temp_out)
        outputs['d_features'] = temp_out  # image_features
        outputs['d'] = discriminator_dense(temp_out)

        alpha = tf.random_uniform(shape=[config.batchsize, 1],
                                  minval=0.,
                                  maxval=1.)  # eps
        diff = tf.reshape(
            (x_ - x), [config.batchsize,
                       np.prod(x.get_shape().as_list()[1:])])
        outputs['x_hat'] = x_hat = x + tf.reshape(
            alpha * diff, [config.batchsize, *x.get_shape().as_list()[1:]])

        temp_out = x_hat
        for layer in discriminator:
            temp_out = layer(temp_out)
        outputs['d_hat_features'] = temp_out
        outputs['d_hat'] = discriminator_dense(temp_out)

        # encoder training:
        temp_out = x_enc
        for layer in discriminator:
            temp_out = layer(temp_out)
        outputs['d_enc_features'] = temp_out  # recon_features
        outputs['d_enc'] = discriminator_dense(temp_out)

    return outputs
def constrained_adversarial_autoencoder_Chen(z, x, dropout_rate, dropout, config):
    outputs = {}
    dim = 64
    with tf.variable_scope('Encoder'):
        encoder = Bunch({
            # Model definition
            'enc_conv': Conv2D(filters=dim, kernel_size=3, padding='same'),

            'enc_res1_conv1': Conv2D(filters=2 * dim, kernel_size=3, padding='same'),
            'enc_res1_layernorm1': LayerNormalization([1, 2]),
            'enc_res1_conv2': Conv2D(filters=2 * dim, kernel_size=3, strides=2, padding='same'),
            'enc_res1_layernorm2': LayerNormalization([1, 2]),
            'enc_res1_shortcut1': Conv2D(filters=2 * dim, kernel_size=1, padding='same'),
            'enc_res1_shortcut2': AvgPool2D(),

            'enc_res2_conv1': Conv2D(filters=4 * dim, kernel_size=3, padding='same'),
            'enc_res2_layernorm1': LayerNormalization([1, 2]),
            'enc_res2_conv2': Conv2D(filters=4 * dim, kernel_size=3, strides=2, padding='same'),
            'enc_res2_layernorm2': LayerNormalization([1, 2]),
            'enc_res2_shortcut1': Conv2D(filters=4 * dim, kernel_size=1, padding='same'),
            'enc_res2_shortcut2': AvgPool2D(),

            'enc_res3_conv1': Conv2D(filters=8 * dim, kernel_size=3, padding='same'),
            'enc_res3_layernorm1': LayerNormalization([1, 2]),
            'enc_res3_conv2': Conv2D(filters=8 * dim, kernel_size=3, strides=2, padding='same'),
            'enc_res3_layernorm2': LayerNormalization([1, 2]),
            'enc_res3_shortcut1': Conv2D(filters=8 * dim, kernel_size=1, padding='same'),
            'enc_res3_shortcut2': AvgPool2D(),

            'enc_res4_conv1': Conv2D(filters=8 * dim, kernel_size=3, padding='same'),
            'enc_res4_layernorm1': LayerNormalization([1, 2]),
            'enc_res4_conv2': Conv2D(filters=8 * dim, kernel_size=3, padding='same'),
            'enc_res4_layernorm2': LayerNormalization([1, 2]),

            'enc_flatten': Flatten(),
            'enc_dense': Dense(config.zDim),
        })
        features, z_ = evaluate_encoder(encoder, x)
        outputs['z_'] = z_

    with tf.variable_scope('Decoder'):
        decoder = Bunch({
            # Model definition
            'dec_1': Dense(np.prod(features.get_shape().as_list()[1:])),

            'dec_res1_conv1': Conv2D(filters=8 * dim, kernel_size=3, padding='same'),
            'dec_res1_layernorm1': LayerNormalization([1, 2]),
            'dec_res1_conv2': Conv2DTranspose(filters=8 * dim, kernel_size=3, padding='same'),
            'dec_res1_layernorm2': LayerNormalization([1, 2]),

            'dec_res2_conv1': Conv2D(filters=4 * dim, kernel_size=3, padding='same'),
            'dec_res2_layernorm1': LayerNormalization([1, 2]),
            'dec_res2_conv2': Conv2DTranspose(filters=4 * dim, kernel_size=3, strides=2, padding='same'),
            'dec_res2_layernorm2': LayerNormalization([1, 2]),
            'dec_res2_shortcut': Conv2DTranspose(filters=4 * dim, kernel_size=1, padding='same', strides=2),

            'dec_res3_conv1': Conv2D(filters=2 * dim, kernel_size=3, padding='same'),
            'dec_res3_layernorm1': LayerNormalization([1, 2]),
            'dec_res3_conv2': Conv2DTranspose(filters=2 * dim, kernel_size=3, strides=2, padding='same'),
            'dec_res3_layernorm2': LayerNormalization([1, 2]),
            'dec_res3_shortcut': Conv2DTranspose(filters=2 * dim, kernel_size=1, padding='same', strides=2),

            'dec_res4_conv1': Conv2D(filters=dim, kernel_size=3, padding='same'),
            'dec_res4_layernorm1': LayerNormalization([1, 2]),
            'dec_res4_conv2': Conv2DTranspose(filters=dim, kernel_size=3, strides=2, padding='same'),
            'dec_res4_layernorm2': LayerNormalization([1, 2]),
            'dec_res4_shortcut': Conv2DTranspose(filters=dim, kernel_size=1, padding='same', strides=2),

            # post process
            'dec_layernorm': LayerNormalization([1, 2]),
            'dec_conv': Conv2D(1, 1, padding='same'),
        })
        outputs['x_hat'] = x_hat = evaluate_decoder(decoder, z_, features.get_shape().as_list()[1:])

    # projecting reconstruction to latent space for constrained part
    outputs['z_rec'] = evaluate_encoder(encoder, x_hat)[1]

    # Discriminator
    with tf.variable_scope('Discriminator'):
        discriminator = [
            Dense(400, activation=leaky_relu),
            Dense(200, activation=leaky_relu),
            Dense(1)
        ]

        # fake
        temp_out = z_
        for layer in discriminator:
            temp_out = layer(temp_out)
        outputs['d_'] = temp_out

        # real
        temp_out = z
        for layer in discriminator:
            temp_out = layer(temp_out)
        outputs['d'] = temp_out

        # reparametrization
        epsilon = tf.random_uniform([], 0.0, 1.0)
        outputs['z_hat'] = z_hat = epsilon * z + (1 - epsilon) * z_

        temp_out = z_hat
        for layer in discriminator:
            temp_out = layer(temp_out)
        outputs['d_hat'] = temp_out

    return outputs
示例#13
0
def anovaegan(x, dropout_rate, dropout, config):
    outputs = {}

    # Encoder
    with tf.variable_scope('Encoder'):
        encoder = build_unified_encoder(x.get_shape().as_list(), config.intermediateResolutions)

        temp_out = x
        for layer in encoder:
            temp_out = layer(temp_out)

        temp_temp_out = temp_out
        intermediate_conv = Conv2D(temp_temp_out.get_shape().as_list()[3] // 8, 1, padding='same')
        dropout_layer = Dropout(dropout_rate)
        temp_out = intermediate_conv(temp_out)

        reshape = temp_out.get_shape().as_list()[1:]
        mu_layer = Dense(config.zDim)
        sigma_layer = Dense(config.zDim)

        flatten = Flatten()(temp_out)
        outputs['z_mu'] = z_mu = dropout_layer(mu_layer(flatten), dropout)
        outputs['z_log_sigma'] = z_log_sigma = dropout_layer(sigma_layer(flatten), dropout)
        outputs['z_sigma'] = z_sigma = tf.exp(z_log_sigma)
        z_vae = z_mu + tf.random_normal(tf.shape(z_sigma)) * z_sigma

    with tf.variable_scope("Generator"):
        intermediate_conv_reverse = Conv2D(temp_temp_out.get_shape().as_list()[3], 1, padding='same')
        dec_dense = Dense(np.prod(reshape))
        decoder = build_unified_decoder(outputWidth=config.outputWidth, intermediateResolutions=config.intermediateResolutions,
                                        outputChannels=config.numChannels,
                                        use_batchnorm=False)

        reshaped = tf.reshape(dropout_layer(dec_dense(z_vae)), [-1, *reshape])
        temp_out = intermediate_conv_reverse(reshaped)

        # Decode: z -> x_hat
        for layer in decoder:
            temp_out = layer(temp_out)

        outputs['out'] = temp_out

    # Discriminator
    with tf.variable_scope('Discriminator'):
        discriminator = build_unified_encoder(temp_out.get_shape().as_list(), intermediateResolutions=config.intermediateResolutions, use_batchnorm=False)
        discriminator_dense = Dense(1)

        # fake/reconstructed:
        for layer in discriminator:
            temp_out = layer(temp_out)
        outputs['d_fake_features'] = temp_out
        outputs['d_'] = discriminator_dense(temp_out)

        # real:
        temp_out = x
        for layer in discriminator:
            temp_out = layer(temp_out)
        outputs['d_features'] = temp_out  # image_features
        outputs['d'] = discriminator_dense(temp_out)

        # for GP
        alpha = tf.random_uniform(shape=[config.batchsize, 1], minval=0., maxval=1.)  # eps
        diff = tf.reshape((outputs['out'] - x), [config.batchsize, np.prod(x.get_shape().as_list()[1:])])
        outputs['x_hat'] = x_hat = x + tf.reshape(alpha * diff, [config.batchsize, *x.get_shape().as_list()[1:]])

        temp_out = x_hat
        for layer in discriminator:
            temp_out = layer(temp_out)
        outputs['d_hat_features'] = temp_out
        outputs['d_hat'] = discriminator_dense(temp_out)
    return outputs
def context_encoder_variational_autoencoder_Zimmerer(x, x_ce, dropout_rate,
                                                     dropout, config):
    outputs = {}
    with tf.variable_scope("Encoder"):
        enc_conv2D_1 = Conv2D(filters=16,
                              kernel_size=4,
                              strides=2,
                              padding='same',
                              name='enc_conv2D_1',
                              activation=leaky_relu)
        enc_conv2D_2 = Conv2D(filters=64,
                              kernel_size=4,
                              strides=2,
                              padding='same',
                              name='enc_conv2D_2',
                              activation=leaky_relu)
        enc_conv2D_3 = Conv2D(filters=256,
                              kernel_size=4,
                              strides=2,
                              padding='same',
                              name='enc_conv2D_3',
                              activation=leaky_relu)
        enc_conv2D_4 = Conv2D(filters=1024,
                              kernel_size=4,
                              strides=2,
                              padding='same',
                              name='enc_conv2D_4',
                              activation=leaky_relu)

        temp_out = enc_conv2D_4(enc_conv2D_3(enc_conv2D_2(enc_conv2D_1(x))))
        temp_out_ce = enc_conv2D_4(
            enc_conv2D_3(enc_conv2D_2(enc_conv2D_1(x_ce))))

    with tf.variable_scope("Bottleneck"):
        flatten_layer = Flatten()
        mu_layer = Dense(config.zDim)
        sigma_layer = Dense(config.zDim)
        reshape = temp_out.get_shape().as_list()[1:]

        dec_dense = Dense(np.prod(reshape))
        flatten = flatten_layer(temp_out)
        flatten_ce = flatten_layer(temp_out_ce)
        outputs['z_mu'] = z_mu = mu_layer(flatten)
        outputs['z_log_sigma'] = z_log_sigma = sigma_layer(flatten)
        outputs['z_sigma'] = z_sigma = tf.exp(z_log_sigma)
        z_vae = z_mu + tf.random_normal(tf.shape(z_sigma)) * z_sigma
        temp_out = tf.reshape(dec_dense(z_vae), [-1, *reshape])
        temp_out_ce = tf.reshape(dec_dense(mu_layer(flatten_ce)),
                                 [-1, *reshape])

    with tf.variable_scope("Decoder"):
        dec_Conv2DT_1 = Conv2DTranspose(filters=1024,
                                        kernel_size=4,
                                        strides=2,
                                        padding='same',
                                        name='dec_Conv2DT_1',
                                        activation=leaky_relu)
        dec_Conv2DT_2 = Conv2DTranspose(filters=256,
                                        kernel_size=4,
                                        strides=2,
                                        padding='same',
                                        name='dec_Conv2DT_2',
                                        activation=leaky_relu)
        dec_Conv2DT_3 = Conv2DTranspose(filters=64,
                                        kernel_size=4,
                                        strides=2,
                                        padding='same',
                                        name='dec_Conv2DT_3',
                                        activation=leaky_relu)
        dec_Conv2DT_4 = Conv2DTranspose(filters=16,
                                        kernel_size=4,
                                        strides=2,
                                        padding='same',
                                        name='dec_Conv2DT_4',
                                        activation=leaky_relu)
        dec_Conv2D_final = Conv2D(filters=1,
                                  kernel_size=4,
                                  strides=1,
                                  padding='same',
                                  name='dec_Conv2D_final')

        outputs['x_hat'] = dec_Conv2D_final(
            dec_Conv2DT_4(dec_Conv2DT_3(dec_Conv2DT_2(
                dec_Conv2DT_1(temp_out)))))

        outputs['x_hat_ce'] = dec_Conv2D_final(
            dec_Conv2DT_4(
                dec_Conv2DT_3(dec_Conv2DT_2(dec_Conv2DT_1(temp_out_ce)))))

    return outputs
示例#15
0
def fanogan_schlegl(z, x, dropout_rate, dropout, config):
    outputs = {}
    dim = 64
    # Encoder
    with tf.variable_scope('Encoder'):
        encoder = build_unified_encoder(x.get_shape().as_list(), intermediateResolutions=config.intermediateResolutions)
        enc_dense = Dense(config.zDim)

        temp_out = x
        for layer in encoder:
            temp_out = layer(temp_out)
        outputs['z_enc'] = z_enc = tf.nn.tanh(enc_dense(Flatten()(temp_out)))  # restricting encoder outputs to range [-1;1]

    # Generator
    with tf.variable_scope('Generator'):
        generator = Bunch({
            # Model definition
            'gen_1': Dense(np.prod(config.intermediateResolutions) * 8 * dim),

            'gen_res1_conv1': Conv2D(filters=8 * dim, kernel_size=3, padding='same'),
            'gen_res1_layernorm1': LayerNormalization([1, 2]),
            'gen_res1_conv2': Conv2DTranspose(filters=8 * dim, kernel_size=3, padding='same'),
            'gen_res1_layernorm2': LayerNormalization([1, 2]),

            'gen_res2_conv1': Conv2D(filters=4 * dim, kernel_size=3, padding='same'),
            'gen_res2_layernorm1': LayerNormalization([1, 2]),
            'gen_res2_conv2': Conv2DTranspose(filters=4 * dim, kernel_size=3, strides=2, padding='same'),
            'gen_res2_layernorm2': LayerNormalization([1, 2]),
            'gen_res2_shortcut': Conv2DTranspose(filters=4 * dim, kernel_size=1, padding='same', strides=2),

            'gen_res3_conv1': Conv2D(filters=2 * dim, kernel_size=3, padding='same'),
            'gen_res3_layernorm1': LayerNormalization([1, 2]),
            'gen_res3_conv2': Conv2DTranspose(filters=2 * dim, kernel_size=3, strides=2, padding='same'),
            'gen_res3_layernorm2': LayerNormalization([1, 2]),
            'gen_res3_shortcut': Conv2DTranspose(filters=2 * dim, kernel_size=1, padding='same', strides=2),

            'gen_res4_conv1': Conv2D(filters=1 * dim, kernel_size=3, padding='same'),
            'gen_res4_layernorm1': LayerNormalization([1, 2]),
            'gen_res4_conv2': Conv2DTranspose(filters=1 * dim, kernel_size=3, strides=2, padding='same'),
            'gen_res4_layernorm2': LayerNormalization([1, 2]),
            'gen_res4_shortcut': Conv2DTranspose(filters=1 * dim, kernel_size=1, padding='same', strides=2),

            # post process
            'gen_layernorm': LayerNormalization([1, 2]),
            'gen_conv': Conv2D(1, 1, padding='same', activation='tanh')
        })

        outputs['x_'] = x_ = evaluate_generator(generator, z, config.intermediateResolutions, dim)

        # encoder training:
        outputs['x_enc'] = x_enc = evaluate_generator(generator, z_enc, config.intermediateResolutions, dim)

    # Discriminator
    with tf.variable_scope('Discriminator'):
        discriminator = Bunch({
            # Model definition
            'dis_conv': Conv2D(dim, 3, padding='same'),

            'dis_res1_conv1': Conv2D(filters=2 * dim, kernel_size=3, padding='same'),
            'dis_res1_layernorm1': LayerNormalization([1, 2]),
            'dis_res1_conv2': Conv2D(filters=2 * dim, kernel_size=3, strides=2, padding='same'),
            'dis_res1_layernorm2': LayerNormalization([1, 2]),
            'dis_res1_shortcut1': Conv2D(filters=2 * dim, kernel_size=1, padding='same'),
            'dis_res1_shortcut2': AvgPool2D(),

            'dis_res2_conv1': Conv2D(filters=4 * dim, kernel_size=3, padding='same'),
            'dis_res2_layernorm1': LayerNormalization([1, 2]),
            'dis_res2_conv2': Conv2D(filters=4 * dim, kernel_size=3, strides=2, padding='same'),
            'dis_res2_layernorm2': LayerNormalization([1, 2]),
            'dis_res2_shortcut1': Conv2D(filters=4 * dim, kernel_size=1, padding='same'),
            'dis_res2_shortcut2': AvgPool2D(),

            'dis_res3_conv1': Conv2D(filters=8 * dim, kernel_size=3, padding='same'),
            'dis_res3_layernorm1': LayerNormalization([1, 2]),
            'dis_res3_conv2': Conv2D(filters=8 * dim, kernel_size=3, strides=2, padding='same'),
            'dis_res3_layernorm2': LayerNormalization([1, 2]),
            'dis_res3_shortcut1': Conv2D(filters=8 * dim, kernel_size=1, padding='same'),
            'dis_res3_shortcut2': AvgPool2D(),

            'dis_res4_conv1': Conv2D(filters=8 * dim, kernel_size=3, padding='same'),
            'dis_res4_layernorm1': LayerNormalization([1, 2]),
            'dis_res4_conv2': Conv2D(filters=8 * dim, kernel_size=3, padding='same'),
            'dis_res4_layernorm2': LayerNormalization([1, 2]),

            # post process
            # 'dis_flatten': Flatten(),
            'dis_dense': Dense(1),
        })

        # fake:
        outputs['d_fake_features'], outputs['d_'] = evaluate_discriminator(discriminator, x_)

        # real:
        outputs['d_features'], outputs['d'] = evaluate_discriminator(discriminator, x)

        # add noise
        alpha = tf.random_uniform(shape=[config.batchsize, 1], minval=0., maxval=1.)  # eps
        diff = tf.reshape((x_ - x), [config.batchsize, np.prod(x.get_shape().as_list()[1:])])
        outputs['x_hat'] = x_hat = x + tf.reshape(alpha * diff, [config.batchsize, *x.get_shape().as_list()[1:]])

        outputs['d_hat_features'], outputs['d_hat'] = evaluate_discriminator(discriminator, x_hat)

        # encoder training:
        outputs['d_enc_features'], outputs['d_enc'] = evaluate_discriminator(discriminator, x_enc)

    return outputs
示例#16
0
def adversarial_autoencoder(z, x, dropout_rate, dropout, config):
    outputs = {}

    with tf.variable_scope('Encoder'):
        encoder = build_unified_encoder(x.get_shape().as_list(),
                                        config.intermediateResolutions)

        temp_out = x
        for layer in encoder:
            temp_out = layer(temp_out)

    with tf.variable_scope("Bottleneck"):
        intermediate_conv = Conv2D(temp_out.get_shape().as_list()[3] // 8,
                                   1,
                                   padding='same')
        intermediate_conv_reverse = Conv2D(temp_out.get_shape().as_list()[3],
                                           1,
                                           padding='same')
        dropout_layer = Dropout(dropout_rate)
        temp_out = intermediate_conv(temp_out)

        reshape = temp_out.get_shape().as_list()[1:]
        z_layer = Dense(config.zDim)
        dec_dense = Dense(np.prod(reshape))

        outputs['z_'] = z_ = dropout_layer(z_layer(Flatten()(temp_out)),
                                           dropout)
        reshaped = tf.reshape(dropout_layer(dec_dense(z_), dropout),
                              [-1, *reshape])
        temp_out = intermediate_conv_reverse(reshaped)

    with tf.variable_scope('Decoder'):
        decoder = build_unified_decoder(config.outputWidth,
                                        config.intermediateResolutions,
                                        config.numChannels)

        # Decode: z -> x_hat
        for layer in decoder:
            temp_out = layer(temp_out)

        outputs['x_hat'] = temp_out

    # Discriminator
    with tf.variable_scope('Discriminator'):
        discriminator = [
            Dense(50, activation=leaky_relu),
            Dense(50, activation=leaky_relu),
            Dense(1)
        ]

        # fake
        temp_out = z_
        for layer in discriminator:
            temp_out = layer(temp_out)
        outputs['d_'] = temp_out

        # real
        temp_out = z
        for layer in discriminator:
            temp_out = layer(temp_out)
        outputs['d'] = temp_out

        # adding noise
        epsilon = tf.random_uniform([config.batchsize, 1],
                                    minval=0.,
                                    maxval=1.)
        outputs['z_hat'] = z_hat = z + epsilon * (z - z_)

        temp_out = z_hat
        for layer in discriminator:
            temp_out = layer(temp_out)
        outputs['d_hat'] = temp_out

    return outputs
示例#17
0
 def setup_dist_params(self, latent_policy):
     return Dense(units=2, activation=None,
                  kernel_initializer="he_uniform")(latent_policy)
示例#18
0
 def setup_dist_params(self, latent_policy):
     log_params = Dense(
         units=2,
         activation=None,
         kernel_initializer=constant(value=1.0e-10))(latent_policy)
     return tf1.add(tf1.exp(log_params), 1)
示例#19
0
def gaussian_mixture_variational_autoencoder(x, dropout_rate, dropout, config):
    layers = {}
    # encoding network q(z|x) and q(w|x)
    with tf.variable_scope('Encoder'):
        encoder = build_unified_encoder(x.get_shape().as_list(), config.intermediateResolutions)

        temp_out = x
        for layer in encoder:
            temp_out = layer(temp_out)

    with tf.variable_scope("Bottleneck"):
        intermediate_conv = Conv2D(temp_out.get_shape().as_list()[3] // 8, 1, padding='same')
        intermediate_conv_reverse = Conv2D(temp_out.get_shape().as_list()[3], 1, padding='same')
        dropout_layer = Dropout(dropout_rate)
        temp_out = intermediate_conv(temp_out)
        reshape = temp_out.get_shape().as_list()[1:]

        w_mu_layer = Dense(config.dim_w)
        w_log_sigma_layer = Dense(config.dim_w)

        z_mu_layer = Dense(config.dim_z)
        z_log_sigma_layer = Dense(config.dim_z)
        dec_dense = Dense(np.prod(reshape))

        flatten = Flatten()(temp_out)

        layers['w_mu'] = w_mu = dropout_layer(w_mu_layer(flatten), dropout)
        layers['w_log_sigma'] = w_log_sigma = dropout_layer(w_log_sigma_layer(flatten), dropout)
        layers['w_sampled'] = w_sampled = w_mu + tf.random_normal(tf.shape(w_log_sigma)) * tf.exp(0.5 * w_log_sigma)

        layers['z_mu'] = z_mu = dropout_layer(z_mu_layer(flatten), dropout)
        layers['z_log_sigma'] = z_log_sigma = dropout_layer(z_log_sigma_layer(flatten))
        layers['z_sampled'] = z_sampled = z_mu + tf.random_normal(tf.shape(z_log_sigma)) * tf.exp(0.5 * z_log_sigma)

        temp_out = intermediate_conv_reverse(tf.reshape(dropout_layer(dec_dense(z_sampled), dropout), [-1, *reshape]))

    # posterior p(z|w,c)
    z_wc_mu_layer = Dense(config.dim_z * config.dim_c)
    z_wc_log_sigma_layer = Dense(config.dim_z * config.dim_c)

    z_wc_mu = z_wc_mu_layer(w_sampled)
    z_wc_log_sigma = z_wc_log_sigma_layer(w_sampled)
    z_wc_log_sigma_inv = tf.nn.bias_add(z_wc_log_sigma, bias=tf.Variable(tf.constant(0.1, shape=[z_wc_log_sigma.get_shape()[-1]], dtype=tf.float32)))
    layers['z_wc_mus'] = z_wc_mus = tf.reshape(z_wc_mu, [-1, config.dim_z, config.dim_c])
    layers['z_wc_log_sigma_invs'] = z_wc_log_sigma_invs = tf.reshape(z_wc_log_sigma_inv, [-1, config.dim_z, config.dim_c])
    # reparametrization
    layers['z_wc_sampled'] = z_wc_mus + tf.random_normal(tf.shape(z_wc_log_sigma_invs)) * tf.exp(z_wc_log_sigma_invs)

    # decoder p(x|z)
    with tf.variable_scope('Decoder'):
        decoder = build_unified_decoder(config.outputWidth, config.intermediateResolutions, config.numChannels)

        for layer in decoder:
            temp_out = layer(temp_out)

        layers['xz_mu'] = temp_out

    # prior p(c)
    z_sample = tf.tile(tf.expand_dims(z_sampled, -1), [1, 1, config.dim_c])
    loglh = -0.5 * (tf.squared_difference(z_sample, z_wc_mus) * tf.exp(z_wc_log_sigma_invs)) - z_wc_log_sigma_invs + tf.log(np.pi)
    loglh_sum = tf.reduce_sum(loglh, 1)
    layers['pc_logit'] = loglh_sum
    layers['pc'] = tf.nn.softmax(loglh_sum)

    return layers