DCGAN

DCGAN

背景介绍

  DCGAN(Deep Convolutional Generative Adversarial Networks, 深度卷积生成式对抗网络):2016年发表于ICLR,是GAN类型网络的升级版本,其中改变的只是将GAN中的全连接层变为卷积层和上采样层,这样可以使用更少的参数实现更大像素图像的生成

Dataset

DCGAN特点

  将全连接层换成了卷积层和上采样层,极大缩小参数量,可以实现大尺寸图像的生成

DCGAN图像分析

generator
discriminator

TensorFlow2.0实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import os
import numpy as np
import cv2 as cv
from functools import reduce
import tensorflow as tf
import tensorflow.keras as keras


def compose(*funcs):
if funcs:
return reduce(lambda f, g: lambda *a, **kw: g(f(*a, **kw)), funcs)
else:
raise ValueError('Composition of empty sequence not supported.')


def generator(input_shape):
input_tensor = keras.layers.Input(input_shape, name='input')
x = input_tensor

x = compose(keras.layers.Dense(1568, activation='relu', name='dense_relu'),
keras.layers.Reshape((7, 7, 32), name='reshape'),
keras.layers.Conv2D(64, (3, 3), (1, 1), 'same', name='conv1'),
keras.layers.BatchNormalization(momentum=0.8, name='bn1'),
keras.layers.ReLU(name='relu1'),
keras.layers.UpSampling2D((2, 2), name='upsampling1'),
keras.layers.Conv2D(128, (3, 3), (1, 1), 'same', name='conv2'),
keras.layers.BatchNormalization(momentum=0.8, name='bn2'),
keras.layers.ReLU(name='relu2'),
keras.layers.UpSampling2D((2, 2), name='upsampling2'),
keras.layers.Conv2D(64, (3, 3), (1, 1), 'same', name='conv3'),
keras.layers.BatchNormalization(momentum=0.8, name='bn3'),
keras.layers.ReLU(name='relu3'),
keras.layers.Conv2D(1, (3, 3), (1, 1), 'same', activation='tanh', name='conv_tanh'))(x)

model = keras.Model(input_tensor, x, name='DCGAN-Generator')

return model


def discriminator(input_shape):
input_tensor = keras.layers.Input(input_shape, name='input')
x = input_tensor

x = compose(keras.layers.Conv2D(32, (3, 3), (2, 2), 'same', name='conv1'),
keras.layers.BatchNormalization(momentum=0.8, name='bn1'),
keras.layers.LeakyReLU(0.2, name='leakyrelu1'),
keras.layers.Conv2D(64, (3, 3), (2, 2), 'same', name='conv2'),
keras.layers.BatchNormalization(momentum=0.8, name='bn2'),
keras.layers.LeakyReLU(0.2, name='leakyrelu2'),
keras.layers.Conv2D(128, (3, 3), (2, 2), 'same', name='conv3'),
keras.layers.BatchNormalization(momentum=0.8, name='bn3'),
keras.layers.LeakyReLU(0.2, name='leakyrelu3'),
keras.layers.GlobalAveragePooling2D(name='global_averagepool'),
keras.layers.Dense(1, activation='sigmoid', name='dense_sigmoid'))(x)

model = keras.Model(input_tensor, x, name='DCGAN-Discriminator')

return model


def dcgan(input_shape, model_g, model_d):
input_tensor = keras.layers.Input(input_shape)
x = input_tensor

x = model_g(x)
model_d.trainable = False
x = model_d(x)

model = keras.Model(input_tensor, x, name='DCGAN')

return model


def save_picture(image, save_path, picture_num):
image = ((image + 1) * 127.5).astype(np.uint8)
image = np.concatenate([image[i * picture_num:(i + 1) * picture_num] for i in range(picture_num)], axis=2)
image = np.concatenate([image[i] for i in range(picture_num)], axis=0)
cv.imwrite(save_path, image)


if __name__ == '__main__':
(x, _), (_, _) = keras.datasets.mnist.load_data()
batch_size = 256
epochs = 20
tf.random.set_seed(22)
save_path = r'.\dcgan'
if not os.path.exists(save_path):
os.makedirs(save_path)

