-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy path248-cifar_GAN.py
276 lines (224 loc) · 10.9 KB
/
248-cifar_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
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
"""
GAN: cifar10 data set
Following code trains and generates images based on the cifar10 dataset.
I've adapted the code by Jason Brownlee from his blogs on https://machinelearningmastery.com/
I seriously urge everyone to follow his blogs and get enlightened.
Original credit goes to Jason.
Regular GAN that generates images using a random latent vector as input.
While it works great we do not know the mapping of latent vector to the generated image.
Conditional GANs can be used to supply a label during taining so the latent vector
can be associated with a specific label - making the generation of images predictable.
"""
from numpy import zeros
from numpy import ones
from numpy.random import randn
from numpy.random import randint
from keras.datasets.cifar10 import load_data
from keras.optimizers import Adam
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Reshape
from keras.layers import Flatten
from keras.layers import Conv2D
from keras.layers import Conv2DTranspose
from keras.layers import LeakyReLU
from keras.layers import Dropout
from matplotlib import pyplot as plt
########################################################################
#Load data and plot to get a quick understanding
#CIFAR10 classes are: airplane, automobile, bird, cat, deer, dog, frog, horse,
# ship, truck
(trainX, trainy), (testX, testy) = load_data()
# plot 25 images
for i in range(25):
plt.subplot(5, 5, 1 + i)
plt.axis('off')
plt.imshow(trainX[i])
plt.show()
#############################################################################
#Define generator, discriminator, gan and other helper functions
#Using Sequential method from Keras as it makes the definition of models easy.
#We will use functional way of defining the model for the conditional gan
#but sequential for descriminator and generator as they are straightforward.
#########################################################################
# define the standalone discriminator model
#Given an input image, the Discriminator outputs the likelihood of the image being real.
#Binary classification - true or false (1 or 0). So using sigmoid activation.
def define_discriminator(in_shape=(32,32,3)):
model = Sequential()
model.add(Conv2D(128, (3,3), strides=(2,2), padding='same', input_shape=in_shape)) #16x16x128
model.add(LeakyReLU(alpha=0.2))
model.add(Conv2D(128, (3,3), strides=(2,2), padding='same')) #8x8x128
model.add(LeakyReLU(alpha=0.2))
model.add(Flatten()) #shape of 8192
model.add(Dropout(0.4))
model.add(Dense(1, activation='sigmoid')) #shape of 1
# compile model
opt = Adam(lr=0.0002, beta_1=0.5)
model.compile(loss='binary_crossentropy', optimizer=opt, metrics=['accuracy'])
return model
test_discr = define_discriminator()
print(test_discr.summary())
# define the standalone generator model
# #Given input of latent vector, the Generator produces an image.(here: 32x32)
#latent_dim, for example, can be 100, 1D array of size 100
#Here we are only using Dense and conv2dlayers. But network can be complicated based
#on the application. For example, you can use VGG for super res. GAN.
def define_generator(latent_dim): #latent_dim is the dimension of the latent vector (e.g., 100)
model = Sequential()
# We will reshape input latent vector into 8x8 image as a starting point.
#So n_nodes for the Dense layer can be 128x8x8 so when we reshape the output
#it would be 8x8x128 and that can be slowly upscaled to 32x32 image for output.
n_nodes = 128 * 8 * 8 #8192 nodes
model.add(Dense(n_nodes, input_dim=latent_dim)) #Dense layer so we can work with 1D latent vector
model.add(LeakyReLU(alpha=0.2))
model.add(Reshape((8, 8, 128))) #8x8x128 dataset from the latent vector.
# upsample to 16x16
model.add(Conv2DTranspose(128, (4,4), strides=(2,2), padding='same')) #16x16x128
model.add(LeakyReLU(alpha=0.2))
# upsample to 32x32
model.add(Conv2DTranspose(128, (4,4), strides=(2,2), padding='same')) #32x32x128
model.add(LeakyReLU(alpha=0.2))
# generate
model.add(Conv2D(3, (8,8), activation='tanh', padding='same')) #32x32x3
return model #Model not compiled as it is not directly trained like the discriminator.
#Generator is trained via GAN combined model.
test_gen = define_generator(100)
print(test_gen.summary())
# define the combined generator and discriminator model, for updating the generator
#Discriminator is trained separately so here only generator will be trained by keeping
#the discriminator constant.
def define_gan(generator, discriminator):
discriminator.trainable = False #Discriminator is trained separately. So set to not trainable.
# connect generator and discriminator
model = Sequential()
model.add(generator)
model.add(discriminator)
# compile model
opt = Adam(lr=0.0002, beta_1=0.5)
model.compile(loss='binary_crossentropy', optimizer=opt)
return model
# load cifar training images
def load_real_samples():
(trainX, _), (_, _) = load_data()
# cConvert to float and scale.
X = trainX.astype('float32')
# scale from [0,255] to [-1,1]
X = (X - 127.5) / 127.5 #Generator uses tanh activation so rescale
#original images to -1 to 1 to match the output of generator.
return X
# pick a batch of random real samples to train the GAN
#In fact, we will train the GAN on a half batch of real images and another
#half batch of fake images.
#For each real image we assign a label 1 and for fake we assign label 0.
def generate_real_samples(dataset, n_samples):
# choose random images
ix = randint(0, dataset.shape[0], n_samples)
# select the random images and assign it to X
X = dataset[ix]
# generate class labels and assign to y
y = ones((n_samples, 1)) ##Label=1 indicating they are real
return X, y
# generate n_samples number of latent vectors as input for the generator
def generate_latent_points(latent_dim, n_samples):
# generate points in the latent space
x_input = randn(latent_dim * n_samples)
# reshape into a batch of inputs for the network
x_input = x_input.reshape(n_samples, latent_dim)
return x_input
# use the generator to generate n fake examples, with class labels
#Supply the generator, latent_dim and number of samples as input.
#Use the above latent point generator to generate latent points.
def generate_fake_samples(generator, latent_dim, n_samples):
# generate points in latent space
x_input = generate_latent_points(latent_dim, n_samples)
# predict using generator to generate fake samples.
X = generator.predict(x_input)
# Class labels will be 0 as these samples are fake.
y = zeros((n_samples, 1)) #Label=0 indicating they are fake
return X, y
# train the generator and discriminator
#We loop through a number of epochs to train our Discriminator by first selecting
#a random batch of images from our true/real dataset.
#Then, generating a set of images using the generator.
#Feed both set of images into the Discriminator.
#Finally, set the loss parameters for both the real and fake images, as well as the combined loss.
def train(g_model, d_model, gan_model, dataset, latent_dim, n_epochs=100, n_batch=128):
bat_per_epo = int(dataset.shape[0] / n_batch)
half_batch = int(n_batch / 2) #the discriminator model is updated for a half batch of real samples
#and a half batch of fake samples, combined a single batch.
# manually enumerate epochs and bacthes.
for i in range(n_epochs):
# enumerate batches over the training set
for j in range(bat_per_epo):
# Train the discriminator on real and fake images, separately (half batch each)
#Research showed that separate training is more effective.
# get randomly selected 'real' samples
X_real, y_real = generate_real_samples(dataset, half_batch)
# update discriminator model weights
##train_on_batch allows you to update weights based on a collection
#of samples you provide
#Let us just capture loss and ignore accuracy value (2nd output below)
d_loss_real, _ = d_model.train_on_batch(X_real, y_real)
# generate 'fake' examples
X_fake, y_fake = generate_fake_samples(g_model, latent_dim, half_batch)
# update discriminator model weights
d_loss_fake, _ = d_model.train_on_batch(X_fake, y_fake)
#d_loss = 0.5 * np.add(d_loss_real, d_loss_fake) #Average loss if you want to report single..
# prepare points in latent space as input for the generator
X_gan = generate_latent_points(latent_dim, n_batch)
# The generator wants the discriminator to label the generated samples
# as valid (ones)
#This is where the generator is trying to trick discriminator into believing
#the generated image is true (hence value of 1 for y)
y_gan = ones((n_batch, 1))
# Generator is part of combined model where it got directly linked with the discriminator
# Train the generator with latent_dim as x and 1 as y.
# Again, 1 as the output as it is adversarial and if generator did a great
#job of folling the discriminator then the output would be 1 (true)
# update the generator via the discriminator's error
g_loss = gan_model.train_on_batch(X_gan, y_gan)
# Print losses on this batch
print('Epoch>%d, Batch %d/%d, d1=%.3f, d2=%.3f g=%.3f' %
(i+1, j+1, bat_per_epo, d_loss_real, d_loss_fake, g_loss))
# save the generator model
g_model.save('cifar_generator_2epochs.h5')
###################################################################
#Train the GAN
# size of the latent space
latent_dim = 100
# create the discriminator
discriminator = define_discriminator()
# create the generator
generator = define_generator(latent_dim)
# create the gan
gan_model = define_gan(generator, discriminator)
# load image data
dataset = load_real_samples()
# train model
train(generator, discriminator, gan_model, dataset, latent_dim, n_epochs=2)
################################################################################
# Now, let us load the generator model and generate images
from keras.models import load_model
from numpy.random import randn
# Plot generated images
def show_plot(examples, n):
for i in range(n * n):
plt.subplot(n, n, 1 + i)
plt.axis('off')
plt.imshow(examples[i, :, :, :])
plt.show()
# load model
model = load_model('cifar_generator_250epochs.h5') #Model trained for 100 epochs
# generate images
latent_points = generate_latent_points(100, 25) #Latent dim and n_samples
# generate images
X = model.predict(latent_points)
# scale from [-1,1] to [0,1]
X = (X + 1) / 2.0
import numpy as np
X = (X*255).astype(np.uint8)
# plot the result
show_plot(X, 5)
#Note: CIFAR10 classes are: airplane, automobile, bird, cat, deer, dog, frog, horse,
# ship, truck