Exemple #1
0
def discriminator_128(image, labels, df_dim, number_classes, act=tf.nn.relu):
    """Builds the discriminator graph.

  Args:
    image: The current batch of images to classify as fake or real.
    labels: The corresponding labels for the images.
    df_dim: The df dimension.
    number_classes: The number of classes in the labels.
    act: The activation function used in the discriminator.
  Returns:
    - A `Tensor` representing the logits of the discriminator.
    - A list containing all trainable varaibles defined by the model.
  """
    with tf.compat.v1.variable_scope(
            'discriminator', reuse=tf.compat.v1.AUTO_REUSE) as dis_scope:
        h0 = optimized_block(image, df_dim, 'd_optimized_block1',
                             act=act)  # 64 * 64
        h1 = block(h0, df_dim * 2, 'd_block2', act=act)  # 32 * 32
        h1 = ops.sn_non_local_block_sim(h1, name='d_ops')  # 32 * 32
        h2 = block(h1, df_dim * 4, 'd_block3', act=act)  # 16 * 16
        h3 = block(h2, df_dim * 8, 'd_block4', act=act)  # 8 * 8
        h4 = block(h3, df_dim * 16, 'd_block5', act=act)  # 4 * 4
        h5 = block(h4, df_dim * 16, 'd_block6', downsample=False, act=act)
        h5_act = act(h5)
        h6 = tf.reduce_sum(input_tensor=h5_act, axis=[1, 2])
        output = ops.snlinear(h6, 1, name='d_sn_linear')
        classification_output = ops.snlinear(h6,
                                             flags.FLAGS.num_classes,
                                             name='d_sn_linear_class')
        if labels is None:
            pseudo_labels = tf.argmax(classification_output, axis=1)
            h_labels = ops.sn_embedding(pseudo_labels,
                                        number_classes,
                                        df_dim * 16,
                                        name='d_embedding')
        else:
            h_labels = ops.sn_embedding(labels,
                                        number_classes,
                                        df_dim * 16,
                                        name='d_embedding')

        output += tf.reduce_sum(input_tensor=h6 * h_labels,
                                axis=1,
                                keepdims=True)

    var_list = tf.compat.v1.get_collection(
        tf.compat.v1.GraphKeys.TRAINABLE_VARIABLES, dis_scope.name)
    return output, classification_output, var_list
Exemple #2
0
def biggan_discriminator_128(image,
                             labels,
                             df_dim,
                             number_classes,
                             act=tf.nn.relu):
    """Builds the discriminator graph.
  
  TODO(ilyak): debug, this implementation doesn't work as is.
  ...
  Only position of the non local block changes
  """
    with tf.compat.v1.variable_scope(
            'discriminator', reuse=tf.compat.v1.AUTO_REUSE) as dis_scope:
        h0 = optimized_block(image, df_dim, 'd_optimized_block1',
                             act=act)  # 64 * 64
        h0 = ops.sn_non_local_block_sim(h0, name='d_ops')  # 64 * 64
        h1 = block(h0, df_dim * 2, 'd_block2', act=act)  # 32 * 32
        h2 = block(h1, df_dim * 4, 'd_block3', act=act)  # 16 * 16
        h3 = block(h2, df_dim * 8, 'd_block4', act=act)  # 8 * 8
        h4 = block(h3, df_dim * 16, 'd_block5', act=act)  # 4 * 4
        h5 = block(h4, df_dim * 16, 'd_block6', downsample=False, act=act)
        h5_act = act(h5)
        h6 = tf.reduce_sum(input_tensor=h5_act, axis=[1, 2])
        output = ops.snlinear(h6, 1, name='d_sn_linear')
        classification_output = ops.snlinear(h6,
                                             flags.FLAGS.num_classes,
                                             name='d_sn_linear_class')
        if labels is None:
            pseudo_labels = tf.argmax(classification_output, axis=1)
            h_labels = ops.sn_embedding(pseudo_labels,
                                        number_classes,
                                        df_dim * 16,
                                        name='d_embedding')
        else:
            h_labels = ops.sn_embedding(labels,
                                        number_classes,
                                        df_dim * 16,
                                        name='d_embedding')

        output += tf.reduce_sum(input_tensor=h6 * h_labels,
                                axis=1,
                                keepdims=True)

    var_list = tf.compat.v1.get_collection(
        tf.compat.v1.GraphKeys.TRAINABLE_VARIABLES, dis_scope.name)
    return output, classification_output, var_list
Exemple #3
0
    def test_sn_embedding_shapes(self):
        """Tests the spectrally normalized embedding layer.

    When label = 10, embedding_size = 128, the
    output shape should be [10, 128]
    """
        if tf.executing_eagerly():
            # `compute_spectral_norm` doesn't work when executing eagerly.
            return
        label = tf.ones([
            10,
        ], dtype=tf.int32)
        vector = ops.sn_embedding(label,
                                  number_classes=1000,
                                  embedding_size=128)
        self.assertEqual([10, 128], vector.shape.as_list())