深度学习GAN(一)之简单介绍
深度学习GAN(二)之基于CIFAR10数据集的例子;
深度学习GAN(三)之基于手写体Mnist数据集的例子;
深度学习GAN(四)之PIX2PIX GAN的例子
CIFAR-10 数据集介绍请看这篇博客
GPU怎么配置请看这篇博客
通过本博客你知道怎么利用GAN产生大量的假的图片、
如下图,左边是真实的图片。右边是利用GAN产生的图片
下图是GAN生成的手写体数字,用了10个epoch
生成式对抗网络(GAN, Generative Adversarial Networks )是一种深度学习模型,是近年来复杂分布上无监督学习最具前景的方法之一。模型通过框架中(至少)两个模块:生成模型(Generative Model)和判别模型(Discriminative Model)的互相博弈学习产生相当好的输出。原始 GAN 理论中,并不要求 G 和 D 都是神经网络,只需要是能拟合相应生成和判别的函数即可。但实用中一般均使用深度神经网络作为 G 和 D 。一个优秀的GAN应用需要有良好的训练方法,否则可能由于神经网络模型的自由性而导致输出不理想。
要了解GAN的工作原理以及如何在GAN架构中训练深层卷积神经网络模型以生成图像,可能是具有挑战性的。对于初学者来说,一个很好的起点是在计算机视觉领域中使用的标准图像数据集(例如CIFAR小物体照片数据集)上练习开发和使用GAN。使用小型且易于理解的数据集意味着可以快速开发和训练较小的模型,从而可以将重点放在模型体系结构和图像生成过程本身上。
在本博客中,您将发现如何使用具有深度卷积网络的生成对抗网络来生成物体的小照片。
在训练过程中,生成器努力地让生成的图像更加真实,而判别器则努力地去识别出图像的真假,这个过程相当与一个二人博弈,随着时间的推移,生成器和判别器在不断地进行对抗。
定义一个判别器模型的函数define_discriminator(),输入参数是图片的大小,默认(32,32,3)
import tensorflow as tf
import tensorflow.keras as keras
import numpy as np
import matplotlib.pyplot as plt
# define the standalone discriminator model
def define_discriminator(in_shape=(32,32,3)):
model = keras.models.Sequential()
# normal
model.add(keras.layers.Conv2D(64, (3,3), padding='same', input_shape=in_shape))
model.add(keras.layers.LeakyReLU(alpha=0.2))
# downsample
model.add(keras.layers.Conv2D(128, (3,3), strides=(2,2), padding='same'))
model.add(keras.layers.LeakyReLU(alpha=0.2))
# downsample
model.add(keras.layers.Conv2D(128, (3,3), strides=(2,2), padding='same'))
model.add(keras.layers.LeakyReLU(alpha=0.2))
# downsample
model.add(keras.layers.Conv2D(256, (3,3), strides=(2,2), padding='same'))
model.add(keras.layers.LeakyReLU(alpha=0.2))
# classifier
model.add(keras.layers.Flatten())
model.add(keras.layers.Dropout(0.4))
model.add(keras.layers.Dense(1, activation='sigmoid'))
# compile model
opt = keras.optimizers.Adam(lr=0.0002, beta_1=0.5)
model.compile(loss='binary_crossentropy', optimizer=opt, metrics=['accuracy'])
return model
if __name__ == '__main__':
model = define_discriminator()
# summarize the model
model.summary()
结果
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 32, 32, 64) 1792
_________________________________________________________________
leaky_re_lu (LeakyReLU) (None, 32, 32, 64) 0
_________________________________________________________________
conv2d_1 (Conv2D) (None, 16, 16, 128) 73856
_________________________________________________________________
leaky_re_lu_1 (LeakyReLU) (None, 16, 16, 128) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 8, 8, 128) 147584
_________________________________________________________________
leaky_re_lu_2 (LeakyReLU) (None, 8, 8, 128) 0
_________________________________________________________________
conv2d_3 (Conv2D) (None, 4, 4, 256) 295168
_________________________________________________________________
leaky_re_lu_3 (LeakyReLU) (None, 4, 4, 256) 0
_________________________________________________________________
flatten (Flatten) (None, 4096) 0
_________________________________________________________________
dropout (Dropout) (None, 4096) 0
_________________________________________________________________
dense (Dense) (None, 1) 4097
=================================================================
Total params: 522,497
Trainable params: 522,497
Non-trainable params: 0
把图片的像素点从[0,255]转成[-1, 1]
# load and prepare cifar10 training images
def load_real_samples():
# load cifar10 dataset
(trainX, _), (_, _) = tf.keras.datasets.cifar10.load_data()
# convert from unsigned ints to floats
X = trainX.astype('float32')
# scale from [0,255] to [-1,1]
X = (X - 127.5) / 127.5
return X
Y 都是1
# select real samples
def generate_real_samples(dataset, n_samples):
# choose random instances
ix = np.random.randint(0, dataset.shape[0], n_samples)
# retrieve selected images
X = dataset[ix]
# generate 'real' class labels (1)
y = np.ones((n_samples, 1))
return X, y
Y都是0
def generate_fake_samples1(n_samples):
# generate uniform random numbers in [0,1]
X = np.random.rand(32 * 32 * 3 * n_samples)
# update to have the range [-1, 1]
X = -1 + X * 2
# reshape into a batch of color images
X = X.reshape((n_samples, 32, 32, 3))
# generate 'fake' class labels (0)
y = np.zeros((n_samples, 1))
return X, y
真实图片与假的图片一起训练。
# train the discriminator model
def train_discriminator(model, dataset, n_iter=20, n_batch=128):
half_batch = int(n_batch / 2)
# manually enumerate epochs
for i in range(n_iter):
# get randomly selected 'real' samples
X_real, y_real = generate_real_samples(dataset, half_batch)
# update discriminator on real samples
_, real_acc = model.train_on_batch(X_real, y_real)
# generate 'fake' examples
X_fake, y_fake = generate_fake_samples1(half_batch)
# update discriminator on fake samples
_, fake_acc = model.train_on_batch(X_fake, y_fake)
# summarize performance
print('>%d real=%.0f%% fake=%.0f%%' % (i+1, real_acc*100, fake_acc*100))
def test_train_discriminator():
# define the discriminator model
model = define_discriminator()
# load image data
dataset = load_real_samples()
# fit the model
train_discriminator(model, dataset)
if __name__ == '__main__':
test_train_discriminator()
结果如下,可以看出训练的时候真实的图片的准确率与假的图片的准确率
>1 real=83% fake=0%
>2 real=100% fake=0%
>3 real=100% fake=8%
>4 real=94% fake=31%
>5 real=97% fake=62%
>6 real=86% fake=92%
>7 real=98% fake=98%
>8 real=92% fake=100%
>9 real=88% fake=100%
>10 real=94% fake=100%
>11 real=94% fake=100%
>12 real=100% fake=100%
>13 real=91% fake=100%
>14 real=97% fake=100%
>15 real=98% fake=100%
>16 real=100% fake=100%
>17 real=97% fake=100%
>18 real=100% fake=100%
>19 real=100% fake=100%
>20 real=100% fake=100%
产生器的生成正好和判别器是相反的。
是由100维的向量反向生成(32,32,3)的图片
此处的Generator不用指定loss函数与优化器,因为它不能直接训练,
下面还有其它的。
# define the standalone generator model
def define_generator(latent_dim):
model = keras.models.Sequential()
# foundation for 4x4 image
n_nodes = 256 * 4 * 4
model.add(keras.layers.Dense(n_nodes, input_dim=latent_dim))
model.add(keras.layers.LeakyReLU(alpha=0.2))
model.add(keras.layers.Reshape((4, 4, 256)))
# upsample to 8x8
model.add(keras.layers.Conv2DTranspose(128, (4,4), strides=(2,2), padding='same'))
model.add(keras.layers.LeakyReLU(alpha=0.2))
# upsample to 16x16
model.add(keras.layers.Conv2DTranspose(128, (4,4), strides=(2,2), padding='same'))
model.add(keras.layers.LeakyReLU(alpha=0.2))
# upsample to 32x32
model.add(keras.layers.Conv2DTranspose(128, (4,4), strides=(2,2), padding='same'))
model.add(keras.layers.LeakyReLU(alpha=0.2))
# output layer
model.add(keras.layers.Conv2D(3, (3,3), activation='tanh', padding='same'))
return model
# define the size of the latent space
latent_dim = 100
# define the generator model
model = define_generator(latent_dim)
# summarize the model
model.summary()
执行结果如下,
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense (Dense) (None, 4096) 413696
_________________________________________________________________
leaky_re_lu (LeakyReLU) (None, 4096) 0
_________________________________________________________________
reshape (Reshape) (None, 4, 4, 256) 0
_________________________________________________________________
conv2d_transpose (Conv2DTran (None, 8, 8, 128) 524416
_________________________________________________________________
leaky_re_lu_1 (LeakyReLU) (None, 8, 8, 128) 0
_________________________________________________________________
conv2d_transpose_1 (Conv2DTr (None, 16, 16, 128) 262272
_________________________________________________________________
leaky_re_lu_2 (LeakyReLU) (None, 16, 16, 128) 0
_________________________________________________________________
conv2d_transpose_2 (Conv2DTr (None, 32, 32, 128) 262272
_________________________________________________________________
leaky_re_lu_3 (LeakyReLU) (None, 32, 32, 128) 0
_________________________________________________________________
conv2d (Conv2D) (None, 32, 32, 3) 3459
=================================================================
Total params: 1,466,115
Trainable params: 1,466,115
Non-trainable params: 0
generate_latent_points()产生n_samples个随机向量,每个向量默认为100维
generate_fake_samples()根据产生器生成假的图片
show_fake_sample()展示假的图片
# generate points in latent space as input for the generator
def generate_latent_points(latent_dim, n_samples):
# generate points in the latent space
x_input = np.random.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
def generate_fake_samples(g_model, latent_dim, n_samples):
# generate points in latent space
x_input = generate_latent_points(latent_dim, n_samples)
# predict outputs
X = g_model.predict(x_input)
# create 'fake' class labels (0)
y = np.zeros((n_samples, 1))
return X, y
def show_fake_sample():
# size of the latent space
latent_dim = 100
# define the discriminator model
model = define_generator(latent_dim)
# generate samples
n_samples = 49
X, _ = generate_fake_samples(model, latent_dim, n_samples)
# scale pixel values from [-1,1] to [0,1]
X = (X + 1) / 2.0
# plot the generated samples
for i in range(n_samples):
# define subplot
plt.subplot(7, 7, 1 + i)
# turn off axis labels
plt.axis('off')
# plot single image
plt.imshow(X[i])
# show the figure
plt.show()
if __name__ == '__main__':
show_fake_sample()
Gan的模型其实是由Generator与Desciminator组成的
# define the combined generator and discriminator model, for updating the generator
def define_gan(g_model, d_model):
# make weights in the discriminator not trainable
d_model.trainable = False
# connect them
model = tf.keras.models.Sequential()
# add generator
model.add(g_model)
# add the discriminator
model.add(d_model)
# compile model
opt = tf.keras.optimizers.Adam(lr=0.0002, beta_1=0.5)
model.compile(loss='binary_crossentropy', optimizer=opt)
return model
def show_gan_module():
# size of the latent space
latent_dim = 100
# create the discriminator
d_model = define_discriminator()
# create the generator
g_model = define_generator(latent_dim)
# create the gan
gan_model = define_gan(g_model, d_model)
# summarize gan model
gan_model.summary()
if __name__ == '__main__':
show_gan_module()
执行结果如下:
Model: "sequential_2"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
sequential_1 (Sequential) (None, 32, 32, 3) 1466115
_________________________________________________________________
sequential (Sequential) (None, 1) 522497
=================================================================
Total params: 1,988,612
Trainable params: 1,466,115
Non-trainable params: 522,497
# evaluate the discriminator, plot generated images, save generator model
def summarize_performance(epoch, g_model, d_model, dataset, latent_dim, n_samples=150):
# prepare real samples
X_real, y_real = generate_real_samples(dataset, n_samples)
# evaluate discriminator on real examples
_, acc_real = d_model.evaluate(X_real, y_real, verbose=0)
# prepare fake examples
x_fake, y_fake = generate_fake_samples(g_model, latent_dim, n_samples)
# evaluate discriminator on fake examples
_, acc_fake = d_model.evaluate(x_fake, y_fake, verbose=0)
# summarize discriminator performance
print('>Accuracy real: %.0f%%, fake: %.0f%%' % (acc_real * 100, acc_fake * 100))
# save plot
#save_plot(x_fake, epoch)
# save the generator model tile file
filename = 'generator_model_%03d.h5' % (epoch + 1)
g_model.save(filename)
# train the generator and discriminator
def train(g_model, d_model, gan_model, dataset, latent_dim, n_epochs=200, n_batch=128):
bat_per_epo = int(dataset.shape[0] / n_batch)
half_batch = int(n_batch / 2)
# manually enumerate epochs
for i in range(n_epochs):
# enumerate batches over the training set
for j in range(bat_per_epo):
# get randomly selected 'real' samples
X_real, y_real = generate_real_samples(dataset, half_batch)
# update discriminator model weights
d_loss1, _ = 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_loss2, _ = d_model.train_on_batch(X_fake, y_fake)
# prepare points in latent space as input for the generator
X_gan = generate_latent_points(latent_dim, n_batch)
# create inverted labels for the fake samples
y_gan = np.ones((n_batch, 1))
# update the generator via the discriminator's error
g_loss = gan_model.train_on_batch(X_gan, y_gan)
# summarize loss on this batch
print('>%d, %d/%d, d1=%.3f, d2=%.3f g=%.3f' %
(i + 1, j + 1, bat_per_epo, d_loss1, d_loss2, g_loss))
# evaluate the model performance, sometimes
if (i + 1) % 10 == 0:
summarize_performance(i, g_model, d_model, dataset, latent_dim)
def train_gan():
# size of the latent space
latent_dim = 100
# create the discriminator
d_model = define_discriminator()
# create the generator
g_model = define_generator(latent_dim)
# create the gan
gan_model = define_gan(g_model, d_model)
# load image data
dataset = load_real_samples()
# train model
train(g_model, d_model, gan_model, dataset, latent_dim)
if __name__ == '__main__':
train_gan()
加载训练好的权重参数,随机产生100个100维的点。利用Generator产生假图片并展示出来
注意:模型的权重参数文件名字有可能要改变一下。
def show_imgs_for_final_generator_model():
# load model
model = tf.keras.models.load_model('generator_model_010.h5')
# generate images
latent_points = generate_latent_points(100, 100)
# generate images
X = model.predict(latent_points)
# scale from [-1,1] to [0,1]
X = (X + 1) / 2.0
# plot the result
create_plot(X, 10)
10个epoch之后,结果如下:感觉虽然每个图片有都有不同。但是还是很难分别它是什么。
90,100个epoch之后。我们就可以识别出具体的图片是什么了。
100 epoch之后, 就稳定了
利用一个所有值都是0.75的向量产生一个图片
def show_single_imgs():
model = tf.keras.models.load_model('generator_model_200.h5')
# all 0s
vector = np.asarray([[0.75 for _ in range(100)]])
# generate image
X = model.predict(vector)
# scale from [-1,1] to [0,1]
X = (X + 1) / 2.0
# plot the result
plt.imshow(X[0, :, :])
plt.show()
import tensorflow as tf
import tensorflow.keras as keras
import numpy as np
import matplotlib.pyplot as plt
# define the standalone discriminator model
def define_discriminator(in_shape=(32,32,3)):
model = keras.models.Sequential()
# normal
model.add(keras.layers.Conv2D(64, (3,3), padding='same', input_shape=in_shape))
model.add(keras.layers.LeakyReLU(alpha=0.2))
# downsample
model.add(keras.layers.Conv2D(128, (3,3), strides=(2,2), padding='same'))
model.add(keras.layers.LeakyReLU(alpha=0.2))
# downsample
model.add(keras.layers.Conv2D(128, (3,3), strides=(2,2), padding='same'))
model.add(keras.layers.LeakyReLU(alpha=0.2))
# downsample
model.add(keras.layers.Conv2D(256, (3,3), strides=(2,2), padding='same'))
model.add(keras.layers.LeakyReLU(alpha=0.2))
# classifier
model.add(keras.layers.Flatten())
model.add(keras.layers.Dropout(0.4))
model.add(keras.layers.Dense(1, activation='sigmoid'))
# compile model
opt = keras.optimizers.Adam(lr=0.0002, beta_1=0.5)
model.compile(loss='binary_crossentropy', optimizer=opt, metrics=['accuracy'])
model.summary()
return model
# load and prepare cifar10 training images
def load_real_samples():
# load cifar10 dataset
(trainX, _), (_, _) = tf.keras.datasets.cifar10.load_data()
# convert from unsigned ints to floats
X = trainX.astype('float32')
# scale from [0,255] to [-1,1]
X = (X - 127.5) / 127.5
return X
# select real samples
def generate_real_samples(dataset, n_samples):
# choose random instances
ix = np.random.randint(0, dataset.shape[0], n_samples)
# retrieve selected images
X = dataset[ix]
# generate 'real' class labels (1)
y = np.ones((n_samples, 1))
return X, y
def generate_fake_samples1(n_samples):
# generate uniform random numbers in [0,1]
X = np.random.rand(32 * 32 * 3 * n_samples)
# update to have the range [-1, 1]
X = -1 + X * 2
# reshape into a batch of color images
X = X.reshape((n_samples, 32, 32, 3))
# generate 'fake' class labels (0)
y = np.zeros((n_samples, 1))
return X, y
# train the discriminator model
def train_discriminator(model, dataset, n_iter=20, n_batch=128):
half_batch = int(n_batch / 2)
# manually enumerate epochs
for i in range(n_iter):
# get randomly selected 'real' samples
X_real, y_real = generate_real_samples(dataset, half_batch)
# update discriminator on real samples
_, real_acc = model.train_on_batch(X_real, y_real)
# generate 'fake' examples
X_fake, y_fake = generate_fake_samples1(half_batch)
# update discriminator on fake samples
_, fake_acc = model.train_on_batch(X_fake, y_fake)
# summarize performance
print('>%d real=%.0f%% fake=%.0f%%' % (i+1, real_acc*100, fake_acc*100))
def test_train_discriminator():
# define the discriminator model
model = define_discriminator()
# load image data
dataset = load_real_samples()
# fit the model
train_discriminator(model, dataset)
# define the standalone generator model
def define_generator(latent_dim):
model = keras.models.Sequential()
# foundation for 4x4 image
n_nodes = 256 * 4 * 4
model.add(keras.layers.Dense(n_nodes, input_dim=latent_dim))
model.add(keras.layers.LeakyReLU(alpha=0.2))
model.add(keras.layers.Reshape((4, 4, 256)))
# upsample to 8x8
model.add(keras.layers.Conv2DTranspose(128, (4,4), strides=(2,2), padding='same'))
model.add(keras.layers.LeakyReLU(alpha=0.2))
# upsample to 16x16
model.add(keras.layers.Conv2DTranspose(128, (4,4), strides=(2,2), padding='same'))
model.add(keras.layers.LeakyReLU(alpha=0.2))
# upsample to 32x32
model.add(keras.layers.Conv2DTranspose(128, (4,4), strides=(2,2), padding='same'))
model.add(keras.layers.LeakyReLU(alpha=0.2))
# output layer
model.add(keras.layers.Conv2D(3, (3,3), activation='tanh', padding='same'))
return model
# generate points in latent space as input for the generator
def generate_latent_points(latent_dim, n_samples):
# generate points in the latent space
x_input = np.random.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
def generate_fake_samples(g_model, latent_dim, n_samples):
# generate points in latent space
x_input = generate_latent_points(latent_dim, n_samples)
# predict outputs
X = g_model.predict(x_input)
# create 'fake' class labels (0)
y = np.zeros((n_samples, 1))
return X, y
def show_fake_sample():
# size of the latent space
latent_dim = 100
# define the discriminator model
model = define_generator(latent_dim)
# generate samples
n_samples = 49
X, _ = generate_fake_samples(model, latent_dim, n_samples)
# scale pixel values from [-1,1] to [0,1]
X = (X + 1) / 2.0
# plot the generated samples
for i in range(n_samples):
# define subplot
plt.subplot(7, 7, 1 + i)
# turn off axis labels
plt.axis('off')
# plot single image
plt.imshow(X[i])
# show the figure
plt.show()
# define the combined generator and discriminator model, for updating the generator
def define_gan(g_model, d_model):
# make weights in the discriminator not trainable
d_model.trainable = False
# connect them
model = tf.keras.models.Sequential()
# add generator
model.add(g_model)
# add the discriminator
model.add(d_model)
# compile model
opt = tf.keras.optimizers.Adam(lr=0.0002, beta_1=0.5)
model.compile(loss='binary_crossentropy', optimizer=opt)
return model
def show_gan_module():
# size of the latent space
latent_dim = 100
# create the discriminator
d_model = define_discriminator()
# create the generator
g_model = define_generator(latent_dim)
# create the gan
gan_model = define_gan(g_model, d_model)
# summarize gan model
gan_model.summary()
# evaluate the discriminator, plot generated images, save generator model
def summarize_performance(epoch, g_model, d_model, dataset, latent_dim, n_samples=150):
# prepare real samples
X_real, y_real = generate_real_samples(dataset, n_samples)
# evaluate discriminator on real examples
_, acc_real = d_model.evaluate(X_real, y_real, verbose=0)
# prepare fake examples
x_fake, y_fake = generate_fake_samples(g_model, latent_dim, n_samples)
# evaluate discriminator on fake examples
_, acc_fake = d_model.evaluate(x_fake, y_fake, verbose=0)
# summarize discriminator performance
print('>Accuracy real: %.0f%%, fake: %.0f%%' % (acc_real * 100, acc_fake * 100))
# save plot
#save_plot(x_fake, epoch)
# save the generator model tile file
filename = 'generator_model_%03d.h5' % (epoch + 1)
g_model.save(filename)
# train the generator and discriminator
def train(g_model, d_model, gan_model, dataset, latent_dim, n_epochs=200, n_batch=128):
bat_per_epo = int(dataset.shape[0] / n_batch)
half_batch = int(n_batch / 2)
# manually enumerate epochs
for i in range(n_epochs):
# enumerate batches over the training set
for j in range(bat_per_epo):
# get randomly selected 'real' samples
X_real, y_real = generate_real_samples(dataset, half_batch)
# update discriminator model weights
d_loss1, _ = 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_loss2, _ = d_model.train_on_batch(X_fake, y_fake)
# prepare points in latent space as input for the generator
X_gan = generate_latent_points(latent_dim, n_batch)
# create inverted labels for the fake samples
y_gan = np.ones((n_batch, 1))
# update the generator via the discriminator's error
g_loss = gan_model.train_on_batch(X_gan, y_gan)
# summarize loss on this batch
print('>%d, %d/%d, d1=%.3f, d2=%.3f g=%.3f' %
(i + 1, j + 1, bat_per_epo, d_loss1, d_loss2, g_loss))
# evaluate the model performance, sometimes
if (i + 1) % 10 == 0:
summarize_performance(i, g_model, d_model, dataset, latent_dim)
def train_gan():
# size of the latent space
latent_dim = 100
# create the discriminator
d_model = define_discriminator()
# create the generator
g_model = define_generator(latent_dim)
# create the gan
gan_model = define_gan(g_model, d_model)
# load image data
dataset = load_real_samples()
# train model
train(g_model, d_model, gan_model, dataset, latent_dim)
# generate points in latent space as input for the generator
def generate_latent_points(latent_dim, n_samples):
# generate points in the latent space
x_input = np.random.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
# plot the generated images
def create_plot(examples, n):
# plot images
for i in range(n * n):
# define subplot
plt.subplot(n, n, 1 + i)
# turn off axis
plt.axis('off')
# plot raw pixel data
plt.imshow(examples[i, :, :])
plt.show()
def show_imgs_for_final_generator_model():
# load model
model = tf.keras.models.load_model('generator_model_010.h5')
# generate images
latent_points = generate_latent_points(100, 100)
# generate images
X = model.predict(latent_points)
# scale from [-1,1] to [0,1]
X = (X + 1) / 2.0
# plot the result
create_plot(X, 10)
def show_single_imgs():
model = tf.keras.models.load_model('generator_model_200.h5')
# all 0s
vector = np.asarray([[0.75 for _ in range(100)]])
# generate image
X = model.predict(vector)
# scale from [-1,1] to [0,1]
X = (X + 1) / 2.0
# plot the result
plt.imshow(X[0, :, :])
plt.show()
if __name__ == '__main__':
#define_discriminator()
#test_train_discriminator()
# show_fake_sample()
#show_gan_module()
train_gan()
show_imgs_for_final_generator_model()