(netG, netD, GANLoss, ReconLoss, DLoss, optG, optD, dataloader, epoch, device=cuda0, val_datas=None)
Train Phase, for training and spectral normalization patch gan in
    Free-Form Image Inpainting with Gated Convolution (snpgan)
Train Phase, for training and spectral normalization patch gan in
    Free-Form Image Inpainting with Gated Convolution (snpgan)
Train Phase for training and spectral normalization patch gan in Free - Form Image Inpainting with Gated Convolution ( snpgan )
def train(netG, netD, GANLoss, ReconLoss, DLoss, optG, optD, dataloader, epoch, device=cuda0, val_datas=None):
    """
    Train Phase, for training and spectral normalization patch gan in
    Free-Form Image Inpainting with Gated Convolution (snpgan)

    """
    netG.to(device)
    netD.to(device)
    batch_time = AverageMeter()
    data_time = AverageMeter()
    losses = {"g_loss":AverageMeter(), "r_loss":AverageMeter(), "whole_loss":AverageMeter(), 'd_loss':AverageMeter()}

    netG.train()
    netD.train()
    end = time.time()
    for i, (imgs, masks) in enumerate(dataloader):
        data_time.update(time.time() - end)
        masks = masks['random_free_form']

        # Optimize Discriminator
        optD.zero_grad(), netD.zero_grad(), netG.zero_grad(), optG.zero_grad()

        imgs, masks = imgs.to(device), masks.to(device)
        imgs = (imgs / 127.5 - 1)
        # mask is 1 on masked region

        coarse_imgs, recon_imgs, attention = netG(imgs, masks)
        #print(attention.size(), )
        complete_imgs = recon_imgs * masks + imgs * (1 - masks)

        pos_imgs = torch.cat([imgs, masks, torch.full_like(masks, 1.)], dim=1)
        neg_imgs = torch.cat([complete_imgs, masks, torch.full_like(masks, 1.)], dim=1)
        pos_neg_imgs = torch.cat([pos_imgs, neg_imgs], dim=0)

        pred_pos_neg = netD(pos_neg_imgs)
        pred_pos, pred_neg = torch.chunk(pred_pos_neg, 2, dim=0)
        d_loss = DLoss(pred_pos, pred_neg)
        losses['d_loss'].update(d_loss.item(), imgs.size(0))
        d_loss.backward(retain_graph=True)

        optD.step()


        # Optimize Generator
        optD.zero_grad(), netD.zero_grad(), optG.zero_grad(), netG.zero_grad()
        pred_neg = netD(neg_imgs)
        #pred_pos, pred_neg = torch.chunk(pred_pos_neg,  2, dim=0)
        g_loss = GANLoss(pred_neg)
        r_loss = ReconLoss(imgs, coarse_imgs, recon_imgs, masks)

        whole_loss = g_loss + r_loss

        # Update the recorder for losses
        losses['g_loss'].update(g_loss.item(), imgs.size(0))
        losses['r_loss'].update(r_loss.item(), imgs.size(0))
        losses['whole_loss'].update(whole_loss.item(), imgs.size(0))
        whole_loss.backward()

        optG.step()


        # Update time recorder
        batch_time.update(time.time() - end)

        if (i+1) % config.SUMMARY_FREQ == 0:
            # Logger logging
            logger.info("Epoch {0}, [{1}/{2}]: Batch Time:{batch_time.val:.4f},\t Data Time:{data_time.val:.4f}, Whole Gen Loss:{whole_loss.val:.4f}\t,"
                        "Recon Loss:{r_loss.val:.4f},\t GAN Loss:{g_loss.val:.4f},\t D Loss:{d_loss.val:.4f}" \
                        .format(epoch, i+1, len(dataloader), batch_time=batch_time, data_time=data_time, whole_loss=losses['whole_loss'], r_loss=losses['r_loss'] \
                        ,g_loss=losses['g_loss'], d_loss=losses['d_loss']))
            # Tensorboard logger for scaler and images
            info_terms = {'WGLoss':whole_loss.item(), 'ReconLoss':r_loss.item(), "GANLoss":g_loss.item(), "DLoss":d_loss.item()}

            for tag, value in info_terms.items():
                tensorboardlogger.scalar_summary(tag, value, epoch*len(dataloader)+i)

            for tag, value in losses.items():
                tensorboardlogger.scalar_summary('avg_'+tag, value.avg, epoch*len(dataloader)+i)

            def img2photo(imgs):
                return ((imgs+1)*127.5).transpose(1,2).transpose(2,3).detach().cpu().numpy()
            # info = { 'train/ori_imgs':img2photo(imgs),
            #          'train/coarse_imgs':img2photo(coarse_imgs),
            #          'train/recon_imgs':img2photo(recon_imgs),
            #          'train/comp_imgs':img2photo(complete_imgs),
            info = {
                     'train/whole_imgs':img2photo(torch.cat([ imgs * (1 - masks), coarse_imgs, recon_imgs, imgs, complete_imgs], dim=3))
                     }

            for tag, images in info.items():
                tensorboardlogger.image_summary(tag, images, epoch*len(dataloader)+i)
        if (i+1) % config.VAL_SUMMARY_FREQ == 0 and val_datas is not None:

            validate(netG, netD, GANLoss, ReconLoss, DLoss, optG, optD, val_datas , epoch, device, batch_n=i)
            netG.train()
            netD.train()
        end = time.time()
