text stringlengths 1 93.6k |
|---|
source = inputs
|
#source = inputs
|
# mid_slice = opt.numOfChannel_singleSource//2
|
residual_source = inputs
|
#inputs = inputs.cuda()
|
#exinputs = exinputs.cuda()
|
source = source.cuda()
|
residual_source = residual_source.cuda()
|
labels = labels.cuda()
|
#we should consider different data to train
|
#wrap them into Variable
|
source, residual_source, labels = Variable(source),Variable(residual_source), Variable(labels)
|
#inputs, exinputs, labels = Variable(inputs),Variable(exinputs), Variable(labels)
|
## (1) update D network: maximize log(D(x)) + log(1 - D(G(z)))
|
if opt.isAdLoss:
|
if opt.whichNet == 3 or opt.whichNet == 4:
|
outputG = net(source, residual_source) # 5x64x64->1*64x64
|
else:
|
outputG = net(source) # 5x64x64->1*64x64
|
#outputG = net(source,residual_source) #5x64x64->1*64x64
|
if len(labels.size())==3:
|
labels = labels.unsqueeze(1)
|
outputD_real = netD(labels)
|
outputD_real = F.sigmoid(outputD_real)
|
if len(outputG.size())==3:
|
outputG = outputG.unsqueeze(1)
|
outputD_fake = netD(outputG)
|
outputD_fake = F.sigmoid(outputD_fake)
|
netD.zero_grad()
|
batch_size = inputs.size(0)
|
real_label = torch.ones(batch_size,1)
|
real_label = real_label.cuda()
|
#print(real_label.size())
|
real_label = Variable(real_label)
|
#print(outputD_real.size())
|
loss_real = criterion_bce(outputD_real,real_label)
|
loss_real.backward()
|
#train with fake data
|
fake_label = torch.zeros(batch_size,1)
|
# fake_label = torch.FloatTensor(batch_size)
|
# fake_label.data.resize_(batch_size).fill_(0)
|
fake_label = fake_label.cuda()
|
fake_label = Variable(fake_label)
|
loss_fake = criterion_bce(outputD_fake,fake_label)
|
loss_fake.backward()
|
lossD = loss_real + loss_fake
|
# print 'loss_real is ',loss_real.data[0],'loss_fake is ',loss_fake.data[0],'outputD_real is',outputD_real.data[0]
|
# print('loss for discriminator is %f'%lossD.data[0])
|
#update network parameters
|
optimizerD.step()
|
## (2) update G network: minimize the L1/L2 loss, maximize the D(G(x))
|
# print inputs.data.shape
|
#outputG = net(source) #here I am not sure whether we should use twice or not
|
if opt.whichNet == 3 or opt.whichNet == 4:
|
outputG = net(source, residual_source) # 5x64x64->1*64x64
|
else:
|
outputG = net(source) # 5x64x64->1*64x64
|
#outputG = net(source,residual_source) #5x64x64->1*64x64
|
net.zero_grad()
|
if opt.whichLoss==1:
|
lossG_G = criterion_L1(torch.squeeze(outputG), torch.squeeze(labels))
|
elif opt.whichLoss==2:
|
lossG_G = criterion_RTL1(torch.squeeze(outputG), torch.squeeze(labels))
|
else:
|
lossG_G = criterion_L2(torch.squeeze(outputG), torch.squeeze(labels))
|
lossG_G = opt.lossBase * lossG_G
|
lossG_G.backward() #compute gradients
|
if opt.isAdLoss:
|
#we want to fool the discriminator, thus we pretend the label here to be real. Actually, we can explain from the
|
#angel of equation (note the max and min difference for generator and discriminator)
|
#outputG = net(inputs)
|
#outputG = net(source,residual_source) #5x64x64->1*64x64
|
if opt.whichNet == 3 or opt.whichNet == 4:
|
outputG = net(source, residual_source) # 5x64x64->1*64x64
|
else:
|
outputG = net(source) # 5x64x64->1*64x64
|
if len(outputG.size())==3:
|
outputG = outputG.unsqueeze(1)
|
outputD = netD(outputG)
|
outputD = F.sigmoid(outputD)
|
lossG_D = opt.lambda_AD*criterion_bce(outputD,real_label) #note, for generator, the label for outputG is real, because the G wants to confuse D
|
lossG_D.backward()
|
#for other losses, we can define the loss function following the pytorch tutorial
|
optimizer.step() #update network parameters
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.