The domain of artificial intelligence has witnessed significant growth and expansion into creative sectors like sketching and doodling. In sketching, conventional AI approaches have mainly concentrated on imitating ordinary and real-life sketches. Still, recent developments in Generative Adversarial Networks (GANs) present an innovative perspective towards creative sketch production. The study explores the details of implementing DCGAN along with the Quick, Draw! Dataset: its techniques and how it can affect human creativity by acting as an inspiration for others involved in their creative projects.
Sketching has been an important form of visual communication since prehistoric times and has become a popular creative tool today. The introduction of touchscreen devices has further expanded their scope. The role of intelligence in this field is only to understand and create true art. However, creative art involves unique characters and emotional responses and presents more complex subject matter. This is where DCGAN shines.
DCGAN, or Deep Convolutional Generative Adversarial Network, is a GAN specifically designed to create high-quality images. It works with two main factors: generator and discrimination
The image depicts the architecture of a Deep Convolutional Generative Adversarial Network (DCGAN). It shows the structure of the generator and discriminator networks, highlighting the layers and operations involved in generating and discriminating images.
The generator transforms a low-dimensional random noise vector into a high-dimensional image. The process involves upsampling and convolutional layers with ReLU activation functions.
The discriminator aims to differentiate between real and fake images by downsampling the input images and applying convolutional layers with Leaky ReLU activations.
To showcase DCGAN’s capabilities, we utilized the Quick, Draw! dataset, which contains millions of doodles across various categories. In this example, we focused on the “flower” category.
First, we loaded and preprocessed the Quick, Draw! flower dataset:
import numpy as np
import requests
from io import BytesIO
# Load Quick, Draw! Data
quickdraw_url = 'https://storage.googleapis.com/quickdraw_dataset/full/numpy_bitmap/flower.npy'
response = requests.get(quickdraw_url)
data = np.load(BytesIO(response.content))
data = (data.astype(np.float32) / 127.5) - 1.0 # Normalize to [-1, 1]
data = data.reshape(-1, 28, 28, 1)
This code downloads the Quick, Draw! dataset, normalizes the pixel values to the range [-1, 1], and reshapes it for use in the model.
Next, we defined the DCGAN architecture, including the generator and discriminator models:
class DCGAN():
def __init__(self):
self.img_shape = (28, 28, 1)
self.latent_dim = 100
self.optimizer = tf.keras.optimizers.legacy.Adam(0.0002, 0.5)
# Build and compile the discriminator
self.discriminator = self.build_discriminator()
self.discriminator.compile(loss='binary_crossentropy', optimizer=self.optimizer)
# Build and compile the generator
self.generator = self.build_generator()
self.generator.compile(loss='binary_crossentropy', optimizer=self.optimizer)
# Build the combined model
self.gan = self.build_GAN()
This initializes the DCGAN class, defining the image shape, latent dimension, and optimizer. It also builds and compiles the generator and discriminator models.
def build_GAN(self):
self.discriminator.trainable = False
gan_input = Input(shape=(self.latent_dim,))
img = self.generator(gan_input)
gan_output = self.discriminator(img)
gan = Model(gan_input, gan_output, name='GAN')
gan.compile(loss='binary_crossentropy', optimizer=self.optimizer)
return gan
This method constructs the combined GAN model, which stacks the generator and discriminator and compiles them.
def build_generator(self):
generator = Sequential()
generator.add(Dense(128 * 7 * 7, activatioDCGAN architecturen="relu", input_dim=self.latent_dim))
generator.add(Reshape((7, 7, 128)))
generator.add(BatchNormalization(momentum=0.8))
generator.add(UpSampling2D())
generator.add(Conv2D(128, kernel_size=3, padding="same"))
generator.add(LeakyReLU(0.2))
generator.add(BatchNormalization(momentum=0.8))
generator.add(UpSampling2D())
generator.add(Conv2D(64, kernel_size=3, padding="same"))
generator.add(LeakyReLU(0.2))
generator.add(BatchNormalization(momentum=0.8))
generator.add(Conv2D(1, kernel_size=3, padding='same', activation="tanh"))
return Model(Input(shape=(self.latent_dim,)), generator(Input(shape=(self.latent_dim,))),
name='Generator')
This method constructs the generator model, transforming random noise into a synthetic image.
def build_discriminator(self):
discriminator = Sequential()
discriminator.add(Conv2D(64, kernel_size=(5, 5), strides=(2, 2), padding='same',
input_shape=self.img_shape,
kernel_initializer=RandomNormal(stddev=0.02)))
discriminator.add(LeakyReLU(0.2))
discriminator.add(Dropout(0.2))
discriminator.add(Conv2D(128, kernel_size=(5, 5), strides=(2, 2), padding='same'))
discriminator.add(LeakyReLU(0.2))
discriminator.add(Dropout(0.2))
discriminator.add(Flatten())
discriminator.add(Dense(1, activation='sigmoid'))
return Model(Input(shape=self.img_shape), discriminator(Input(shape=self.img_shape)),
name='Discriminator')
This method constructs the discriminator model, differentiating between real and synthetic images.
def train(self, X_train, epochs, batch_size=128, sample_interval=50):
real = np.ones((batch_size, 1))
fake = np.zeros((batch_size, 1))
for epoch in range(epochs):
for _ in range(X_train.shape[0] // batch_size):
idx = np.random.randint(0, X_train.shape[0], batch_size)
imgs = X_train[idx]
noise = np.random.normal(0, 1, (batch_size, self.latent_dim))
gen_imgs = self.generator.predict(noise)
d_loss_real = self.discriminator.train_on_batch(imgs, real)
d_loss_fake = self.discriminator.train_on_batch(gen_imgs, fake)
d_loss = 0.5 * np.add(d_loss_real, d_loss_fake)
noise = np.random.normal(0, 1, (batch_size, self.latent_dim))
g_loss = self.gan.train_on_batch(noise, real)
if epoch % sample_interval == 0:
self.sample_images(epoch)
This method trains the DCGAN by alternating between training the discriminator and the generator. It periodically generates sample images to visualize the generator’s progress.
def sample_images(self, epoch):
noise = np.random.normal(0, 1, (100, self.latent_dim))
gen_imgs = self.generator.predict(noise)
gen_imgs = 0.5 * gen_imgs + 0.5
fig, axs = plt.subplots(10, 10, figsize=(10, 10))
cnt = 0
for i in range(10):
for j in range(10):
axs[i, j].imshow(gen_imgs[cnt, :, :, 0], cmap='gray')
axs[i, j].axis('off')
cnt += 1
plt.show()
This method generates and displays a grid of images the generator produces at each sampling interval during training.
gan = DCGAN()
gan.train(data, epochs=5, batch_size=128, sample_interval=5)
1st Epoch: We can see the flowers don’t look good enough
After training many epochs, it gets considerably better!
The loss over epochs is shown. The generator loss seems to be diverging. However, we visually inspected the generated samples on each epoch, and the results were improving.
To evaluate the DCGAN’s performance, we compared it with other sketch generation models. We used metrics such as Fréchet Inception Distance (FID), generation diversity (GD), characteristic score (CS), and semantic diversity score (SDS).
DCGAN’s ability to generate unique, high-quality sketches has significant implications for various applications. It can be integrated into interactive sketching tools, providing users with creative suggestions and helping artists overcome creative blocks. The model’s approach opens new avenues for exploring human-machine collaborative creative processes.
In summary, DCGAN(Deep Convolutional Generative Adversarial Network) represents a significant advance in AI design. It sets a new standard for AI-driven creativity by using innovative training methods and focusing on creating distinctive, beautiful images. As artificial intelligence continues to evolve, models such as DCGAN will undoubtedly play an important role in developing and improving human reasoning ability.
Ans. DCGAN can be integrated into interactive sketching tools to provide creative suggestions, help artists overcome creative blocks, and enhance human-machine collaborative creative processes.
Ans. Common challenges include training instability, mode collapse (where the generator produces limited varieties of images), and the need for large amounts of data and computational resources.
Ans. Future advancements may include more sophisticated models with higher image quality, greater control over the generated content, improved training stability, and broader applications in various creative and industrial fields.
Ans. DCGAN can be integrated into interactive sketching tools to provide creative suggestions, help artists overcome creative blocks, and enhance human-machine collaborative creative processes.