def train ( netG , netD , GANLoss , ReconLoss , DLoss , optG , optD , dataloader , epoch , device = cuda0 , val_datas = None ) : netG . to ( device ) netD . to ( device ) batch_time = AverageMeter ( ) data_time = AverageMeter ( ) losses = { "g_loss" : AverageMeter ( ) , "r_loss" : AverageMeter ( ) , "whole_loss" : AverageMeter ( ) , 'd_loss' : AverageMeter ( ) } netG . train ( ) netD . train ( ) end = time . time ( ) for i , ( imgs , masks ) in enumerate ( dataloader ) : data_time . update ( time . time ( ) - end ) masks = masks [ 'random_free_form' ] # Optimize Discriminator optD . zero_grad ( ) , netD . zero_grad ( ) , netG . zero_grad ( ) , optG . zero_grad ( ) imgs , masks = imgs . to ( device ) , masks . to ( device ) imgs = ( imgs / 127.5 - 1 ) # mask is 1 on masked region coarse_imgs , recon_imgs , attention = netG ( imgs , masks ) #print(attention.size(), ) complete_imgs = recon_imgs * masks + imgs * ( 1 - masks ) pos_imgs = torch . cat ( [ imgs , masks , torch . full_like ( masks , 1. ) ] , dim = 1 ) neg_imgs = torch . cat ( [ complete_imgs , masks , torch . full_like ( masks , 1. ) ] , dim = 1 ) pos_neg_imgs = torch . cat ( [ pos_imgs , neg_imgs ] , dim = 0 ) pred_pos_neg = netD ( pos_neg_imgs ) pred_pos , pred_neg = torch . chunk ( pred_pos_neg , 2 , dim = 0 ) d_loss = DLoss ( pred_pos , pred_neg ) losses [ 'd_loss' ] . update ( d_loss . item ( ) , imgs . size ( 0 ) ) d_loss . backward ( retain_graph = True ) optD . step ( ) # Optimize Generator optD . zero_grad ( ) , netD . zero_grad ( ) , optG . zero_grad ( ) , netG . zero_grad ( ) pred_neg = netD ( neg_imgs ) #pred_pos, pred_neg = torch.chunk(pred_pos_neg,  2, dim=0) g_loss = GANLoss ( pred_neg ) r_loss = ReconLoss ( imgs , coarse_imgs , recon_imgs , masks ) whole_loss = g_loss + r_loss # Update the recorder for losses losses [ 'g_loss' ] . update ( g_loss . item ( ) , imgs . size ( 0 ) ) losses [ 'r_loss' ] . update ( r_loss . item ( ) , imgs . size ( 0 ) ) losses [ 'whole_loss' ] . update ( whole_loss . item ( ) , imgs . size ( 0 ) ) whole_loss . backward ( ) optG . step ( ) # Update time recorder batch_time . update ( time . time ( ) - end ) if ( i + 1 ) % config . SUMMARY_FREQ == 0 : # Logger logging logger . info ( "Epoch {0}, [{1}/{2}]: Batch Time:{batch_time.val:.4f},\t Data Time:{data_time.val:.4f}, Whole Gen Loss:{whole_loss.val:.4f}\t," "Recon Loss:{r_loss.val:.4f},\t GAN Loss:{g_loss.val:.4f},\t D Loss:{d_loss.val:.4f}" . format ( epoch , i + 1 , len ( dataloader ) , batch_time = batch_time , data_time = data_time , whole_loss = losses [ 'whole_loss' ] , r_loss = losses [ 'r_loss' ] , g_loss = losses [ 'g_loss' ] , d_loss = losses [ 'd_loss' ] ) ) # Tensorboard logger for scaler and images info_terms = { 'WGLoss' : whole_loss . item ( ) , 'ReconLoss' : r_loss . item ( ) , "GANLoss" : g_loss . item ( ) , "DLoss" : d_loss . item ( ) } for tag , value in info_terms . items ( ) : tensorboardlogger . scalar_summary ( tag , value , epoch * len ( dataloader ) + i ) for tag , value in losses . items ( ) : tensorboardlogger . scalar_summary ( 'avg_' + tag , value . avg , epoch * len ( dataloader ) + i ) def img2photo ( imgs ) : return ( ( imgs + 1 ) * 127.5 ) . transpose ( 1 , 2 ) . transpose ( 2 , 3 ) . detach ( ) . cpu ( ) . numpy ( ) # info = { 'train/ori_imgs':img2photo(imgs), #          'train/coarse_imgs':img2photo(coarse_imgs), #          'train/recon_imgs':img2photo(recon_imgs), #          'train/comp_imgs':img2photo(complete_imgs), info = { 'train/whole_imgs' : img2photo ( torch . cat ( [ imgs * ( 1 - masks ) , coarse_imgs , recon_imgs , imgs , complete_imgs ] , dim = 3 ) ) } for tag , images in info . items ( ) : tensorboardlogger . image_summary ( tag , images , epoch * len ( dataloader ) + i ) if ( i + 1 ) % config . VAL_SUMMARY_FREQ == 0 and val_datas is not None : validate ( netG , netD , GANLoss , ReconLoss , DLoss , optG , optD , val_datas , epoch , device , batch_n = i ) netG . train ( ) netD . train ( ) end = time . time ( )