Python 使用PyTorch生成新图像

Python 使用PyTorch生成新图像,python,neural-network,pytorch,Python,Neural Network,Pytorch,我正在学习GANs,我已经完成了一门课程,这门课程给了我一个程序示例,该程序根据输入的示例生成图像 示例可在此处找到: https://github.com/davidsonmizael/gan 所以我决定用它来生成基于人脸正面照片数据集的新图像,但我没有任何成功。与上面的示例不同,代码只生成噪声,而输入具有实际图像 事实上,我不知道我应该做些什么来让代码指向正确的方向并从图像中学习。我没有对示例中提供的代码更改任何值,但它不起作用 如果有人能帮助我理解这一点,并为我指出正确的方向,那将非常有

我正在学习GANs,我已经完成了一门课程,这门课程给了我一个程序示例,该程序根据输入的示例生成图像

示例可在此处找到:

https://github.com/davidsonmizael/gan
所以我决定用它来生成基于人脸正面照片数据集的新图像,但我没有任何成功。与上面的示例不同,代码只生成噪声,而输入具有实际图像

事实上,我不知道我应该做些什么来让代码指向正确的方向并从图像中学习。我没有对示例中提供的代码更改任何值,但它不起作用

如果有人能帮助我理解这一点,并为我指出正确的方向,那将非常有帮助。提前谢谢

我的鉴别器:

class D(nn.Module):

    def __init__(self):
        super(D, self).__init__()
        self.main = nn.Sequential(
                nn.Conv2d(3, 64, 4, 2, 1, bias = False),
                nn.LeakyReLU(0.2, inplace = True),
                nn.Conv2d(64, 128, 4, 2, 1, bias = False),
                nn.BatchNorm2d(128),
                nn.LeakyReLU(0.2, inplace = True),
                nn.Conv2d(128, 256, 4, 2, 1, bias = False),
                nn.BatchNorm2d(256),
                nn.LeakyReLU(0.2, inplace = True),
                nn.Conv2d(256, 512, 4, 2, 1, bias = False),
                nn.BatchNorm2d(512),
                nn.LeakyReLU(0.2, inplace = True),
                nn.Conv2d(512, 1, 4, 1, 0, bias = False),
                nn.Sigmoid()
                )

    def forward(self, input):
        return self.main(input).view(-1)
我的发电机:

class G(nn.Module):

    def __init__(self):
        super(G, self).__init__()
        self.main = nn.Sequential(
                nn.ConvTranspose2d(100, 512, 4, 1, 0, bias = False),
                nn.BatchNorm2d(512),
                nn.ReLU(True),
                nn.ConvTranspose2d(512, 256, 4, 2, 1, bias = False),
                nn.BatchNorm2d(256),
                nn.ReLU(True),
                nn.ConvTranspose2d(256, 128, 4, 2, 1, bias = False),
                nn.BatchNorm2d(128),
                nn.ReLU(True),
                nn.ConvTranspose2d(128, 64, 4, 2, 1, bias = False),
                nn.BatchNorm2d(64),
                nn.ReLU(True),
                nn.ConvTranspose2d(64, 3, 4, 2, 1, bias = False),
                nn.Tanh()
                )

    def forward(self, input):
        return self.main(input)
我的功能是启动重量:

def weights_init(m):
    classname = m.__class__.__name__
    if classname.find('Conv') != -1:
        m.weight.data.normal_(0.0, 0.02)
    elif classname.find('BatchNorm') != -1:
        m.weight.data.normal_(1.0, 0.02)
        m.bias.data.fill_(0)
完整的代码可以在这里看到:

https://github.com/davidsonmizael/criminal-gan
第25纪元产生的噪音:

输入真实图像:

训练不是很快。我假设你不是在使用预先训练过的模型,而是从零开始学习。在第25纪元,在样本中看不到任何有意义的模式是很正常的。 我意识到github项目在25个时代之后向您展示了一些很酷的东西,但这也取决于数据集的大小。 CIFAR-10(github页面上使用的一个)有60000个图像。 25个时代意味着网络已经看到了所有的25次

我不知道您使用的是哪一个数据集,但如果数据集更小,则可能需要更多的时间才能看到结果,因为网络总共可以看到更少的图像。如果数据集中的图像具有更高的分辨率,则可能需要更长的时间

如果不是几千年的话,你至少应该在几百年后再检查一次


例如,在25个时代后的正面照片数据集上:

50年后:

示例()中的代码给了我与您显示的相同的噪音。发电机的损耗下降得太快了

有一些事情是有问题的,我甚至不确定是什么-但我想你自己很容易找出区别。要进行比较,请参阅本教程:

CIFAR-10上25个时代(批次大小256)后的结果:

