<commit_before>"""
Standard GAN implemented on top of keras/tensorflow.

Note: Currently only supports Python 3.
"""

import os
import sys

from keras import applications
from keras import layers
from keras import models
from keras import optimizers
from keras.preprocessing import image
import numpy as np

from utils import plot_images


#
# directory paths
#

path = os.path.dirname(os.path.abspath(__file__))
cache_dir = os.path.join(path, 'cache')

# dimension of generator's input tensor
rand_dim = 112

#
# image dimensions
#

img_height = 112
img_width = 112
img_channels = 3

#
# training params
#

nb_steps = 10000
batch_size = 128
k_d = 1  # number of discriminator network updates per step
k_g = 2  # number of generative network updates per step
log_interval = 100  # interval (in steps) at which to log loss summaries & save plots of image samples to disc


#
# shared network params
#

kernel_size = (3, 3)
conv_layer_keyword_args = {'border_mode': 'same', 'subsample': (2, 2)}


def generator_network(input_tensor):
    def add_common_layers(y):
        y = layers.Activation('relu')(y)
        return y

    x = layers.Dense(1024)(input_tensor)
    x = add_common_layers(x)

    #
    # input dimensions to the first conv layer in the generator
    #

    height_dim = 7
    width_dim = 7
    nb_feature_maps = 128

    assert img_height % height_dim == 0 and img_width % width_dim == 0, \
        'Generator network must be able to transform `x` into a tensor of shape (img_height, img_width, img_channels).'

    x = layers.Dense(height_dim * width_dim * nb_feature_maps)(x)
    x = add_common_layers(x)

    x = layers.Reshape((height_dim, width_dim, nb_feature_maps))(x)

    nb_feature_maps = 512 * 2

    # generator will transform `x` into a tensor w/ the desired shape by up-sampling the spatial dimension of `x`
    # through a series of strided de-convolutions (each de-conv layer up-samples spatial dim of `x` by a factor of 2).
    while height_dim != img_height:
        height_dim *= 2  # (14 => 28 => 56 => 112 == img_height)
        width_dim *= 2
        nb_feature_maps //= 2  # (512 => 256 => 128 => 64)

        x = layers.convolutional.Deconvolution2D(nb_feature_maps, *kernel_size,
                                                 output_shape=(None, height_dim, width_dim, nb_feature_maps),
                                                 **conv_layer_keyword_args)(x)
        x = add_common_layers(x)

    # number of feature maps => number of image channels
    return layers.convolutional.Deconvolution2D(img_channels, *kernel_size, activation='tanh',
                                                border_mode='same',
                                                output_shape=(None, img_height, img_width, img_channels))(x)


def discriminator_network(input_image_tensor):
    def add_common_layers(y):
        y = layers.advanced_activations.LeakyReLU()(y)
        return y

    height_dim = 7

    x = input_image_tensor
    nb_feature_maps = 64 // 2

    # down sample with strided convolutions until we reach the desired spatial dimension
    while x.get_shape()[1] != height_dim:
        nb_feature_maps *= 2  # (64 => 128 => 256 => 512)

        x = layers.convolutional.Convolution2D(nb_feature_maps, *kernel_size, **conv_layer_keyword_args)(x)
        x = add_common_layers(x)

    x = layers.Flatten()(x)

    x = layers.Dense(1024)(x)
    x = add_common_layers(x)

    return layers.Dense(1, activation='sigmoid')(x)


