<commit_before>import numpy as np
import pickle
import random
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import time, os
import tensorflow as tf
import layers


EPOCHS = 100
STEPS_PER_CHECKPOINT = 5
BATCH_SIZE = 100

TRAINING_DIR = './model/'


with open('../dataset/mnist/mnist.pkl', 'rb') as f:
    data = pickle.load(f, encoding='latin')


(train_images, train_labels), (valid_images, valid_labels), (test_images, test_labels) = data
#разбивка на тренировочные картинки, проверочные, тестовые.
train_images = np.reshape(train_images, [-1, 28, 28, 1])
train_labels = np.reshape(train_labels, [-1, 1])
valid_images = np.reshape(valid_images, [-1, 28, 28, 1])
valid_labels = np.reshape(valid_labels, [-1, 1])
test_images = np.reshape(test_images, [-1, 28, 28, 1])
test_labels = np.reshape(test_labels, [-1, 1])

# дополнение до размера 32x32
train_images = np.pad(train_images, ((0, 0), (2, 2), (2, 2), (0, 0)), mode='edge')
valid_images = np.pad(valid_images, ((0, 0), (2, 2), (2, 2), (0, 0)), mode='edge')
test_images = np.pad(test_images, ((0, 0), (2, 2), (2, 2), (0, 0)), mode='edge')

#функция рисования результатов работы генератора
def plot(samples):
    fig = plt.figure(figsize=(4, 4))
    gs = gridspec.GridSpec(4, 4)
    gs.update(wspace=0.05, hspace=0.05)

    for i, sample in enumerate(samples):
        ax = plt.subplot(gs[i])
        plt.axis('off')
        ax.set_xticklabels([])
        ax.set_yticklabels([])
        ax.set_aspect('equal')
        plt.imshow(sample.squeeze(), cmap='gray')

    return fig


def sample_seed_inputs(m, n):
    return np.random.uniform(-1., 1., size=[m, n])

GENERATOR_SEED_SIZE = 100

def generator(inputs, batch_size, training):
    with tf.name_scope('generator'):
        net = layers.fully_connected_layer(1, inputs, 4 * 4 * 512, None)
        net = tf.reshape(net, [batch_size, 4, 4, 512])
        net = layers.batch_norm(net, training, name='bn1')
        net = layers.conv2d_transpose_layer(1, net, [5, 5, 256], batch_size, stride=2)
        net = layers.batch_norm(net, training, name='bn2')
        net = layers.conv2d_transpose_layer(2, net, [5, 5, 128], batch_size, stride=2)
        net = layers.batch_norm(net, training, name='bn3')
        net = layers.conv2d_transpose_layer(3, net, [5, 5, 1], batch_size, tf.nn.sigmoid, stride=2, zero_biases=True)

        return net

def discriminator(inputs, labels, training):
    with tf.name_scope('discriminator'):
        #net = layers.batch_norm(inputs, training, name='bn1')
        net = layers.conv2d_layer(1, inputs, [5, 5, 16], lambda x: layers.lrelu(x, 0.2), stride=2)
        #net = layers.batch_norm(net, training, name='bn2')
        net = layers.conv2d_layer(2, net, [5, 5, 32], lambda x: layers.lrelu(x, 0.2), stride=2)
        #net = layers.batch_norm(net, training, name='bn3')
        net = layers.conv2d_layer(3, net, [5, 5, 64], lambda x: layers.lrelu(x, 0.2), stride=2)
        #net = layers.batch_norm(net, training, name='bn4')
        net = layers.conv2d_layer(4, net, [5, 5, 128], lambda x: layers.lrelu(x, 0.2), stride=2)
        net = layers.max_pool2d(net, [2, 2])
        #net = layers.batch_norm(net, training, name='bn5')
        net = layers.conv2d_layer(5, net, [1, 1, 1], tf.nn.sigmoid)
        net = tf.reshape(net, [-1, 1])

        return net

#обнуление графа
tf.reset_default_graph()

#создание сети в графе
with tf.name_scope('GAN'):
    training_mode = tf.placeholder(tf.bool, name='training_mode')
    labels_inputs = tf.placeholder(tf.int32, [BATCH_SIZE, 1], name='labels_inputs')
    _labels_inputs = tf.cast(tf.one_hot(tf.squeeze(labels_inputs), 10), tf.float32)
    _labels_inputs = tf.reshape(_labels_inputs, [-1, 10])

    generator_seed_inputs = tf.placeholder(tf.float32, [BATCH_SIZE, GENERATOR_SEED_SIZE], name='generator_seed_inputs')

    _generator_inputs = generator_seed_inputs
    with tf.variable_scope('generator'):
        _inputs = _generator_inputs# tf.concat([_generator_inputs, _labels_inputs], axis=1)
        generator_outputs = generator(_inputs, BATCH_SIZE, training_mode)

    discriminator_inputs = tf.placeholder(tf.float32, [BATCH_SIZE] + list(train_images.shape[1:]), name='inputs')
    with tf.variable_scope('discriminator') as vs:
        with tf.name_scope('real'):
            discriminator_outputs_real_prob = discriminator(discriminator_inputs, _labels_inputs, training_mode)
        vs.reuse_variables()<commit_msg>добавили лейблы к DC GAN<commit_after>import numpy as np
import pickle
import random
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import time, os
import tensorflow as tf
import layers


EPOCHS = 100
STEPS_PER_CHECKPOINT = 5
BATCH_SIZE = 100

