- 🍨 本文为🔗365天深度学习训练营 中的学习记录博客
- 🍖 原作者:K同学啊
基础任务
- 学习DCGAN的基本原理
- 了解DCGAN与GAN的区别
- 绘制DCGAN网络结构图
- 学习DCGAN代码,并跑通代码
一、前期准备
1、导入第三方库
python
import torch, random, os
import torch.nn as nn
import torch.nn.parallel
import torch.optim as optim
import torch.utils.data
import torchvision.datasets as dset
import torchvision.transforms as transforms
import torchvision.utils as vutils
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from IPython.display import HTML
manualSeed =999
print('Random Seed:',manualSeed)
random.seed(manualSeed)
torch.manual_seed(manualSeed)
torch.use_deterministic_algorithms(True)
- 定义超参数
python
# 设置超参数
dataroot = "F:/365data/G2/"
batch_size = 128
image_size = 64
nz = 100
ngf = 64
ndf = 64
num_epochs = 50
lr = 0.0002
beta1 = 0.5
3、导入数据
python
# 设置数据集,并用matplotlib展示一些图片
dataset = dset.ImageFolder(root=dataroot,
transform=transforms.Compose([
transforms.Resize(image_size),
transforms.CenterCrop(image_size),#中心裁剪
transforms.ToTensor(),
transforms.Normalize((0.5,0.5,0.5),(0.5,0.5,0.5))
]))
dataloader = torch.utils.data.DataLoader(dataset,
batch_size=batch_size,
shuffle=True,
num_workers=5)
device = torch.device('cuda:0' if (torch.cuda.is_available()) else 'cpu')
print('使用的设备是:',device)
real_batch = next(iter(dataloader))
plt.figure(figsize=(8,8))
plt.axis('off')
plt.title('Training Images')
plt.imshow(np.transpose(vutils.make_grid(real_batch[0].to(device)[:24],
padding=2,
normalize=True).cpu(),(1,2,0)))
三、定义模型
1、初始化权重
python
# 自定义权重初始化函数,作用于netG和netD
def weights_init(m):
# 获取当前层的类名
classname = m.__class__.__name__
# 如果类名中包含'Conv',即当前层是卷积层
if classname.find('Conv') != -1:
# 使用正态分布初始化权重数据,均值为0,标准差为0.02
nn.init.normal_(m.weight.data,0.0,0.02)
# 如果类名中包含'BatchNorm',即当前层是批归一化层
elif classname.find('BatchNorm') != -1:
# 使用正态分布初始化权重数据,均值为1,标准差为0.02
nn.init.normal_(m.weight.data,1.0,0.02)
# 使用常数初始化偏置项数据,值为0
nn.init.constant_(m.bias.data,0)
2、定义生成器
python
# 定义生成器
class Generator(nn.Module):
def __init__(self):
super(Generator,self).__init__()
self.main = nn.Sequential(
# 输入为Z,经过一个转置卷积层
nn.ConvTranspose2d(nz,ngf*8,4,1,0,bias=False),
nn.BatchNorm2d(ngf*8),
nn.ReLU(True),
# 输出尺寸:(ngf*8) x 4 x 4
nn.ConvTranspose2d(ngf*8,ngf*4,4,2,1,bias=False),
nn.BatchNorm2d(ngf*4),
nn.ReLU(True),
# 输出尺寸:(ngf*4) x 8 x 8
nn.ConvTranspose2d(ngf*4,ngf*2,4,2,1,bias=False),
nn.BatchNorm2d(ngf*2),
nn.ReLU(True),
# 输出尺寸:(ngf*2) x 16 x 16
nn.ConvTranspose2d(ngf*2,ngf,4,2,1,bias=False),
nn.BatchNorm2d(ngf),
nn.ReLU(True),
# 输出尺寸:(ngf) x 32 x 32
nn.ConvTranspose2d(ngf,3,4,2,1,bias=False),
nn.Tanh()
# 输出尺寸:3 x 64 x 64
)
def forward(self,input):
return self.main(input)
python
# 创建生成器
netG = Generator().to(device)
# 使用'weights_init'函数对所有权重进行随机初始化,
# 平均值(mean)设置为0,标准差(stdev)设置为0.02.
netG.apply(weights_init)
# 打印生成器
print(netG)
3、定义判别器
python
# 定义判别器
class Discriminator(nn.Module):
def __init__(self):
super(Discriminator,self).__init__()
# 定义判别器的主要结构,使用Sequential容器将多个层按顺序组合在一起
self.main = nn.Sequential(
# 输入尺寸:3 x 64 x 64
nn.Conv2d(3,ndf,4,2,1,bias=False),
nn.LeakyReLU(0.2,inplace=True),
# 输出尺寸:(ndf) x 32 x 32
nn.Conv2d(ndf,ndf*2,4,2,1,bias=False),
nn.BatchNorm2d(ndf*2),
nn.LeakyReLU(0.2,inplace=True),
# 输出尺寸:(ndf*2) x 16 x 16
nn.Conv2d(ndf*2,ndf*4,4,2,1,bias=False),
nn.BatchNorm2d(ndf*4),
nn.LeakyReLU(0.2,inplace=True),
# 输出尺寸:(ndf*4) x 8 x 8
nn.Conv2d(ndf*4,ndf*8,4,2,1,bias=False),
nn.BatchNorm2d(ndf*8),
nn.LeakyReLU(0.2,inplace=True),
# 输出尺寸:(ndf*8) x 4 x 4
nn.Conv2d(ndf*8,1,4,1,0,bias=False),
nn.Sigmoid()
)
def forward(self,input):
return self.main(input)
python
# 创建判别器
netD = Discriminator().to(device)
# 使用'weights_init'函数对所有权重进行随机初始化,
# 平均值(mean)设置为0,标准差(stdev)设置为0.2
netD.apply(weights_init)
# 打印判别器
print(netD)
四、训练模型
1、定义训练参数
python
# 初始化二进制交叉熵损失函数
criterion = nn.BCELoss()
# 创建用于可视化生成器进程的潜在向量批次
fixed_noise = torch.randn(64,nz,1,1,device=device)
real_label = 1.
fake_label = 0.
# 设置Adam优化器
optimizerD = optim.Adam(netD.parameters(),lr=lr,betas=(beta1,0.999))
optimizerG = optim.Adam(netG.parameters(),lr=lr,betas=(beta1,0.999))
2、训练模型
python
img_list = [] # 用于保存生成器生成的图片
G_losses = [] # 用于保存生成器的损失
D_losses = [] # 用于保存判别器的损失
iters = 0 # 迭代次数
print('Starting Training Loop...')
for epoch in range(num_epochs):
# 对于dataloader中的每个batch
for i, data in enumerate(dataloader,0):
############################
# (1) 更新判别器网络:最大化 log(D(x)) + log(1-D(G(z)))
###########################
## 训练真实数据
netD.zero_grad()
# 准备真实数据
real_cpu = data[0].to(device)
b_size = real_cpu.size(0)
label = torch.full((b_size,), real_label, dtype=torch.float,device=device) # 创建真实标签
# 通过判别器前向传播真实数据
output = netD(real_cpu).view(-1)
# 计算真实数据的损失
errD_real = criterion(output,label)
errD_real.backward()
D_x = output.mean().item()
## 训练生成数据
# 准备生成数据
noise = torch.randn(b_size,nz,1,1,device=device)
# 通过生成器生成数据
fake = netG(noise)
label.fill_(fake_label)
# 通过判别器前向传播生成数据
output = netD(fake.detach()).view(-1)
# 计算生成数据的损失
errD_fake = criterion(output,label)
errD_fake.backward()
D_G_z1 = output.mean().item()
# 将真实数据和生成数据的损失相加
errD = errD_real + errD_fake
# 更新判别器参数
optimizerD.step()
############################
# (2) 更新生成器网络:最大化 log(D(G(z)))
###########################
netG.zero_grad()
label.fill_(real_label) # 为真实标签填充1
# 通过判别器前向传播生成数据
output = netD(fake).view(-1)
# 计算生成数据的损失
errG = criterion(output,label)
# 更新生成器参数
errG.backward()
D_G_z2 = output.mean().item()
optimizerG.step()
# 输出训练统计信息
if i % 400 == 0:
print('[%d/%d][%d/%d]\tLoss_D: %.4f\tLoss_G: %.4f\tD(x): %.4f\tD(G(z)): %.4f / %.4f'
% (epoch, num_epochs, i, len(dataloader),
errD.item(), errG.item(), D_x, D_G_z1, D_G_z2))
# 保存损失以便后续绘图
G_losses.append(errG.item())
D_losses.append(errD.item())
# 通过固定噪声生成的图片来跟踪生成器的训练进度
if (iters % 500 == 0) or ((epoch == num_epochs-1) and (i == len(dataloader)-1)):
with torch.no_grad():
fake = netG(fixed_noise).detach().cpu()
img_list.append(vutils.make_grid(fake,padding=2,normalize=True))
iters += 1
python
Starting Training Loop...
[0/50][0/36] Loss_D: 1.7508 Loss_G: 5.3002 D(x): 0.5574 D(G(z)): 0.5731 / 0.0074
[1/50][0/36] Loss_D: 0.0362 Loss_G: 14.2664 D(x): 0.9773 D(G(z)): 0.0000 / 0.0000
[2/50][0/36] Loss_D: 0.2026 Loss_G: 16.0342 D(x): 0.9112 D(G(z)): 0.0001 / 0.0000
[3/50][0/36] Loss_D: 2.0525 Loss_G: 15.8344 D(x): 0.9709 D(G(z)): 0.7586 / 0.0000
[4/50][0/36] Loss_D: 0.6356 Loss_G: 8.1305 D(x): 0.9236 D(G(z)): 0.3685 / 0.0009
[5/50][0/36] Loss_D: 0.6821 Loss_G: 5.9364 D(x): 0.6237 D(G(z)): 0.0144 / 0.0069
[6/50][0/36] Loss_D: 1.2046 Loss_G: 7.3426 D(x): 0.7934 D(G(z)): 0.5032 / 0.0014
[7/50][0/36] Loss_D: 0.3649 Loss_G: 2.4782 D(x): 0.8240 D(G(z)): 0.0923 / 0.1542
[8/50][0/36] Loss_D: 0.4195 Loss_G: 3.9613 D(x): 0.7799 D(G(z)): 0.0813 / 0.0324
[9/50][0/36] Loss_D: 0.4080 Loss_G: 3.5926 D(x): 0.7544 D(G(z)): 0.0522 / 0.0381
[10/50][0/36] Loss_D: 0.5388 Loss_G: 3.2718 D(x): 0.7955 D(G(z)): 0.1924 / 0.0625
[11/50][0/36] Loss_D: 0.5069 Loss_G: 4.6123 D(x): 0.8644 D(G(z)): 0.2365 / 0.0201
[12/50][0/36] Loss_D: 0.3624 Loss_G: 6.0753 D(x): 0.9865 D(G(z)): 0.2575 / 0.0056
[13/50][0/36] Loss_D: 0.5918 Loss_G: 7.9663 D(x): 0.9450 D(G(z)): 0.3553 / 0.0015
[14/50][0/36] Loss_D: 0.7028 Loss_G: 2.9400 D(x): 0.6269 D(G(z)): 0.0732 / 0.0965
[15/50][0/36] Loss_D: 0.5989 Loss_G: 7.1686 D(x): 0.9460 D(G(z)): 0.3633 / 0.0016
[16/50][0/36] Loss_D: 0.4842 Loss_G: 3.3526 D(x): 0.8576 D(G(z)): 0.1826 / 0.0679
[17/50][0/36] Loss_D: 0.5359 Loss_G: 3.9497 D(x): 0.7646 D(G(z)): 0.1681 / 0.0320
[18/50][0/36] Loss_D: 0.5714 Loss_G: 3.7671 D(x): 0.6718 D(G(z)): 0.0380 / 0.0435
[19/50][0/36] Loss_D: 0.9133 Loss_G: 9.8651 D(x): 0.9621 D(G(z)): 0.5022 / 0.0003
[20/50][0/36] Loss_D: 0.3539 Loss_G: 4.9887 D(x): 0.8234 D(G(z)): 0.0916 / 0.0127
[21/50][0/36] Loss_D: 0.4090 Loss_G: 5.5089 D(x): 0.8455 D(G(z)): 0.1559 / 0.0068
[22/50][0/36] Loss_D: 0.2700 Loss_G: 3.9109 D(x): 0.8547 D(G(z)): 0.0828 / 0.0305
[23/50][0/36] Loss_D: 0.3666 Loss_G: 4.6487 D(x): 0.7987 D(G(z)): 0.0728 / 0.0169
[24/50][0/36] Loss_D: 0.2080 Loss_G: 4.8461 D(x): 0.9183 D(G(z)): 0.0987 / 0.0132
[25/50][0/36] Loss_D: 0.2491 Loss_G: 4.2578 D(x): 0.8474 D(G(z)): 0.0466 / 0.0284
[26/50][0/36] Loss_D: 1.4370 Loss_G: 0.9225 D(x): 0.4111 D(G(z)): 0.0110 / 0.4851
[27/50][0/36] Loss_D: 0.1547 Loss_G: 5.1120 D(x): 0.8961 D(G(z)): 0.0276 / 0.0129
[28/50][0/36] Loss_D: 0.8567 Loss_G: 6.5480 D(x): 0.9418 D(G(z)): 0.4856 / 0.0033
[29/50][0/36] Loss_D: 0.6378 Loss_G: 4.9804 D(x): 0.8771 D(G(z)): 0.3173 / 0.0184
[30/50][0/36] Loss_D: 0.3486 Loss_G: 7.5059 D(x): 0.9430 D(G(z)): 0.1735 / 0.0045
[31/50][0/36] Loss_D: 0.2469 Loss_G: 5.3903 D(x): 0.9004 D(G(z)): 0.1147 / 0.0071
[32/50][0/36] Loss_D: 2.1140 Loss_G: 4.0502 D(x): 0.2535 D(G(z)): 0.0006 / 0.0527
[33/50][0/36] Loss_D: 0.3779 Loss_G: 3.3574 D(x): 0.7900 D(G(z)): 0.0785 / 0.0629
[34/50][0/36] Loss_D: 0.7457 Loss_G: 6.2508 D(x): 0.9369 D(G(z)): 0.4182 / 0.0050
[35/50][0/36] Loss_D: 0.4192 Loss_G: 4.5746 D(x): 0.7539 D(G(z)): 0.0488 / 0.0286
[36/50][0/36] Loss_D: 0.4010 Loss_G: 3.1014 D(x): 0.7694 D(G(z)): 0.0720 / 0.0785
[37/50][0/36] Loss_D: 0.4838 Loss_G: 3.9613 D(x): 0.8002 D(G(z)): 0.1559 / 0.0388
[38/50][0/36] Loss_D: 0.6112 Loss_G: 3.6062 D(x): 0.6384 D(G(z)): 0.0287 / 0.0572
[39/50][0/36] Loss_D: 0.5417 Loss_G: 3.0678 D(x): 0.7755 D(G(z)): 0.1755 / 0.0730
[40/50][0/36] Loss_D: 0.5360 Loss_G: 3.0343 D(x): 0.7394 D(G(z)): 0.1202 / 0.0796
[41/50][0/36] Loss_D: 0.3049 Loss_G: 5.8082 D(x): 0.7885 D(G(z)): 0.0089 / 0.0091
[42/50][0/36] Loss_D: 0.3132 Loss_G: 3.4717 D(x): 0.8981 D(G(z)): 0.1557 / 0.0584
[43/50][0/36] Loss_D: 0.2647 Loss_G: 5.5635 D(x): 0.9601 D(G(z)): 0.1828 / 0.0060
[44/50][0/36] Loss_D: 0.5790 Loss_G: 4.6106 D(x): 0.9657 D(G(z)): 0.3423 / 0.0241
[45/50][0/36] Loss_D: 0.3232 Loss_G: 3.9199 D(x): 0.8089 D(G(z)): 0.0699 / 0.0371
[46/50][0/36] Loss_D: 0.4083 Loss_G: 4.3659 D(x): 0.8922 D(G(z)): 0.2183 / 0.0195
[47/50][0/36] Loss_D: 0.5366 Loss_G: 5.3078 D(x): 0.9345 D(G(z)): 0.3024 / 0.0120
[48/50][0/36] Loss_D: 0.3728 Loss_G: 3.7532 D(x): 0.8676 D(G(z)): 0.1756 / 0.0430
[49/50][0/36] Loss_D: 0.8418 Loss_G: 1.9751 D(x): 0.5530 D(G(z)): 0.0795 / 0.2058
3、可视化
python
# 可视化
plt.figure(figsize=(10,5))
plt.title('Generator and Discriminator Loss During Training')
plt.plot(G_losses,label='G')
plt.plot(D_losses,label='D')
plt.xlabel('iterations')
plt.ylabel('Loss')
plt.legend()
plt.show()
python
fig = plt.figure(figsize=(8,8))
plt.axis('off')
ims = [[plt.imshow(np.transpose(i,(1,2,0)),animated=True)] for i in img_list]
ani = animation.ArtistAnimation(fig,ims,interval=1000,repeat_delay=1000,blit=True)
HTML(ani.to_jshtml())
python
# 从数据加载器中获取一批真实图像
real_batch = next(iter(dataloader))
# 将真实图像可视化
plt.figure(figsize=(15,15))
plt.subplot(1,2,1)
plt.axis('off')
plt.title('Real Images')
plt.imshow(np.transpose(vutils.make_grid(real_batch[0].to(device)[:64],padding=5,normalize=True).cpu(),(1,2,0)))
# 绘制上一个时期生成的假图像
plt.subplot(1,2,2)
plt.axis('off')
plt.title('Fake Images')
plt.imshow(np.transpose(img_list[-1],(1,2,0)))
plt.show()
五、总结
- DCGAN与GAN的区别是,前者的生成器中使用了反卷积操作,它能放大特征图,从而改变尺寸。
- 而判别器中则使用卷积步长取代空间池化。
- 经过训练,生成的图像已经有部分接近真实图像了。