16.03.2021 Views

Advanced Deep Learning with Keras

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Disentangled Representation GANs

Discriminator is trained first with real and fake images,

corresponding one-hot labels and latent codes.

Adversarial is trained next with fake images pretending

to be real,

corresponding one-hot labels and latent codes.

Generate sample images per save_interval.

# Arguments

models (Models): Encoder, Generator, Discriminator,

Adversarial models

data (tuple): x_train, y_train data

params (tuple): Network parameters

"""

# the StackedGAN and Encoder models

enc0, enc1, gen0, gen1, dis0, dis1, adv0, adv1 = models

# network parameters

batch_size, train_steps, num_labels, z_dim, model_name = params

# train dataset

(x_train, y_train), (_, _) = data

# the generator image is saved every 500 steps

save_interval = 500

# label and noise codes for generator testing

z0 = np.random.normal(scale=0.5, size=[16, z_dim])

z1 = np.random.normal(scale=0.5, size=[16, z_dim])

noise_class = np.eye(num_labels)[np.arange(0, 16) % num_labels]

noise_params = [noise_class, z0, z1]

# number of elements in train dataset

train_size = x_train.shape[0]

print(model_name,

"Labels for generated images: ",

np.argmax(noise_class, axis=1))

for i in range(train_steps):

# train the discriminator1 for 1 batch

# 1 batch of real (label=1.0) and fake feature1 (label=0.0)

# randomly pick real images from dataset

rand_indexes = np.random.randint(0, train_size,

size=batch_size)

real_images = x_train[rand_indexes]

# real feature1 from encoder0 output

real_feature1 = enc0.predict(real_images)

# generate random 50-dim z1 latent code

[ 194 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!