Higher pytorch version raise error when training with GAN Net

username: leigaoxiang1
cooked: <p>import torch<br>
from torch import nn<br>
from torch import autograd<br>
import torch.nn.functional as F<br>
from tensorboardX import SummaryWriter<br>
from models.discriminator import Discriminator_STE<br>
from PIL import Image<br>
import numpy as np</p>
<p>def gram_matrix(feat):<br>
# <a href="https://github.com/pytorch/examples/blob/master/fast_neural_style/neural_style/utils.py" rel="noopener nofollow ugc">https://github.com/pytorch/examples/blob/master/fast_neural_style/neural_style/utils.py</a><br>
(b, ch, h, w) = feat.size()<br>
feat = feat.view(b, ch, h * w)<br>
feat_t = feat.transpose(1, 2)<br>
gram = torch.bmm(feat, feat_t) / (ch * h * w)<br>
return gram</p>
<p>def visual(image):<br>
im = image.transpose(1,2).transpose(2,3).detach().cpu().numpy()<br>
Image.fromarray(im[0].astype(np.uint8)).show()</p>
<p>def dice_loss(input, target):<br>
input = torch.sigmoid(input)</p>
<pre><code>input = input.contiguous().view(input.size()[0], -1)
target = target.contiguous().view(target.size()[0], -1)

input = input 
target = target

a = torch.sum(input * target, 1)
b = torch.sum(input * input, 1) + 0.001
c = torch.sum(target * target, 1) + 0.001
d = (2 * a) / (b + c)
dice_loss = torch.mean(d)
return 1 - dice_loss
</code></pre>
<p>class LossWithGAN_STE(nn.Module):<br>
def <strong>init</strong>(self, logPath, extractor, Lamda, lr, betasInit=(0.5, 0.9)):<br>
super(LossWithGAN_STE, self).<strong>init</strong>()<br>
self.l1 = nn.L1Loss()<br>
self.extractor = extractor<br>
self.discriminator = Discriminator_STE(3)    ## local_global sn patch gan<br>
self.D_optimizer = torch.optim.Adam(self.discriminator.parameters(), lr=lr, betas=betasInit)<br>
self.cudaAvailable = torch.cuda.is_available()<br>
self.numOfGPUs = torch.cuda.device_count()<br>
self.lamda = Lamda<br>
self.writer = SummaryWriter(logPath)</p>
<pre><code>def forward(self, input, mask, x_o1,x_o2,x_o3,output,mm, gt, count, epoch):
    self.discriminator.zero_grad()
    D_real = self.discriminator(gt, mask)
    D_real = D_real.mean().sum() * -1
    D_fake = self.discriminator(output, mask)
    D_fake = D_fake.mean().sum() * 1
    D_loss = torch.mean(F.relu(1.+D_real)) + torch.mean(F.relu(1.+D_fake))  #SN-patch-GAN loss
    D_fake = -torch.mean(D_fake)     #  SN-Patch-GAN loss

    self.D_optimizer.zero_grad()
    D_loss.backward(retain_graph=True)
    self.D_optimizer.step()

    self.writer.add_scalar('LossD/Discrinimator loss', D_loss.item(), count)
    
    output_comp = mask * input + (1 - mask) * output
   # import pdb;pdb.set_trace()
    holeLoss = 10 * self.l1((1 - mask) * output, (1 - mask) * gt)
    validAreaLoss = 2*self.l1(mask * output, mask * gt)  

    mask_loss = dice_loss(mm, 1-mask)
    ### MSR loss ###
    masks_a = F.interpolate(mask, scale_factor=0.25)
    masks_b = F.interpolate(mask, scale_factor=0.5)
    imgs1 = F.interpolate(gt, scale_factor=0.25)
    imgs2 = F.interpolate(gt, scale_factor=0.5)
    msrloss = 8 * self.l1((1-mask)*x_o3,(1-mask)*gt) + 0.8*self.l1(mask*x_o3, mask*gt)+\
                6 * self.l1((1-masks_b)*x_o2,(1-masks_b)*imgs2)+1*self.l1(masks_b*x_o2,masks_b*imgs2)+\
                5 * self.l1((1-masks_a)*x_o1,(1-masks_a)*imgs1)+0.8*self.l1(masks_a*x_o1,masks_a*imgs1)

    feat_output_comp = self.extractor(output_comp)
    feat_output = self.extractor(output)
    feat_gt = self.extractor(gt)

    prcLoss = 0.0
    for i in range(3):
        prcLoss += 0.01 * self.l1(feat_output[i], feat_gt[i])
        prcLoss += 0.01 * self.l1(feat_output_comp[i], feat_gt[i])

    styleLoss = 0.0
    for i in range(3):
        styleLoss += 120 * self.l1(gram_matrix(feat_output[i]),
                                      gram_matrix(feat_gt[i]))
        styleLoss += 120 * self.l1(gram_matrix(feat_output_comp[i]),
                                      gram_matrix(feat_gt[i]))
    """ if self.numOfGPUs &gt; 1:
        holeLoss = holeLoss.sum() / self.numOfGPUs
        validAreaLoss = validAreaLoss.sum() / self.numOfGPUs
        prcLoss = prcLoss.sum() / self.numOfGPUs
        styleLoss = styleLoss.sum() / self.numOfGPUs """
    self.writer.add_scalar('LossG/Hole loss', holeLoss.item(), count)    
    self.writer.add_scalar('LossG/Valid loss', validAreaLoss.item(), count) 
    self.writer.add_scalar('LossG/msr loss', msrloss.item(), count)   
    self.writer.add_scalar('LossPrc/Perceptual loss', prcLoss.item(), count)    
    self.writer.add_scalar('LossStyle/style loss', styleLoss.item(), count)

    GLoss = msrloss+ holeLoss + validAreaLoss+ prcLoss + styleLoss + 0.1 * D_fake + 1*mask_loss
    self.writer.add_scalar('Generator/Joint loss', GLoss.item(), count)    
    return GLoss
