目录
案例1:鲜花生成
参考:鲜花生成-DCGAN案例
代码位置:E:\项目例程\GNN\DCGAN\02DCGAN_oxford17
可学习点:函数分文件写法,可反复使用;loss,可能性的可视化;最后生成图片与真实图片的对比;生成图片的动态显示,并保存到项目文件
具体实现
整体分为4个文件:data.py、model.py、net.py、main.py
数据展示
1.oxford17_all 是原始下载的数据集,没有分类17*80=1360
2.oxford_class 是写了个程序处理过的,1-80是class1,81-160是class2以此类推
数据分类(processdata.py)
1.oxford17_all 是原始下载的数据集,没有分类17*80=1360
2.oxford_class 是写了个程序处理过的,1-80是class1,81-160是class2以此类推
3.处理程序写在processdata.py
import os
from imutils import paths
import shutil
classpath=[]
for i in range(1, 18):
i = r'./data/class/'+str(i)
os.makedirs(i)
classpath.append(i)
dataset_dir = os.path.abspath(r".oxford17_all/oxford17_all/jpg")
picture_list = list(paths.list_images(dataset_dir)) # 存放1360张图片的文件夹
pic_num = 0 # 用来计数1360,便于接下来遍历存储图片
for i in range(1, 18):
value = 0
file_path = os.path.abspath(classpath[i-1])
while value < 80:
shutil.copy(picture_list[pic_num], file_path) # 这个是复制语句,将picture_list的图片复制到file文件夹里
pic_num += 1
value += 1
数据预处理(data.py;可反复使用)
from torch.utils.data import DataLoader
from torchvision import utils, datasets, transforms
class ReadData():
def __init__(self,data_path,image_size=64):
self.root=data_path
self.image_size=image_size
self.dataset=self.getdataset()
def getdataset(self):
#3.dataset
dataset = datasets.ImageFolder(root=self.root,
transform=transforms.Compose([
transforms.Resize(self.image_size),
transforms.CenterCrop(self.image_size),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
]))
# Create the dataloader
print(f'Total Size of Dataset: {len(dataset)}')
return dataset
def getdataloader(self,batch_size=128):
dataloader = DataLoader(
self.dataset,
batch_size=batch_size,
shuffle=True,
num_workers=0)
return dataloader
if __name__ =='__main__':
dset=ReadData('./oxford17_all/oxford17_all')
print('数据加载完成')
dloader=dset.getdataloader()
输出:
模型Generator,Discriminator,权重初始化(model.py)
(1)导入包
import torch.nn as nn
(2)Discriminator
class Discriminator(nn.Module):
def __init__(self, ndf,nc):
super(Discriminator, self).__init__()
self.ndf=ndf
self.nc=nc
self.main = nn.Sequential(
# input is (nc) x 64 x 64
nn.Conv2d(self.nc, self.ndf, 4, 2, 1, bias=False),
nn.LeakyReLU(0.2, inplace=True),
# state size. (ndf) x 32 x 32
nn.Conv2d(self.ndf, self.ndf * 2, 4, 2, 1, bias=False),
nn.BatchNorm2d(self.ndf * 2),
nn.LeakyReLU(0.2, inplace=True),
# state size. (ndf*2) x 16 x 16
nn.Conv2d(self.ndf * 2, self.ndf * 4, 4, 2, 1, bias=False),
nn.BatchNorm2d(self.ndf * 4),
nn.LeakyReLU(0.2, inplace=True),
# state size. (ndf*4) x 8 x 8
nn.Conv2d(self.ndf * 4, self.ndf * 8, 4, 2, 1, bias=False),
nn.BatchNorm2d(self.ndf * 8),
nn.LeakyReLU(0.2, inplace=True),
# state size. (ndf*8) x 4 x 4
nn.Conv2d(self.ndf * 8, 1, 4, 1, 0, bias=False),
# state size. (1) x 1 x 1
nn.Sigmoid()
)
def forward(self, input):
return self.main(input)
(3)Generator
class Generator(nn.Module):
def __init__(self, nz,ngf,nc):
super(Generator, self).__init__()
self.nz = nz
self.ngf = ngf
self.nc=nc
self.main = nn.Sequential(
# input is Z, going into a convolution
nn.ConvTranspose2d(self.nz, self.ngf * 8, 4, 1, 0, bias=False),
nn.BatchNorm2d(self.ngf * 8),
nn.ReLU(True),
# state size. (ngf*8) x 4 x 4
nn.ConvTranspose2d(self.ngf * 8, self.ngf * 4, 4, 2, 1, bias=False),
nn.BatchNorm2d(self.ngf * 4),
nn.ReLU(True),
# state size. (ngf*4) x 8 x 8
nn.ConvTranspose2d(self.ngf * 4, self.ngf * 2, 4, 2, 1, bias=False),
nn.BatchNorm2d(self.ngf * 2),
nn.ReLU(True),
# state size. (ngf*2) x 16 x 16
nn.ConvTranspose2d(self.ngf * 2, self.ngf, 4, 2, 1, bias=False),
nn.BatchNorm2d(self.ngf),
nn.ReLU(True),
# state size. (ngf) x 32 x 32
nn.ConvTranspose2d(self.ngf, self.nc, 4, 2, 1, bias=False),
nn.Tanh()
# state size. (nc) x 64 x 64
)
def forward(self, input):
return self.main(input)
(4)权重初始化
def weights_init(m):
classname = m.__class__.__name__
if classname.find('Conv') != -1:
nn.init.normal_(m.weight.data, 0.0, 0.02)
elif classname.find('BatchNorm') != -1:
nn.init.normal_(m.weight.data, 1.0, 0.02)
nn.init.constant_(m.bias.data, 0)
网络训练(net.py)
包括曲线可视化,真假图片对比,生成图片gif,结果保存
import torch
import torch.nn as nn
from torchvision import utils, datasets, transforms
import time
import matplotlib.pyplot as plt
import matplotlib.animation as animation
#from IPython.display import HTML
class DCGAN():
def __init__(self,lr,beta1,nz, batch_size,num_showimage,device, model_save_path,figure_save_path,generator, discriminator, data_loader,):
self.real_label=1
self.fake_label=0
self.nz=nz
self.batch_size=batch_size
self.num_showimage=num_showimage
self.device = device
self.model_save_path=model_save_path
self.figure_save_path=figure_save_path
self.G = generator.to(device)
self.D = discriminator.to(device)
self.opt_G=torch.optim.Adam(self.G.parameters(), lr=lr, betas=(beta1, 0.999))
self.opt_D = torch.optim.Adam(self.D.parameters(), lr=lr, betas=(beta1, 0.999))
self.criterion = nn.BCELoss().to(device)
self.dataloader=data_loader
self.fixed_noise = torch.randn(self.num_showimage, nz, 1, 1, device=device)
self.img_list = []
self.G_loss_list = []
self.D_loss_list = []
self.D_x_list = []
self.D_z_list = []
def train(self,num_epochs):
loss_tep = 10
G_loss=0
D_loss=0
print("Starting Training Loop...")
# For each epoch
for epoch in range(num_epochs):
#**********计时*********************
beg_time = time.time()
# For each batch in the dataloader
for i, data in enumerate(self.dataloader):
############################
# (1) Update D network: maximize log(D(x)) + log(1 - D(G(z)))
###########################
x = data[0].to(self.device)
b_size = x.size(0)
lbx = torch.full((b_size,), self.real_label, dtype=torch.float, device=self.device)
D_x = self.D(x).view(-1)
LossD_x = self.criterion(D_x, lbx)
D_x_item = D_x.mean().item()
# print("log(D(x))")
z = torch.randn(b_size, self.nz, 1, 1, device=self.device)
gz = self.G(z)
lbz1 = torch.full((b_size,), self.fake_label, dtype=torch.float, device=self.device)
D_gz1 = self.D(gz.detach()).view(-1)
LossD_gz1 = self.criterion(D_gz1, lbz1)
D_gz1_item = D_gz1.mean().item()
# print("log(1 - D(G(z)))")
LossD = LossD_x + LossD_gz1
# print("log(D(x)) + log(1 - D(G(z)))")
self.opt_D.zero_grad()
LossD.backward()
self.opt_D.step()
# print("update LossD")
D_loss+=LossD
############################
# (2) Update G network: maximize log(D(G(z)))
###########################
lbz2 = torch.full((b_size,), self.real_label, dtype=torch.float, device=self.device) # fake labels are real for generator cost
D_gz2 = self.D(gz).view(-1)
D_gz2_item = D_gz2.mean().item()
LossG = self.criterion(D_gz2, lbz2)
# print("log(D(G(z)))")
self.opt_G.zero_grad()
LossG.backward()
self.opt_G.step()
# print("update LossG")
G_loss+=LossG
end_time = time.time()
# **********计时*********************
run_time = round(end_time - beg_time)
# print('lalala')
print(
f'Epoch: [{epoch + 1:0>{len(str(num_epochs))}}/{num_epochs}]',
f'Step: [{i + 1:0>{len(str(len(self.dataloader)))}}/{len(self.dataloader)}]',
f'Loss-D: {LossD.item():.4f}',
f'Loss-G: {LossG.item():.4f}',
f'D(x): {D_x_item:.4f}',
f'D(G(z)): [{D_gz1_item:.4f}/{D_gz2_item:.4f}]',
f'Time: {run_time}s',
end='\r\n'
)
# print("lalalal2")
# Save Losses for plotting later
self.G_loss_list.append(LossG.item())
self.D_loss_list.append(LossD.item())
# Save D(X) and D(G(z)) for plotting later
self.D_x_list.append(D_x_item)
self.D_z_list.append(D_gz2_item)
# # Save the Best Model
# if LossG < loss_tep:
# torch.save(self.G.state_dict(), 'model.pt')
# loss_tep = LossG
torch.save(self.D.state_dict(), self.model_save_path + 'disc_{}.pth'.format(epoch))
torch.save(self.G.state_dict(), self.model_save_path + 'gen_{}.pth'.format(epoch))
# Check how the generator is doing by saving G's output on fixed_noise
with torch.no_grad():
fake = self.G(self.fixed_noise).detach().cpu()
self.img_list.append(utils.make_grid(fake * 0.5 + 0.5, nrow=10))
print()
#----可视化loss曲线及保存------
plt.figure(1,figsize=(8, 4))
plt.title("Generator and Discriminator Loss During Training")
plt.plot(self.G_loss_list[::10], label="G")
plt.plot(self.D_loss_list[::10], label="D")
plt.xlabel("iterations")
plt.ylabel("Loss")
plt.axhline(y=0, label="0", c="g") # asymptote
plt.legend()
plt.savefig(self.figure_save_path + str(num_epochs) + 'epochs_' + 'loss.jpg', bbox_inches='tight')
plt.figure(2,figsize=(8, 4))
plt.title("D(x) and D(G(z)) During Training")
plt.plot(self.D_x_list[::10], label="D(x)")
plt.plot(self.D_z_list[::10], label="D(G(z))")
plt.xlabel("iterations")
plt.ylabel("Probability")
plt.axhline(y=0.5, label="0.5", c="g") # asymptote
plt.legend()
plt.savefig(self.figure_save_path + str(num_epochs) + 'epochs_' + 'D(x)D(G(z)).jpg', bbox_inches='tight')
#-----可视化每次生成图片,制作gif,并保存-------
# 制作gif:animation.ArtistAnimation
fig = plt.figure(3,figsize=(5, 5))
plt.axis("off")
ims = [[plt.imshow(item.permute(1, 2, 0), animated=True)] for item in self.img_list]
ani = animation.ArtistAnimation(fig, ims, interval=1000, repeat_delay=1000, blit=True)
#HTML(ani.to_jshtml())
# ani.to_html5_video()
ani.save(self.figure_save_path + str(num_epochs) + 'epochs_' + 'generation.gif')
#--------真实图片与生成图片对比--------
plt.figure(4,figsize=(8, 4))
# Plot the real images
plt.subplot(1, 2, 1)
plt.axis("off")
plt.title("Real Images")
real = next(iter(self.dataloader)) # real[0]image,real[1]label
plt.imshow(utils.make_grid(real[0][:self.num_showimage] * 0.5 + 0.5, nrow=10).permute(1, 2, 0))
# Load the Best Generative Model
# self.G.load_state_dict(
# torch.load(self.model_save_path + 'disc_{}.pth'.format(epoch), map_location=torch.device(self.device)))
self.G.eval()
# Generate the Fake Images
with torch.no_grad():
fake = self.G(self.fixed_noise).cpu()
# Plot the fake images
plt.subplot(1, 2, 2)
plt.axis("off")
plt.title("Fake Images")
fake = utils.make_grid(fake[:self.num_showimage] * 0.5 + 0.5, nrow=10)
plt.imshow(fake.permute(1, 2, 0))
# Save the comparation result
plt.savefig(self.figure_save_path + str(num_epochs) + 'epochs_' + 'result.jpg', bbox_inches='tight')
plt.show()
def test(self,epoch):
# Size of the Figure
plt.figure(figsize=(8, 4))
# Plot the real images
plt.subplot(1, 2, 1)
plt.axis("off")
plt.title("Real Images")
real = next(iter(self.dataloader))#real[0]image,real[1]label
plt.imshow(utils.make_grid(real[0][:self.num_showimage] * 0.5 + 0.5, nrow=10).permute(1, 2, 0))
# Load the Best Generative Model
self.G.load_state_dict(torch.load(self.model_save_path + 'disc_{}.pth'.format(epoch), map_location=torch.device(self.device)))
self.G.eval()
# Generate the Fake Images
with torch.no_grad():
fake = self.G(self.fixed_noise.to(self.device))
# Plot the fake images
plt.subplot(1, 2, 2)
plt.axis("off")
plt.title("Fake Images")
fake = utils.make_grid(fake * 0.5 + 0.5, nrow=10)
plt.imshow(fake.permute(1, 2, 0))
# Save the comparation result
plt.savefig(self.figure_save_path+'result.jpg', bbox_inches='tight')
plt.show()
主函数(main.py)
(1)导入包
from data import ReadData
from model import Discriminator, Generator, weights_init
from net import DCGAN
import torch
(2)定义超参数
ngpu=1
ngf=64
ndf=64
nc=3
nz=100
lr=0.003
beta1=0.5
batch_size=100
num_showimage=100
data_path="./oxford17_class/oxford17_class"
model_save_path="./models/"
figure_save_path="./figures/"
device = torch.device('cuda:0' if (torch.cuda.is_available() and ngpu > 0) else 'cpu')
(3)实例化
dataset=ReadData(data_path)
dataloader=dataset.getdataloader(batch_size=batch_size)
G = Generator(nz,ngf,nc).apply(weights_init)
D = Discriminator(ndf,nc).apply(weights_init)
dcgan=DCGAN( lr,beta1,nz,batch_size,num_showimage,device, model_save_path,figure_save_path,G, D, dataloader)
(4)进行训练
dcgan.train(num_epochs=20)
结果展示
epoch=20
epoch=100
案例2: 动漫人脸生成
参考:dcgan实现动漫人脸生成-详解
代码位置:E:\项目例程\GNN\动漫人脸\1
数据集
数据集约有16000 1600016000张,像素为96X96
定义超参数
# config类中定义超参数,
class Config(object):
"""
定义一个配置类
"""
# 0.参数调整
data_path = './faces/'
virs = "result"
num_workers = 4 # 多线程
img_size = 96 # 剪切图片的像素大小
batch_size = 128 # 批处理数量
max_epoch = 200 # 最大轮次
lr1 = 2e-4 # 生成器学习率
lr2 = 2e-4 # 判别器学习率
beta1 = 0.5 # 正则化系数,Adam优化器参数
gpu = False # 是否使用GPU运算(建议使用)
nz = 100 # 噪声维度
ngf = 64 # 生成器的卷积核个数
ndf = 64 # 判别器的卷积核个数
# 1.模型保存路径
save_path = 'imgs2/' # opt.netg_path生成图片的保存路径
# 判别模型的更新频率要高于生成模型
d_every = 1 # 每一个batch 训练一次判别器
g_every = 1 # 每1个batch训练一次生成模型
save_every = 5 # 每save_every次保存一次模型
netd_path = None
netg_path = None
# 测试数据
gen_img = "result.png"
# 选择保存的照片
# 一次生成保存64张图片
gen_num = 64
gen_search_num = 512
gen_mean = 0 # 生成模型的噪声均值
gen_std = 1 # 噪声方差
# 实例化Config类,设定超参数,并设置为全局参数
opt = Config()
定义生成器
# 定义Generation生成模型,通过输入噪声向量来生成图片
class NetG(nn.Module):
# 构建初始化函数,传入opt类
def __init__(self, opt):
super(NetG, self).__init__()
# self.ngf生成器特征图数目
self.ngf = opt.ngf
self.Gene = nn.Sequential(
# 假定输入为opt.nz*1*1维的数据,opt.nz维的向量
# output = (input - 1)*stride + output_padding - 2*padding + kernel_size
# 把一个像素点扩充卷积,让机器自己学会去理解噪声的每个元素是什么意思。
nn.ConvTranspose2d(in_channels=opt.nz, out_channels=self.ngf * 8, kernel_size=4, stride=1, padding=0, bias =False),
nn.BatchNorm2d(self.ngf * 8),
nn.ReLU(inplace=True),
# 输入一个4*4*ngf*8
nn.ConvTranspose2d(in_channels=self.ngf * 8, out_channels=self.ngf * 4, kernel_size=4, stride=2, padding=1, bias =False),
nn.BatchNorm2d(self.ngf * 4),
nn.ReLU(inplace=True),
# 输入一个8*8*ngf*4
nn.ConvTranspose2d(in_channels=self.ngf * 4, out_channels=self.ngf * 2, kernel_size=4, stride=2, padding=1,bias=False),
nn.BatchNorm2d(self.ngf * 2),
nn.ReLU(inplace=True),
# 输入一个16*16*ngf*2
nn.ConvTranspose2d(in_channels=self.ngf * 2, out_channels=self.ngf, kernel_size=4, stride=2, padding=1, bias =False),
nn.BatchNorm2d(self.ngf),
nn.ReLU(inplace=True),
# 输入一张32*32*ngf
nn.ConvTranspose2d(in_channels=self.ngf, out_channels=3, kernel_size=5, stride=3, padding=1, bias =False),
# Tanh收敛速度快于sigmoid,远慢于relu,输出范围为[-1,1],输出均值为0
nn.Tanh(),
)# 输出一张96*96*3
def forward(self, x):
return self.Gene(x)
定义判别器
class NetD(nn.Module):
def __init__(self, opt):
super(NetD, self).__init__()
self.ndf = opt.ndf
# DCGAN定义的判别器,生成器没有池化层
self.Discrim = nn.Sequential(
# 卷积层
# 输入通道数in_channels,输出通道数(即卷积核的通道数)out_channels,此处设定filer过滤器有64个,输出通道自然也就是64。
# 因为对图片作了灰度处理,此处通道数为1,
# 卷积核大小kernel_size,步长stride,对称填0行列数padding
# input:(bitch_size, 3, 96, 96),bitch_size = 单次训练的样本量
# output:(bitch_size, ndf, 32, 32), (96 - 5 +2 *1)/3 + 1 =32
# LeakyReLu= x if x>0 else nx (n为第一个函数参数),开启inplace(覆盖)可以节省内存,取消反复申请内存的过程
# LeakyReLu取消了Relu的负数硬饱和问题,是否对模型优化有效有待考证
nn.Conv2d(in_channels=3, out_channels= self.ndf, kernel_size= 5, stride= 3, padding= 1, bias=False),
nn.LeakyReLU(negative_slope=0.2, inplace= True),
# input:(ndf, 32, 32)
nn.Conv2d(in_channels= self.ndf, out_channels= self.ndf * 2, kernel_size= 4, stride= 2, padding= 1, bias=False),
nn.BatchNorm2d(self.ndf * 2),
nn.LeakyReLU(0.2, True),
# input:(ndf *2, 16, 16)
nn.Conv2d(in_channels= self.ndf * 2, out_channels= self.ndf *4, kernel_size= 4, stride= 2, padding= 1,bias=False),
nn.BatchNorm2d(self.ndf * 4),
nn.LeakyReLU(0.2, True),
# input:(ndf *4, 8, 8)
nn.Conv2d(in_channels= self.ndf *4, out_channels= self.ndf *8, kernel_size= 4, stride= 2, padding= 1, bias=False),
nn.BatchNorm2d(self.ndf *8),
nn.LeakyReLU(0.2, True),
# input:(ndf *8, 4, 4)
# output:(1, 1, 1)
nn.Conv2d(in_channels= self.ndf *8, out_channels= 1, kernel_size= 4, stride= 1, padding= 0, bias=True),
# 调用sigmoid函数解决分类问题
# 因为判别模型要做的是二分类,故用sigmoid即可,因为sigmoid返回值区间为[0,1],
# 可作判别模型的打分标准
nn.Sigmoid()
)
def forward(self, x):
# 展平后返回
return self.Discrim(x).view(-1)
训练
def train(**kwargs):
# 配置属性
# 如果函数无字典输入则使用opt中设定好的默认超参数
for k_, v_ in kwargs.items():
setattr(opt, k_, v_)
# device(设备),分配设备
if opt.gpu:
device = torch.device("cuda")
else:
device = torch.device('cpu')
# 数据预处理1
# transforms 模块提供一般图像转换操作类的功能,最后转成floatTensor
# tv.transforms.Compose用于组合多个tv.transforms操作,定义好transforms组合操作后,直接传入图片即可进行处理
# tv.transforms.Resize,对PIL Image对象作resize运算, 数值保存类型为float64
# tv.transforms.CenterCrop, 中心裁剪
# tv.transforms.ToTensor,将opencv读到的图片转为torch image类型(通道,像素,像素),且把像素范围转为[0,1]
# tv.transforms.Normalize,执行image = (image - mean)/std 数据归一化操作,一参数是mean,二参数std
# 因为是三通道,所以mean = (0.5, 0.5, 0.5),从而转成[-1, 1]范围
transforms = tv.transforms.Compose([
# 3*96*96
tv.transforms.Resize(opt.img_size), # 缩放到 img_size* img_size
# 中心裁剪成96*96的图片。因为本实验数据已满足96*96尺寸,可省略
tv.transforms.CenterCrop(opt.img_size),
# ToTensor 和 Normalize 搭配使用
tv.transforms.ToTensor(),
tv.transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])
# 加载数据并使用定义好的transforms对图片进行预处理,这里用的是直接定义法
# dataset是一个包装类,将数据包装成Dataset类,方便之后传入DataLoader中
# 写法2:
# 定义类Dataset(Datasets)包装类,重写__getitem__(进行transforms系列操作)、__len__方法(获取样本个数)
# ### 两种写法有什么区别
dataset = tv.datasets.ImageFolder(root=opt.data_path, transform=transforms)
# 数据预处理2
# 查看drop_last操作,
dataloader = DataLoader(
dataset, # 数据加载
batch_size=opt.batch_size, # 批处理大小设置
shuffle=True, # 是否进行洗牌操作
# num_workers=opt.num_workers, # 是否进行多线程加载数据设置
drop_last=True # 为True时,如果数据集大小不能被批处理大小整除,则设置为删除最后一个不完整的批处理。
)
# 初始化网络
netg, netd = NetG(opt), NetD(opt)
# 判断网络是否有权重数值
# ### storage存储
map_location = lambda storage, loc: storage
# torch.load模型加载,即有模型加载模型在该模型基础上进行训练,没有模型则从头开始
# f:类文件对象,如果有模型对象路径,则加载返回
# map_location:一个函数或字典规定如何remap存储位置
# net.load_state_dict将加载出来的模型数据加载到构建好的net网络中去
if opt.netg_path:
netg.load_state_dict(torch.load(f=opt.netg_path, map_location=map_location))
if opt.netd_path:
netd.load_state_dict(torch.load(f=opt.netd_path, map_location=map_location))
# 搬移模型到之前指定设备,本文采用的是cpu,分配设备
netd.to(device)
netg.to(device)
# 定义优化策略
# torch.optim包内有多种优化算法,
# Adam优化算法,是带动量的惯性梯度下降算法
optimize_g = torch.optim.Adam(netg.parameters(), lr=opt.lr1, betas=(opt.beta1, 0.999))
optimize_d = torch.optim.Adam(netd.parameters(), lr=opt.lr2, betas=(opt.beta1, 0.999))
# 计算目标值和预测值之间的交叉熵损失函数
# BCEloss:-w(ylog x +(1 - y)log(1 - x))
# y为真实标签,x为判别器打分(sigmiod,1为真0为假),加上负号,等效于求对应标签下的最大得分
# to(device),用于指定CPU/GPU
criterions = nn.BCELoss().to(device)
# 定义标签,并且开始注入生成器的输入noise
true_labels = torch.ones(opt.batch_size).to(device)
fake_labels = torch.zeros(opt.batch_size).to(device)
# 生成满足N(1,1)标准正态分布,opt.nz维(100维),opt.batch_size个数的随机噪声
noises = torch.randn(opt.batch_size, opt.nz, 1, 1).to(device)
# 用于保存模型时作生成图像示例
fix_noises = torch.randn(opt.batch_size, opt.nz, 1, 1).to(device)
# 训练网络
# 设置迭代
G_loss = 0
D_loss = 0
G_loss_list = []
D_loss_list = []
figure_save_path = "./figures/"
print("Starting Training Loop...")
for epoch in range(opt.max_epoch):
# tqdm(iterator()),函数内嵌迭代器,用作循环的进度条显示
for ii_, (img, _) in tqdm((enumerate(dataloader))):
# 将处理好的图片赋值
real_img = img.to(device)
# 开始训练生成器和判别器
# 注意要使得生成的训练次数小于一些
# 每一轮更新一次判别器
if ii_ % opt.d_every == 0:
# 优化器梯度清零
optimize_d.zero_grad()
# 训练判别器
# 把判别器的目标函数分成两段分别进行反向求导,再统一优化
# 真图
# 把所有的真样本传进netd进行训练,
output = netd(real_img)
# 用之前定义好的交叉熵损失函数计算损失
error_d_real = criterions(output, true_labels)
# 误差反向计算
error_d_real.backward()
# 随机生成的假图
# .detach() 返回相同数据的 tensor ,且 requires_grad=False
# .detach()做截断操作,生成器不记录判别器采用噪声的梯度
noises = noises.detach()
# 通过生成模型将随机噪声生成为图片矩阵数据
fake_image = netg(noises).detach()
# 将生成的图片交给判别模型进行判别
output = netd(fake_image)
# 再次计算损失函数的计算损失
error_d_fake = criterions(output, fake_labels)
# 误差反向计算
# 求导和优化(权重更新)是两个独立的过程,只不过优化时一定需要对应的已求取的梯度值。
# 所以求得梯度值很关键,而且,经常会累积多种loss对某网络参数造成的梯度,一并更新网络。
error_d_fake.backward()
'''
关于为什么要分两步计算loss:
我们已经知道,BCEloss相当于计算对应标签下的得分,那么我们
把真样本传入时,因为标签恒为1,BCE此时只有第一项,即真样本得分项
要补齐成前文提到的判别器目标函数,需要再添置假样本得分项,故两次分开计算梯度,各自最大化各自的得分(假样本得分是log(1 - D(x)))
再统一进行梯度下降即可
'''
# 计算一次Adam算法,完成判别模型的参数迭代
# 多个不同loss的backward()来累积同一个网络的grad,计算一次Adam即可
optimize_d.step()
D_loss += error_d_fake
# 训练判别器
if ii_ % opt.g_every == 0:
optimize_g.zero_grad()
# 用于netd作判别训练和用于netg作生成训练两组噪声需不同
noises.data.copy_(torch.randn(opt.batch_size, opt.nz, 1, 1))
fake_image = netg(noises)
output = netd(fake_image)
# 此时判别器已经固定住了,BCE的一项为定值,再求最小化相当于求二项即G得分的最大化
error_g = criterions(output, true_labels)
error_g.backward()
# 计算一次Adam算法,完成判别模型的参数迭代
optimize_g.step()
G_loss += error_g
# Save Losses for plotting later
G_loss_list.append(error_g.item())
D_loss_list.append(error_d_fake.item())
print(
f'Epoch: [{epoch + 1:0>{len(str(opt.max_epoch))}}/{opt.max_epoch}]',
f'Loss-D: {error_d_fake.item():.4f}',
f'Loss-G: {error_g.item():.4f}',
end='\r\n'
)
# 保存模型
if (epoch + 1) % opt.save_every == 0:
fix_fake_image = netg(fix_noises)
tv.utils.save_image(fix_fake_image.data[:64], "%s/%s.png" % (opt.save_path, epoch), normalize=True)
torch.save(netd.state_dict(), 'imgs3/' + 'netd_{0}.pth'.format(epoch))
torch.save(netg.state_dict(), 'imgs3/' + 'netg_{0}.pth'.format(epoch))
plt.figure(1, figsize=(8, 4))
plt.title("Generator and Discriminator Loss During Training")
plt.plot(G_loss_list[::10], label="G")
plt.plot(D_loss_list[::10], label="D")
plt.xlabel("iterations")
plt.ylabel("Loss")
plt.axhline(y=0, label="0", c="g") # asymptote
plt.legend()
plt.savefig(figure_save_path + str(opt.max_epoch) + 'epochs_' + 'loss.jpg', bbox_inches='tight')
生成器生成图片
# @torch.no_grad():数据不需要计算梯度,也不会进行反向传播
@torch.no_grad()
def generate(**kwargs):
# 用训练好的模型来生成图片
for k_, v_ in kwargs.items():
setattr(opt, k_, v_)
device = torch.device("cuda") if opt.gpu else torch.device("cpu")
# 加载训练好的权重数据
netg, netd = NetG(opt).eval(), NetD(opt).eval()
# 两个参数返回第一个
map_location = lambda storage, loc: storage
# opt.netd_path等参数有待修改
netd.load_state_dict(torch.load('imgs2/netd_399.pth', map_location=map_location), False)
netg.load_state_dict(torch.load('imgs2/netg_399.pth', map_location=map_location), False)
netd.to(device)
netg.to(device)
# 生成训练好的图片
# 初始化512组噪声,选其中好的拿来保存输出。
noise = torch.randn(opt.gen_search_num, opt.nz, 1, 1).normal_(opt.gen_mean, opt.gen_std).to(device)
fake_image = netg(noise)
score = netd(fake_image).detach()
# 挑选出合适的图片
# 取出得分最高的图片
indexs = score.topk(opt.gen_num)[1]
result = []
for ii in indexs:
result.append(fake_image.data[ii])
# 以opt.gen_img为文件名保存生成图片
tv.utils.save_image(torch.stack(result), opt.gen_img, normalize=True, range=(-1, 1))
主函数
def main():
# 训练模型
train()
# 生成图片
generate()
if __name__ == '__main__':
main()
结果
第五次
第50次
第145次
第175次