= glob.glob('../input/data/data/*.png')
image_files shuffle(image_files)
Deep Convolutional AnimeGAN
computervision
deeplearning
keras
python
tensorflow
Generating Anime Faces using Deep Convolutional GAN
Project Repository: https://github.com/soumik12345/Adventures-with-GANS
::: {#cell-2 .cell _cell_guid=‘b1076dfc-b9ad-4769-8c92-a6c4dae69d19’ _uuid=‘8f2839f25d086af736a60e9eeb907d3b93b6e0e5’ execution_count=1}
import os, warnings
'ignore') warnings.filterwarnings(
:::
::: {#cell-3 .cell _cell_guid=‘79c7e3d0-c299-4dcb-8224-4455121ee9b0’ _uuid=‘d629ff2d2480ee46fbb7e2d37f6b5fab8052498a’ execution_count=2}
import numpy as np
import cv2, glob
from random import shuffle, randint
from tqdm import tqdm
import matplotlib.pyplot as plt
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten, Reshape, BatchNormalization
from tensorflow.keras.layers import Activation, Conv2DTranspose, Conv2D, LeakyReLU
from tensorflow.keras.optimizers import Adam
from keras.utils.vis_utils import model_to_dot
from IPython.display import SVG
:::
= []
x for file in tqdm(image_files):
= cv2.imread(file)
image = image / 127.5
image = image - 1
image
x.append(image)= np.array(x)
x x.shape
(21551, 64, 64, 3)
= plt.subplots(nrows = 4, ncols = 4, figsize = (16, 16))
fig, axes = [], yticks = [])
plt.setp(axes.flat, xticks for i, ax in enumerate(axes.flat):
= randint(0, 10000)
index = 'gray')
ax.imshow(x[index], cmap plt.show()
def build_discriminator(image_shape, learning_rate, beta_1):
= Sequential([
discriminator
Conv2D(= 64,
filters = (5, 5),
kernel_size = (2, 2),
strides = 'same',
padding = 'channels_last',
data_format = 'glorot_uniform',
kernel_initializer = (image_shape)
input_shape
),0.2),
LeakyReLU(
Conv2D(= 128,
filters = (5, 5),
kernel_size = (2, 2),
strides = 'same',
padding = 'channels_last',
data_format = 'glorot_uniform',
kernel_initializer
),= 0.5),
BatchNormalization(momentum 0.2),
LeakyReLU(
Conv2D(= 256,
filters = (5, 5),
kernel_size = (2, 2),
strides = 'same',
padding = 'channels_last',
data_format = 'glorot_uniform',
kernel_initializer
),= 0.5),
BatchNormalization(momentum 0.2),
LeakyReLU(
Conv2D(= 512,
filters = (5, 5),
kernel_size = (2, 2),
strides = 'same',
padding = 'channels_last',
data_format = 'glorot_uniform',
kernel_initializer
),= 0.5),
BatchNormalization(momentum 0.2),
LeakyReLU(
Flatten(),1),
Dense('sigmoid')
Activation(= 'Discriminator')
], name
compile(
discriminator.= 'binary_crossentropy',
loss = Adam(
optimizer = learning_rate,
lr = beta_1
beta_1
),= None
metrics
)
return discriminator
def build_generator(input_shape, learning_rate, beta_1):
= Sequential([
generator
Dense(
input_shape,= 'glorot_uniform',
kernel_initializer = (1, 1, 100)
input_shape
),= (4, 4, 512)),
Reshape(target_shape = 0.5),
BatchNormalization(momentum 'relu'),
Activation(
Conv2DTranspose(= 256,
filters = (5, 5),
kernel_size = (2, 2),
strides = 'same',
padding = 'channels_last',
data_format = 'glorot_uniform'
kernel_initializer
),= 0.5),
BatchNormalization(momentum 'relu'),
Activation(
Conv2DTranspose(= 128,
filters = (5, 5),
kernel_size = (2, 2),
strides = 'same',
padding = 'channels_last',
data_format = 'glorot_uniform'
kernel_initializer
),= 0.5),
BatchNormalization(momentum 'relu'),
Activation(
Conv2DTranspose(= 64,
filters = (5, 5),
kernel_size = (2, 2),
strides = 'same',
padding = 'channels_last',
data_format = 'glorot_uniform'
kernel_initializer
),= 0.5),
BatchNormalization(momentum 'relu'),
Activation(
Conv2DTranspose(= 3,
filters = (5, 5),
kernel_size = (2, 2),
strides = 'same',
padding = 'channels_last',
data_format = 'glorot_uniform'
kernel_initializer
),'tanh'),
Activation(= 'Generator')
], name
compile(
generator.= 'binary_crossentropy',
loss = Adam(
optimizer = learning_rate,
lr = beta_1
beta_1
),= None
metrics
)
return generator
def build_gan(generator, discriminator, learning_rate, beta_1):
= Sequential([
gan
generator,
discriminator= 'GAN')
], name compile(
gan.= 'binary_crossentropy',
loss = Adam(
optimizer = learning_rate,
lr = beta_1
beta_1
),= None
metrics
)return gan
= build_discriminator((64, 64, 3), 0.0002, 0.5)
discriminator discriminator.summary()
WARNING:tensorflow:From /opt/conda/lib/python3.6/site-packages/tensorflow/python/ops/resource_variable_ops.py:435: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 32, 32, 64) 4864
_________________________________________________________________
leaky_re_lu (LeakyReLU) (None, 32, 32, 64) 0
_________________________________________________________________
conv2d_1 (Conv2D) (None, 16, 16, 128) 204928
_________________________________________________________________
batch_normalization_v1 (Batc (None, 16, 16, 128) 512
_________________________________________________________________
leaky_re_lu_1 (LeakyReLU) (None, 16, 16, 128) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 8, 8, 256) 819456
_________________________________________________________________
batch_normalization_v1_1 (Ba (None, 8, 8, 256) 1024
_________________________________________________________________
leaky_re_lu_2 (LeakyReLU) (None, 8, 8, 256) 0
_________________________________________________________________
conv2d_3 (Conv2D) (None, 4, 4, 512) 3277312
_________________________________________________________________
batch_normalization_v1_2 (Ba (None, 4, 4, 512) 2048
_________________________________________________________________
leaky_re_lu_3 (LeakyReLU) (None, 4, 4, 512) 0
_________________________________________________________________
flatten (Flatten) (None, 8192) 0
_________________________________________________________________
dense (Dense) (None, 1) 8193
_________________________________________________________________
activation (Activation) (None, 1) 0
=================================================================
Total params: 4,318,337
Trainable params: 4,316,545
Non-trainable params: 1,792
_________________________________________________________________
= True, show_layer_names = True).create(prog = 'dot', format = 'svg')) SVG(model_to_dot(discriminator, show_shapes
= build_generator(np.prod(discriminator.layers[-4].output_shape[1:]), 0.00015, 0.5)
generator generator.summary()
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_1 (Dense) (None, 1, 1, 8192) 827392
_________________________________________________________________
reshape (Reshape) (None, 4, 4, 512) 0
_________________________________________________________________
batch_normalization_v1_3 (Ba (None, 4, 4, 512) 2048
_________________________________________________________________
activation_1 (Activation) (None, 4, 4, 512) 0
_________________________________________________________________
conv2d_transpose (Conv2DTran (None, 8, 8, 256) 3277056
_________________________________________________________________
batch_normalization_v1_4 (Ba (None, 8, 8, 256) 1024
_________________________________________________________________
activation_2 (Activation) (None, 8, 8, 256) 0
_________________________________________________________________
conv2d_transpose_1 (Conv2DTr (None, 16, 16, 128) 819328
_________________________________________________________________
batch_normalization_v1_5 (Ba (None, 16, 16, 128) 512
_________________________________________________________________
activation_3 (Activation) (None, 16, 16, 128) 0
_________________________________________________________________
conv2d_transpose_2 (Conv2DTr (None, 32, 32, 64) 204864
_________________________________________________________________
batch_normalization_v1_6 (Ba (None, 32, 32, 64) 256
_________________________________________________________________
activation_4 (Activation) (None, 32, 32, 64) 0
_________________________________________________________________
conv2d_transpose_3 (Conv2DTr (None, 64, 64, 3) 4803
_________________________________________________________________
activation_5 (Activation) (None, 64, 64, 3) 0
=================================================================
Total params: 5,137,283
Trainable params: 5,135,363
Non-trainable params: 1,920
_________________________________________________________________
= True, show_layer_names = True).create(prog = 'dot', format = 'svg')) SVG(model_to_dot(generator, show_shapes
= False
discriminator.trainable = build_gan(generator, discriminator, 0.00015, 0.5)
gan gan.summary()
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
Generator (Sequential) (None, 64, 64, 3) 5137283
_________________________________________________________________
Discriminator (Sequential) (None, 1) 4318337
=================================================================
Total params: 9,455,620
Trainable params: 5,135,363
Non-trainable params: 4,320,257
_________________________________________________________________
= True, show_layer_names = True).create(prog = 'dot', format = 'svg')) SVG(model_to_dot(gan, show_shapes
= 15000
EPOCHS = 32 BATCH_SIZE
def plot_images(nrows, ncols, figsize, generator):
= np.random.normal(0, 1, size = (BATCH_SIZE * 2, ) + (1, 1, 100))
noise = generator.predict(noise)
prediction = plt.subplots(nrows = nrows, ncols = ncols, figsize = figsize)
fig, axes = [], yticks = [])
plt.setp(axes.flat, xticks for i, ax in enumerate(axes.flat):
= randint(0, 63)
index = 'gray')
ax.imshow(cv2.cvtColor(prediction[index], cv2.COLOR_BGR2RGB), cmap plt.show()
= [], []
discriminator_loss_history, generator_loss_history
for epoch in tqdm(range(1, EPOCHS + 1)):
# Select a random batch of images from training data
= np.random.randint(0, x.shape[0], BATCH_SIZE)
index = x[index]
batch_images
# Adversarial Noise
= np.random.normal(0, 1, size = (BATCH_SIZE, ) + (1, 1, 100))
noise
# Fenerate Fake Images
= generator.predict(noise)
generated_images
# Adding noise to the labels before passing to the discriminator
= (np.ones(BATCH_SIZE) - np.random.random_sample(BATCH_SIZE) * 0.2)
real_y = np.random.random_sample(BATCH_SIZE) * 0.2
fake_y
# Training the discriminator
= True
discriminator.trainable = discriminator.train_on_batch(batch_images, real_y)
discriminator_loss += discriminator.train_on_batch(generated_images, fake_y)
discriminator_loss = False
discriminator.trainable
# Adversarial Noise
= np.random.normal(0, 1, size = (BATCH_SIZE * 2,) + (1, 1, 100))
noise
# We try to mislead the discriminator by giving the opposite labels
= (np.ones(BATCH_SIZE * 2) - np.random.random_sample(BATCH_SIZE * 2) * 0.2)
fake_y
# Training the Generator
= gan.train_on_batch(noise, fake_y)
generator_loss
if epoch % 100 == 0:
discriminator_loss_history.append(discriminator_loss)
generator_loss_history.append(generator_loss)if epoch % 1000 == 0:
2, 8, (16, 4), generator) plot_images(
WARNING:tensorflow:From /opt/conda/lib/python3.6/site-packages/tensorflow/python/ops/math_ops.py:3066: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.cast instead.
= (20, 8))
plt.figure(figsize
plt.plot(generator_loss_history)'Generator Loss History')
plt.title( plt.show()
= (20, 8))
plt.figure(figsize
plt.plot(discriminator_loss_history)'Discriminator Loss History')
plt.title( plt.show()
4, 4, (16, 16), generator) plot_images(
'./generator.h5') generator.save(