forked from tensorfreitas/DCGAN-for-Bird-Generation
-
Notifications
You must be signed in to change notification settings - Fork 1
/
traindcgan.py
288 lines (224 loc) · 10.7 KB
/
traindcgan.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import time
import os
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import numpy as np
from keras.models import Sequential
from keras.layers import Conv2D, Conv2DTranspose, Reshape
from keras.layers import Flatten, BatchNormalization, Dense, Activation
from keras.layers.advanced_activations import LeakyReLU
from keras.optimizers import Adam
from keras.preprocessing.image import ImageDataGenerator
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
# Here is where we will load the dataset stored in dataset_path. In this script
# we will use the Caltech-UCSD Birds-200-2011 dataset which includes 11788
# images from 200 different birds. We will feed the images without applying
# the provided bounding boxes from the dataset. The data will only be resized
# and normalized. Keras ImageDataGenerator will be used for loading the dataset
def load_dataset(dataset_path, batch_size, image_shape):
dataset_generator = ImageDataGenerator()
dataset_generator = dataset_generator.flow_from_directory(
dataset_path, target_size=(image_shape[0], image_shape[1]),
batch_size=batch_size,
class_mode=None)
return dataset_generator
# Creates the discriminator model. This model tries to classify images as real
# or fake.
def construct_discriminator(image_shape):
discriminator = Sequential()
discriminator.add(Conv2D(filters=64, kernel_size=(5, 5),
strides=(2, 2), padding='same',
data_format='channels_last',
kernel_initializer='glorot_uniform',
input_shape=(image_shape)))
discriminator.add(LeakyReLU(0.2))
discriminator.add(Conv2D(filters=128, kernel_size=(5, 5),
strides=(2, 2), padding='same',
data_format='channels_last',
kernel_initializer='glorot_uniform'))
discriminator.add(BatchNormalization(momentum=0.5))
discriminator.add(LeakyReLU(0.2))
discriminator.add(Conv2D(filters=256, kernel_size=(5, 5),
strides=(2, 2), padding='same',
data_format='channels_last',
kernel_initializer='glorot_uniform'))
discriminator.add(BatchNormalization(momentum=0.5))
discriminator.add(LeakyReLU(0.2))
discriminator.add(Conv2D(filters=512, kernel_size=(5, 5),
strides=(2, 2), padding='same',
data_format='channels_last',
kernel_initializer='glorot_uniform'))
discriminator.add(BatchNormalization(momentum=0.5))
discriminator.add(LeakyReLU(0.2))
discriminator.add(Flatten())
discriminator.add(Dense(1))
discriminator.add(Activation('sigmoid'))
optimizer = Adam(lr=0.0002, beta_1=0.5)
discriminator.compile(loss='binary_crossentropy',
optimizer=optimizer,
metrics=None)
return discriminator
# Creates the generator model. This model has an input of random noise and
# generates an image that will try mislead the discriminator.
def construct_generator():
generator = Sequential()
generator.add(Dense(units=4 * 4 * 512,
kernel_initializer='glorot_uniform',
input_shape=(1, 1, 100)))
generator.add(Reshape(target_shape=(4, 4, 512)))
generator.add(BatchNormalization(momentum=0.5))
generator.add(Activation('relu'))
generator.add(Conv2DTranspose(filters=256, kernel_size=(5, 5),
strides=(2, 2), padding='same',
data_format='channels_last',
kernel_initializer='glorot_uniform'))
generator.add(BatchNormalization(momentum=0.5))
generator.add(Activation('relu'))
generator.add(Conv2DTranspose(filters=128, kernel_size=(5, 5),
strides=(2, 2), padding='same',
data_format='channels_last',
kernel_initializer='glorot_uniform'))
generator.add(BatchNormalization(momentum=0.5))
generator.add(Activation('relu'))
generator.add(Conv2DTranspose(filters=64, kernel_size=(5, 5),
strides=(2, 2), padding='same',
data_format='channels_last',
kernel_initializer='glorot_uniform'))
generator.add(BatchNormalization(momentum=0.5))
generator.add(Activation('relu'))
generator.add(Conv2DTranspose(filters=3, kernel_size=(5, 5),
strides=(2, 2), padding='same',
data_format='channels_last',
kernel_initializer='glorot_uniform'))
generator.add(Activation('tanh'))
optimizer = Adam(lr=0.00015, beta_1=0.5)
generator.compile(loss='binary_crossentropy',
optimizer=optimizer,
metrics=None)
return generator
# Displays a figure of the generated images and saves them in as .png image
def save_generated_images(generated_images, epoch, batch_number):
plt.figure(figsize=(8, 8), num=2)
gs1 = gridspec.GridSpec(8, 8)
gs1.update(wspace=0, hspace=0)
for i in range(64):
ax1 = plt.subplot(gs1[i])
ax1.set_aspect('equal')
image = generated_images[i, :, :, :]
image += 1
image *= 127.5
fig = plt.imshow(image.astype(np.uint8))
plt.axis('off')
fig.axes.get_xaxis().set_visible(False)
fig.axes.get_yaxis().set_visible(False)
plt.tight_layout()
save_name = 'generated images/generatedSamples_epoch' + str(
epoch + 1) + '_batch' + str(batch_number + 1) + '.png'
plt.savefig(save_name, bbox_inches='tight', pad_inches=0)
plt.pause(0.0000000001)
plt.show()
# Main train function
def train_dcgan(batch_size, epochs, image_shape, dataset_path):
# Build the adversarial model that consists in the generator output
# connected to the discriminator
generator = construct_generator()
discriminator = construct_discriminator(image_shape)
gan = Sequential()
# Only false for the adversarial model
discriminator.trainable = False
gan.add(generator)
gan.add(discriminator)
optimizer = Adam(lr=0.00015, beta_1=0.5)
gan.compile(loss='binary_crossentropy', optimizer=optimizer,
metrics=None)
# Create a dataset Generator with help of keras
dataset_generator = load_dataset(dataset_path, batch_size, image_shape)
# 11788 is the total number of images on the bird dataset
number_of_batches = int(11788 / batch_size)
# Variables that will be used to plot the losses from the discriminator and
# the adversarial models
adversarial_loss = np.empty(shape=1)
discriminator_loss = np.empty(shape=1)
batches = np.empty(shape=1)
# Allo plot updates inside for loop
plt.ion()
current_batch = 0
# Let's train the DCGAN for n epochs
for epoch in range(epochs):
print("Epoch " + str(epoch+1) + "/" + str(epochs) + " :")
for batch_number in range(number_of_batches):
start_time = time.time()
# Get the current batch and normalize the images between -1 and 1
real_images = dataset_generator.next()
real_images /= 127.5
real_images -= 1
# The last batch is smaller than the other ones, so we need to
# take that into account
current_batch_size = real_images.shape[0]
# Generate noise
noise = np.random.normal(0, 1,
size=(current_batch_size,) + (1, 1, 100))
# Generate images
generated_images = generator.predict(noise)
# Add some noise to the labels that will be
# fed to the discriminator
real_y = (np.ones(current_batch_size) -
np.random.random_sample(current_batch_size) * 0.2)
fake_y = np.random.random_sample(current_batch_size) * 0.2
# Let's train the discriminator
discriminator.trainable = True
d_loss = discriminator.train_on_batch(real_images, real_y)
d_loss += discriminator.train_on_batch(generated_images, fake_y)
discriminator_loss = np.append(discriminator_loss, d_loss)
# Now it's time to train the generator
discriminator.trainable = False
noise = np.random.normal(0, 1,
size=(current_batch_size * 2,) +
(1, 1, 100))
# We try to mislead the discriminator by giving the opposite labels
fake_y = (np.ones(current_batch_size * 2) -
np.random.random_sample(current_batch_size * 2) * 0.2)
g_loss = gan.train_on_batch(noise, fake_y)
adversarial_loss = np.append(adversarial_loss, g_loss)
batches = np.append(batches, current_batch)
# Each 50 batches show and save images
if((batch_number + 1) % 50 == 0 and
current_batch_size == batch_size):
save_generated_images(generated_images, epoch, batch_number)
time_elapsed = time.time() - start_time
# Display and plot the results
print(" Batch " + str(batch_number + 1) + "/" +
str(number_of_batches) +
" generator loss | discriminator loss : " +
str(g_loss) + " | " + str(d_loss) + ' - batch took ' +
str(time_elapsed) + ' s.')
current_batch += 1
# Save the model weights each 5 epochs
if (epoch + 1) % 5 == 0:
discriminator.trainable = True
generator.save('models/generator_epoch' + str(epoch) + '.hdf5')
discriminator.save('models/discriminator_epoch' +
str(epoch) + '.hdf5')
# Each epoch update the loss graphs
plt.figure(1)
plt.plot(batches, adversarial_loss, color='green',
label='Generator Loss')
plt.plot(batches, discriminator_loss, color='blue',
label='Discriminator Loss')
plt.title("DCGAN Train")
plt.xlabel("Batch Iteration")
plt.ylabel("Loss")
if epoch == 0:
plt.legend()
plt.pause(0.0000000001)
plt.show()
plt.savefig('trainingLossPlot.png')
def main():
dataset_path = '/home/tfreitas/Datasets/CUB_200_2011/CUB_200_2011/images/'
batch_size = 64
image_shape = (64, 64, 3)
epochs = 190
train_dcgan(batch_size, epochs,
image_shape, dataset_path)
if __name__ == "__main__":
main()