x = x[..., np.newaxis].astype(np.float32) / 127.5 - 1
x = tf.data.Dataset.from_tensor_slices(x).batch(batch_size)

optimizer = keras.optimizers.Adam(0.0002, 0.5)
loss = keras.losses.BinaryCrossentropy()

real_dacc = keras.metrics.BinaryAccuracy()
fake_dacc = keras.metrics.BinaryAccuracy()
gacc = keras.metrics.BinaryAccuracy()

model_d = discriminator(input_shape=(28, 28, 1))
model_d.compile(optimizer=optimizer, loss='binary_crossentropy')

model_g = generator(input_shape=(100,))

model_g.build(input_shape=(100,))
model_g.summary()
keras.utils.plot_model(model_g, 'DCGAN-generator.png', show_shapes=True, show_layer_names=True)

model_d.build(input_shape=(28, 28, 1))
model_d.summary()
keras.utils.plot_model(model_d, 'DCGAN-discriminator.png', show_shapes=True, show_layer_names=True)

model = dcgan(input_shape=(100,), model_g=model_g, model_d=model_d)
model.compile(optimizer=optimizer, loss='binary_crossentropy')

model.build(input_shape=(100,))
model.summary()
keras.utils.plot_model(model, 'DCGAN.png', show_shapes=True, show_layer_names=True)

for epoch in range(epochs):
x = x.shuffle(np.random.randint(0, 10000))
x_db = iter(x)

for step, real_image in enumerate(x_db):
noise = np.random.normal(0, 1, (real_image.shape[0], 100))
fake_image = model_g(noise)

real_dacc(np.ones((real_image.shape[0], 1)), model_d(real_image))
fake_dacc(np.zeros((real_image.shape[0], 1)), model_d(fake_image))
gacc(np.ones((real_image.shape[0], 1)), model(noise))

real_dloss = model_d.train_on_batch(real_image, np.ones((real_image.shape[0], 1)))
fake_dloss = model_d.train_on_batch(fake_image, np.zeros((real_image.shape[0], 1)))
gloss = model.train_on_batch(noise, np.ones((real_image.shape[0], 1)))

if step % 20 == 0:
print('epoch = {}, step = {}, real_dacc = {}, fake_dacc = {}, gacc = {}'.format(epoch, step, real_dacc.result(), fake_dacc.result(), gacc.result()))
real_dacc.reset_states()
fake_dacc.reset_states()
gacc.reset_states()
fake_data = np.random.normal(0, 1, (100, 100))
fake_image = model_g(fake_data)
save_picture(fake_image.numpy(), save_path + '\\epoch{}_step{}.jpg'.format(epoch, step), 10)

gan

模型运行结果

gan

小技巧

  1. 图像输入可以先将其归一化到0-1之间或者-1-1之间,因为网络的参数一般都比较小,所以归一化后计算方便,收敛较快。
  2. 注意其中的一些维度变换和numpytensorflow常用操作,否则在阅读代码时可能会产生一些困难。
  3. 可以设置一些权重的保存方式学习率的下降方式早停方式
  4. DCGAN对于网络结构,优化器参数,网络层的一些超参数都是非常敏感的,效果不好不容易发现原因,这可能需要较多的工程实践经验
  5. 先创建判别器,然后进行compile,这样判别器就固定了,然后创建生成器时,不要训练判别器,需要将判别器的trainable改成False,此时不会影响之前固定的判别器,这个可以通过模型的_collection_collected_trainable_weights属性查看,如果该属性为空,则模型不训练,否则模型可以训练,compile之后,该属性固定,无论后面如何修改trainable,只要不重新compile,都不影响训练。

DCGAN小结

  DCGAN是一种非常简单的生成式对抗网络,从上图可以看出DCGAN模型的参数量只有0.4M,这样使得大图像的生成变得可能,DCGAN没有特别的创新点,运用了深度卷积在初代GAN上,为以后GAN的发展提供了思路。

-------------本文结束感谢您的阅读-------------
0%