赞
踩
- from __future__ import print_function, division
-
- from keras.datasets import mnist
- from keras.layers import Input, Dense, Reshape, Flatten, Dropout
- from keras.layers import BatchNormalization, Activation, ZeroPadding2D
- from keras.layers.advanced_activations import LeakyReLU
- from keras.layers.convolutional import UpSampling2D, Conv2D
- from keras.models import Sequential, Model
- from keras.optimizers import Adam
-
- import matplotlib.pyplot as plt
-
- import sys
-
- import numpy as np
-
- class GAN():
- def __init__(self):
- self.img_rows = 28
- self.img_cols = 28
- self.channels = 1
- self.img_shape = (self.img_rows, self.img_cols, self.channels)
- self.latent_dim = 100
-
- optimizer = Adam(0.0002, 0.5)
-
- # Build and compile the discriminator
- # 建立和编译判别器
- self.discriminator = self.build_discriminator()
- self.discriminator.compile(loss='binary_crossentropy',
- optimizer=optimizer,
- metrics=['accuracy'])
-
- # Build the generator
- # 建立生成器
- self.generator = self.build_generator()
-
- # The generator takes noise as input and generates imgs
- # 生成器输入随机数值(噪声)生成图片
- z = Input(shape=(self.latent_dim,))
- img = self.generator(z)
-
- # For the combined model we will only train the generator
- # 联合模型,只训练生成器
- self.discriminator.trainable = False
-
- # The discriminator takes generated images as input and determines validity
- # 判别器以生成的图片为输入判别有效性
- validity = self.discriminator(img)
-
- # The combined model (stacked generator and discriminator)
- # 联合模型(叠加生成器和判别器)
- # Trains the generator to fool the discriminator
- # 训练生成器欺骗判别器
- self.combined = Model(z, validity)
- self.combined.compile(loss='binary_crossentropy', optimizer=optimizer)
-
-
- def build_generator(self):
-
- model = Sequential()
-
- model.add(Dense(256, input_dim=self.latent_dim))
- model.add(LeakyReLU(alpha=0.2))
- model.add(BatchNormalization(momentum=0.8))
- model.add(Dense(512))
- model.add(LeakyReLU(alpha=0.2))
- model.add(BatchNormalization(momentum=0.8))
- model.add(Dense(1024))
- model.add(LeakyReLU(alpha=0.2))
- model.add(BatchNormalization(momentum=0.8))
- model.add(Dense(np.prod(self.img_shape), activation='tanh'))
- model.add(Reshape(self.img_shape))
-
- model.summary()
-
- noise = Input(shape=(self.latent_dim,))
- img = model(noise)
-
- return Model(noise, img)
-
- def build_discriminator(self):
-
- model = Sequential()
-
- model.add(Flatten(input_shape=self.img_shape))
- model.add(Dense(512))
- model.add(LeakyReLU(alpha=0.2))
- model.add(Dense(256))
- model.add(LeakyReLU(alpha=0.2))
- model.add(Dense(1, activation='sigmoid'))
- model.summary()
-
- img = Input(shape=self.img_shape)
- validity = model(img)
-
- return Model(img, validity)
-
- def train(self, epochs, batch_size=128, sample_interval=50):
-
- # Load the dataset
- # 加载数据
- (X_train, _), (_, _) = mnist.load_data()
-
- # Rescale -1 to 1
- # 数据缩放到-1至1之间
- X_train = X_train / 127.5 - 1.
- X_train = np.expand_dims(X_train, axis=3)
-
- # Adversarial ground truths
- # 对抗性的基本事实
- valid = np.ones((batch_size, 1)) # 32行1列,每个值都是1
- fake = np.zeros((batch_size, 1)) # 32行1列,每个值都是0
-
- for epoch in range(epochs):
-
- # ---------------------
- # Train Discriminator 训练判别器
- # ---------------------
-
- # Select a random batch of images
- # 随机选择一批图像
- idx = np.random.randint(0, X_train.shape[0], batch_size) # X_train.shape (6000,28,28,1)
- imgs = X_train[idx]
-
- noise = np.random.normal(0, 1, (batch_size, self.latent_dim))
-
- # Generate a batch of new images
- # 随机生成一批图像
- gen_imgs = self.generator.predict(noise)
-
- # Train the discriminator
- # 训练判别器
- d_loss_real = self.discriminator.train_on_batch(imgs, valid)
- d_loss_fake = self.discriminator.train_on_batch(gen_imgs, fake)
- d_loss = 0.5 * np.add(d_loss_real, d_loss_fake)
-
- # ---------------------
- # Train Generator 训练生成器
- # ---------------------
-
- noise = np.random.normal(0, 1, (batch_size, self.latent_dim))
-
- # Train the generator (to have the discriminator label samples as valid)
- # 训练生成器(使判别器标签样本有效)
- g_loss = self.combined.train_on_batch(noise, valid)
-
- # Plot the progress
- # 规划进度
- print ("%d [D loss: %f, acc.: %.2f%%] [G loss: %f]" % (epoch, d_loss[0], 100*d_loss[1], g_loss))
-
- # If at save interval => save generated image samples
- # 如果在保证图像间隔 => 保存生成的图像样本
- if epoch % sample_interval == 0:
- self.sample_images(epoch)
-
- def sample_images(self, epoch):
- r, c = 5, 5
- noise = np.random.normal(0, 1, (r * c, self.latent_dim))
- gen_imgs = self.generator.predict(noise)
-
- # Rescale images 0 - 1
- # 数据缩放到-1至1之间
- gen_imgs = 0.5 * gen_imgs + 0.5
-
- fig, axs = plt.subplots(r, c)
- cnt = 0
- for i in range(r):
- for j in range(c):
- axs[i,j].imshow(gen_imgs[cnt, :,:,0], cmap='gray')
- axs[i,j].axis('off')
- cnt += 1
- fig.savefig("images/%d.png" % epoch)
- plt.close()
-
-
- if __name__ == '__main__':
- gan = GAN()
- gan.train(epochs=30000, batch_size=32, sample_interval=200)
代码解释
from __future__ import print_function, division
在开头加上from __future__ import print_function, division这句之后,即使在python2.X,使用print、division就得像python3.X那样加括号使用。python2.X中print不需要括号,而在python3.X中则需要。
详解:
https://blog.csdn.net/xiaotao_1/article/details/79460365
https://blog.csdn.net/feixingfei/article/details/7081446
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。