Ответ 1
Код из вашего примера (https://github.com/davidsonmizael/gan) дал мне тот же шум, что и вы. Потеря генератора слишком быстро снизилась.
Было несколько ошибок, я даже не уверен, что - но я думаю, что легко разобраться в различиях. Для сравнения также рассмотрим этот учебник: GAN в 50 строках PyTorch
.... 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.")