discriminators

Convolutional Discriminators.

Classes

MultiScaleDiscriminator Multi-Scale discriminator.
PatchDiscriminator Pix2Pix discriminator.
class ashpy.models.convolutional.discriminators.MultiScaleDiscriminator(input_res, min_res, kernel_size, initial_filters, filters_cap, use_dropout=True, dropout_prob=0.3, non_linearity=<class 'tensorflow.python.keras.layers.advanced_activations.LeakyReLU'>, normalization_layer=<class 'ashpy.layers.instance_normalization.InstanceNormalization'>, use_attention=False, n_discriminators=1)[source]

Bases: tensorflow.python.keras.engine.training.Model

Multi-Scale discriminator.

This discriminator architecture is composed by multiple discriminators working at different scales. Each discriminator is a ashpy.models.convolutional.discriminators.PatchDiscriminator.

Examples

x = tf.ones((1, 256, 256, 3))

# instantiate the PathDiscriminator
multiScaleDiscriminator = MultiScaleDiscriminator(input_res=256,
                                                  min_res=16,
                                                  kernel_size=5,
                                                  initial_filters=64,
                                                  filters_cap=512,
                                                  n_discriminators=3
                                                  )

# evaluate passing x
outputs = multiScaleDiscriminator(x)

# the output shape is
# the same as the input shape
print(len(outputs))
for output in outputs:
    print(output.shape)
3
(1, 12, 12, 1)
(1, 12, 12, 1)
(1, 12, 12, 1)
__init__(input_res, min_res, kernel_size, initial_filters, filters_cap, use_dropout=True, dropout_prob=0.3, non_linearity=<class 'tensorflow.python.keras.layers.advanced_activations.LeakyReLU'>, normalization_layer=<class 'ashpy.layers.instance_normalization.InstanceNormalization'>, use_attention=False, n_discriminators=1)[source]

Multi Scale Discriminator.

Different generator for different scales of the input image.

Used by Pix2PixHD [1] .

Parameters:
  • input_res (int) – input resolution
  • min_res (int) – minimum resolution reached by the discriminators
  • kernel_size (int) – kernel size of discriminators
  • initial_filters (int) – number of initial filters in the first layer of the discriminators
  • filters_cap (int) – maximum number of filters in the discriminators
  • use_dropout (bool) – whether to use dropout
  • dropout_prob (float) – probability of dropout
  • non_linearity (tf.keras.layers.Layer) – non linearity used in discriminators
  • normalization_layer (tf.keras.layers.Layer) – normalization used by the discriminators
  • use_attention (bool) – whether to use attention
  • n_discriminators (int) – Number of discriminators
[1]High-Resolution Image Synthesis and Semantic Manipulation with Conditional GANs https://arxiv.org/abs/1711.11585
build_discriminator(input_res)[source]

Build a single discriminator using parameters defined in this object.

Parameters:input_res – input resolution of the discriminator.
Return type:Encoder
Returns:A Discriminator (PatchDiscriminator).
call(inputs, training=True, return_features=False)[source]

Forward pass of the Multi Scale Discriminator.

Parameters:
  • inputs (tf.Tensor) – input tensor.
  • training (bool) – whether is training or not.
  • return_features (bool) – whether to return features or not.
Return type:

Union[List[Tensor], Tuple[List[Tensor], List[Tensor]]]

Returns:

([tf.Tensor]) –

A List of Tensors containing the

value of D_i for each input.

([tf.Tensor]): A List of features for each discriminator if

return_features.

class ashpy.models.convolutional.discriminators.PatchDiscriminator(input_res, min_res, kernel_size, initial_filters, filters_cap, use_dropout=True, dropout_prob=0.3, non_linearity=<class 'tensorflow.python.keras.layers.advanced_activations.LeakyReLU'>, normalization_layer=<class 'ashpy.layers.instance_normalization.InstanceNormalization'>, use_attention=False)[source]

Bases: ashpy.models.convolutional.encoders.Encoder

Pix2Pix discriminator.

The last layer is an image in which each pixels is the probability of being fake or real.

Examples

x = tf.ones((1, 64, 64, 3))

# instantiate the PathDiscriminator
patchDiscriminator = PatchDiscriminator(input_res=64,
                                        min_res=16,
                                        kernel_size=5,
                                        initial_filters=64,
                                        filters_cap=512,
                                        )

# evaluate passing x
output = patchDiscriminator(x)

# the output shape is the same as the input shape
print(output.shape)
(1, 12, 12, 1)
__init__(input_res, min_res, kernel_size, initial_filters, filters_cap, use_dropout=True, dropout_prob=0.3, non_linearity=<class 'tensorflow.python.keras.layers.advanced_activations.LeakyReLU'>, normalization_layer=<class 'ashpy.layers.instance_normalization.InstanceNormalization'>, use_attention=False)[source]

Patch Discriminator used by pix2pix.

When min_res=1 this is the same as a standard fully convolutional discriminator.

Parameters:
  • input_res (int) – Input Resolution.
  • min_res (int) – Minimum Resolution reached by the discriminator.
  • kernel_size (int) – Kernel Size used in Conv Layer.
  • initial_filters (int) – number of filters in the first convolutional layer.
  • filters_cap (int) – Maximum number of filters.
  • use_dropout (bool) – whether to use dropout.
  • dropout_prob (float) – probability of dropout.
  • non_linearity (tf.keras.layers.Layer) – non linearity used in the model.
  • normalization_layer (tf.keras.layers.Layer) – normalization layer used in the model.
  • use_attention (bool) – whether to use attention.
_add_building_block(filters, use_bn=False)[source]

Construct the core of the tf.keras.Model.

The layers specified here get added to the tf.keras.Model multiple times consuming the hyper-parameters generated in the _get_layer_spec().

Parameters:filters (int) – Number of filters to use for this iteration of the Building Block.
_add_final_block(output_shape)[source]

Prepare the results of _add_building_block() for the final output.

Parameters:output_shape (int) – Amount of units of the last tf.keras.layers.Dense
call(inputs, training=False, return_features=False)[source]

Forward pass of the PatchDiscriminator.