(netG, netD, GANLoss, ReconLoss, DLoss, optG, optD, dataloader, epoch, device=cuda0, batch_n="whole")
validate phase
validate phase
validate phase
def validate(netG, netD, GANLoss, ReconLoss, DLoss, optG, optD, dataloader, epoch, device=cuda0, batch_n="whole"):
    """
    validate phase
    """
    netG.to(device)
    netD.to(device)
    netG.eval()
    netD.eval()
    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()
    val_save_dir = os.path.join(result_dir, "val_{}_{}".format(epoch, batch_n if isinstance(batch_n, str) else batch_n+1))
    val_save_real_dir = os.path.join(val_save_dir, "real")
    val_save_gen_dir = os.path.join(val_save_dir, "gen")
    val_save_inf_dir = os.path.join(val_save_dir, "inf")
    if not os.path.exists(val_save_real_dir):
        os.makedirs(val_save_real_dir)
        os.makedirs(val_save_gen_dir)
        os.makedirs(val_save_inf_dir)
    info = {}
    for i, (imgs, masks) in enumerate(dataloader):

        data_time.update(time.time() - end)
        masks = masks['val']
        #masks = (masks > 0).type(torch.FloatTensor)

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

        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)


        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))

        d_loss = DLoss(pred_pos, pred_neg)
        losses['d_loss'].update(d_loss.item(), imgs.size(0))
        # Update time recorder
        batch_time.update(time.time() - end)


        # Logger logging


        if i+1 < config.STATIC_VIEW_SIZE:

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

        else:
            logger.info("Validation Epoch {0}, [{1}/{2}]: Batch Time:{batch_time.val:.4f},\t Data Time:{data_time.val:.4f},\t 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']))
            j = 0
            for tag, images in info.items():
                h, w = images.shape[1], images.shape[2] // 5
                for val_img in images:
                    real_img = val_img[:,(3*w):(4*w),:]
                    gen_img = val_img[:,(4*w):,:]
                    real_img = Image.fromarray(real_img.astype(np.uint8))
                    gen_img = Image.fromarray(gen_img.astype(np.uint8))
                    real_img.save(os.path.join(val_save_real_dir, "{}.png".format(j)))
                    gen_img.save(os.path.join(val_save_gen_dir, "{}.png".format(j)))
                    j += 1
                tensorboardlogger.image_summary(tag, images, epoch)
            path1, path2 = val_save_real_dir, val_save_gen_dir
            fid_score = metrics['fid']([path1, path2], cuda=False)
            ssim_score = metrics['ssim']([path1, path2])
            tensorboardlogger.scalar_summary('val/fid', fid_score.item(), epoch*len(dataloader)+i)
            tensorboardlogger.scalar_summary('val/ssim', ssim_score.item(), epoch*len(dataloader)+i)
            break

        end = time.time()
def validate ( netG , netD , GANLoss , ReconLoss , DLoss , optG , optD , dataloader , epoch , device = cuda0 , batch_n = "whole" ) : netG . to ( device ) netD . to ( device ) netG . eval ( ) netD . eval ( ) 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 ( ) val_save_dir = os . path . join ( result_dir , "val_{}_{}" . format ( epoch , batch_n if isinstance ( batch_n , str ) else batch_n + 1 ) ) val_save_real_dir = os . path . join ( val_save_dir , "real" ) val_save_gen_dir = os . path . join ( val_save_dir , "gen" ) val_save_inf_dir = os . path . join ( val_save_dir , "inf" ) if not os . path . exists ( val_save_real_dir ) : os . makedirs ( val_save_real_dir ) os . makedirs ( val_save_gen_dir ) os . makedirs ( val_save_inf_dir ) info = { } for i , ( imgs , masks ) in enumerate ( dataloader ) : data_time . update ( time . time ( ) - end ) masks = masks [ 'val' ] #masks = (masks > 0).type(torch.FloatTensor) imgs , masks = imgs . to ( device ) , masks . to ( device ) imgs = ( imgs / 127.5 - 1 ) # mask is 1 on masked region # forward coarse_imgs , recon_imgs = netG ( imgs , masks ) 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 ) 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 ) ) d_loss = DLoss ( pred_pos , pred_neg ) losses [ 'd_loss' ] . update ( d_loss . item ( ) , imgs . size ( 0 ) ) # Update time recorder batch_time . update ( time . time ( ) - end ) # Logger logging if i + 1 < config . STATIC_VIEW_SIZE : def img2photo ( imgs ) : return ( ( imgs + 1 ) * 127.5 ) . transpose ( 1 , 2 ) . transpose ( 2 , 3 ) . detach ( ) . cpu ( ) . numpy ( ) # info = { 'val/ori_imgs':img2photo(imgs), #          'val/coarse_imgs':img2photo(coarse_imgs), #          'val/recon_imgs':img2photo(recon_imgs), #          'val/comp_imgs':img2photo(complete_imgs), info [ 'val/whole_imgs/{}' . format ( i ) ] = img2photo ( torch . cat ( [ imgs * ( 1 - masks ) , coarse_imgs , recon_imgs , imgs , complete_imgs ] , dim = 3 ) ) else : logger . info ( "Validation Epoch {0}, [{1}/{2}]: Batch Time:{batch_time.val:.4f},\t Data Time:{data_time.val:.4f},\t 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' ] ) ) j = 0 for tag , images in info . items ( ) : h , w = images . shape [ 1 ] , images . shape [ 2 ] // 5 for val_img in images : real_img = val_img [ : , ( 3 * w ) : ( 4 * w ) , : ] gen_img = val_img [ : , ( 4 * w ) : , : ] real_img = Image . fromarray ( real_img . astype ( np . uint8 ) ) gen_img = Image . fromarray ( gen_img . astype ( np . uint8 ) ) real_img . save ( os . path . join ( val_save_real_dir , "{}.png" . format ( j ) ) ) gen_img . save ( os . path . join ( val_save_gen_dir , "{}.png" . format ( j ) ) ) j += 1 tensorboardlogger . image_summary ( tag , images , epoch ) path1 , path2 = val_save_real_dir , val_save_gen_dir fid_score = metrics [ 'fid' ] ( [ path1 , path2 ] , cuda = False ) ssim_score = metrics [ 'ssim' ] ( [ path1 , path2 ] ) tensorboardlogger . scalar_summary ( 'val/fid' , fid_score . item ( ) , epoch * len ( dataloader ) + i ) tensorboardlogger . scalar_summary ( 'val/ssim' , ssim_score . item ( ) , epoch * len ( dataloader ) + i ) break end = time . time ( )