###### Training ###### with open('./output_' + opt.output_str + '/loss.csv', 'w', newline='') as csvfile: writer = csv.writer(csvfile, delimiter=' ') writer.writerow([ 'D_real_loss', 'D_fake_loss', 'G_loss', 'recon_loss', 'l2', 'l1', 'preceptual' ]) for epoch in range(opt.n_epochs): print("epoch = " + str(epoch)) model.train(mode=True) discriminator.train(mode=True) G_loss_e = 0 D_real_loss_e = 0 D_fake_loss_e = 0 recon_loss_e = 0 seq_l2_loss_e = 0 seq_l1_loss_e = 0 seq_preceptual_e = 0 for i, batch in enumerate(trainloader): curr_image, curr_depth = batch['image'], batch['depth'] next_image, next_depth = batch['next_image'], batch['next_depth']
label = torch.FloatTensor(opt.batchSize) real_A = Variable(real_A) real_B = Variable(real_B) label = Variable(label) if (opt.cuda): real_A = real_A.cuda() real_B = real_B.cuda() label = label.cuda() real_label = 1 fake_label = 0 ########### Training ########### netD.train() netG.train() for epoch in range(1, opt.niter + 1): nowtime = time.time() for i, image in enumerate(train_loader): ########### fDx ########### netD.zero_grad() imgA = image[0] imgB = image[1] # train with real data real_A.data.copy_(imgA) real_B.data.copy_(imgB) real_AB = torch.cat((real_A, real_B), 1) output = netD(real_AB)
########### Testing ########### def test(niter): loaderA, loaderB = iter(loader_A), iter(loader_B) imgA = loaderA.next() imgB = loaderB.next() real_A.data.resize_(imgA.size()).copy_(imgA) real_B.data.resize_(imgB.size()).copy_(imgB) AB = G_AB(real_A) BA = G_BA(real_B) vutils.save_image(AB.data, 'AB_niter_%03d.png' % (niter), normalize=True) vutils.save_image(BA.data, 'BA_niter_%03d.png' % (niter), normalize=True) ########### Training ########### D_A.train() D_B.train() G_AB.train() G_BA.train() for iteration in range(1, opt.niter + 1): ########### data ########### try: imgA = loaderA.next() imgB = loaderB.next() except StopIteration: loaderA, loaderB = iter(loader_A), iter(loader_B) imgA = loaderA.next() imgB = loaderB.next() real_A.data.resize_(imgA.size()).copy_(imgA) real_B.data.resize_(imgB.size()).copy_(imgB)