CGAN代码并解释代码
时间: 2024-04-14 13:07:46 浏览: 122
CGAN是一种条件生成对抗网络,它可以根据给定的条件生成具有所需特征的图像。以下是一个简单的CGAN代码示例,其中使用MNIST数据集生成手写数字图像:
首先,我们需要导入所需的库和数据集:
```
import tensorflow as tf
from tensorflow.keras import layers
import numpy as np
import matplotlib.pyplot as plt
# 加载MNIST数据集
(train_images, train_labels), (_, _) = tf.keras.datasets.mnist.load_data()
train_images = train_images.reshape(train_images.shape[0], 28, 28, 1).astype('float32')
train_images = (train_images - 127.5) / 127.5 # 归一化为[-1, 1]的范围
```
接下来,我们定义生成器和判别器模型:
```
# 生成器模型
def make_generator_model():
model = tf.keras.Sequential()
model.add(layers.Dense(7*7*256, use_bias=False, input_shape=(100,)))
model.add(layers.BatchNormalization())
model.add(layers.LeakyReLU())
model.add(layers.Reshape((7, 7, 256)))
assert model.output_shape == (None, 7, 7, 256) # 注意:batch size没有限制
model.add(layers.Conv2DTranspose(128, (5, 5), strides=(1, 1), padding='same', use_bias=False))
assert model.output_shape == (None, 7, 7, 128)
model.add(layers.BatchNormalization())
model.add(layers.LeakyReLU())
model.add(layers.Conv2DTranspose(64, (5, 5), strides=(2, 2), padding='same', use_bias=False))
assert model.output_shape == (None, 14, 14, 64)
model.add(layers.BatchNormalization())
model.add(layers.LeakyReLU())
model.add(layers.Conv2DTranspose(1, (5, 5), strides=(2, 2), padding='same', use_bias=False, activation='tanh'))
assert model.output_shape == (None, 28, 28, 1)
return model
# 判别器模型
def make_discriminator_model():
model = tf.keras.Sequential()
model.add(layers.Conv2D(64, (5, 5), strides=(2, 2), padding='same',
input_shape=[28, 28, 1]))
model.add(layers.LeakyReLU())
model.add(layers.Dropout(0.3))
model.add(layers.Conv2D(128, (5, 5), strides=(2, 2), padding='same'))
model.add(layers.LeakyReLU())
model.add(layers.Dropout(0.3))
model.add(layers.Flatten())
model.add(layers.Dense(1))
return model
```
然后,我们定义损失函数和优化器:
```
# 定义损失函数和优化器
cross_entropy = tf.keras.losses.BinaryCrossentropy(from_logits=True)
def discriminator_loss(real_output, fake_output):
real_loss = cross_entropy(tf.ones_like(real_output), real_output)
fake_loss = cross_entropy(tf.zeros_like(fake_output), fake_output)
total_loss = real_loss + fake_loss
return total_loss
def generator_loss(fake_output):
return cross_entropy(tf.ones_like(fake_output), fake_output)
generator_optimizer = tf.keras.optimizers.Adam(1e-4)
discriminator_optimizer = tf.keras.optimizers.Adam(1e-4)
```
接下来,我们定义训练循环:
```
# 定义训练循环
@tf.function
def train_step(images, labels):
noise = tf.random.normal([BATCH_SIZE, 100])
with tf.GradientTape() as gen_tape, tf.GradientTape() as disc_tape:
generated_images = generator([noise, labels], training=True)
real_output = discriminator([images, labels], training=True)
fake_output = discriminator([generated_images, labels], training=True)
gen_loss = generator_loss(fake_output)
disc_loss = discriminator_loss(real_output, fake_output)
gradients_of_generator = gen_tape.gradient(gen_loss, generator.trainable_variables)
gradients_of_discriminator = disc_tape.gradient(disc_loss, discriminator.trainable_variables)
generator_optimizer.apply_gradients(zip(gradients_of_generator, generator.trainable_variables))
discriminator_optimizer.apply_gradients(zip(gradients_of_discriminator, discriminator.trainable_variables))
```
最后,我们开始训练模型,生成手写数字图像:
```
# 开始训练模型
EPOCHS = 50
BATCH_SIZE = 256
num_examples_to_generate = 16
# 我们将重复使用该种子(因此在动画 GIF 中更容易可视化进度)
seed = tf.random.normal([num_examples_to_generate, 100])
# 将标签与种子一起生成图像
def generate_and_save_images(model, epoch, test_input, labels):
# 注意 training` 设定为 False
# 因此,所有层都在推理模式下运行(batchnorm)。
predictions = model([test_input, labels], training=False)
fig = plt.figure(figsize=(4, 4))
for i in range(predictions.shape[0]):
plt.subplot(4, 4, i+1)
plt.imshow((predictions[i, :, :, 0] + 1) / 2, cmap='gray')
plt.axis('off')
plt.savefig('image_at_epoch_{:04d}.png'.format(epoch))
plt.show()
generator = make_generator_model()
discriminator = make_discriminator_model()
for epoch in range(EPOCHS):
for i in range(train_images.shape[0] // BATCH_SIZE):
images = train_images[i*BATCH_SIZE:(i+1)*BATCH_SIZE]
labels = train_labels[i*BATCH_SIZE:(i+1)*BATCH_SIZE]
train_step(images, labels)
if epoch % 5 == 0:
generate_and_save_images(generator, epoch, seed, np.array([0,1,2,3,4,5,6,7,8,9]))
```
这段代码中,我们使用了条件变量labels,它可以让我们控制生成的图像具有所需的特征,例如生成特定数字的图像。在训练循环中,我们使用了两个损失函数:判别器损失和生成器损失。在每个epoch结束时,我们生成一些手写数字图像并保存它们。
阅读全文