</code></pre>
<p>train.py:</p>
<p>import os<br>
import math<br>
import argparse<br>
import torch<br>
import torch.nn as nn<br>
import torch.optim as optim<br>
import torch.backends.cudnn as cudnn<br>
from PIL import Image<br>
import numpy as np<br>
from torch.autograd import Variable<br>
from torchvision.utils import save_image<br>
from torchvision import datasets<br>
from torch.utils.data import DataLoader<br>
from torchvision import utils<br>
from data.dataloader import ErasingData<br>
from loss.Loss import LossWithGAN_STE<br>
from models.Model import VGG16FeatureExtractor<br>
from models.sa_gan import STRnet2</p>
<p>torch.set_num_threads(5)</p>
<p><span class="hashtag-raw">#os</span>.environ[“CUDA_VISIBLE_DEVICES”] = “3”    ### set the gpu as No…</p>
<p>parser = argparse.ArgumentParser()<br>
parser.add_argument(‘–numOfWorkers’, type=int, default=0,<br>
help=‘workers for dataloader’)<br>
parser.add_argument(‘–modelsSavePath’, type=str, default=‘./checkpoint’,<br>
help=‘path for saving models’)<br>
parser.add_argument(‘–logPath’, type=str,<br>
default=‘’)<br>
parser.add_argument(‘–batchSize’, type=int, default=3)<br>
parser.add_argument(‘–loadSize’, type=int, default=512,<br>
help=‘image loading size’)<br>
parser.add_argument(‘–dataRoot’, type=str, default=“”)<br>
parser.add_argument(‘–pretrained’,type=str, default=‘’, help=‘pretrained models for finetuning’)<br>
parser.add_argument(‘–num_epochs’, type=int, default=500, help=‘epochs’)<br>
args = parser.parse_args()</p>
<p>torch.autograd.set_detect_anomaly(True)</p>
<p>def visual(image):<br>
im = image.transpose(1,2).transpose(2,3).detach().cpu().numpy()<br>
Image.fromarray(im[0].astype(np.uint8)).show()</p>
<p>cuda = torch.cuda.is_available()<br>
if cuda:<br>
print(‘Cuda is available!’)<br>
cudnn.enable = True<br>
cudnn.benchmark = True</p>
<p>batchSize = args.batchSize<br>
loadSize = (args.loadSize, args.loadSize)</p>
<p>if not os.path.exists(args.modelsSavePath):<br>
os.makedirs(args.modelsSavePath)</p>
<p>dataRoot = args.dataRoot</p>
<h1><a name="import-pdbpdbset_trace-1" class="anchor" href="#import-pdbpdbset_trace-1"></a>import pdb;pdb.set_trace()</h1>
<p>Erase_data = ErasingData(dataRoot, loadSize, training=True)<br>
Erase_data = DataLoader(Erase_data, batch_size=batchSize,<br>
shuffle=True, num_workers=args.numOfWorkers, drop_last=False, pin_memory=True)<br>
device = torch.device(‘cuda:0’)<br>
netG = STRnet2(3)</p>
<p>if args.pretrained != ‘’:<br>
print('loaded ')<br>
netG.load_state_dict(torch.load(args.pretrained))</p>
<p>numOfGPUs = torch.cuda.device_count()</p>
<p>if cuda:<br>
netG = netG.to(device)<br>
if numOfGPUs &gt; 3:<br>
netG = nn.DataParallel(netG, device_ids=range(numOfGPUs))</p>
<p>count = 1</p>
<p>G_optimizer = optim.Adam(netG.parameters(), lr=0.0001, betas=(0.5, 0.9))</p>
<p>criterion = LossWithGAN_STE(args.logPath, VGG16FeatureExtractor(), lr=0.00001, betasInit=(0.0, 0.9), Lamda=10.0)</p>
<p>if cuda:<br>
criterion = criterion.to(device)</p>
<pre><code>if numOfGPUs &gt; 3:
    criterion = nn.DataParallel(criterion, device_ids=range(numOfGPUs))