我现在没有时间下载您的代码和数据来尝试,但我已经尝试了一遍代码,在gan.py的第80行,您有
target=Variable(torch.ones(input.size()[0])).cuda()
-就像总是调用cuda(),即使不一定使用(因此其他变量不是cuda()).也许它需要超过25个时代才能开始产生有意义的东西?@KenSyme是的,忘了cuda吧。我在所有事情之后都添加了它,我还没有机会测试它,但我想添加支持。这不是问题所在:/@vasia使用我自己编写的代码,我可以看到从第一个纪元开始出现的一些结果,对于反向传播,输出在每种情况下应至少有一点不同epoch@davis这并不是说你忘记了cuda——在大多数情况下,你都正确地检查了你的标志,并且只在设置了cuda的情况下才将变量移动到cuda。在这种情况下,您总是在变量上调用cuda(之后也会进行检查)-看起来像是复制粘贴错误。如果这个变量在cuda上,而其他变量不是,我无法想象事情会进展顺利。为了生成这些示例,您只使用了与我相同的代码和数据集?我刚刚运行了它,它只在150个时代之后产生了噪音。我试着修改了你在这里展示的代码,它成功了!在第十个纪元,它已经开始显示结果。非常感谢。
.... same as your code
print("# Starting generator and descriminator...")
netG = G()
netG.apply(weights_init)

netD = D()
netD.apply(weights_init)

if torch.cuda.is_available():
    netG.cuda()
    netD.cuda()

#training the DCGANs
criterion = nn.BCELoss()
optimizerD = optim.Adam(netD.parameters(), lr = 0.0002, betas = (0.5, 0.999))
optimizerG = optim.Adam(netG.parameters(), lr = 0.0002, betas = (0.5, 0.999))

epochs = 25

timeElapsed = []
for epoch in range(epochs):
    print("# Starting epoch [%d/%d]..." % (epoch, epochs))
    for i, data in enumerate(dataloader, 0):
        start = time.time()
        time.clock()  

        #updates the weights of the discriminator nn
        netD.zero_grad()

        #trains the discriminator with a real image
        real, _ = data

        if torch.cuda.is_available():
            inputs = Variable(real.cuda()).cuda()
            target = Variable(torch.ones(inputs.size()[0]).cuda()).cuda()
        else:
            inputs = Variable(real)
            target = Variable(torch.ones(inputs.size()[0]))

        output = netD(inputs)
        errD_real = criterion(output, target)
        errD_real.backward() #retain_graph=True

        #trains the discriminator with a fake image
        if torch.cuda.is_available():
            D_noise = Variable(torch.randn(inputs.size()[0], 100, 1, 1).cuda()).cuda()
            target = Variable(torch.zeros(inputs.size()[0]).cuda()).cuda()
        else:
            D_noise = Variable(torch.randn(inputs.size()[0], 100, 1, 1))
            target = Variable(torch.zeros(inputs.size()[0]))
        D_fake = netG(D_noise).detach()
        D_fake_ouput = netD(D_fake)
        errD_fake = criterion(D_fake_ouput, target)
        errD_fake.backward()

        # NOT:backpropagating the total error
        # errD = errD_real + errD_fake

        optimizerD.step()

    #for i, data in enumerate(dataloader, 0):

        #updates the weights of the generator nn
        netG.zero_grad()

        if torch.cuda.is_available():
            G_noise = Variable(torch.randn(inputs.size()[0], 100, 1, 1).cuda()).cuda()
            target = Variable(torch.ones(inputs.size()[0]).cuda()).cuda()
        else:
            G_noise = Variable(torch.randn(inputs.size()[0], 100, 1, 1))
            target = Variable(torch.ones(inputs.size()[0]))

        fake = netG(G_noise)
        G_output = netD(fake)
        errG  = criterion(G_output, target)

        #backpropagating the error
        errG.backward()
        optimizerG.step()


        if i % 50 == 0:
            #prints the losses and save the real images and the generated images
            print("# Progress: ")
            print("[%d/%d][%d/%d] Loss_D: %.4f Loss_G: %.4f" % (epoch, epochs, i, len(dataloader), errD_real.data[0], errG.data[0]))

            #calculates the remaining time by taking the avg seconds that every loop
            #and multiplying by the loops that still need to run
            timeElapsed.append(time.time() - start)
            avg_time = (sum(timeElapsed) / float(len(timeElapsed)))
            all_dtl = (epoch * len(dataloader)) + i
            rem_dtl = (len(dataloader) - i) + ((epochs - epoch) * len(dataloader))
            remaining =  (all_dtl - rem_dtl) * avg_time
            print("# Estimated remaining time: %s" % (time.strftime("%H:%M:%S", time.gmtime(remaining))))

        if i % 100 == 0:
            vutils.save_image(real, "%s/real_samples.png" % "./results", normalize = True)
            vutils.save_image(fake.data, "%s/fake_samples_epoch_%03d.png" % ("./results", epoch), normalize = True)

print ("# Finished.")