From 5c5b78d0fb6859ddf39f1e878df33d701f221668 Mon Sep 17 00:00:00 2001 From: kagudkovArt Date: Wed, 5 Dec 2018 16:13:36 +0300 Subject: [PATCH 1/6] run code on local machine. Rewrite dump log images. I my opinion there are some bugs in code --- config/inpaint_places2_sagan.yml | 20 ++++++++++---------- config/test_places2_sagan.yml | 24 ++++++++++++------------ models/networks.py | 1 + models/sa_gan.py | 9 +++++---- test.py | 2 +- test_images.py | 26 ++++++++++++++++---------- train_sagan.py | 2 +- 7 files changed, 46 insertions(+), 38 deletions(-) diff --git a/config/inpaint_places2_sagan.yml b/config/inpaint_places2_sagan.yml index 07d9ba2..500d540 100644 --- a/config/inpaint_places2_sagan.yml +++ b/config/inpaint_places2_sagan.yml @@ -79,8 +79,8 @@ DATA_FLIST: ] # http://places2.csail.mit.edu/, please download the high-resolution dataset and use RANDOM_CROP: True places2: [ - '/home/lhy/datasets/Places2/train_flist.txt', - '/home/lhy/datasets/InpaintBenchmark/val_img_flist.txt' + '/home/kagudkov/PycharmProjects/GatedConvolution_pytorch/InpaintBenchmark/Places2/train_flist.txt', + '/home/kagudkov/PycharmProjects/GatedConvolution_pytorch/InpaintBenchmark/InpaintBenchmark/val_img_flist.txt' ] # http://www.image-net.org/, please use RANDOM_CROP: True imagenet: [ @@ -90,24 +90,24 @@ DATA_FLIST: irrmask: random_free_form: [ - '/home/lhy/datasets/InpaintBenchmark/MaskData/val_ff_mask_flist.txt', - '/home/lhy/datasets/InpaintBenchmark/MaskData/val_ff_mask_flist.txt' + '/home/kagudkov/PycharmProjects/GatedConvolution_pytorch/InpaintBenchmark/InpaintBenchmark/MaskData/val_ff_mask_flist.txt', + '/home/kagudkov/PycharmProjects/GatedConvolution_pytorch/InpaintBenchmark/InpaintBenchmark/MaskData/val_ff_mask_flist.txt' ] val: [ - '/home/lhy/datasets/InpaintBenchmark/MaskData/val_ff_mask_flist.txt', - '/home/lhy/datasets/InpaintBenchmark/MaskData/val_ff_mask_flist.txt' + '/home/kagudkov/PycharmProjects/GatedConvolution_pytorch/InpaintBenchmark/InpaintBenchmark/MaskData/val_ff_mask_flist.txt', + '/home/kagudkov/PycharmProjects/GatedConvolution_pytorch/InpaintBenchmark/InpaintBenchmark/MaskData/val_ff_mask_flist.txt' ] rectmask: random: [ - '/home/lhy/datasets/InpaintBenchmark/MaskData/val_rect_mask_flist.txt', - '/home/lhy/datasets/InpaintBenchmark/MaskData/val_rect_mask_flist.txt' + '/home/kagudkov/PycharmProjects/GatedConvolution_pytorch/InpaintBenchmark/InpaintBenchmark/MaskData/val_rect_100_mask_flist.txt', + '/home/kagudkov/PycharmProjects/GatedConvolution_pytorch/InpaintBenchmark/InpaintBenchmark/MaskData/val_rect_100_mask_flist.txt' ] val: [ - '/home/lhy/datasets/InpaintBenchmark/MaskData/val_rect_mask_flist.txt', - '/home/lhy/datasets/InpaintBenchmark/MaskData/val_rect_mask_flist.txt' + '/home/kagudkov/PycharmProjects/GatedConvolution_pytorch/InpaintBenchmark/InpaintBenchmark/MaskData/val_rect_100_mask_flist.txt', + '/home/kagudkov/PycharmProjects/GatedConvolution_pytorch/InpaintBenchmark/InpaintBenchmark/MaskData/val_rect_100_mask_flist.txt' ] horse: [ '/unsullied/sharefs/linhangyu/Inpainting/Data/VOCData/voc_horse_train_flist.txt', diff --git a/config/test_places2_sagan.yml b/config/test_places2_sagan.yml index 2ea5774..fa0b7bb 100644 --- a/config/test_places2_sagan.yml +++ b/config/test_places2_sagan.yml @@ -18,7 +18,7 @@ GAN: 'sn_pgan' # 'dcgan', 'lsgan', 'wgan_gp', 'one_wgan_gp' IMG_SHAPES: [256, 256] RANDOM_BBOX_SHAPE: [32, 32] RANDOM_BBOX_MARGIN: [64, 64] -BATCH_SIZE: 6 +BATCH_SIZE: 2 RANDOM_SEED: False PADDING: 'SAME' BATCH_NORM: True @@ -37,7 +37,7 @@ RANDOM_FF_SETTING: # training NUM_GPUS: 1 -GPU_IDS: [0,1] # -1 indicate select any available one, otherwise select gpu ID, e.g. [0,1,3] +GPU_IDS: [0,0] # -1 indicate select any available one, otherwise select gpu ID, e.g. [0,1,3] EPOCH: 50 SUMMARY_FREQ: 100 VAL_SUMMARY_FREQ: 10000 @@ -85,8 +85,8 @@ DATA_FLIST: ] # http://places2.csail.mit.edu/, please download the high-resolution dataset and use RANDOM_CROP: True places2: [ - '/home/lhy/datasets/Places2/trainsmall_flist.txt', - '/home/lhy/datasets/InpaintBenchmark/test_show_img_flist.txt' + 'InpaintBenchmark/Places2/trainsmall_flist.txt', + '/home/kagudkov/PycharmProjects/GatedConvolution_pytorch/InpaintBenchmark/InpaintBenchmark/val_img_flist.txt' ] # http://www.image-net.org/, please use RANDOM_CROP: True imagenet: [ @@ -96,24 +96,24 @@ DATA_FLIST: irrmask: random_free_form: [ - '/home/lhy/datasets/InpaintBenchmark/MaskData/val_ff_mask_flist.txt', - '/home/lhy/datasets/InpaintBenchmark/MaskData/val_ff_mask_flist.txt' + 'InpaintBenchmark/InpaintBenchmark/MaskData/val_ff_mask_img_flist.txt', + 'InpaintBenchmark/InpaintBenchmark/MaskData/val_ff_mask_flist.txt' ] val: [ - '/home/lhy/datasets/InpaintBenchmark/MaskData/val_ff_mask_flist.txt', - '/home/lhy/datasets/InpaintBenchmark/MaskData/val_ff_mask_flist.txt' + 'InpaintBenchmark/InpaintBenchmark/MaskData/val_ff_mask_flist.txt', + 'InpaintBenchmark/InpaintBenchmark/MaskData/val_ff_mask_flist.txt' ] rectmask: random: [ - '/home/lhy/datasets/InpaintBenchmark/MaskData/val_rect_mask_flist.txt', - '/home/lhy/datasets/InpaintBenchmark/MaskData/val_rect_mask_flist.txt' + 'InpaintBenchmark/InpaintBenchmark/MaskData/val_rect_100_mask_flist.txt', + 'InpaintBenchmark/InpaintBenchmark/MaskData/val_rect_100_mask_flist.txt' ] val: [ - '/home/lhy/datasets/InpaintBenchmark/MaskData/val_rect_mask_flist.txt', - '/home/lhy/datasets/InpaintBenchmark/MaskData/val_rect_mask_flist.txt' + 'InpaintBenchmark/InpaintBenchmark/MaskData/val_rect_100_mask_flist.txt', + 'InpaintBenchmark/InpaintBenchmark/MaskData/val_rect_100_mask_flist.txt' ] horse: [ '/unsullied/sharefs/linhangyu/Inpainting/Data/VOCData/voc_horse_train_flist.txt', diff --git a/models/networks.py b/models/networks.py index d84ad63..0a4448c 100644 --- a/models/networks.py +++ b/models/networks.py @@ -26,6 +26,7 @@ def init_func(m): print('initialize network with %s' % init_type) net.apply(init_func) + def get_pad(in_, ksize, stride, atrous=1): out_ = np.ceil(float(in_)/stride) return int(((out_ - 1) * stride + atrous*(ksize-1) + 1 - in_)/2) diff --git a/models/sa_gan.py b/models/sa_gan.py index b8f9f93..51d725a 100644 --- a/models/sa_gan.py +++ b/models/sa_gan.py @@ -46,7 +46,7 @@ class SAGenerator(nn.Module): """Generator.""" def __init__(self, batch_size, image_size=64, z_dim=100, conv_dim=64): - super(Generator, self).__init__() + super(SAGenerator, self).__init__() self.imsize = image_size layer1 = [] layer2 = [] @@ -190,11 +190,12 @@ def forward(self, imgs, masks, img_exs=None): else: input_imgs = torch.cat([masked_imgs, img_exs, masks, torch.full_like(masks, 1.)], dim=1) x = self.refine_conv_net(input_imgs) - x= self.refine_attn(x) + x = self.refine_attn(x) + x_attn = x #print(x.size(), attention.size()) x = self.refine_upsample_net(x) x = torch.clamp(x, -1., 1.) - return coarse_x, x + return coarse_x, x, x_attn class InpaintSADirciminator(nn.Module): def __init__(self): @@ -223,7 +224,7 @@ class SADiscriminator(nn.Module): """Discriminator, Auxiliary Classifier.""" def __init__(self, batch_size=64, image_size=64, conv_dim=64): - super(Discriminator, self).__init__() + super(SADiscriminator, self).__init__() self.imsize = image_size layer1 = [] layer2 = [] diff --git a/test.py b/test.py index 323d023..7705d7e 100644 --- a/test.py +++ b/test.py @@ -2,7 +2,7 @@ from evaluation import metrics from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter import os -#os.environ['CUDA_VISIBLE_DEVICES'] = '3' +os.environ['CUDA_VISIBLE_DEVICES'] = '0' parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter) parser.add_argument('path', type=str, nargs=2, diff --git a/test_images.py b/test_images.py index ce04ea3..23082ce 100644 --- a/test_images.py +++ b/test_images.py @@ -89,6 +89,7 @@ def validate(nets, loss_terms, opts, dataloader, epoch, network_type, devices=(c masks = ori_masks['val'] masks = F.interpolate(masks, size) + saved_mask = masks masks = (masks > 0).type(torch.FloatTensor) imgs = F.interpolate(ori_imgs, size) if imgs.size(1) != 3: @@ -107,7 +108,7 @@ def validate(nets, loss_terms, opts, dataloader, epoch, network_type, devices=(c elif network_type == 'l2h_gated': recon_imgs = netG(imgs, masks, pre_inter_imgs) elif network_type == 'sa_gated': - recon_imgs, _ = netG(imgs, masks) + recon_imgs, _, _ = netG.forward(imgs, masks) complete_imgs = recon_imgs * masks + imgs * (1 - masks) @@ -115,11 +116,11 @@ def validate(nets, loss_terms, opts, dataloader, epoch, network_type, devices=(c neg_imgs = torch.cat([recon_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) + # pred_pos_neg = netD(pos_neg_imgs) + # pred_pos, pred_neg = torch.chunk(pred_pos_neg, 2, dim=0) - g_loss = GANLoss(pred_neg) + # g_loss = GANLoss(pred_neg) r_loss = ReconLoss(imgs, recon_imgs, recon_imgs, masks) @@ -132,14 +133,14 @@ def validate(nets, loss_terms, opts, dataloader, epoch, network_type, devices=(c whole_loss = r_loss + p_loss #g_loss + r_loss # Update the recorder for losses - losses['g_loss'].update(g_loss.item(), imgs.size(0)) + # losses['g_loss'].update(g_loss.item(), imgs.size(0)) losses['r_loss'].update(r_loss.item(), imgs.size(0)) losses['p_loss'].update(p_loss.item(), imgs.size(0)) losses['s_loss'].update(s_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)) + # d_loss = DLoss(pred_pos, pred_neg) + # losses['d_loss'].update(d_loss.item(), imgs.size(0)) pre_complete_imgs = complete_imgs # Update time recorder batch_time.update(time.time() - end) @@ -153,11 +154,16 @@ def validate(nets, loss_terms, opts, dataloader, epoch, network_type, devices=(c real_img = img2photo(imgs) gen_img = img2photo(recon_imgs) comp_img = img2photo(complete_imgs) + res_image = Image.fromarray(np.hstack((real_img[0].astype(np.uint8), + gen_img[0].astype(np.uint8), + comp_img[0].astype(np.uint8), + np.asarray(saved_mask[0, 0, :, :, None].expand(-1, -1, 3) * 255).astype(np.uint8) + ))) real_img = Image.fromarray(real_img[0].astype(np.uint8)) gen_img = Image.fromarray(gen_img[0].astype(np.uint8)) comp_img = Image.fromarray(comp_img[0].astype(np.uint8)) - real_img.save(os.path.join(val_save_real_dir, SIZES_TAGS[s_i], "{}.png".format(i))) + res_image.save(os.path.join(val_save_real_dir, SIZES_TAGS[s_i], "{}.png".format(i))) gen_img.save(os.path.join(val_save_gen_dir, SIZES_TAGS[s_i], "{}.png".format(i))) comp_img.save(os.path.join(val_save_comp_dir, SIZES_TAGS[s_i], "{}.png".format(i))) @@ -178,7 +184,7 @@ def main(): random_ff_setting=config.RANDOM_FF_SETTING) val_loader = val_dataset.loader(batch_size=1, shuffle=False, num_workers=1) - #print(len(val_loader)) + print(len(val_loader)) ### Generate a new val data @@ -238,7 +244,7 @@ def main(): # Start Training logger.info("Start Validation") - validate(nets, losses, opts, val_loader,0 , config.NETWORK_TYPE,devices=(cuda0,cuda1)) + validate(nets, losses, opts, val_loader,0 , config.NETWORK_TYPE,devices=(cuda0,cuda0)) if __name__ == '__main__': main() diff --git a/train_sagan.py b/train_sagan.py index e0ca583..e9886b2 100644 --- a/train_sagan.py +++ b/train_sagan.py @@ -74,7 +74,7 @@ def validate(netG, netD, GANLoss, ReconLoss, DLoss, optG, optD, dataloader, epoc imgs = (imgs / 127.5 - 1) # mask is 1 on masked region # forward - coarse_imgs, recon_imgs, attention = netG(imgs, masks) + coarse_imgs, recon_imgs, attention = netG.forward(imgs, masks) complete_imgs = recon_imgs * masks + imgs * (1 - masks) From cdcbb3350e9d7399f459877693586d9cf065a3f3 Mon Sep 17 00:00:00 2001 From: kagudkovArt Date: Wed, 5 Dec 2018 17:58:38 +0300 Subject: [PATCH 2/6] add script for flist creation --- config/inpaint_places2_sagan.yml | 11 +++-- config/test_places2_sagan.yml | 3 +- prepare_data.py | 76 ++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 4 deletions(-) create mode 100644 prepare_data.py diff --git a/config/inpaint_places2_sagan.yml b/config/inpaint_places2_sagan.yml index 500d540..da12149 100644 --- a/config/inpaint_places2_sagan.yml +++ b/config/inpaint_places2_sagan.yml @@ -1,8 +1,8 @@ #Dataset and Loading Setting -DATASET: 'places2' # 'tmnist', 'dtd', 'places2', 'celeba', 'imagenet', 'cityscapes' +DATASET: 'bald' # 'bald', 'tmnist', 'dtd', 'places2', 'celeba', 'imagenet', 'cityscapes' MASKDATASET: 'irrmask' MASK_TYPES: ['random_free_form'] -RANDOM_CROP: False +RANDOM_CROP: True MASKFROMFILE: False LOG_DIR: full_model_places2_sa_bn_256 MODEL_RESTORE: '' #'201809200859_full_model_places2_256/epoch_1_ckpt.pth.tar' # '20180115220926508503_jyugpu0_places2_NORMAL_wgan_gp_full_model' @@ -16,7 +16,7 @@ RANDOM_BBOX_MARGIN: [64, 64] BATCH_SIZE: 4 RANDOM_SEED: False PADDING: 'SAME' -BATCH_NORM: True +BATCH_NORM: False #random free_form settings MAXVERTEX: 5 MAXANGLE: 4.0 #pi @@ -113,6 +113,11 @@ DATA_FLIST: '/unsullied/sharefs/linhangyu/Inpainting/Data/VOCData/voc_horse_train_flist.txt', '/unsullied/sharefs/linhangyu/Inpainting/Data/VOCData/voc_horse_val_flist.txt' ] + bald: [ + '/home/kagudkov/PycharmProjects/GatedConvolution_pytorch/data/bald/train_shuffled.flist', + '/home/kagudkov/PycharmProjects/GatedConvolution_pytorch/data/bald/validation_shuffled.flist' + ] + horse_mask: [ '/unsullied/sharefs/linhangyu/Inpainting/Data/VOCData/voc_horse_bbox_train_flist.txt', '/unsullied/sharefs/linhangyu/Inpainting/Data/VOCData/voc_horse_bbox_val_flist.txt' diff --git a/config/test_places2_sagan.yml b/config/test_places2_sagan.yml index fa0b7bb..883826c 100644 --- a/config/test_places2_sagan.yml +++ b/config/test_places2_sagan.yml @@ -5,7 +5,8 @@ MASK_TYPES: ['random_free_form'] RANDOM_CROP: False MASKFROMFILE: False LOG_DIR: small_model_places2_sa_bn_l2h_unet_perc_style_nocon_256 -MODEL_RESTORE: 201810162252_full_model_places2_sa_bn_256/latest_ckpt.pth.tar +MODEL_RESTORE: 201812051742_full_model_places2_sa_bn_256/latest_ckpt.pth.tar + # 201810162252_full_model_places2_sa_bn_256/latest_ckpt.pth.tar #201811041826_full_model_places2_sa_bn_l2h_unet_256/latest_ckpt.pth.tar #201810162252_full_model_places2_sa_bn_256/latest_ckpt.pth.tar # diff --git a/prepare_data.py b/prepare_data.py new file mode 100644 index 0000000..b0b04bf --- /dev/null +++ b/prepare_data.py @@ -0,0 +1,76 @@ +#!/usr/bin/python + +import argparse +import os +from random import shuffle + +parser = argparse.ArgumentParser() +parser.add_argument('--folder_path', default='./data', type=str, + help='The folder path') +parser.add_argument('--train_filename', default='./data_flist_baldalligned/train_shuffled.flist', type=str, + help='The output filename.') +parser.add_argument('--validation_filename', default='./data_flist_baldalligned/validation_shuffled.flist', type=str, + help='The output filename.') +parser.add_argument('--is_shuffled', default='1', type=int, + help='Needed to shuffle') + +if __name__ == "__main__": + + args = parser.parse_args() + + # get the list of directories + dirs = [args.folder_path] + dirs_name_list = [] + + # make 2 lists to save file paths + training_file_names = [] + validation_file_names = [] + + # print all directory names + for dir_item in dirs: + # modify to full path -> directory + dir_item = args.folder_path# + "/" + dir_item + # print(dir_item) + + training_folder = os.listdir(dir_item + "/training") + for training_item in training_folder: + if training_item[-4:] != '.bmp': + training_item = dir_item + "/training" + "/" + training_item + training_file_names.append(training_item) + + validation_folder = os.listdir(dir_item + "/validation") + for validation_item in validation_folder: + if validation_item[-4:] != '.bmp': + validation_item = dir_item + "/validation" + "/" + validation_item + validation_file_names.append(validation_item) + # print all file paths + for i in training_file_names: + print(i) + for i in validation_file_names: + print(i) + + # This would print all the files and directories + + # shuffle file names if set + if args.is_shuffled == 1: + shuffle(training_file_names) + shuffle(validation_file_names) + + # make output file if not existed + if not os.path.exists(args.train_filename): + os.mknod(args.train_filename) + + if not os.path.exists(args.validation_filename): + os.mknod(args.validation_filename) + + # write to file + fo = open(args.train_filename, "w") + fo.write("\n".join(training_file_names)) + fo.close() + + fo = open(args.validation_filename, "w") + fo.write("\n".join(validation_file_names)) + fo.close() + + # print process + print("Written file is: ", args.train_filename, ", is_shuffle: ", args.is_shuffled) \ No newline at end of file From 725f2fa1b643c2c8fd1b61709084bcb92e05eaab Mon Sep 17 00:00:00 2001 From: kagudkovArt Date: Wed, 5 Dec 2018 21:59:45 +0300 Subject: [PATCH 3/6] fix scale factor in GatedDeConv2D --- models/networks.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/models/networks.py b/models/networks.py index 0a4448c..d5a7380 100644 --- a/models/networks.py +++ b/models/networks.py @@ -54,6 +54,7 @@ def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0, def gated(self, mask): #return torch.clamp(mask, -1, 1) return self.sigmoid(mask) + def forward(self, input): x = self.conv2d(input) mask = self.mask_conv2d(input) @@ -81,7 +82,7 @@ def __init__(self, scale_factor, in_channels, out_channels, kernel_size, stride= def forward(self, input): #print(input.size()) - x = F.interpolate(input, scale_factor=2) + x = F.interpolate(input, scale_factor=self.scale_factor) return self.conv2d(x) class SNGatedConv2dWithActivation(torch.nn.Module): From 22486304472df174fe1ca504fc89527271b78c41 Mon Sep 17 00:00:00 2001 From: kagudkovArt Date: Wed, 5 Dec 2018 22:02:59 +0300 Subject: [PATCH 4/6] Revert "run code on local machine. Rewrite dump log images. I my opinion there are some bugs in code" This reverts commit 5c5b78d0 --- config/inpaint_places2_sagan.yml | 20 ++++++++++---------- config/test_places2_sagan.yml | 24 ++++++++++++------------ models/networks.py | 1 - models/sa_gan.py | 9 ++++----- test.py | 2 +- test_images.py | 26 ++++++++++---------------- train_sagan.py | 2 +- 7 files changed, 38 insertions(+), 46 deletions(-) diff --git a/config/inpaint_places2_sagan.yml b/config/inpaint_places2_sagan.yml index da12149..7ea3ccb 100644 --- a/config/inpaint_places2_sagan.yml +++ b/config/inpaint_places2_sagan.yml @@ -79,8 +79,8 @@ DATA_FLIST: ] # http://places2.csail.mit.edu/, please download the high-resolution dataset and use RANDOM_CROP: True places2: [ - '/home/kagudkov/PycharmProjects/GatedConvolution_pytorch/InpaintBenchmark/Places2/train_flist.txt', - '/home/kagudkov/PycharmProjects/GatedConvolution_pytorch/InpaintBenchmark/InpaintBenchmark/val_img_flist.txt' + '/home/lhy/datasets/Places2/train_flist.txt', + '/home/lhy/datasets/InpaintBenchmark/val_img_flist.txt' ] # http://www.image-net.org/, please use RANDOM_CROP: True imagenet: [ @@ -90,24 +90,24 @@ DATA_FLIST: irrmask: random_free_form: [ - '/home/kagudkov/PycharmProjects/GatedConvolution_pytorch/InpaintBenchmark/InpaintBenchmark/MaskData/val_ff_mask_flist.txt', - '/home/kagudkov/PycharmProjects/GatedConvolution_pytorch/InpaintBenchmark/InpaintBenchmark/MaskData/val_ff_mask_flist.txt' + '/home/lhy/datasets/InpaintBenchmark/MaskData/val_ff_mask_flist.txt', + '/home/lhy/datasets/InpaintBenchmark/MaskData/val_ff_mask_flist.txt' ] val: [ - '/home/kagudkov/PycharmProjects/GatedConvolution_pytorch/InpaintBenchmark/InpaintBenchmark/MaskData/val_ff_mask_flist.txt', - '/home/kagudkov/PycharmProjects/GatedConvolution_pytorch/InpaintBenchmark/InpaintBenchmark/MaskData/val_ff_mask_flist.txt' + '/home/lhy/datasets/InpaintBenchmark/MaskData/val_ff_mask_flist.txt', + '/home/lhy/datasets/InpaintBenchmark/MaskData/val_ff_mask_flist.txt' ] rectmask: random: [ - '/home/kagudkov/PycharmProjects/GatedConvolution_pytorch/InpaintBenchmark/InpaintBenchmark/MaskData/val_rect_100_mask_flist.txt', - '/home/kagudkov/PycharmProjects/GatedConvolution_pytorch/InpaintBenchmark/InpaintBenchmark/MaskData/val_rect_100_mask_flist.txt' + '/home/lhy/datasets/InpaintBenchmark/MaskData/val_rect_mask_flist.txt', + '/home/lhy/datasets/InpaintBenchmark/MaskData/val_rect_mask_flist.txt' ] val: [ - '/home/kagudkov/PycharmProjects/GatedConvolution_pytorch/InpaintBenchmark/InpaintBenchmark/MaskData/val_rect_100_mask_flist.txt', - '/home/kagudkov/PycharmProjects/GatedConvolution_pytorch/InpaintBenchmark/InpaintBenchmark/MaskData/val_rect_100_mask_flist.txt' + '/home/lhy/datasets/InpaintBenchmark/MaskData/val_rect_mask_flist.txt', + '/home/lhy/datasets/InpaintBenchmark/MaskData/val_rect_mask_flist.txt' ] horse: [ '/unsullied/sharefs/linhangyu/Inpainting/Data/VOCData/voc_horse_train_flist.txt', diff --git a/config/test_places2_sagan.yml b/config/test_places2_sagan.yml index 883826c..99beed8 100644 --- a/config/test_places2_sagan.yml +++ b/config/test_places2_sagan.yml @@ -19,7 +19,7 @@ GAN: 'sn_pgan' # 'dcgan', 'lsgan', 'wgan_gp', 'one_wgan_gp' IMG_SHAPES: [256, 256] RANDOM_BBOX_SHAPE: [32, 32] RANDOM_BBOX_MARGIN: [64, 64] -BATCH_SIZE: 2 +BATCH_SIZE: 6 RANDOM_SEED: False PADDING: 'SAME' BATCH_NORM: True @@ -38,7 +38,7 @@ RANDOM_FF_SETTING: # training NUM_GPUS: 1 -GPU_IDS: [0,0] # -1 indicate select any available one, otherwise select gpu ID, e.g. [0,1,3] +GPU_IDS: [0,1] # -1 indicate select any available one, otherwise select gpu ID, e.g. [0,1,3] EPOCH: 50 SUMMARY_FREQ: 100 VAL_SUMMARY_FREQ: 10000 @@ -86,8 +86,8 @@ DATA_FLIST: ] # http://places2.csail.mit.edu/, please download the high-resolution dataset and use RANDOM_CROP: True places2: [ - 'InpaintBenchmark/Places2/trainsmall_flist.txt', - '/home/kagudkov/PycharmProjects/GatedConvolution_pytorch/InpaintBenchmark/InpaintBenchmark/val_img_flist.txt' + '/home/lhy/datasets/Places2/trainsmall_flist.txt', + '/home/lhy/datasets/InpaintBenchmark/test_show_img_flist.txt' ] # http://www.image-net.org/, please use RANDOM_CROP: True imagenet: [ @@ -97,24 +97,24 @@ DATA_FLIST: irrmask: random_free_form: [ - 'InpaintBenchmark/InpaintBenchmark/MaskData/val_ff_mask_img_flist.txt', - 'InpaintBenchmark/InpaintBenchmark/MaskData/val_ff_mask_flist.txt' + '/home/lhy/datasets/InpaintBenchmark/MaskData/val_ff_mask_flist.txt', + '/home/lhy/datasets/InpaintBenchmark/MaskData/val_ff_mask_flist.txt' ] val: [ - 'InpaintBenchmark/InpaintBenchmark/MaskData/val_ff_mask_flist.txt', - 'InpaintBenchmark/InpaintBenchmark/MaskData/val_ff_mask_flist.txt' + '/home/lhy/datasets/InpaintBenchmark/MaskData/val_ff_mask_flist.txt', + '/home/lhy/datasets/InpaintBenchmark/MaskData/val_ff_mask_flist.txt' ] rectmask: random: [ - 'InpaintBenchmark/InpaintBenchmark/MaskData/val_rect_100_mask_flist.txt', - 'InpaintBenchmark/InpaintBenchmark/MaskData/val_rect_100_mask_flist.txt' + '/home/lhy/datasets/InpaintBenchmark/MaskData/val_rect_mask_flist.txt', + '/home/lhy/datasets/InpaintBenchmark/MaskData/val_rect_mask_flist.txt' ] val: [ - 'InpaintBenchmark/InpaintBenchmark/MaskData/val_rect_100_mask_flist.txt', - 'InpaintBenchmark/InpaintBenchmark/MaskData/val_rect_100_mask_flist.txt' + '/home/lhy/datasets/InpaintBenchmark/MaskData/val_rect_mask_flist.txt', + '/home/lhy/datasets/InpaintBenchmark/MaskData/val_rect_mask_flist.txt' ] horse: [ '/unsullied/sharefs/linhangyu/Inpainting/Data/VOCData/voc_horse_train_flist.txt', diff --git a/models/networks.py b/models/networks.py index d5a7380..976e9db 100644 --- a/models/networks.py +++ b/models/networks.py @@ -26,7 +26,6 @@ def init_func(m): print('initialize network with %s' % init_type) net.apply(init_func) - def get_pad(in_, ksize, stride, atrous=1): out_ = np.ceil(float(in_)/stride) return int(((out_ - 1) * stride + atrous*(ksize-1) + 1 - in_)/2) diff --git a/models/sa_gan.py b/models/sa_gan.py index 51d725a..b8f9f93 100644 --- a/models/sa_gan.py +++ b/models/sa_gan.py @@ -46,7 +46,7 @@ class SAGenerator(nn.Module): """Generator.""" def __init__(self, batch_size, image_size=64, z_dim=100, conv_dim=64): - super(SAGenerator, self).__init__() + super(Generator, self).__init__() self.imsize = image_size layer1 = [] layer2 = [] @@ -190,12 +190,11 @@ def forward(self, imgs, masks, img_exs=None): else: input_imgs = torch.cat([masked_imgs, img_exs, masks, torch.full_like(masks, 1.)], dim=1) x = self.refine_conv_net(input_imgs) - x = self.refine_attn(x) - x_attn = x + x= self.refine_attn(x) #print(x.size(), attention.size()) x = self.refine_upsample_net(x) x = torch.clamp(x, -1., 1.) - return coarse_x, x, x_attn + return coarse_x, x class InpaintSADirciminator(nn.Module): def __init__(self): @@ -224,7 +223,7 @@ class SADiscriminator(nn.Module): """Discriminator, Auxiliary Classifier.""" def __init__(self, batch_size=64, image_size=64, conv_dim=64): - super(SADiscriminator, self).__init__() + super(Discriminator, self).__init__() self.imsize = image_size layer1 = [] layer2 = [] diff --git a/test.py b/test.py index 7705d7e..323d023 100644 --- a/test.py +++ b/test.py @@ -2,7 +2,7 @@ from evaluation import metrics from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter import os -os.environ['CUDA_VISIBLE_DEVICES'] = '0' +#os.environ['CUDA_VISIBLE_DEVICES'] = '3' parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter) parser.add_argument('path', type=str, nargs=2, diff --git a/test_images.py b/test_images.py index 23082ce..ce04ea3 100644 --- a/test_images.py +++ b/test_images.py @@ -89,7 +89,6 @@ def validate(nets, loss_terms, opts, dataloader, epoch, network_type, devices=(c masks = ori_masks['val'] masks = F.interpolate(masks, size) - saved_mask = masks masks = (masks > 0).type(torch.FloatTensor) imgs = F.interpolate(ori_imgs, size) if imgs.size(1) != 3: @@ -108,7 +107,7 @@ def validate(nets, loss_terms, opts, dataloader, epoch, network_type, devices=(c elif network_type == 'l2h_gated': recon_imgs = netG(imgs, masks, pre_inter_imgs) elif network_type == 'sa_gated': - recon_imgs, _, _ = netG.forward(imgs, masks) + recon_imgs, _ = netG(imgs, masks) complete_imgs = recon_imgs * masks + imgs * (1 - masks) @@ -116,11 +115,11 @@ def validate(nets, loss_terms, opts, dataloader, epoch, network_type, devices=(c neg_imgs = torch.cat([recon_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) + pred_pos_neg = netD(pos_neg_imgs) + pred_pos, pred_neg = torch.chunk(pred_pos_neg, 2, dim=0) - # g_loss = GANLoss(pred_neg) + g_loss = GANLoss(pred_neg) r_loss = ReconLoss(imgs, recon_imgs, recon_imgs, masks) @@ -133,14 +132,14 @@ def validate(nets, loss_terms, opts, dataloader, epoch, network_type, devices=(c whole_loss = r_loss + p_loss #g_loss + r_loss # Update the recorder for losses - # losses['g_loss'].update(g_loss.item(), imgs.size(0)) + losses['g_loss'].update(g_loss.item(), imgs.size(0)) losses['r_loss'].update(r_loss.item(), imgs.size(0)) losses['p_loss'].update(p_loss.item(), imgs.size(0)) losses['s_loss'].update(s_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)) + d_loss = DLoss(pred_pos, pred_neg) + losses['d_loss'].update(d_loss.item(), imgs.size(0)) pre_complete_imgs = complete_imgs # Update time recorder batch_time.update(time.time() - end) @@ -154,16 +153,11 @@ def validate(nets, loss_terms, opts, dataloader, epoch, network_type, devices=(c real_img = img2photo(imgs) gen_img = img2photo(recon_imgs) comp_img = img2photo(complete_imgs) - res_image = Image.fromarray(np.hstack((real_img[0].astype(np.uint8), - gen_img[0].astype(np.uint8), - comp_img[0].astype(np.uint8), - np.asarray(saved_mask[0, 0, :, :, None].expand(-1, -1, 3) * 255).astype(np.uint8) - ))) real_img = Image.fromarray(real_img[0].astype(np.uint8)) gen_img = Image.fromarray(gen_img[0].astype(np.uint8)) comp_img = Image.fromarray(comp_img[0].astype(np.uint8)) - res_image.save(os.path.join(val_save_real_dir, SIZES_TAGS[s_i], "{}.png".format(i))) + real_img.save(os.path.join(val_save_real_dir, SIZES_TAGS[s_i], "{}.png".format(i))) gen_img.save(os.path.join(val_save_gen_dir, SIZES_TAGS[s_i], "{}.png".format(i))) comp_img.save(os.path.join(val_save_comp_dir, SIZES_TAGS[s_i], "{}.png".format(i))) @@ -184,7 +178,7 @@ def main(): random_ff_setting=config.RANDOM_FF_SETTING) val_loader = val_dataset.loader(batch_size=1, shuffle=False, num_workers=1) - print(len(val_loader)) + #print(len(val_loader)) ### Generate a new val data @@ -244,7 +238,7 @@ def main(): # Start Training logger.info("Start Validation") - validate(nets, losses, opts, val_loader,0 , config.NETWORK_TYPE,devices=(cuda0,cuda0)) + validate(nets, losses, opts, val_loader,0 , config.NETWORK_TYPE,devices=(cuda0,cuda1)) if __name__ == '__main__': main() diff --git a/train_sagan.py b/train_sagan.py index e9886b2..e0ca583 100644 --- a/train_sagan.py +++ b/train_sagan.py @@ -74,7 +74,7 @@ def validate(netG, netD, GANLoss, ReconLoss, DLoss, optG, optD, dataloader, epoc imgs = (imgs / 127.5 - 1) # mask is 1 on masked region # forward - coarse_imgs, recon_imgs, attention = netG.forward(imgs, masks) + coarse_imgs, recon_imgs, attention = netG(imgs, masks) complete_imgs = recon_imgs * masks + imgs * (1 - masks) From 037a1152ba3469fa02b2aef4948f6e09683f2f54 Mon Sep 17 00:00:00 2001 From: kagudkovArt Date: Wed, 5 Dec 2018 22:07:13 +0300 Subject: [PATCH 5/6] Revert "add script for flist creation" This reverts commit cdcbb335 --- prepare_data.py | 76 ------------------------------------------------- 1 file changed, 76 deletions(-) delete mode 100644 prepare_data.py diff --git a/prepare_data.py b/prepare_data.py deleted file mode 100644 index b0b04bf..0000000 --- a/prepare_data.py +++ /dev/null @@ -1,76 +0,0 @@ -#!/usr/bin/python - -import argparse -import os -from random import shuffle - -parser = argparse.ArgumentParser() -parser.add_argument('--folder_path', default='./data', type=str, - help='The folder path') -parser.add_argument('--train_filename', default='./data_flist_baldalligned/train_shuffled.flist', type=str, - help='The output filename.') -parser.add_argument('--validation_filename', default='./data_flist_baldalligned/validation_shuffled.flist', type=str, - help='The output filename.') -parser.add_argument('--is_shuffled', default='1', type=int, - help='Needed to shuffle') - -if __name__ == "__main__": - - args = parser.parse_args() - - # get the list of directories - dirs = [args.folder_path] - dirs_name_list = [] - - # make 2 lists to save file paths - training_file_names = [] - validation_file_names = [] - - # print all directory names - for dir_item in dirs: - # modify to full path -> directory - dir_item = args.folder_path# + "/" + dir_item - # print(dir_item) - - training_folder = os.listdir(dir_item + "/training") - for training_item in training_folder: - if training_item[-4:] != '.bmp': - training_item = dir_item + "/training" + "/" + training_item - training_file_names.append(training_item) - - validation_folder = os.listdir(dir_item + "/validation") - for validation_item in validation_folder: - if validation_item[-4:] != '.bmp': - validation_item = dir_item + "/validation" + "/" + validation_item - validation_file_names.append(validation_item) - # print all file paths - for i in training_file_names: - print(i) - for i in validation_file_names: - print(i) - - # This would print all the files and directories - - # shuffle file names if set - if args.is_shuffled == 1: - shuffle(training_file_names) - shuffle(validation_file_names) - - # make output file if not existed - if not os.path.exists(args.train_filename): - os.mknod(args.train_filename) - - if not os.path.exists(args.validation_filename): - os.mknod(args.validation_filename) - - # write to file - fo = open(args.train_filename, "w") - fo.write("\n".join(training_file_names)) - fo.close() - - fo = open(args.validation_filename, "w") - fo.write("\n".join(validation_file_names)) - fo.close() - - # print process - print("Written file is: ", args.train_filename, ", is_shuffle: ", args.is_shuffled) \ No newline at end of file From 5b0c23ec3999e2fa43883f25f6fa36a6c598ffd5 Mon Sep 17 00:00:00 2001 From: kagudkovArt Date: Wed, 5 Dec 2018 22:08:56 +0300 Subject: [PATCH 6/6] Revert "add script for flist creation" This reverts commit cdcbb335 --- config/inpaint_places2_sagan.yml | 11 +++-------- config/test_places2_sagan.yml | 3 +-- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/config/inpaint_places2_sagan.yml b/config/inpaint_places2_sagan.yml index 7ea3ccb..07d9ba2 100644 --- a/config/inpaint_places2_sagan.yml +++ b/config/inpaint_places2_sagan.yml @@ -1,8 +1,8 @@ #Dataset and Loading Setting -DATASET: 'bald' # 'bald', 'tmnist', 'dtd', 'places2', 'celeba', 'imagenet', 'cityscapes' +DATASET: 'places2' # 'tmnist', 'dtd', 'places2', 'celeba', 'imagenet', 'cityscapes' MASKDATASET: 'irrmask' MASK_TYPES: ['random_free_form'] -RANDOM_CROP: True +RANDOM_CROP: False MASKFROMFILE: False LOG_DIR: full_model_places2_sa_bn_256 MODEL_RESTORE: '' #'201809200859_full_model_places2_256/epoch_1_ckpt.pth.tar' # '20180115220926508503_jyugpu0_places2_NORMAL_wgan_gp_full_model' @@ -16,7 +16,7 @@ RANDOM_BBOX_MARGIN: [64, 64] BATCH_SIZE: 4 RANDOM_SEED: False PADDING: 'SAME' -BATCH_NORM: False +BATCH_NORM: True #random free_form settings MAXVERTEX: 5 MAXANGLE: 4.0 #pi @@ -113,11 +113,6 @@ DATA_FLIST: '/unsullied/sharefs/linhangyu/Inpainting/Data/VOCData/voc_horse_train_flist.txt', '/unsullied/sharefs/linhangyu/Inpainting/Data/VOCData/voc_horse_val_flist.txt' ] - bald: [ - '/home/kagudkov/PycharmProjects/GatedConvolution_pytorch/data/bald/train_shuffled.flist', - '/home/kagudkov/PycharmProjects/GatedConvolution_pytorch/data/bald/validation_shuffled.flist' - ] - horse_mask: [ '/unsullied/sharefs/linhangyu/Inpainting/Data/VOCData/voc_horse_bbox_train_flist.txt', '/unsullied/sharefs/linhangyu/Inpainting/Data/VOCData/voc_horse_bbox_val_flist.txt' diff --git a/config/test_places2_sagan.yml b/config/test_places2_sagan.yml index 99beed8..2ea5774 100644 --- a/config/test_places2_sagan.yml +++ b/config/test_places2_sagan.yml @@ -5,8 +5,7 @@ MASK_TYPES: ['random_free_form'] RANDOM_CROP: False MASKFROMFILE: False LOG_DIR: small_model_places2_sa_bn_l2h_unet_perc_style_nocon_256 -MODEL_RESTORE: 201812051742_full_model_places2_sa_bn_256/latest_ckpt.pth.tar - # 201810162252_full_model_places2_sa_bn_256/latest_ckpt.pth.tar +MODEL_RESTORE: 201810162252_full_model_places2_sa_bn_256/latest_ckpt.pth.tar #201811041826_full_model_places2_sa_bn_l2h_unet_256/latest_ckpt.pth.tar #201810162252_full_model_places2_sa_bn_256/latest_ckpt.pth.tar #