使用GAN生成圖片


一:卷積神經網絡的搭建

class NetG(nn.Module):
    '''
    生成器定義
    '''

    def __init__(self, opt):
        super(NetG, self).__init__()
        ngf = opt.ngf  # 生成器feature map數

        self.maina = nn.Sequential(
            # 輸入是一個nz維度的噪聲,我們可以認為它是一個1*1*nz的feature map
            nn.ConvTranspose2d(opt.nz, ngf * 16, 4, 1, 0, bias=False),
            #noises = t.randn(opt.gen_search_num, opt.nz, 1, 1).normal_(opt.gen_mean, opt.gen_std)
            nn.BatchNorm2d(ngf * 16),
            nn.ReLU(True),

            # 上一步的輸出形狀:(ngf*8) x 4 x 4
            #
            nn.ConvTranspose2d(ngf * 16, ngf * 10, 4, 2, 1, bias=False),
            nn.BatchNorm2d(ngf * 10),
            nn.ReLU(True),
            # # # 上一步的輸出形狀: (ngf*4) x 8 x 8
            # # #
            nn.ConvTranspose2d(ngf * 10, ngf * 8, 4, 3, 1, bias=False),
            nn.BatchNorm2d(ngf * 8),
            nn.ReLU(True),
            # # # 上一步的輸出形狀: (ngf*2) x 16 x 16
            # #
            nn.ConvTranspose2d(ngf * 8, ngf*8, 4, 3, 1, bias=False),
            nn.BatchNorm2d(ngf*8),
            nn.ReLU(True),
            # # # 上一步的輸出形狀:(ngf) x 32 x 32
            nn.ConvTranspose2d(ngf * 8, ngf * 6, 4, 2, 1, bias=False),
            nn.BatchNorm2d(ngf * 6),
            nn.ReLU(True),
            # #
            nn.ConvTranspose2d(ngf * 6, ngf , 4, 1, 1, bias=False),
            nn.BatchNorm2d(ngf ),
            nn.ReLU(True),
            nn.ConvTranspose2d(ngf, 3, 5, 3, 1, bias=False),
            nn.Tanh()  # 輸出范圍 -1~1 故而采用Tanh
            # 輸出形狀:3 x 96 x 96
        )

    def forward(self, input):
        return self.maina(input)

  

class NetD(nn.Module):
    '''
    判別器定義
    '''

    def __init__(self, opt):
        super(NetD, self).__init__()
        ndf = opt.ndf
        self.main = nn.Sequential(
            # 輸入 3 x 96 x 96
            nn.Conv2d(3, ndf, 5, 3, 1, bias=False),
            nn.LeakyReLU(0.2, inplace=True),
            # 輸出 (ndf) x 32 x 32

            nn.Conv2d(ndf, ndf * 6, 4, 1, 1, bias=False),
            nn.BatchNorm2d(ndf * 6),
            nn.LeakyReLU(0.2, inplace=True),
            # 輸出 (ndf*2) x 16 x 16
            #
            nn.Conv2d(ndf * 6, ndf * 8, 4, 2, 1, bias=False),
            nn.BatchNorm2d(ndf * 8),
            nn.LeakyReLU(0.2, inplace=True),
            # # 輸出 (ndf*4) x 8 x 8
            #
            nn.Conv2d(ndf * 8, ndf * 8, 4, 3, 1, bias=False),
            nn.BatchNorm2d(ndf * 8),
            nn.LeakyReLU(0.2, inplace=True),
            nn.Conv2d(ndf * 8, ndf * 10, 4, 3, 1, bias=False),
            nn.BatchNorm2d(ndf * 10),
            nn.LeakyReLU(0.2, inplace=True),
            nn.Conv2d(ndf * 10, ndf * 16, 4, 2, 1, bias=False),
            nn.BatchNorm2d(ndf * 16),
            nn.LeakyReLU(0.2, inplace=True),
            # 輸出 (ndf*8) x 4 x 4
            #
            nn.Conv2d(ndf * 16, 1, 4, 1, 0, bias=False),
            nn.Sigmoid()  # 輸出一個數(概率)
        )

    def forward(self, input):
        return self.main(input).view(-1)

  訓練后生成的圖片

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM