一、原始GAN网络存在问题
原始GAN网络判别器(D)损失函数为: − E x − P r [ l o g D ( x ) ] − E x − P g [ l o g ( 1 − D ( x ) ) ] -E_{x-P_r}[logD(x)]-E_{x-P_g}[log(1-D(x))] −Ex−Pr[logD(x)]−Ex−Pg[log(1−D(x))]
原始GAN网络生成器(G)损失函数为: E x − P g [ l o g ( 1 − D ( x ) ) ] E_{x-P_g}[log(1-D(x))] Ex−Pg[log(1−D(x))]
此处 P r P_r Pr为真实样本分布, P g P_g Pg为生成样本分布。对于一个具体的样本x,其可能来自真实数据分布,也有可能来自生成数据分布,将样本x带入至判别器损失函数中,然后对 D ( x ) D(x) D(x)求导,可以解出最优判别器的形式: D ∗ = P r ( x ) P g ( x ) + P r ( x ) D^*=\frac{P_r(x)}{P_g(x)+P_r(x)} D∗=Pg(x)+Pr(x)Pr(x)。此时,判别器作为最优判别器,可以给出样本来自真实数据和生成数据的概率。当 P r ( x ) = P g ( x ) P_r(x)=P_g(x) Pr(x)=Pg(x)时,最优判别器给出样本概率为0.5。
在生成器的损失函数中,加入一项不依赖于生成器的项 E x − P r [ l o g D ( x ) ] E_{x-P_r}[logD(x)] Ex−Pr[logD(x)],此时最小化生成器的损失函数相当于优化此式 E x − P g [ l o g ( 1 − D ( x ) ) ] + E x − P r [ l o g D ( x ) ] E_{x-P_g}[log(1-D(x))]+E_{x-P_r}[logD(x)] Ex−Pg[log(1−D(x))]+Ex−Pr[logD(x)]。而将D(x)替换为最优判别器形式可以化简得到最终结果为 2 J S ( P r ( x ) ∣ ∣ P g ( x ) ) − 2 l o g 2 2JS(P_r(x)||P_g(x))-2log2 2JS(Pr(x)∣∣Pg(x))−2log2。而当两组数据不重合时,JS散度值恒为log2。如下图所示,下图为两个几乎不重合的正态数据分布。
可以看出,当x≥3时, 2 J S ( P r ( x ) ∣ ∣ P g ( x ) ) − 2 l o g 2 = P g ( x ) l o g P r ( x ) P g ( x ) + P r ( x ) + P r ( x ) l o g P r ( x ) P g ( x ) + P r ( x ) 2JS(P_r(x)||P_g(x))-2log2=P_g(x)log\frac{P_r(x)}{P_g(x)+P_r(x)}+P_r(x)log\frac{P_r(x)}{P_g(x)+P_r(x)} 2JS(Pr(x)∣∣Pg(x))−2log2=Pg(x)logPg(x)+Pr(x)Pr(x)+Pr(x)logPg(x)+Pr(x)Pr(x),可以很容易的得出此式为0。同理,当x<3时,生成器损失函数值为0。所以,我们可以得到一个结论:判别器达到最优判别器状态时,生成器没有任何的梯度信息,无法完成学习任务。
二、Wasserstain距离
在WGAN一文中引入Wasserstain距离来代替JS散度来表示两组数据之间的距离。 W ( P r ( x ) , P g ( x ) ) = inf r ∈ ∏ ( P r ( x ) , P g ( x ) ) E ( x , y ) − r [ ∣ ∣ x − y ∣ ∣ ] W(P_r(x),P_g(x))=\displaystyle \inf_{r\in\prod(P_r(x),P_g(x))}E_{(x,y)-r}[||x-y||] W(Pr(x),Pg(x))=r∈∏(Pr(x),Pg(x))infE(x,y)−r[∣∣x−y∣∣]。其中, ∏ ( P r ( x ) , P g ( x ) ) \prod(P_r(x),P_g(x)) ∏(Pr(x),Pg(x))代表 P r ( x ) P_r(x) Pr(x)和 P g ( x ) P_g(x) Pg(x)组合起来的所有可能的联合分布的集合; ∣ ∣ x − y ∣ ∣ ||x-y|| ∣∣x−y∣∣代表生成样本和真实样本之间的距离; i n f inf inf为这对样本的距离期望取下界。Wasserstain距离直观上理解为将生成数据分布移动至真实数据分布的最短平均距离。假设两组数据没有重叠,比如两组数据分布为均匀分布,如下图所示。
其中 θ \theta θ为两组数据之间的距离,即Wasserstain距离,此时,两者之间的Wasserstain随着生成器的学习而变化,所以不会出现梯度为零的情况,反观JS散度则在 θ = 0 \theta=0 θ=0时为0,在 θ \theta θ ≠ a \not=a =a时为log2。
三、WGAN
因为 W ( P r ( x ) , P g ( x ) ) = inf r ∈ ∏ ( P r ( x ) , P g ( x ) ) E ( x , y ) − r [ ∣ ∣ x − y ∣ ∣ ] W(P_r(x),P_g(x))=\displaystyle \inf_{r\in\prod(P_r(x),P_g(x))}E_{(x,y)-r}[||x-y||] W(Pr(x),Pg(x))=r∈∏(Pr(x),Pg(x))infE(x,y)−r[∣∣x−y∣∣]在实际情况下无法直接对下界进行求取,所以将上式等价为 W ( P r ( x ) , P g ( x ) ) = sup ∣ ∣ f ∣ ∣ l ≤ 1 E x − P r [ f ( x ) ] − E x − P θ [ f ( x ) ] W(P_r(x),P_g(x))=\displaystyle \sup_{||f||_l≤1}E_{x-P_r}[f(x)]-E_{x-P_θ}[f(x)] W(Pr(x),Pg(x))=∣∣f∣∣l≤1supEx−Pr[f(x)]−Ex−Pθ[f(x)]。此处需要对x加以限制,要存在K≥0使得定义域中的任何两元素 X 1 X_1 X1、 X 2 X_2 X2满足 ∣ f ( x 1 ) − f ( x 2 ) ∣ ≤ K ∣ x 1 − x 2 ∣ |f(x_1)-f(x_2)|≤K|x_1-x_2| ∣f(x1)−f(x2)∣≤K∣x1−x2∣,即函数f(x)的导数值不超过K,然后对满足此条件下的 E x − P r [ f ( x ) ] − E x − P θ [ f ( x ) ] E_{x-P_r}[f(x)]-E_{x-P_θ}[f(x)] Ex−Pr[f(x)]−Ex−Pθ[f(x)]取上界。我们限制判别器神经网络 f w f_w fw中的参数不超过某个范围[-c,c],即使得关于输入样本x的导数也不会超过某个范围,所以存在常数K使得上式成立。
在满足上述条件的情况下,损失函数可以写为 L = E x − P r [ f ( x ) ] − E x − P θ [ f ( x ) ] L=E_{x-P_r}[f(x)]-E_{x-P_θ}[f(x)] L=Ex−Pr[f(x)]−Ex−Pθ[f(x)],因为损失函数中第一项与生成器无关,所以生成器的损失函数去掉第一项。
判别器损失函数为: − E x − P g [ f w ( x ) ] -E_{x-P_g}[f_w(x)] −Ex−Pg[fw(x)]
生成器损失函数为: E x − P θ [ f ( x ) ] − E x − P r [ f ( x ) ] E_{x-P_θ}[f(x)]-E_{x-P_r}[f(x)] Ex−Pθ[f(x)]−Ex−Pr[f(x)]
WGAN的伪代码如下图所示
四、WGAN代码
原始代码(原始代码中所使用的数据集为MNIST手写数字数据集)
import argparse
import os
import numpy as np
import math
import sys
import torchvision.transforms as transforms
from torchvision.utils import save_image
from torch.utils.data import DataLoader
from torchvision import datasets
from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F
import torch
os.makedirs("images", exist_ok=True)
parser = argparse.ArgumentParser()
parser.add_argument("--n_epochs", type=int, default=200, help="number of epochs of training")
parser.add_argument("--batch_size", type=int, default=64, help="size of the batches")
parser.add_argument("--lr", type=float, default=0.00005, help="learning rate")
parser.add_argument("--n_cpu", type=int, default=8, help="number of cpu threads to use during batch generation")
parser.add_argument("--latent_dim", type=int, default=100, help="dimensionality of the latent space")
parser.add_argument("--img_size", type=int, default=28, help="size of each image dimension")
parser.add_argument("--channels", type=int, default=1, help="number of image channels")
parser.add_argument("--n_critic", type=int, default=5, help="number of training steps for discriminator per iter")
parser.add_argument("--clip_value", type=float, default=0.01, help="lower and upper clip value for disc. weights")
parser.add_argument("--sample_interval", type=int, default=400, help="interval betwen image samples")
opt = parser.parse_args()
print(opt)
img_shape = (opt.channels, opt.img_size, opt.img_size)
cuda = True if torch.cuda.is_available() else False
class Generator(nn.Module):
def __init__(self):
super(Generator, self).__init__()
def block(in_feat, out_feat, normalize=True):
layers = [nn.Linear(in_feat, out_feat)]
if normalize:
layers.append(nn.BatchNorm1d(out_feat, 0.8))
layers.append(nn.LeakyReLU(0.2, inplace=True))
return layers
self.model = nn.Sequential(
*block(opt.latent_dim, 128, normalize=False),
*block(128, 256),
*block(256, 512),
*block(512, 1024),
nn.Linear(1024, int(np.prod(img_shape))),
nn.Tanh()
)
def forward(self, z):
img = self.model(z)
img = img.view(img.shape[0], *img_shape)
return img
class Discriminator(nn.Module):
def __init__(self):
super(Discriminator, self).__init__()
self.model = nn.Sequential(
nn.Linear(int(np.prod(img_shape)), 512),
nn.LeakyReLU(0.2, inplace=True),
nn.Linear(512, 256),
nn.LeakyReLU(0.2, inplace=True),
nn.Linear(256, 1),
)
def forward(self, img):
img_flat = img.view(img.shape[0], -1)
validity = self.model(img_flat)
return validity
# Initialize generator and discriminator
generator = Generator()
discriminator = Discriminator()
if cuda:
generator.cuda()
discriminator.cuda()
# Configure data loader
os.makedirs("../../data/mnist", exist_ok=True)
dataloader = torch.utils.data.DataLoader(
datasets.MNIST(
"../../data/mnist",
train=True,
download=True,
transform=transforms.Compose([transforms.ToTensor(), transforms.Normalize([0.5], [0.5])]),
),
batch_size=opt.batch_size,
shuffle=True,
)
# Optimizers
optimizer_G = torch.optim.RMSprop(generator.parameters(), lr=opt.lr)
optimizer_D = torch.optim.RMSprop(discriminator.parameters(), lr=opt.lr)
Tensor = torch.cuda.FloatTensor if cuda else torch.FloatTensor
# ----------
# Training
# ----------
batches_done = 0
for epoch in range(opt.n_epochs):
for i, (imgs, _) in enumerate(dataloader):
# Configure input
real_imgs = Variable(imgs.type(Tensor))
# ---------------------
# Train Discriminator
# ---------------------
optimizer_D.zero_grad()
# Sample noise as generator input
z = Variable(Tensor(np.random.normal(0, 1, (imgs.shape[0], opt.latent_dim))))
# Generate a batch of images
fake_imgs = generator(z).detach()
# Adversarial loss
loss_D = -torch.mean(discriminator(real_imgs)) + torch.mean(discriminator(fake_imgs))
loss_D.backward()
optimizer_D.step()
# Clip weights of discriminator
for p in discriminator.parameters():
p.data.clamp_(-opt.clip_value, opt.clip_value)
# Train the generator every n_critic iterations
if i % opt.n_critic == 0:
# -----------------
# Train Generator
# -----------------
optimizer_G.zero_grad()
# Generate a batch of images
gen_imgs = generator(z)
# Adversarial loss
loss_G = -torch.mean(discriminator(gen_imgs))
loss_G.backward()
optimizer_G.step()
print(
"[Epoch %d/%d] [Batch %d/%d] [D loss: %f] [G loss: %f]"
% (epoch, opt.n_epochs, batches_done % len(dataloader), len(dataloader), loss_D.item(), loss_G.item())
)
if batches_done % opt.sample_interval == 0:
save_image(gen_imgs.data[:25], "images/%d.png" % batches_done, nrow=5, normalize=True)
batches_done += 1
自定义数据集代码(自定义数据集代码中使用的本地自己做的数据集,在路径中进行改动即可运行)
import argparse
import os
import numpy as np
import math
import sys
import torchvision.transforms as transforms
from torchvision.utils import save_image
from torch.utils.data import DataLoader,Dataset#此处加入dataset
from torchvision import datasets
from torch.autograd import Variable
from PIL import Image
import torch.nn as nn
import torch.nn.functional as F
import torch
os.makedirs("images", exist_ok=True)#输出图片保存路径
parser = argparse.ArgumentParser()#设置超参数
parser.add_argument("--n_epochs", type=int, default=10000, help="number of epochs of training")
parser.add_argument("--batch_size", type=int, default=64, help="size of the batches")
parser.add_argument("--lr", type=float, default=0.00005, help="learning rate")
parser.add_argument("--n_cpu", type=int, default=8, help="number of cpu threads to use during batch generation")
parser.add_argument("--latent_dim", type=int, default=100, help="dimensionality of the latent space")#噪声向量的维度
parser.add_argument("--img_size", type=int, default=64, help="size of each image dimension")#输入图片的尺寸
parser.add_argument("--channels", type=int, default=3, help="number of image channels")#输入图片的通道数
parser.add_argument("--n_critic", type=int, default=5, help="number of training steps for discriminator per iter")
parser.add_argument("--clip_value", type=float, default=0.01, help="lower and upper clip value for disc. weights")
parser.add_argument("--sample_interval", type=int, default=400, help="interval betwen image samples")
opt = parser.parse_args()
print(opt)
img_shape = (opt.channels, opt.img_size, opt.img_size)#图片格式
cuda = True if torch.cuda.is_available() else False
class Generator(nn.Module):#生成器网络模型
def __init__(self):
super(Generator, self).__init__()
def block(in_feat, out_feat, normalize=True):
layers = [nn.Linear(in_feat, out_feat)]#设置全连接层
if normalize:
layers.append(nn.BatchNorm1d(out_feat, 0.8))#归一化处理,加快训练速度
layers.append(nn.LeakyReLU(0.2, inplace=True))#加入LeakyReLU激活函数
return layers
self.model = nn.Sequential(
*block(opt.latent_dim, 128, normalize=False),
*block(128, 256),
*block(256, 512),
*block(512, 1024),
nn.Linear(1024, int(np.prod(img_shape))),
nn.Tanh()
)
def forward(self, z):
img = self.model(z)
img = img.view(img.shape[0], *img_shape)
return img
class Discriminator(nn.Module):#判别器网络模型
def __init__(self):
super(Discriminator, self).__init__()
self.model = nn.Sequential(
nn.Linear(int(np.prod(img_shape)), 512),
nn.LeakyReLU(0.2, inplace=True),
nn.Linear(512, 256),
nn.LeakyReLU(0.2, inplace=True),
nn.Linear(256, 1),
)
def forward(self, img):
img_flat = img.view(img.shape[0], -1)
validity = self.model(img_flat)
return validity
# Initialize generator and discriminator
generator = Generator()
discriminator = Discriminator()
if cuda:
generator.cuda()
discriminator.cuda()
img_transform=transforms.Compose([
transforms.Resize((64, 64)),#图片进行裁剪
transforms.ToTensor(),#图片转换为tensor格式
transforms.Normalize((0.5,),(0.5,))#图片进行标准化处理
])
class MyData(Dataset):#设置数据集
def __init__(self,root_dir,transform=None):
self.root_dir=root_dir#文件目录
self.transform=transform#图像变换
self.images=os.listdir(self.root_dir)#遍历目录下的图片文件
def __len__(self):
return len(self.images)#返回数据集中所有图片的个数
def __getitem__(self, index):
image_index=self.images[index]
image_path=os.path.join(self.root_dir,image_index)
image=Image.open(image_path).convert('RGB')
if self.transform:
image=self.transform(image)
return image
mydataset=MyData(
root_dir='D:\Sonar Dataset',transform=img_transform
)
dataloader=DataLoader(
dataset=mydataset,batch_size=opt.batch_size,shuffle=True
)
# Configure data loader
#os.makedirs("D:\Sonar Dataset", exist_ok=True)#此处进行改动,改变文件夹位置
#dataloader = torch.utils.data.DataLoader(
# datasets.SONAR(#需要改动,改动为自定义的数据集
# "D:\Sonar Dataset",#数据集位置
# train=True,
# download=True,
# transform=transforms.Compose([transforms.ToTensor(), transforms.Normalize([0.5], [0.5])]),
# ),
# batch_size=opt.batch_size,
# shuffle=True,
#)
# Optimizers
optimizer_G = torch.optim.RMSprop(generator.parameters(), lr=opt.lr)
optimizer_D = torch.optim.RMSprop(discriminator.parameters(), lr=opt.lr)
Tensor = torch.cuda.FloatTensor if cuda else torch.FloatTensor
# ----------
# Training
# ----------
batches_done = 0
for epoch in range(opt.n_epochs):
for i,imgs in enumerate(dataloader):
#for i, (imgs, _) in enumerate(dataloader):
# Configure input
real_imgs = Variable(imgs.type(Tensor))
# ---------------------
# Train Discriminator
# ---------------------
optimizer_D.zero_grad()
# Sample noise as generator input
z = Variable(Tensor(np.random.normal(0, 1, (imgs.shape[0], opt.latent_dim))))
# Generate a batch of images
fake_imgs = generator(z).detach()
# Adversarial loss
loss_D = -torch.mean(discriminator(real_imgs)) + torch.mean(discriminator(fake_imgs))#判别器损失函数
loss_D.backward()
optimizer_D.step()
# Clip weights of discriminator
for p in discriminator.parameters():
p.data.clamp_(-opt.clip_value, opt.clip_value)
# Train the generator every n_critic iterations
if i % opt.n_critic == 0:
# -----------------
# Train Generator
# -----------------
optimizer_G.zero_grad()
# Generate a batch of images
gen_imgs = generator(z)
# Adversarial loss
loss_G = -torch.mean(discriminator(gen_imgs))#生成器损失函数
loss_G.backward()
optimizer_G.step()
print(
"[Epoch %d/%d] [Batch %d/%d] [D loss: %f] [G loss: %f]"
% (epoch, opt.n_epochs, batches_done % len(dataloader), len(dataloader), loss_D.item(), loss_G.item())
)
if batches_done % opt.sample_interval == 0:
save_image(gen_imgs.data[:25], "images/%d.png" % batches_done, nrow=5, normalize=True)
batches_done += 1
五、实验结果
MNIST数据集运行结果
自定义侧扫声呐数据集运行结果
模模糊糊可以看出是个声呐图像中的飞机形状(捂脸.jpg)
令人拍案叫绝的Wasserstein GAN;https://zhuanlan.zhihu.com/p/25071913
pytorch实现GAN网络及训练自己的数据集;https://blog.csdn.net/weixin_50113231/article/details/122959899?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522166202179316782390594954%2522%252C%2522scm%2522%253A%252220140713.130102334…%2522%257D&request_id=166202179316782390594954&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2alltop_positive~default-1-122959899-null-null.142v44pc_ran_alice&utm_term=GAN%E8%AE%AD%E7%BB%83%E8%87%AA%E5%B7%B1%E7%9A%84%E6%95%B0%E6%8D%AE%E9%9B%86&spm=1018.2226.3001.4187
WGAN论文;https://arxiv.org/abs/1701.07875
WGAN的GitHub源代码;https://github.com/eriklindernoren/PyTorch-GAN/blob/master/implementations/wgan/wgan.py
如果对你有帮助的话,麻烦点赞,收藏,敲公式实在太麻烦啦!!!