</code></pre>
<p>print(‘OK!’)<br>
num_epochs = args.num_epochs</p>
<p>for i in range(1, num_epochs + 1):<br>
netG.train()</p>
<pre><code>for k,(imgs, gt, masks, path) in enumerate(Erase_data):
    if cuda:
        imgs = imgs.to(device)
        gt = gt.to(device)
        masks = masks.to(device)
    netG.zero_grad()

    x_o1,x_o2,x_o3,fake_images,mm = netG(imgs)
    G_loss = criterion(imgs, masks, x_o1, x_o2, x_o3, fake_images, mm, gt, count, i)
    G_loss = G_loss.sum()
    #G_loss =  torch.sum(G_loss)
    G_optimizer.zero_grad()
    G_loss.backward()
    G_optimizer.step()       

    print('[{}/{}] Generator Loss of epoch{} is {}'.format(k,len(Erase_data),i, G_loss.item()))

    count += 1

if ( i % 10 == 0):
    if numOfGPUs &gt; 1 :
        torch.save(netG.module.state_dict(), args.modelsSavePath +
                '/STE_{}.pth'.format(i))
    else:
        torch.save(netG.state_dict(), args.modelsSavePath +
                '/STE_{}.pth'.format(i))
</code></pre>
<p>when run G_loss.backward()  raise error as follows</p>
<p>Variable._execution_engine.run_backward(  # Calls into the C++ engine to run the backward pass<br>
RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [1, 512, 4, 4]] is at version 2; expected version 1 instead. Hint: the backtrace further above shows the operation that failed to compute its gradient. The variable in question was changed in there or anywhere later. Good luck!</p>

username: ptrblck
cooked: <p>Your code is unfortunately not properly formatted so quite hard to read.<br>
However, you might be running into <a href="https://discuss.pytorch.org/t/struggling-with-a-runtime-error-related-to-in-place-operations/102222/6">this error</a> which is correctly raised if stale forward activations would lead to a wrong gradient computation.</p>

username: leigaoxiang1
cooked: <p>the whole code is here <a href="https://github.com/lcy0604/EraseNet" class="inline-onebox" rel="noopener nofollow ugc">GitHub - lcy0604/EraseNet</a></p>
<p>can u take a few time to help me?</p>

username: leigaoxiang1
cooked: <p><a href="https://discuss.pytorch.org/u/leigaoxiang1"><img src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/leigaoxiang1/48/65312_2.png" alt="" width="48" height="48" role="presentation"></a></p>
<p><a href="https://discuss.pytorch.org/u/leigaoxiang1">leigaoxiang1</a><a href="https://discuss.pytorch.org/u/leigaoxiang1">Leigaoxiang</a></p>
<p><a href="https://discuss.pytorch.org/t/higher-pytorch-version-raise-error-when-training-with-gan-net/191451/3">21m</a></p>
<p>the whole code is here <a href="https://github.com/lcy0604/EraseNet" rel="noopener nofollow ugc">GitHub - lcy0604/EraseNet</a></p>
<p>can u take a few time to help me?</p>