Training the model

Now, it's time to train the model and see how the generator will be able to fool, to some extent, the discriminator, by generating images very close to the original CelebA dataset.

But first, let's define a helper function that will display some generated images by the generator:

# define a function to visualize some generated images from the generator
def show_generator_output(sess, num_images, input_latent_z, output_channel_dim, img_mode):
cmap = None if img_mode == 'RGB' else 'gray'
latent_space_z_dim = input_latent_z.get_shape().as_list()[-1]
examples_z = np.random.uniform(-1, 1, size=[num_images, latent_space_z_dim])

examples = sess.run(
generator(input_latent_z, output_channel_dim, False),
feed_dict={input_latent_z: examples_z})

images_grid = utils.images_square_grid(examples, img_mode)
pyplot.imshow(images_grid, cmap=cmap)
pyplot.show()

Then, we will use the helper functions that we have defined before to build the model inputs, loss, and optimization criteria. We stack them together and start training our model based on the CelebA dataset:

def model_train(num_epocs, train_batch_size, z_dim, learning_rate, beta1, get_batches, input_data_shape, data_img_mode):
_, image_width, image_height, image_channels = input_data_shape

actual_input, z_input, leaningRate = inputs(
image_width, image_height, image_channels, z_dim)

disc_loss, gen_loss = model_losses(actual_input, z_input, image_channels)

disc_opt, gen_opt = model_optimizer(disc_loss, gen_loss, learning_rate, beta1)

steps = 0
print_every = 50
show_every = 100
model_loss = []
num_images = 25

with tf.Session() as sess:

# initializing all the variables
sess.run(tf.global_variables_initializer())

for epoch_i in range(num_epocs):
for batch_images in get_batches(train_batch_size):

steps += 1
batch_images *= 2.0
z_sample = np.random.uniform(-1, 1, (train_batch_size, z_dim))

_ = sess.run(disc_opt, feed_dict={
actual_input: batch_images, z_input: z_sample, leaningRate: learning_rate})
_ = sess.run(gen_opt, feed_dict={
z_input: z_sample, leaningRate: learning_rate})

if steps % print_every == 0:
train_loss_disc = disc_loss.eval({z_input: z_sample, actual_input: batch_images})
train_loss_gen = gen_loss.eval({z_input: z_sample})

print("Epoch {}/{}...".format(epoch_i + 1, num_epocs),
"Discriminator Loss: {:.4f}...".format(train_loss_disc),
"Generator Loss: {:.4f}".format(train_loss_gen))
model_loss.append((train_loss_disc, train_loss_gen))

if steps % show_every == 0:
show_generator_output(sess, num_images, z_input, image_channels, data_img_mode)

Kick off the training process, which might take some time depending on your host machine specs:

# Training the model on CelebA dataset
train_batch_size = 64
z_dim = 100
learning_rate = 0.002
beta1 = 0.5

num_epochs = 1

celeba_dataset = utils.Dataset('celeba', glob(os.path.join(data_dir, 'img_align_celeba/*.jpg')))
with tf.Graph().as_default():
model_train(num_epochs, train_batch_size, z_dim, learning_rate, beta1, celeba_dataset.get_batches,
celeba_dataset.shape, celeba_dataset.image_mode)

Output:


Epoch 1/1... Discriminator Loss: 0.9118... Generator Loss: 12.2238
Epoch 1/1... Discriminator Loss: 0.6119... Generator Loss: 3.2168
Epoch 1/1... Discriminator Loss: 0.5383... Generator Loss: 2.8054
Epoch 1/1... Discriminator Loss: 1.4381... Generator Loss: 0.4672
Epoch 1/1... Discriminator Loss: 0.7815... Generator Loss: 14.8220
Epoch 1/1... Discriminator Loss: 0.6435... Generator Loss: 9.2591
Epoch 1/1... Discriminator Loss: 1.5661... Generator Loss: 10.4747
Epoch 1/1... Discriminator Loss: 1.5407... Generator Loss: 0.5811
Epoch 1/1... Discriminator Loss: 0.6470... Generator Loss: 2.9002
Epoch 1/1... Discriminator Loss: 0.5671... Generator Loss: 2.0700
Figure 3: Sample generated output at this point of training
Epoch 1/1... Discriminator Loss: 0.7950... Generator Loss: 1.5818
Epoch 1/1... Discriminator Loss: 1.2417... Generator Loss: 0.7094
Epoch 1/1... Discriminator Loss: 1.1786... Generator Loss: 1.0948
Epoch 1/1... Discriminator Loss: 1.0427... Generator Loss: 2.8878
Epoch 1/1... Discriminator Loss: 0.8409... Generator Loss: 2.6785
Epoch 1/1... Discriminator Loss: 0.8557... Generator Loss: 1.7706
Epoch 1/1... Discriminator Loss: 0.8241... Generator Loss: 1.2898
Epoch 1/1... Discriminator Loss: 0.8590... Generator Loss: 1.8217
Epoch 1/1... Discriminator Loss: 1.1694... Generator Loss: 0.8490
Epoch 1/1... Discriminator Loss: 0.9984... Generator Loss: 1.0042
Figure 4: Sample generated output at this point of training

After some time of training, you should get something like this:

Figure 5: Sample generated output at this point of training
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset