0
点赞
收藏
分享

微信扫一扫

极智AI | GAN 应用于玻璃表面水珠样本生成


  ​​欢迎关注我的公众号 [极智视界],获取我的更多笔记分享​

  大家好,我是极智视界,本文介绍一下 GAN 应用于玻璃表面水珠样本生成的方法。

  在分类任务中,若类别数量不平衡,则分类结果会向样本量明显多的类别倾斜,造成分类结果准确率不高。在实际项目中,往往某一类特殊的样本复现程度低,比较难收集,在做分类任务的时候就容易造成样本不均衡的问题。下面使用 pytorch 搭建 GAN 网络进行玻璃表面水珠样本生成。GAN有生成器和判别器两个模型,生成器不断生成样本,判别器来判断生成样本的好坏,并反馈给生成器以调整参数生成更好的样本,目的是迷惑判别器的判断,两者互相博弈,循序渐进,直至平衡。

文章目录

  • ​​1 生成器结构​​
  • ​​2 判别器结构​​
  • ​​3 生成效果展示​​

1 生成器结构

class NetG(nn.Module):
def __init__(self, ngf, nz):
super(NetG, self).__init__()
# layer1 输入的是一个 100x1x1 的随机噪声, 输出尺寸 (ngf*8)x4x4
self.layer1 = nn.Sequential(
nn.ConvTranspose2d(nz, ngf * 8, kernel_size=4, stride=1, padding=0, bias=False),
nn.BatchNorm2d(ngf * 8),
nn.ReLU(inplace=True)
)
# layer2 输出尺寸 (ngf*4)x8x8
self.layer2 = nn.Sequential(
nn.ConvTranspose2d(ngf * 8, ngf * 4, 4, 2, 1, bias=False),
nn.BatchNorm2d(ngf * 4),
nn.ReLU(inplace=True)
) # layer3 输出尺寸 (ngf*2)x16x16
self.layer3 = nn.Sequential(
nn.ConvTranspose2d(ngf * 4, ngf * 2, 4, 2, 1, bias=False),
nn.BatchNorm2d(ngf * 2),
nn.ReLU(inplace=True)
)
# layer4 输出尺寸 (ngf)x32x32
self.layer4 = nn.Sequential(
nn.ConvTranspose2d(ngf * 2, ngf, 4, 2, 1, bias=False),
nn.BatchNorm2d(ngf),
nn.ReLU(inplace=True)
)
# layer5 输出尺寸 3x96x96
self.layer5 = nn.Sequential(
nn.ConvTranspose2d(ngf, 3, 5, 3, 1, bias=False),
nn.Tanh()
)

# 定义 NetG 的前向传播
def forward(self, x):
out = self.layer1(x)
out = self.layer2(out)
out = self.layer3(out)
out = self.layer4(out)
out = self.layer5(out)
return



2 判别器结构

# 定义鉴别器网络 D
class NetD(nn.Module):
def __init__(self, ndf):
super(NetD, self).__init__() # 对继承自父类的属性进行初始化
# layer1 输入 3 x 96 x 96, 输出 (ndf) x 32 x 32
self.layer1 = nn.Sequential(
nn.Conv2d(3, ndf, kernel_size=5, stride=3, padding=1, bias=False),
nn.BatchNorm2d(ndf),
nn.LeakyReLU(0.2, inplace=True)
)
# layer2 输出 (ndf*2) x 16 x 16
self.layer2 = nn.Sequential(
nn.Conv2d(ndf, ndf * 2, 4, 2, 1, bias=False),
nn.BatchNorm2d(ndf * 2),
nn.LeakyReLU(0.2, inplace=True)
)
# layer3 输出 (ndf*4) x 8 x 8
self.layer3 = nn.Sequential(
nn.Conv2d(ndf * 2, ndf * 4, 4, 2, 1, bias=False),
nn.BatchNorm2d(ndf * 4),
nn.LeakyReLU(0.2, inplace=True)
)
# layer4 输出 (ndf*8) x 4 x 4
self.layer4 = nn.Sequential(
nn.Conv2d(ndf * 4, ndf * 8, 4, 2, 1, bias=False),
nn.BatchNorm2d(ndf * 8),
nn.LeakyReLU(0.2, inplace=True)
)
# layer5 输出一个数(概率)
self.layer5 = nn.Sequential(
nn.Conv2d(ndf * 8, 1, 4, 1, 0, bias=False),
nn.Sigmoid() # 也是一个激活函数,二分类问题中,是真是假
# sigmoid 可以将实数映射到【0,1】,作为概率值,
# 多分类用 softmax 函数
)

# 定义 NetD 的前向传播
def forward(self,x):
out = self.layer1(x)
out = self.layer2(out)
out = self.layer3(out)
out = self.layer4(out)
out = self.layer5(out)
return



3 生成效果展示

  利用 1000 张左右原始玻璃表面水珠样本,使用GAN迭代 600000 次后生成的假样本示意,可以看出十分逼真,可用于分类样本扩充。

极智AI | GAN 应用于玻璃表面水珠样本生成_GAN



  好了,以上分享了 GAN 应用于玻璃表面水珠样本生成的方法,希望我的分享能对你的学习有一点帮助。






极智AI | GAN 应用于玻璃表面水珠样本生成_深度学习_02

微信公众号【极智视界】,获取我的更多经验分享,让我们用极致+极客的心态来迎接AI !

极智AI | GAN 应用于玻璃表面水珠样本生成_计算机视觉_03


举报

相关推荐

0 条评论