• 昇思25天学习打卡营第23天|DCGAN生成漫画头像


    今天是参加昇思25天学习打卡营的第23天,今天打卡的课程是“DCGAN生成漫画头像”,这里做一个简单的分享。

    1.简介

    DCGAN(深度卷积对抗生成网络,Deep Convolutional Generative Adversarial Networks)是GAN的直接扩展。不同之处在于,DCGAN会分别在判别器和生成器中使用卷积和转置卷积层。

    它最早由Radford等人在论文Unsupervised Representation Learning With Deep Convolutional Generative Adversarial Networks中进行描述。判别器由分层的卷积层、BatchNorm层和LeakyReLU激活层组成。输入是3x64x64的图像,输出是该图像为真图像的概率。生成器则是由转置卷积层、BatchNorm层和ReLU激活层组成。输入是标准正态分布中提取出的隐向量𝑧𝑧,输出是3x64x64的RGB图像。

    本次学习的内容是使用动漫头像数据集来训练一个生成式对抗网络,接着使用该网络生成动漫头像图片。

    2.模型架构

    • 生成器部分

    生成器G的功能是将隐向量z映射到数据空间。由于数据是图像,这一过程也会创建与真实图像大小相同的 RGB 图像。在实践场景中,该功能是通过一系列Conv2dTranspose转置卷积层来完成的,每个层都与BatchNorm2d层和ReLu激活层配对,输出数据会经过tanh函数,使其返回[-1,1]的数据范围内。

    DCGAN论文生成图像如下所示:

    dcgangenerator

    图片来源:Unsupervised Representation Learning With Deep Convolutional Generative Adversarial Networks.

    生成器代码:

    import mindspore as ms
    from mindspore import nn, ops
    from mindspore.common.initializer import Normal
    
    weight_init = Normal(mean=0, sigma=0.02)
    gamma_init = Normal(mean=1, sigma=0.02)
    
    class Generator(nn.Cell):
        """DCGAN网络生成器"""
    
        def __init__(self):
            super(Generator, self).__init__()
            self.generator = nn.SequentialCell(
                nn.Conv2dTranspose(nz, ngf * 8, 4, 1, 'valid', weight_init=weight_init),
                nn.BatchNorm2d(ngf * 8, gamma_init=gamma_init),
                nn.ReLU(),
                nn.Conv2dTranspose(ngf * 8, ngf * 4, 4, 2, 'pad', 1, weight_init=weight_init),
                nn.BatchNorm2d(ngf * 4, gamma_init=gamma_init),
                nn.ReLU(),
                nn.Conv2dTranspose(ngf * 4, ngf * 2, 4, 2, 'pad', 1, weight_init=weight_init),
                nn.BatchNorm2d(ngf * 2, gamma_init=gamma_init),
                nn.ReLU(),
                nn.Conv2dTranspose(ngf * 2, ngf, 4, 2, 'pad', 1, weight_init=weight_init),
                nn.BatchNorm2d(ngf, gamma_init=gamma_init),
                nn.ReLU(),
                nn.Conv2dTranspose(ngf, nc, 4, 2, 'pad', 1, weight_init=weight_init),
                nn.Tanh()
                )
    
        def construct(self, x):
            return self.generator(x)
    
    generator = Generator()
    
    • 判别器

    如前所述,判别器D是一个二分类网络模型,输出判定该图像为真实图的概率。通过一系列的Conv2dBatchNorm2dLeakyReLU层对其进行处理,最后通过Sigmoid激活函数得到最终概率。

    DCGAN论文提到,使用卷积而不是通过池化来进行下采样是一个好方法,因为它可以让网络学习自己的池化特征。

    class Discriminator(nn.Cell):
        """DCGAN网络判别器"""
    
        def __init__(self):
            super(Discriminator, self).__init__()
            self.discriminator = nn.SequentialCell(
                nn.Conv2d(nc, ndf, 4, 2, 'pad', 1, weight_init=weight_init),
                nn.LeakyReLU(0.2),
                nn.Conv2d(ndf, ndf * 2, 4, 2, 'pad', 1, weight_init=weight_init),
                nn.BatchNorm2d(ngf * 2, gamma_init=gamma_init),
                nn.LeakyReLU(0.2),
                nn.Conv2d(ndf * 2, ndf * 4, 4, 2, 'pad', 1, weight_init=weight_init),
                nn.BatchNorm2d(ngf * 4, gamma_init=gamma_init),
                nn.LeakyReLU(0.2),
                nn.Conv2d(ndf * 4, ndf * 8, 4, 2, 'pad', 1, weight_init=weight_init),
                nn.BatchNorm2d(ngf * 8, gamma_init=gamma_init),
                nn.LeakyReLU(0.2),
                nn.Conv2d(ndf * 8, 1, 4, 1, 'valid', weight_init=weight_init),
                )
            self.adv_layer = nn.Sigmoid()
    
        def construct(self, x):
            out = self.discriminator(x)
            out = out.reshape(out.shape[0], -1)
            return self.adv_layer(out)
    
    discriminator = Discriminator()
    
    • 损失函数和优化器
    # 定义损失函数
    adversarial_loss = nn.BCELoss(reduction='mean')
    # 为生成器和判别器设置优化器
    optimizer_D = nn.Adam(discriminator.trainable_params(), learning_rate=lr, beta1=beta1)
    optimizer_G = nn.Adam(generator.trainable_params(), learning_rate=lr, beta1=beta1)
    optimizer_G.update_parameters_name('optim_g.')
    optimizer_D.update_parameters_name('optim_d.')
    

    3.小结

    今天学习了GAN的一种扩展模型DCGAN(深度卷积对抗生成网络)。DCGAN结合了卷积神经网络(Convolutional Neural Networks,简称CNN)和生成对抗网络(Generative Adversarial Networks,简称GAN)的思想,用于生成逼真的图像。DCGAN的一些特点:DCGAN的生成器和判别器都舍弃了CNN的池化层,判别器保留CNN整体架构,生成器则是将卷积层替换成反卷积层。在判别器和生成器中使用了Batch Normalization(BN)层,这有助于处理初始化不良导致的训练问题,加速模型训练,提升了训练的稳定性。在生成器中除输出层使用Tanh激活函数,其余层全部使用ReLu激活函数。而在判别器中,除输出层外所有层都使用LeakyReLu激活函数,防止梯度稀疏。基于此网络构型,采用不同的训练数据可以用于生成不同类型的图片。

    以上是第23天的学习内容,附上今日打卡记录:
    在这里插入图片描述

  • 相关阅读:
    条件变量解决生产者消费者问题
    SpringBoot基于javaweb的养老院敬老院管理系统
    头歌MySQL数据库实训答案 有目录
    腾讯云CVM服务器5年可选2核4G和4核8G配置
    SpringCloud详解
    牛客小白月赛77
    MARKLLM——LLM 水印开源工具包
    react hooks 封装svg 双色(可拓展多色)图标组件
    AI-数学-高中-40法向量求法
    【21天学习挑战赛】快速排序
  • 原文地址:https://blog.csdn.net/randomize0/article/details/140459134