def adversarial_training(data_dir, generator_model_path, discriminator_model_path):
    """
    Adversarial training of the generator network Gθ and discriminator network Dφ.

    """
    #
    # define model input and output tensors
    #

    generator_input_tensor = layers.Input(shape=(rand_dim, ))
    generated_image_tensor = generator_network(generator_input_tensor)

    generated_or_real_image_tensor = layers.Input(shape=(img_height, img_width, img_channels))
    discriminator_output = discriminator_network(generated_or_real_image_tensor)

    combined_output = discriminator_network(generator_network(generator_input_tensor))

    #
    # define models
    #

    generator_model = models.Model(input=generator_input_tensor, output=generated_image_tensor, name='generator')
    discriminator_model = models.Model(input=generated_or_real_image_tensor, output=discriminator_output,
                                       name='discriminator')
    combined_model = models.Model(input=generator_input_tensor, output=combined_output, name='combined')

    #
    # compile models
    #

    sgd = optimizers.SGD(lr=0.001)

    generator_model.compile(optimizer=sgd, loss='binary_crossentropy')
    discriminator_model.compile(optimizer=sgd, loss='binary_crossentropy', metrics=['accuracy'])
    discriminator_model.trainable = False
    combined_model.compile(optimizer=sgd, loss='binary_crossentropy', metrics=['accuracy'])

    #
    # data generators
    #

    data_generator = image.ImageDataGenerator(
        preprocessing_function=applications.xception.preprocess_input,
        dim_ordering='tf')

    flow_from_directory_params = {'target_size': (img_height, img_width),
                                  'color_mode': 'grayscale' if img_channels == 1 else 'rgb',
                                  'class_mode': None,
                                  'batch_size': batch_size}

    real_image_generator = data_generator.flow_from_directory(
        directory=data_dir,<commit_msg>Make the gen and disc have similar # of params.<commit_after>"""
Standard GAN implemented on top of keras/tensorflow.

Note: Currently only supports Python 3.
"""

import os
import sys

from keras import applications
from keras import layers
from keras import models
from keras import optimizers
from keras.preprocessing import image
import numpy as np

from utils import plot_images


#
# directory paths
#

path = os.path.dirname(os.path.abspath(__file__))
cache_dir = os.path.join(path, 'cache')

# dimension of generator's input tensor
rand_dim = 112

#
# image dimensions
#

img_height = 112
img_width = 112
img_channels = 3

#
# training params
#

nb_steps = 10000
batch_size = 128
k_d = 1  # number of discriminator network updates per step
k_g = 2  # number of generative network updates per step
log_interval = 100  # interval (in steps) at which to log loss summaries & save plots of image samples to disc


#
# shared network params
#

kernel_size = (3, 3)
conv_layer_keyword_args = {'border_mode': 'same', 'subsample': (2, 2)}


def generator_network(input_tensor):
    def add_common_layers(y):
        y = layers.Activation('relu')(y)
        return y

    #
    # input dimensions to the first conv layer in the generator
    #

    height_dim = 7
    width_dim = 7
    assert img_height % height_dim == 0 and img_width % width_dim == 0, \
        'Generator network must be able to transform `x` into a tensor of shape (img_height, img_width, img_channels).'

    # 7 * 7 * 16 == 784 input neurons
    x = layers.Dense(height_dim * width_dim * 16)(input_tensor)
    x = add_common_layers(x)

    x = layers.Reshape((height_dim, width_dim, -1))(x)

    # generator will transform `x` into a tensor w/ the desired shape by up-sampling the spatial dimension of `x`
    # through a series of strided de-convolutions (each de-conv layer up-samples spatial dim of `x` by a factor of 2).
    while height_dim != img_height:
        # spatial dim: (14 => 28 => 56 => 112 == img_height == img_width)
        height_dim *= 2
        width_dim *= 2

        # nb_feature_maps: (512 => 256 => 128 => 64)
        try:
            nb_feature_maps //= 2
        except NameError:
            nb_feature_maps = 512

        x = layers.convolutional.Deconvolution2D(nb_feature_maps, *kernel_size,
                                                 output_shape=(None, height_dim, width_dim, nb_feature_maps),
                                                 **conv_layer_keyword_args)(x)
        x = add_common_layers(x)

    # number of feature maps => number of image channels
    return layers.convolutional.Deconvolution2D(img_channels, 1, 1, activation='tanh',
                                                border_mode='same',
                                                output_shape=(None, img_height, img_width, img_channels))(x)


def discriminator_network(x):
    def add_common_layers(y):
        y = layers.advanced_activations.LeakyReLU()(y)
        return y

    height_dim = 7

    # down sample with strided convolutions until we reach the desired spatial dimension (7 * 7 * nb_feature_maps)
    while x.get_shape()[1] != height_dim:
        # nb_feature_maps: (64 => 128 => 256 => 512)
        try:
            nb_feature_maps *= 2
        except NameError:
            nb_feature_maps = 64

        x = layers.convolutional.Convolution2D(nb_feature_maps, *kernel_size, **conv_layer_keyword_args)(x)
        x = add_common_layers(x)

    x = layers.Flatten()(x)

    x = layers.Dense(16)(x)
    x = add_common_layers(x)

    return layers.Dense(1, activation='sigmoid')(x)


def adversarial_training(data_dir, generator_model_path, discriminator_model_path):
    """
    Adversarial training of the generator network Gθ and discriminator network Dφ.

    """
    #
    # define model input and output tensors
    #

    generator_input_tensor = layers.Input(shape=(rand_dim, ))
    generated_image_tensor = generator_network(generator_input_tensor)

    generated_or_real_image_tensor = layers.Input(shape=(img_height, img_width, img_channels))
    discriminator_output = discriminator_network(generated_or_real_image_tensor)

    combined_output = discriminator_network(generator_network(generator_input_tensor))

    #
    # define models
    #

    generator_model = models.Model(input=generator_input_tensor, output=generated_image_tensor, name='generator')
    discriminator_model = models.Model(input=generated_or_real_image_tensor, output=discriminator_output,
                                       name='discriminator')
    combined_model = models.Model(input=generator_input_tensor, output=combined_output, name='combined')

    #
    # compile models
    #

    sgd = optimizers.SGD(lr=0.001)

    generator_model.compile(optimizer=sgd, loss='binary_crossentropy')
    discriminator_model.compile(optimizer=sgd, loss='binary_crossentropy', metrics=['accuracy'])
    discriminator_model.trainable = False
    combined_model.compile(optimizer=sgd, loss='binary_crossentropy', metrics=['accuracy'])

    print(generator_model.summary())
    print(discriminator_model.summary())

    #
    # data generators
    #

    data_generator = image.ImageDataGenerator(
        preprocessing_function=applications.xception.preprocess_input,
        dim_ordering='tf')

    flow_from_directory_params = {'target_size': (img_height, img_width),
                                  'color_mode': 'grayscale' if img_channels == 1 else 'rgb',
                                  'class_mode': None,
                                  'batch_size': batch_size}

    real_image_generator = data_generator.flow_from_directory(
        directory=data_dir,
{'fraction_non_alphanumeric': 0.08971710821733274, 'fraction_numerical': 0.017691962281095645, 'mean_word_length': 3.0260303687635575, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3410098', 'n_tokens_mistral': 3511, 'n_tokens_neox': 3251, 'n_words': 830}