forked from selfedu-rus/neural-network
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lesson 32. GAN.py
161 lines (115 loc) · 4.78 KB
/
lesson 32. GAN.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import numpy as np
import matplotlib.pyplot as plt
import time
from tensorflow.keras.datasets import mnist
import tensorflow.keras.backend as K
import tensorflow as tf
from tensorflow.keras.layers import Dense, Flatten, Reshape, Input, BatchNormalization, Dropout
from tensorflow.keras.layers import Conv2D, Conv2DTranspose, LeakyReLU
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train[y_train == 7]
y_train = y_train[y_train == 7]
BUFFER_SIZE = x_train.shape[0]
BATCH_SIZE = 100
BUFFER_SIZE = BUFFER_SIZE // BATCH_SIZE * BATCH_SIZE
x_train = x_train[:BUFFER_SIZE]
y_train = y_train[:BUFFER_SIZE]
print(x_train.shape, y_train.shape)
# стандартизация входных данных
x_train = x_train / 255
x_test = x_test / 255
x_train = np.reshape(x_train, (len(x_train), 28, 28, 1))
x_test = np.reshape(x_test, (len(x_test), 28, 28, 1))
train_dataset = tf.data.Dataset.from_tensor_slices(x_train).shuffle(BUFFER_SIZE).batch(BATCH_SIZE)
# формирование сетей
hidden_dim = 2
def dropout_and_batch():
return Dropout(0.3)(BatchNormalization())
# генератор
generator = tf.keras.Sequential([
Dense(7 * 7 * 256, activation='relu', input_shape=(hidden_dim,)),
BatchNormalization(),
Reshape((7, 7, 256)),
Conv2DTranspose(128, (5, 5), strides=(1, 1), padding='same', activation='relu'),
BatchNormalization(),
Conv2DTranspose(64, (5, 5), strides=(2, 2), padding='same', activation='relu'),
BatchNormalization(),
Conv2DTranspose(1, (5, 5), strides=(2, 2), padding='same', activation='sigmoid'),
])
# дискриминатор
discriminator = tf.keras.Sequential()
discriminator.add(Conv2D(64, (5, 5), strides=(2, 2), padding='same', input_shape=[28, 28, 1]))
discriminator.add(LeakyReLU())
discriminator.add(Dropout(0.3))
discriminator.add(Conv2D(128, (5, 5), strides=(2, 2), padding='same'))
discriminator.add(LeakyReLU())
discriminator.add(Dropout(0.3))
discriminator.add(Flatten())
discriminator.add(Dense(1))
# потери
cross_entropy = tf.keras.losses.BinaryCrossentropy(from_logits=True)
def generator_loss(fake_output):
loss = cross_entropy(tf.ones_like(fake_output), fake_output)
return loss
def discriminator_loss(real_output, fake_output):
real_loss = cross_entropy(tf.ones_like(real_output), real_output)
fake_loss = cross_entropy(tf.zeros_like(fake_output), fake_output)
total_loss = real_loss + fake_loss
return total_loss
generator_optimizer = tf.keras.optimizers.Adam(1e-4)
discriminator_optimizer = tf.keras.optimizers.Adam(1e-4)
# обучение
@tf.function
def train_step(images):
noise = tf.random.normal([BATCH_SIZE, hidden_dim])
with tf.GradientTape() as gen_tape, tf.GradientTape() as disc_tape:
generated_images = generator(noise, training=True)
real_output = discriminator(images, training=True)
fake_output = discriminator(generated_images, training=True)
gen_loss = generator_loss(fake_output)
disc_loss = discriminator_loss(real_output, fake_output)
gradients_of_generator = gen_tape.gradient(gen_loss, generator.trainable_variables)
gradients_of_discriminator = disc_tape.gradient(disc_loss, discriminator.trainable_variables)
generator_optimizer.apply_gradients(zip(gradients_of_generator, generator.trainable_variables))
discriminator_optimizer.apply_gradients(zip(gradients_of_discriminator, discriminator.trainable_variables))
return gen_loss, disc_loss
def train(dataset, epochs):
history = []
MAX_PRINT_LABEL = 10
th = BUFFER_SIZE // (BATCH_SIZE * MAX_PRINT_LABEL)
for epoch in range(1, epochs + 1):
print(f'{epoch}/{EPOCHS}: ', end='')
start = time.time()
n = 0
gen_loss_epoch = 0
for image_batch in dataset:
gen_loss, disc_loss = train_step(image_batch)
gen_loss_epoch += K.mean(gen_loss)
if (n % th == 0): print('=', end='')
n += 1
history += [gen_loss_epoch / n]
print(': ' + str(history[-1]))
print('Время эпохи {} составляет {} секунд'.format(epoch, time.time() - start))
return history
# запуск процесса обучения
EPOCHS = 20
history = train(train_dataset, EPOCHS)
plt.plot(history)
plt.grid(True)
plt.show()
# отображение результатов генерации
n = 2
total = 2 * n + 1
plt.figure(figsize=(total, total))
num = 1
for i in range(-n, n + 1):
for j in range(-n, n + 1):
ax = plt.subplot(total, total, num)
num += 1
img = generator.predict(np.expand_dims([0.5 * i / n, 0.5 * j / n], axis=0))
plt.imshow(img[0, :, :, 0], cmap='gray')
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.show()