TRAINING_DIR = './model/'


with open('../dataset/mnist/mnist.pkl', 'rb') as f:
    data = pickle.load(f, encoding='latin')


(train_images, train_labels), (valid_images, valid_labels), (test_images, test_labels) = data
#разбивка на тренировочные картинки, проверочные, тестовые.
train_images = np.reshape(train_images, [-1, 28, 28, 1])
train_labels = np.reshape(train_labels, [-1, 1])
valid_images = np.reshape(valid_images, [-1, 28, 28, 1])
valid_labels = np.reshape(valid_labels, [-1, 1])
test_images = np.reshape(test_images, [-1, 28, 28, 1])
test_labels = np.reshape(test_labels, [-1, 1])

# дополнение до размера 32x32
train_images = np.pad(train_images, ((0, 0), (2, 2), (2, 2), (0, 0)), mode='edge')
valid_images = np.pad(valid_images, ((0, 0), (2, 2), (2, 2), (0, 0)), mode='edge')
test_images = np.pad(test_images, ((0, 0), (2, 2), (2, 2), (0, 0)), mode='edge')

#функция рисования результатов работы генератора
def plot(samples):
    fig = plt.figure(figsize=(4, 4))
    gs = gridspec.GridSpec(4, 4)
    gs.update(wspace=0.05, hspace=0.05)

    for i, sample in enumerate(samples):
        ax = plt.subplot(gs[i])
        plt.axis('off')
        ax.set_xticklabels([])
        ax.set_yticklabels([])
        ax.set_aspect('equal')
        plt.imshow(sample.squeeze(), cmap='gray')

    return fig


def sample_seed_inputs(m, n):
    return np.random.uniform(-1., 1., size=[m, n])

GENERATOR_SEED_SIZE = 100

def generator(inputs, batch_size, training):
    with tf.name_scope('generator'):
        net = layers.fully_connected_layer(1, inputs, 4 * 4 * 512, None)
        net = tf.reshape(net, [batch_size, 4, 4, 512])
        net = layers.batch_norm(net, training, name='bn1')
        net = layers.conv2d_transpose_layer(1, net, [5, 5, 256], batch_size, stride=2)
        net = layers.batch_norm(net, training, name='bn2')
        net = layers.conv2d_transpose_layer(2, net, [5, 5, 128], batch_size, stride=2)
        net = layers.batch_norm(net, training, name='bn3')
        net = layers.conv2d_transpose_layer(3, net, [5, 5, 1], batch_size, tf.nn.sigmoid, stride=2, zero_biases=True)

        return net

def discriminator(inputs, labels, training):
    with tf.name_scope('discriminator'):
        #net = layers.batch_norm(inputs, training, name='bn1')
        net = layers.conv2d_layer(1, inputs, [5, 5, 16], lambda x: layers.lrelu(x, 0.2), stride=2)
        #net = layers.batch_norm(net, training, name='bn2')
        net = layers.conv2d_layer(2, net, [5, 5, 32], lambda x: layers.lrelu(x, 0.2), stride=2)
        #net = layers.batch_norm(net, training, name='bn3')
        net = layers.conv2d_layer(3, net, [5, 5, 64], lambda x: layers.lrelu(x, 0.2), stride=2)
        #net = layers.batch_norm(net, training, name='bn4')
        net = layers.conv2d_layer(4, net, [5, 5, 128], lambda x: layers.lrelu(x, 0.2), stride=2)
        net = layers.max_pool2d(net, [2, 2])
        #net = layers.batch_norm(net, training, name='bn5')
        _labels = tf.reshape(labels, [-1, 1, 1, 10])
        net = tf.concat([net, _labels], axis=3)
        net = tf.reshape(net, [-1, 1, 1, 138])
        net = layers.conv2d_layer(5, net, [1, 1, 1], tf.nn.sigmoid)
        net = tf.reshape(net, [-1, 1])

        return net

#обнуление графа
tf.reset_default_graph()

#создание сети в графе
with tf.name_scope('GAN'):
    training_mode = tf.placeholder(tf.bool, name='training_mode')
    labels_inputs = tf.placeholder(tf.int32, [BATCH_SIZE, 1], name='labels_inputs')
    _labels_inputs = tf.cast(tf.one_hot(tf.squeeze(labels_inputs), 10), tf.float32)
    _labels_inputs = tf.reshape(_labels_inputs, [-1, 10])

    generator_seed_inputs = tf.placeholder(tf.float32, [BATCH_SIZE, GENERATOR_SEED_SIZE], name='generator_seed_inputs')

    _generator_inputs = generator_seed_inputs
    with tf.variable_scope('generator'):
        _inputs = tf.concat([_generator_inputs, _labels_inputs], axis=1)
        generator_outputs = generator(_inputs, BATCH_SIZE, training_mode)

    discriminator_inputs = tf.placeholder(tf.float32, [BATCH_SIZE] + list(train_images.shape[1:]), name='inputs')
    with tf.variable_scope('discriminator') as vs:
        with tf.name_scope('real'):
            discriminator_outputs_real_prob = discriminator(discriminator_inputs, _labels_inputs, training_mode)
        vs.reuse_variables()
{'fraction_non_alphanumeric': 0.15245648821326283, 'fraction_numerical': 0.04053756333994272, 'mean_word_length': 3.972070098576123, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1143387', 'n_tokens_mistral': 3544, 'n_tokens_neox': 3361, 'n_words': 756}