├── ASGIT ├── .ipynb_checkpoints │ └── train_attn-checkpoint.py ├── data │ ├── .ipynb_checkpoints │ │ └── unaligned_dataset-checkpoint.py │ ├── __init__.py │ ├── aligned_dataset.py │ ├── aux_dataset.py │ ├── base_dataset.py │ ├── colorization_dataset.py │ ├── image_folder.py │ ├── single_dataset.py │ ├── template_dataset.py │ └── unaligned_dataset.py ├── datasets │ ├── combine_A_and_B.py │ ├── download_cyclegan_dataset.sh │ ├── download_pix2pix_dataset.sh │ ├── make_dataset_aligned.py │ ├── prepare_cityscapes_dataset.py │ └── prepare_cityscapes_dataset_v2.py ├── histogram.py ├── metric │ ├── .ipynb_checkpoints │ │ └── fid_kid-checkpoint.py │ ├── SSIM.py │ ├── fid_kid.py │ ├── imqual_utils.py │ ├── inception.py │ └── psnr_ssim.py ├── models │ ├── .ipynb_checkpoints │ │ └── attn_cycle_gan_model-checkpoint.py │ ├── __init__.py │ ├── attn_cycle_gan_model.py │ ├── attn_cycle_gan_v2_model.py │ ├── base_model.py │ ├── colorization_model.py │ ├── cycle_gan_model.py │ ├── decompose_attn_model.py │ ├── fal_model.py │ ├── networks.py │ ├── pix2pix_attn_model.py │ ├── pix2pix_model.py │ ├── raw_gan_model.py │ ├── template_model.py │ ├── test_model.py │ └── unit_model.py ├── my_scripts │ ├── .ipynb_checkpoints │ │ ├── test_asgit_v2-checkpoint.sh │ │ └── train_asgit_v2-checkpoint.sh │ ├── test_asgit_v2.sh │ └── train_asgit_v2.sh ├── options │ ├── .ipynb_checkpoints │ │ ├── base_options-checkpoint.py │ │ ├── test_options-checkpoint.py │ │ └── train_options-checkpoint.py │ ├── __init__.py │ ├── base_options.py │ ├── test_options.py │ └── train_options.py ├── results │ └── split_real_fake.py ├── run_time.sh ├── runtime.py ├── scripts │ ├── .ipynb_checkpoints │ │ └── train_asgit-checkpoint.sh │ ├── conda_deps.sh │ ├── download_cyclegan_model.sh │ ├── download_pix2pix_model.sh │ ├── install_deps.sh │ ├── test_before_push.py │ ├── test_colorization.sh │ ├── test_cyclegan.sh │ ├── test_pix2pix.sh │ ├── test_single.sh │ ├── train_asgit.sh │ ├── train_asgit_v2.sh │ ├── train_colorization.sh │ ├── train_cyclegan.sh │ └── train_pix2pix.sh ├── test.py ├── train_attn.py ├── train_attn_v2.py ├── train_v2.py └── util │ ├── __init__.py │ ├── get_data.py │ ├── html.py │ ├── image_pool.py │ ├── util.py │ └── visualizer.py ├── BicycleGAN ├── .ipynb_checkpoints │ ├── test-checkpoint.py │ └── train-checkpoint.py ├── data │ ├── .ipynb_checkpoints │ │ ├── __init__-checkpoint.py │ │ ├── base_dataset-checkpoint.py │ │ ├── image_folder-checkpoint.py │ │ └── single_dataset-checkpoint.py │ ├── __init__.py │ ├── aligned_dataset.py │ ├── base_dataset.py │ ├── image_folder.py │ ├── single_dataset.py │ └── template_dataset.py ├── datasets │ ├── .ipynb_checkpoints │ │ └── download_dataset-checkpoint.sh │ ├── download_dataset.sh │ ├── download_mini_dataset.sh │ └── download_testset.sh ├── metric │ ├── .ipynb_checkpoints │ │ └── psnr_ssim-checkpoint.py │ ├── SSIM.py │ ├── fid_kid.py │ ├── imqual_utils.py │ ├── inception.py │ └── psnr_ssim.py ├── models │ ├── __init__.py │ ├── base_model.py │ ├── bicycle_gan_model.py │ ├── networks.py │ ├── pix2pix_model.py │ └── template_model.py ├── options │ ├── .ipynb_checkpoints │ │ ├── __init__-checkpoint.py │ │ ├── base_options-checkpoint.py │ │ └── train_options-checkpoint.py │ ├── __init__.py │ ├── base_options.py │ ├── test_options.py │ ├── train_options.py │ └── video_options.py ├── pretrained_models │ ├── .ipynb_checkpoints │ │ └── download_model-checkpoint.sh │ └── download_model.sh ├── results │ └── split_real_fake.py ├── run_time.sh ├── runtime.py ├── scripts │ ├── .ipynb_checkpoints │ │ ├── test_sar2opt-checkpoint.sh │ │ ├── train-checkpoint.sh │ │ ├── train_combined_SEN1_2_10%_fall-checkpoint.sh │ │ ├── train_combined_SEN1_2_10%_spring-checkpoint.sh │ │ ├── train_combined_SEN1_2_10%_summer-checkpoint.sh │ │ ├── train_combined_SEN1_2_10%_winter-checkpoint.sh │ │ ├── train_combined_SEN1_2_20%_fall-checkpoint.sh │ │ ├── train_combined_SEN1_2_20%_spring-checkpoint.sh │ │ ├── train_combined_SEN1_2_20%_summer-checkpoint.sh │ │ ├── train_combined_SEN1_2_20%_winter-checkpoint.sh │ │ └── train_opt2sar-checkpoint.sh │ ├── demo │ │ ├── .ipynb_checkpoints │ │ │ ├── train_edges2shoes-checkpoint.sh │ │ │ └── train_facades-checkpoint.sh │ │ ├── check_all.sh │ │ ├── install_conda.sh │ │ ├── install_pip.sh │ │ ├── test_before_push.py │ │ ├── test_edges2handbags.sh │ │ ├── test_edges2shoes.sh │ │ ├── test_facades.sh │ │ ├── test_night2day.sh │ │ ├── train_edges2shoes.sh │ │ ├── train_facades.sh │ │ └── video_edges2shoes.sh │ ├── test_scripts │ │ ├── .ipynb_checkpoints │ │ │ ├── test_combined_SEN1_2_10%_spring-checkpoint.sh │ │ │ ├── test_maps-checkpoint.sh │ │ │ └── test_sar2opt-checkpoint.sh │ │ ├── test_combined_SEN1_2_10%_fall.sh │ │ ├── test_combined_SEN1_2_10%_spring.sh │ │ ├── test_combined_SEN1_2_10%_summer.sh │ │ ├── test_combined_SEN1_2_10%_winter.sh │ │ ├── test_combined_SEN1_2_20%_fall.sh │ │ ├── test_combined_SEN1_2_20%_spring.sh │ │ ├── test_combined_SEN1_2_20%_summer.sh │ │ ├── test_combined_SEN1_2_20%_winter.sh │ │ ├── test_combined_sar2opt.sh │ │ └── test_maps.sh │ ├── train.sh │ └── train_scripts │ │ ├── .ipynb_checkpoints │ │ └── train_combined_SEN1_2_10%_spring-checkpoint.sh │ │ ├── train_combined_SEN1_2_10%_fall.sh │ │ ├── train_combined_SEN1_2_10%_spring.sh │ │ ├── train_combined_SEN1_2_10%_summer.sh │ │ ├── train_combined_SEN1_2_10%_winter.sh │ │ ├── train_combined_SEN1_2_20%_fall.sh │ │ ├── train_combined_SEN1_2_20%_spring.sh │ │ ├── train_combined_SEN1_2_20%_summer.sh │ │ ├── train_combined_SEN1_2_20%_winter.sh │ │ └── train_combined_sar2opt.sh ├── test.py ├── train.py ├── util │ ├── __init__.py │ ├── html.py │ ├── util.py │ └── visualizer.py └── video.py ├── CUT ├── .ipynb_checkpoints │ ├── test-checkpoint.py │ └── train-checkpoint.py ├── data │ ├── .ipynb_checkpoints │ │ ├── base_dataset-checkpoint.py │ │ ├── image_folder-checkpoint.py │ │ └── unaligned_dataset-checkpoint.py │ ├── __init__.py │ ├── base_dataset.py │ ├── image_folder.py │ ├── single_dataset.py │ ├── singleimage_dataset.py │ ├── template_dataset.py │ └── unaligned_dataset.py ├── datasets │ ├── combine_A_and_B.py │ ├── download_cut_dataset.sh │ ├── make_dataset_aligned.py │ └── prepare_cityscapes_dataset.py ├── experiments │ ├── __init__.py │ ├── __main__.py │ ├── grumpifycat_launcher.py │ ├── placeholder_launcher.py │ └── tmux_launcher.py ├── metric │ ├── SSIM.py │ ├── fid_kid.py │ ├── imqual_utils.py │ ├── inception.py │ └── psnr_ssim.py ├── models │ ├── .ipynb_checkpoints │ │ ├── __init__-checkpoint.py │ │ ├── base_model-checkpoint.py │ │ ├── cut_model-checkpoint.py │ │ └── networks-checkpoint.py │ ├── __init__.py │ ├── base_model.py │ ├── cut_model.py │ ├── cycle_gan_model.py │ ├── nce.py │ ├── networks.py │ ├── stylegan_networks.py │ └── template_model.py ├── options │ ├── .ipynb_checkpoints │ │ ├── base_options-checkpoint.py │ │ ├── test_options-checkpoint.py │ │ └── train_options-checkpoint.py │ ├── __init__.py │ ├── base_options.py │ ├── test_options.py │ └── train_options.py ├── run_time.sh ├── runtime.py ├── scripts │ ├── conda_deps.sh │ └── install_deps.sh ├── test.py ├── train.py └── util │ ├── .ipynb_checkpoints │ ├── __init__-checkpoint.py │ ├── get_data-checkpoint.py │ ├── html-checkpoint.py │ ├── image_pool-checkpoint.py │ ├── util-checkpoint.py │ └── visualizer-checkpoint.py │ ├── __init__.py │ ├── get_data.py │ ├── html.py │ ├── image_pool.py │ ├── util.py │ └── visualizer.py ├── CycleGAN-Pix2Pix ├── .ipynb_checkpoints │ └── train-checkpoint.py ├── data │ ├── __init__.py │ ├── aligned_dataset.py │ ├── base_dataset.py │ ├── colorization_dataset.py │ ├── image_folder.py │ ├── single_dataset.py │ ├── template_dataset.py │ └── unaligned_dataset.py ├── datasets │ ├── .ipynb_checkpoints │ │ ├── combine_AB-checkpoint.py │ │ └── combine_A_and_B-checkpoint.py │ ├── combine_AB.py │ ├── combine_A_and_B.py │ ├── download_cyclegan_dataset.sh │ ├── download_pix2pix_dataset.sh │ ├── make_dataset_aligned.py │ └── prepare_cityscapes_dataset.py ├── metric │ ├── .ipynb_checkpoints │ │ └── fid_kid-checkpoint.py │ ├── SSIM.py │ ├── fid_kid.py │ ├── imqual_utils.py │ ├── inception.py │ └── psnr_ssim.py ├── models │ ├── __init__.py │ ├── base_model.py │ ├── colorization_model.py │ ├── cycle_gan_model.py │ ├── networks.py │ ├── pix2pix_model.py │ ├── template_model.py │ └── test_model.py ├── options │ ├── .ipynb_checkpoints │ │ ├── base_options-checkpoint.py │ │ ├── test_options-checkpoint.py │ │ └── train_options-checkpoint.py │ ├── __init__.py │ ├── base_options.py │ ├── test_options.py │ └── train_options.py ├── results │ ├── sar2opt_cyclegan │ │ └── test_latest │ │ │ ├── .ipynb_checkpoints │ │ │ └── split_real_fake-checkpoint.py │ │ │ └── split_real_fake.py │ ├── sar2opt_pix2pix_1 │ │ └── test_latest │ │ │ ├── .ipynb_checkpoints │ │ │ └── split_real_fake-checkpoint.py │ │ │ └── split_real_fake.py │ ├── sar2opt_pix2pix_A2B │ │ └── test_latest │ │ │ ├── .ipynb_checkpoints │ │ │ └── split_real_fake-checkpoint.py │ │ │ └── split_real_fake.py │ └── split_real_fake.py ├── run_time.sh ├── runtime.py ├── scripts │ ├── conda_deps.sh │ ├── download_cyclegan_model.sh │ ├── download_pix2pix_model.sh │ ├── edges │ │ └── batch_hed.py │ ├── eval_cityscapes │ │ ├── cityscapes.py │ │ ├── download_fcn8s.sh │ │ ├── evaluate.py │ │ └── util.py │ ├── install_deps.sh │ ├── test_before_push.py │ ├── test_colorization.sh │ ├── test_cyclegan.sh │ ├── test_pix2pix.sh │ ├── test_single.sh │ ├── train_colorization.sh │ ├── train_cyclegan.sh │ └── train_pix2pix.sh ├── test.py ├── train.py └── util │ ├── __init__.py │ ├── get_data.py │ ├── html.py │ ├── image_pool.py │ ├── util.py │ └── visualizer.py ├── LICENSE ├── MUNIT ├── .ipynb_checkpoints │ ├── test-checkpoint.py │ ├── test_batch-checkpoint.py │ ├── train-checkpoint.py │ ├── trainer-checkpoint.py │ └── utils-checkpoint.py ├── data.py ├── metric │ ├── SSIM.py │ ├── fid_kid.py │ ├── imqual_utils.py │ ├── inception.py │ └── psnr_ssim.py ├── networks.py ├── results │ └── split_real_fake.py ├── run_time.sh ├── runtime.py ├── scripts │ ├── demo_train_edges2handbags.sh │ ├── demo_train_edges2shoes.sh │ └── demo_train_summer2winter_yosemite256.sh ├── test.py ├── test_batch.py ├── train.py ├── trainer.py └── utils.py ├── NICE-GAN ├── .ipynb_checkpoints │ ├── NICE-checkpoint.py │ ├── dataset-checkpoint.py │ ├── main-checkpoint.py │ ├── networks-checkpoint.py │ └── utils-checkpoint.py ├── NICE.py ├── dataset.py ├── main.py ├── metric │ ├── .ipynb_checkpoints │ │ └── fid_kid-checkpoint.py │ ├── SSIM.py │ ├── fid_kid.py │ ├── imqual_utils.py │ ├── inception.py │ └── psnr_ssim.py ├── networks.py ├── run_time.sh ├── runtime.py └── utils.py ├── README.md └── imgs ├── Honeyview_sar2opt.png ├── Honeyview_translated_results.png ├── overview.png └── results /ASGIT/data/aux_dataset.py: -------------------------------------------------------------------------------- 1 | import torch 2 | from torch.utils.data import Dataset 3 | 4 | class AuxAttnDataset(Dataset): 5 | def __init__(self, size_A, size_B, device, init_method='one', mask_size=256, given_constant=1): 6 | super(AuxAttnDataset, self).__init__() 7 | self.A_size, self.B_size = size_A, size_B 8 | if init_method == 'one': 9 | self.A_attns = torch.ones((size_A, 1, mask_size, mask_size), dtype=torch.float32) * given_constant 10 | self.B_attns = torch.ones((size_B, 1, mask_size, mask_size), dtype=torch.float32) * given_constant 11 | elif init_method == 'random': 12 | self.A_attns = torch.rand((size_A, 1, mask_size, mask_size), dtype=torch.float32) 13 | self.B_attns = torch.rand((size_B, 1, mask_size, mask_size), dtype=torch.float32) 14 | 15 | self.A_attns = self.A_attns.to(device) 16 | self.B_attns = self.B_attns.to(device) 17 | 18 | def __getitem__(self, index_tuple): 19 | raise NotImplementedError('Auxiliary Dataset, doesn\'t support this operation') 20 | 21 | def __len__(self): 22 | return max(self.A_size, self.B_size) 23 | 24 | def get_attn_map(self, adx, bdx): 25 | return self.A_attns[adx], self.B_attns[bdx] 26 | 27 | def update_attn_map(self, idx, tgt_tensor, a_flag=True): 28 | if a_flag: 29 | self.A_attns[idx] = tgt_tensor 30 | else: 31 | self.B_attns[idx] = tgt_tensor -------------------------------------------------------------------------------- /ASGIT/data/image_folder.py: -------------------------------------------------------------------------------- 1 | """A modified image folder class 2 | 3 | We modify the official PyTorch image folder (https://github.com/pytorch/vision/blob/master/torchvision/datasets/folder.py) 4 | so that this class can load images from both current directory and its subdirectories. 5 | """ 6 | 7 | import torch.utils.data as data 8 | 9 | from PIL import Image 10 | import os 11 | import os.path 12 | 13 | IMG_EXTENSIONS = [ 14 | '.jpg', '.JPG', '.jpeg', '.JPEG', 15 | '.png', '.PNG', '.ppm', '.PPM', '.bmp', '.BMP', 16 | ] 17 | 18 | 19 | def is_image_file(filename): 20 | return any(filename.endswith(extension) for extension in IMG_EXTENSIONS) 21 | 22 | 23 | def make_dataset(dir, max_dataset_size=float("inf")): 24 | images = [] 25 | assert os.path.isdir(dir), '%s is not a valid directory' % dir 26 | 27 | for root, _, fnames in sorted(os.walk(dir)): 28 | for fname in fnames: 29 | if is_image_file(fname): 30 | path = os.path.join(root, fname) 31 | images.append(path) 32 | return images[:min(max_dataset_size, len(images))] 33 | 34 | 35 | def default_loader(path): 36 | return Image.open(path).convert('RGB') 37 | 38 | 39 | class ImageFolder(data.Dataset): 40 | 41 | def __init__(self, root, transform=None, return_paths=False, 42 | loader=default_loader): 43 | imgs = make_dataset(root) 44 | if len(imgs) == 0: 45 | raise(RuntimeError("Found 0 images in: " + root + "\n" 46 | "Supported image extensions are: " + 47 | ",".join(IMG_EXTENSIONS))) 48 | 49 | self.root = root 50 | self.imgs = imgs 51 | self.transform = transform 52 | self.return_paths = return_paths 53 | self.loader = loader 54 | 55 | def __getitem__(self, index): 56 | path = self.imgs[index] 57 | img = self.loader(path) 58 | if self.transform is not None: 59 | img = self.transform(img) 60 | if self.return_paths: 61 | return img, path 62 | else: 63 | return img 64 | 65 | def __len__(self): 66 | return len(self.imgs) 67 | -------------------------------------------------------------------------------- /ASGIT/data/single_dataset.py: -------------------------------------------------------------------------------- 1 | from data.base_dataset import BaseDataset, get_transform 2 | from data.image_folder import make_dataset 3 | from PIL import Image 4 | 5 | 6 | class SingleDataset(BaseDataset): 7 | """This dataset class can load a set of images specified by the path --dataroot /path/to/data. 8 | 9 | It can be used for generating CycleGAN results only for one side with the model option '-model test'. 10 | """ 11 | 12 | def __init__(self, opt): 13 | """Initialize this dataset class. 14 | 15 | Parameters: 16 | opt (Option class) -- stores all the experiment flags; needs to be a subclass of BaseOptions 17 | """ 18 | BaseDataset.__init__(self, opt) 19 | self.A_paths = sorted(make_dataset(opt.dataroot, opt.max_dataset_size)) 20 | input_nc = self.opt.output_nc if self.opt.direction == 'BtoA' else self.opt.input_nc 21 | self.transform = get_transform(opt, grayscale=(input_nc == 1)) 22 | 23 | def __getitem__(self, index): 24 | """Return a data point and its metadata information. 25 | 26 | Parameters: 27 | index - - a random integer for data indexing 28 | 29 | Returns a dictionary that contains A and A_paths 30 | A(tensor) - - an image in one domain 31 | A_paths(str) - - the path of the image 32 | """ 33 | A_path = self.A_paths[index] 34 | A_img = Image.open(A_path).convert('RGB') 35 | A = self.transform(A_img) 36 | return {'A': A, 'A_paths': A_path} 37 | 38 | def __len__(self): 39 | """Return the total number of images in the dataset.""" 40 | return len(self.A_paths) 41 | -------------------------------------------------------------------------------- /ASGIT/datasets/combine_A_and_B.py: -------------------------------------------------------------------------------- 1 | import os 2 | import numpy as np 3 | import cv2 4 | import argparse 5 | 6 | parser = argparse.ArgumentParser('create image pairs') 7 | parser.add_argument('--fold_A', dest='fold_A', help='input directory for image A', type=str, default='../dataset/50kshoes_edges') 8 | parser.add_argument('--fold_B', dest='fold_B', help='input directory for image B', type=str, default='../dataset/50kshoes_jpg') 9 | parser.add_argument('--fold_AB', dest='fold_AB', help='output directory', type=str, default='../dataset/test_AB') 10 | parser.add_argument('--num_imgs', dest='num_imgs', help='number of images', type=int, default=1000000) 11 | parser.add_argument('--use_AB', dest='use_AB', help='if true: (0001_A, 0001_B) to (0001_AB)', action='store_true') 12 | args = parser.parse_args() 13 | 14 | for arg in vars(args): 15 | print('[%s] = ' % arg, getattr(args, arg)) 16 | 17 | splits = os.listdir(args.fold_A) 18 | 19 | for sp in splits: 20 | img_fold_A = os.path.join(args.fold_A, sp) 21 | img_fold_B = os.path.join(args.fold_B, sp) 22 | img_list = os.listdir(img_fold_A) 23 | if args.use_AB: 24 | img_list = [img_path for img_path in img_list if '_A.' in img_path] 25 | 26 | num_imgs = min(args.num_imgs, len(img_list)) 27 | print('split = %s, use %d/%d images' % (sp, num_imgs, len(img_list))) 28 | img_fold_AB = os.path.join(args.fold_AB, sp) 29 | if not os.path.isdir(img_fold_AB): 30 | os.makedirs(img_fold_AB) 31 | print('split = %s, number of images = %d' % (sp, num_imgs)) 32 | for n in range(num_imgs): 33 | name_A = img_list[n] 34 | path_A = os.path.join(img_fold_A, name_A) 35 | if args.use_AB: 36 | name_B = name_A.replace('_A.', '_B.') 37 | else: 38 | name_B = name_A 39 | path_B = os.path.join(img_fold_B, name_B) 40 | if os.path.isfile(path_A) and os.path.isfile(path_B): 41 | name_AB = name_A 42 | if args.use_AB: 43 | name_AB = name_AB.replace('_A.', '.') # remove _A 44 | path_AB = os.path.join(img_fold_AB, name_AB) 45 | im_A = cv2.imread(path_A, 1) # python2: cv2.CV_LOAD_IMAGE_COLOR; python3: cv2.IMREAD_COLOR 46 | im_B = cv2.imread(path_B, 1) # python2: cv2.CV_LOAD_IMAGE_COLOR; python3: cv2.IMREAD_COLOR 47 | im_AB = np.concatenate([im_A, im_B], 1) 48 | cv2.imwrite(path_AB, im_AB) 49 | -------------------------------------------------------------------------------- /ASGIT/datasets/download_cyclegan_dataset.sh: -------------------------------------------------------------------------------- 1 | FILE=$1 2 | 3 | if [[ $FILE != "ae_photos" && $FILE != "apple2orange" && $FILE != "summer2winter_yosemite" && $FILE != "horse2zebra" && $FILE != "monet2photo" && $FILE != "cezanne2photo" && $FILE != "ukiyoe2photo" && $FILE != "vangogh2photo" && $FILE != "maps" && $FILE != "cityscapes" && $FILE != "facades" && $FILE != "iphone2dslr_flower" && $FILE != "mini" && $FILE != "mini_pix2pix" && $FILE != "mini_colorization" ]]; then 4 | echo "Available datasets are: apple2orange, summer2winter_yosemite, horse2zebra, monet2photo, cezanne2photo, ukiyoe2photo, vangogh2photo, maps, cityscapes, facades, iphone2dslr_flower, ae_photos" 5 | exit 1 6 | fi 7 | 8 | if [[ $FILE == "cityscapes" ]]; then 9 | echo "Due to license issue, we cannot provide the Cityscapes dataset from our repository. Please download the Cityscapes dataset from https://cityscapes-dataset.com, and use the script ./datasets/prepare_cityscapes_dataset.py." 10 | echo "You need to download gtFine_trainvaltest.zip and leftImg8bit_trainvaltest.zip. For further instruction, please read ./datasets/prepare_cityscapes_dataset.py" 11 | exit 1 12 | fi 13 | 14 | echo "Specified [$FILE]" 15 | URL=https://people.eecs.berkeley.edu/~taesung_park/CycleGAN/datasets/$FILE.zip 16 | ZIP_FILE=./datasets/$FILE.zip 17 | TARGET_DIR=./datasets/$FILE/ 18 | wget -N $URL -O $ZIP_FILE 19 | mkdir $TARGET_DIR 20 | unzip $ZIP_FILE -d ./datasets/ 21 | rm $ZIP_FILE 22 | -------------------------------------------------------------------------------- /ASGIT/datasets/download_pix2pix_dataset.sh: -------------------------------------------------------------------------------- 1 | FILE=$1 2 | 3 | if [[ $FILE != "cityscapes" && $FILE != "night2day" && $FILE != "edges2handbags" && $FILE != "edges2shoes" && $FILE != "facades" && $FILE != "maps" ]]; then 4 | echo "Available datasets are cityscapes, night2day, edges2handbags, edges2shoes, facades, maps" 5 | exit 1 6 | fi 7 | 8 | if [[ $FILE == "cityscapes" ]]; then 9 | echo "Due to license issue, we cannot provide the Cityscapes dataset from our repository. Please download the Cityscapes dataset from https://cityscapes-dataset.com, and use the script ./datasets/prepare_cityscapes_dataset.py." 10 | echo "You need to download gtFine_trainvaltest.zip and leftImg8bit_trainvaltest.zip. For further instruction, please read ./datasets/prepare_cityscapes_dataset.py" 11 | exit 1 12 | fi 13 | 14 | echo "Specified [$FILE]" 15 | 16 | URL=http://efrosgans.eecs.berkeley.edu/pix2pix/datasets/$FILE.tar.gz 17 | TAR_FILE=./datasets/$FILE.tar.gz 18 | TARGET_DIR=./datasets/$FILE/ 19 | wget -N $URL -O $TAR_FILE 20 | mkdir -p $TARGET_DIR 21 | tar -zxvf $TAR_FILE -C ./datasets/ 22 | rm $TAR_FILE 23 | -------------------------------------------------------------------------------- /ASGIT/datasets/make_dataset_aligned.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | from PIL import Image 4 | 5 | 6 | def get_file_paths(folder): 7 | image_file_paths = [] 8 | for root, dirs, filenames in os.walk(folder): 9 | filenames = sorted(filenames) 10 | for filename in filenames: 11 | input_path = os.path.abspath(root) 12 | file_path = os.path.join(input_path, filename) 13 | if filename.endswith('.png') or filename.endswith('.jpg'): 14 | image_file_paths.append(file_path) 15 | 16 | break # prevent descending into subfolders 17 | return image_file_paths 18 | 19 | 20 | def align_images(a_file_paths, b_file_paths, target_path): 21 | if not os.path.exists(target_path): 22 | os.makedirs(target_path) 23 | 24 | for i in range(len(a_file_paths)): 25 | img_a = Image.open(a_file_paths[i]) 26 | img_b = Image.open(b_file_paths[i]) 27 | assert(img_a.size == img_b.size) 28 | 29 | aligned_image = Image.new("RGB", (img_a.size[0] * 2, img_a.size[1])) 30 | aligned_image.paste(img_a, (0, 0)) 31 | aligned_image.paste(img_b, (img_a.size[0], 0)) 32 | aligned_image.save(os.path.join(target_path, '{:04d}.jpg'.format(i))) 33 | 34 | 35 | if __name__ == '__main__': 36 | import argparse 37 | parser = argparse.ArgumentParser() 38 | parser.add_argument( 39 | '--dataset-path', 40 | dest='dataset_path', 41 | help='Which folder to process (it should have subfolders testA, testB, trainA and trainB' 42 | ) 43 | args = parser.parse_args() 44 | 45 | dataset_folder = args.dataset_path 46 | print(dataset_folder) 47 | 48 | test_a_path = os.path.join(dataset_folder, 'testA') 49 | test_b_path = os.path.join(dataset_folder, 'testB') 50 | test_a_file_paths = get_file_paths(test_a_path) 51 | test_b_file_paths = get_file_paths(test_b_path) 52 | assert(len(test_a_file_paths) == len(test_b_file_paths)) 53 | test_path = os.path.join(dataset_folder, 'test') 54 | 55 | train_a_path = os.path.join(dataset_folder, 'trainA') 56 | train_b_path = os.path.join(dataset_folder, 'trainB') 57 | train_a_file_paths = get_file_paths(train_a_path) 58 | train_b_file_paths = get_file_paths(train_b_path) 59 | assert(len(train_a_file_paths) == len(train_b_file_paths)) 60 | train_path = os.path.join(dataset_folder, 'train') 61 | 62 | align_images(test_a_file_paths, test_b_file_paths, test_path) 63 | align_images(train_a_file_paths, train_b_file_paths, train_path) 64 | -------------------------------------------------------------------------------- /ASGIT/histogram.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | import pandas as pd 4 | import argparse 5 | 6 | 7 | def main(opt): 8 | in_dir = opt.in_dir 9 | df = pd.read_csv(in_dir, header=None) 10 | 11 | ax = df.plot.bar(x=0, y=[1,2,3], legend=True, rot=50, fontsize=12, width=0.7) 12 | ax.tick_params(axis='x', which='major', labelsize=9) 13 | ax.legend(['Epoch 20', 'Epoch 40', 'Epoch 60']) 14 | plt.show() 15 | pass 16 | 17 | 18 | if __name__ == '__main__': 19 | parser = argparse.ArgumentParser() 20 | parser.add_argument('--in_dir', type=str, required=True) 21 | parser.add_argument('--out_dir', type=str) 22 | opt = parser.parse_args() 23 | main(opt) -------------------------------------------------------------------------------- /ASGIT/metric/SSIM.py: -------------------------------------------------------------------------------- 1 | from skimage.metrics import structural_similarity as ssim 2 | from PIL import Image 3 | import os 4 | import numpy as np 5 | from glob import glob 6 | from tqdm import tqdm 7 | import argparse 8 | 9 | 10 | def cal_SSIM(x, y): 11 | return ssim(x, y, multichannel=True) 12 | 13 | 14 | def get_dirImage(real_dir, fake_dir): 15 | real_image_paths = sorted(glob(os.path.join(real_dir, "*.*"))) 16 | fake_image_paths = sorted(glob(os.path.join(fake_dir, "*.*"))) 17 | return real_image_paths, fake_image_paths 18 | 19 | 20 | def cal_batch_SSIM(real_paths, fake_paths): 21 | assert len(real_paths) == len(fake_paths) 22 | 23 | SSIMs = [] 24 | for i in tqdm(range((len(real_paths)))): 25 | 26 | img1 = Image.open(real_paths[i]).resize((256, 256)) 27 | img2 = Image.open(fake_paths[i]).resize((256, 256)) 28 | 29 | tmp_ssim = cal_SSIM(np.array(img1), np.array(img2)) 30 | SSIMs.append(tmp_ssim) 31 | return SSIMs 32 | 33 | 34 | if __name__ == '__main__': 35 | parser = argparse.ArgumentParser() 36 | 37 | # parser.add_argument('--dir_root', type=str, default='/workspace/Image_translation_codes/pytorch-CycleGAN-and-pix2pix/results/SEN1_2_20%_spring_cyclegan/test_latest') 38 | parser.add_argument('--fake_dir', type=str, default='fake') 39 | parser.add_argument('--real_dir', type=str, default='real') 40 | 41 | # 只需要修改此处 42 | # root = r'D:\ZYT\Codes\1.Heterogeneous_CD\Image_Translation\pytorch-CycleGAN-and-pix2pix\results\sar2opt_pix2pix\test_latest' 43 | 44 | opt = parser.parse_args() 45 | # real_dir = opt.dir_root + '/' + opt.real_subdir 46 | # fake_dir = opt.dir_root + '/' + opt.fake_subdir 47 | 48 | real_paths, fake_paths = get_dirImage(opt.real_dir, opt.fake_dir) 49 | print(len(real_paths)) 50 | 51 | SSIMs = cal_batch_SSIM(real_paths, fake_paths) 52 | 53 | print("mean SSIM: ", np.mean(SSIMs)) 54 | 55 | -------------------------------------------------------------------------------- /ASGIT/metric/imqual_utils.py: -------------------------------------------------------------------------------- 1 | """ 2 | # > Implementation of the classic paper by Zhou Wang et. al.: 3 | # - Image quality assessment: from error visibility to structural similarity 4 | # - https://ieeexplore.ieee.org/stamp/stamp.jsp?arnumber=1284395 5 | # > Maintainer: https://github.com/xahidbuffon 6 | """ 7 | from __future__ import division 8 | import numpy as np 9 | import math 10 | from scipy.ndimage import gaussian_filter 11 | 12 | 13 | def getSSIM(X, Y): 14 | """ 15 | Computes the mean structural similarity between two images. 16 | """ 17 | # assert (X.shape[0] == Y.shape[0]), "Image-patche provided have different dimensions" 18 | assert (X.shape[0] == Y.shape[0] & X.shape[1] == Y.shape[1]), "Image-patche provided have different dimensions" 19 | nch = 1 if X.ndim==2 else X.shape[-1] 20 | mssim = [] 21 | for ch in range(nch): 22 | Xc, Yc = X[...,ch].astype(np.float64), Y[...,ch].astype(np.float64) 23 | mssim.append(compute_ssim(Xc, Yc)) 24 | return np.mean(mssim) 25 | 26 | 27 | def compute_ssim(X, Y): 28 | """ 29 | Compute the structural similarity per single channel (given two images) 30 | """ 31 | # variables are initialized as suggested in the paper 32 | K1 = 0.01 33 | K2 = 0.03 34 | sigma = 1.5 35 | win_size = 5 36 | 37 | # means 38 | ux = gaussian_filter(X, sigma) 39 | uy = gaussian_filter(Y, sigma) 40 | 41 | # variances and covariances 42 | uxx = gaussian_filter(X * X, sigma) 43 | uyy = gaussian_filter(Y * Y, sigma) 44 | uxy = gaussian_filter(X * Y, sigma) 45 | 46 | # normalize by unbiased estimate of std dev 47 | N = win_size ** X.ndim 48 | unbiased_norm = N / (N - 1) # eq. 4 of the paper 49 | vx = (uxx - ux * ux) * unbiased_norm 50 | vy = (uyy - uy * uy) * unbiased_norm 51 | vxy = (uxy - ux * uy) * unbiased_norm 52 | 53 | R = 255 54 | C1 = (K1 * R) ** 2 55 | C2 = (K2 * R) ** 2 56 | # compute SSIM (eq. 13 of the paper) 57 | sim = (2 * ux * uy + C1) * (2 * vxy + C2) 58 | D = (ux ** 2 + uy ** 2 + C1) * (vx + vy + C2) 59 | SSIM = sim/D 60 | mssim = SSIM.mean() 61 | 62 | return mssim 63 | 64 | 65 | 66 | def getPSNR(X, Y): 67 | #assume RGB image 68 | target_data = np.array(X, dtype=np.float64) 69 | ref_data = np.array(Y, dtype=np.float64) 70 | diff = ref_data - target_data 71 | diff = diff.flatten('C') 72 | rmse = math.sqrt(np.mean(diff ** 2.) ) 73 | if rmse == 0: return 100 74 | else: return 20*math.log10(255.0/rmse) 75 | 76 | 77 | -------------------------------------------------------------------------------- /ASGIT/metric/psnr_ssim.py: -------------------------------------------------------------------------------- 1 | """ 2 | # > Script for measuring quantitative performances in terms of 3 | # - Structural Similarity Metric (SSIM) 4 | # - Peak Signal to Noise Ratio (PSNR) 5 | # > Maintainer: https://github.com/xahidbuffon 6 | """ 7 | ## python libs 8 | import numpy as np 9 | from PIL import Image 10 | from glob import glob 11 | from os.path import join 12 | from ntpath import basename 13 | ## local libs 14 | from imqual_utils import getSSIM, getPSNR 15 | import cv2 16 | 17 | 18 | ## compares avg ssim and psnr 19 | def SSIMs_PSNRs(gtr_dir, gen_dir, im_res=(256, 256)): 20 | """ 21 | - gtr_dir contain ground-truths 22 | - gen_dir contain generated images 23 | """ 24 | gtr_paths = sorted(glob(join(gtr_dir, "*.*"))) 25 | gen_paths = sorted(glob(join(gen_dir, "*.*"))) 26 | ssims, psnrs = [], [] 27 | for gtr_path, gen_path in zip(gtr_paths, gen_paths): 28 | gtr_f = basename(gtr_path).split('.')[0] 29 | gen_f = basename(gen_path).split('.')[0] 30 | 31 | # read images from two datasets 32 | r_im = Image.open(gtr_path).resize(im_res) 33 | g_im = Image.open(gen_path).resize(im_res) 34 | 35 | # get ssim on RGB channels 36 | ssim = getSSIM(np.array(r_im), np.array(g_im)) 37 | ssims.append(ssim) 38 | # get psnt on L channel (SOTA norm) 39 | r_im = r_im.convert("L"); g_im = g_im.convert("L") 40 | psnr = getPSNR(np.array(r_im), np.array(g_im)) 41 | psnrs.append(psnr) 42 | return np.array(ssims), np.array(psnrs) 43 | 44 | 45 | """ 46 | Get datasets from 47 | - http://irvlab.cs.umn.edu/resources/euvp-dataset 48 | - http://irvlab.cs.umn.edu/resources/ufo-120-dataset 49 | """ 50 | 51 | root = 'Image_translation_codes/ASGIT/' 52 | 53 | gtr_dir = root + 'results/re-SEN1-2_20%_spring/real' 54 | 55 | # generated im paths 56 | gen_dir = root + 'results/re-SEN1-2_20%_spring/fake' 57 | 58 | 59 | ### compute SSIM and PSNR 60 | SSIM_measures, PSNR_measures = SSIMs_PSNRs(gtr_dir, gen_dir) 61 | print ("SSIM on {0} samples".format(len(SSIM_measures))) 62 | print ("Mean: {0} std: {1}".format(np.mean(SSIM_measures), np.std(SSIM_measures))) 63 | 64 | print ("PSNR on {0} samples".format(len(PSNR_measures))) 65 | print ("Mean: {0} std: {1}".format(np.mean(PSNR_measures), np.std(PSNR_measures))) 66 | 67 | 68 | -------------------------------------------------------------------------------- /ASGIT/my_scripts/.ipynb_checkpoints/test_asgit_v2-checkpoint.sh: -------------------------------------------------------------------------------- 1 | # 修改此处数据集路径 2 | path='./datasets/SAR2Opt' 3 | 4 | # 修改此处 5 | name='SAR2Opt' 6 | 7 | # 修改此处测试集样本数量(test.py无需修改) 8 | # 10%: spring: 488 summer: 1220 fall: 2028 winter: 1012 9 | # 20%: spring: 3898 summer: 2440 fall: 4058 winter: 2025 10 | # 50%: spring: 9746 summer: 6102 fall: 10145 winter: 5064 11 | # data_length=1220 12 | 13 | 14 | python test.py --netG resnet_9blocks \ 15 | --netD posthoc_attn \ 16 | --model attn_cycle_gan \ 17 | --concat rmult \ 18 | --dataroot $path \ 19 | --name $name 20 | -------------------------------------------------------------------------------- /ASGIT/my_scripts/.ipynb_checkpoints/train_asgit_v2-checkpoint.sh: -------------------------------------------------------------------------------- 1 | # 修改此处数据集路径 2 | path='./datasets/SEN1-2/20%_train/ROIs1158_spring_20%_train' 3 | 4 | # 修改此处 5 | name='re-SEN1-2_20%_spring' 6 | 7 | # 修改此处数据集样本数量 8 | data_length=3898 9 | 10 | # (弃用)10%: spring: 1948 summer: 1220 fall: 2028 winter: 1012 11 | # 20%: spring: 3898 summer: 2440 fall: 4058 winter: 2025 12 | # (弃用)50%: spring: 9746 summer: 6102 fall: 10145 winter: 5064 13 | # SAR2Opt: 1450 14 | 15 | # train_attn_v2.py中修复了使用visdom存储数据时,训练到epoch=20就自动中断的bug 16 | python train_attn_v2.py \ 17 | --netG resnet_9blocks \ 18 | --netD posthoc_attn \ 19 | --model attn_cycle_gan \ 20 | --concat rmult \ 21 | --dataroot $path \ 22 | --name $name \ 23 | --mask_size 256 \ 24 | --data_length $data_length -------------------------------------------------------------------------------- /ASGIT/my_scripts/test_asgit_v2.sh: -------------------------------------------------------------------------------- 1 | # 修改此处数据集路径 2 | path='./datasets/SEN1-2/20%_train/ROIs1158_spring_20%_train' 3 | # path='./datasets/SAR2Opt' 4 | 5 | # 修改此处 6 | name='re-SEN1-2_20%_spring' 7 | # name='SAR2Opt' 8 | 9 | # 修改此处测试集样本数量(test.py无需修改) 10 | # 10%: spring: 488 summer: 1220 fall: 2028 winter: 1012 11 | # 20%: spring: 3898 summer: 2440 fall: 4058 winter: 2025 12 | # 50%: spring: 9746 summer: 6102 fall: 10145 winter: 5064 13 | # data_length=1220 14 | 15 | 16 | python test.py --netG resnet_9blocks \ 17 | --netD posthoc_attn \ 18 | --model attn_cycle_gan \ 19 | --concat rmult \ 20 | --dataroot $path \ 21 | --name $name 22 | -------------------------------------------------------------------------------- /ASGIT/my_scripts/train_asgit_v2.sh: -------------------------------------------------------------------------------- 1 | # 修改此处数据集路径 2 | path='./datasets/SEN1-2/20%_train/ROIs1158_spring_20%_train' 3 | 4 | # 修改此处 5 | name='re-SEN1-2_20%_spring' 6 | 7 | # 修改此处数据集样本数量 8 | data_length=3898 9 | 10 | # (弃用)10%: spring: 1948 summer: 1220 fall: 2028 winter: 1012 11 | # 20%: spring: 3898 summer: 2440 fall: 4058 winter: 2025 12 | # (弃用)50%: spring: 9746 summer: 6102 fall: 10145 winter: 5064 13 | # SAR2Opt: 1450 14 | 15 | # train_attn_v2.py中修复了使用visdom存储数据时,训练到epoch=20就自动中断的bug 16 | python train_attn_v2.py \ 17 | --netG resnet_9blocks \ 18 | --netD posthoc_attn \ 19 | --model attn_cycle_gan \ 20 | --concat rmult \ 21 | --dataroot $path \ 22 | --name $name \ 23 | --mask_size 256 \ 24 | --data_length $data_length -------------------------------------------------------------------------------- /ASGIT/options/.ipynb_checkpoints/test_options-checkpoint.py: -------------------------------------------------------------------------------- 1 | from .base_options import BaseOptions 2 | 3 | 4 | class TestOptions(BaseOptions): 5 | """This class includes test options. 6 | 7 | It also includes shared options defined in BaseOptions. 8 | """ 9 | 10 | def initialize(self, parser): 11 | parser = BaseOptions.initialize(self, parser) # define shared options 12 | parser.add_argument('--ntest', type=int, default=float("inf"), help='# of test examples.') 13 | parser.add_argument('--results_dir', type=str, default='./results/', help='saves results here.') 14 | parser.add_argument('--aspect_ratio', type=float, default=1.0, help='aspect ratio of result images') 15 | parser.add_argument('--phase', type=str, default='test', help='train, val, test, etc') 16 | # Dropout and Batchnorm has different behavioir during training and test. 17 | parser.add_argument('--eval', action='store_true', help='use eval mode during test time.') 18 | parser.add_argument('--num_test', type=int, default=2500, help='how many test images to run') 19 | 20 | # 新增参数,用于指定不同数据集的长度(图像对个数) 21 | parser.add_argument('--data_length', type=int, default=1948, help='number of images in trainA directory') 22 | 23 | # rewrite devalue values 24 | parser.set_defaults(model='test') 25 | # To avoid cropping, the load_size should be the same as crop_size 26 | parser.set_defaults(load_size=parser.get_default('crop_size')) 27 | self.isTrain = False 28 | return parser 29 | -------------------------------------------------------------------------------- /ASGIT/options/__init__.py: -------------------------------------------------------------------------------- 1 | """This package options includes option modules: training options, test options, and basic options (used in both training and test).""" 2 | -------------------------------------------------------------------------------- /ASGIT/options/test_options.py: -------------------------------------------------------------------------------- 1 | from .base_options import BaseOptions 2 | 3 | 4 | class TestOptions(BaseOptions): 5 | """This class includes test options. 6 | 7 | It also includes shared options defined in BaseOptions. 8 | """ 9 | 10 | def initialize(self, parser): 11 | parser = BaseOptions.initialize(self, parser) # define shared options 12 | parser.add_argument('--ntest', type=int, default=float("inf"), help='# of test examples.') 13 | parser.add_argument('--results_dir', type=str, default='./results/', help='saves results here.') 14 | parser.add_argument('--aspect_ratio', type=float, default=1.0, help='aspect ratio of result images') 15 | parser.add_argument('--phase', type=str, default='test', help='train, val, test, etc') 16 | # Dropout and Batchnorm has different behavioir during training and test. 17 | parser.add_argument('--eval', action='store_true', help='use eval mode during test time.') 18 | parser.add_argument('--num_test', type=int, default=2500, help='how many test images to run') 19 | 20 | # 新增参数,用于指定不同数据集的长度(图像对个数) 21 | parser.add_argument('--data_length', type=int, default=1948, help='number of images in trainA directory') 22 | 23 | # rewrite devalue values 24 | parser.set_defaults(model='test') 25 | # To avoid cropping, the load_size should be the same as crop_size 26 | parser.set_defaults(load_size=parser.get_default('crop_size')) 27 | self.isTrain = False 28 | return parser 29 | -------------------------------------------------------------------------------- /ASGIT/results/split_real_fake.py: -------------------------------------------------------------------------------- 1 | import os 2 | from shutil import copy 3 | 4 | def check_dir_exists(path): 5 | if not os.path.exists(path): 6 | os.makedirs(path) 7 | 8 | def split_by_name(dir_path, real_save_path, fake_save_path): 9 | check_dir_exists(real_save_path) 10 | check_dir_exists(fake_save_path) 11 | 12 | if not os.path.exists(real_save_path): 13 | os.makedirs(real_save_path) 14 | 15 | if not os.path.exists(fake_save_path): 16 | os.makedirs(fake_save_path) 17 | 18 | for file in os.listdir(dir_path): 19 | tmp_file = os.path.join(os.path.abspath(dir_path), file) 20 | if 'real_B' in file: 21 | copy(tmp_file, real_save_path + os.sep + file) 22 | elif 'fake_B' in file: 23 | copy(tmp_file, fake_save_path + os.sep + file) 24 | 25 | if __name__ == '__main__': 26 | source_root = 'Image_translation_codes/ASGIT/results/re-SEN1-2_20%_spring/test_latest/images' 27 | save_root = 'Image_translation_codes/ASGIT/results/re-SEN1-2_20%_spring' 28 | 29 | 30 | # tmp_source_path = source_root + '_' + str(i) + '/images/' 31 | 32 | real_path = save_root + '/real/' 33 | fake_path = save_root + '/fake/' 34 | 35 | split_by_name(source_root, real_path, fake_path) 36 | -------------------------------------------------------------------------------- /ASGIT/run_time.sh: -------------------------------------------------------------------------------- 1 | # 修改此处数据集路径 2 | path='/workspace/Image_translation_codes/ASGIT/datasets/SAR2Opt' 3 | # path='./datasets/SAR2Opt' 4 | 5 | # 修改此处 6 | name='SAR2Opt' 7 | # name='SAR2Opt' 8 | 9 | 10 | for ((i=1; i<=5; i++)); 11 | do 12 | echo "Processing"$i"th epoch..." 13 | 14 | python /workspace/Image_translation_codes/ASGIT/test.py \ 15 | --netG resnet_9blocks \ 16 | --netD posthoc_attn \ 17 | --model attn_cycle_gan \ 18 | --concat rmult \ 19 | --dataroot $path \ 20 | --name $name 21 | 22 | done -------------------------------------------------------------------------------- /ASGIT/runtime.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | from subprocess import run 3 | import numpy as np 4 | 5 | # spring: 974 summer: 610 fall: 1015 winter: 506 sar2opt: 587/ 627 6 | num_test = 627 7 | 8 | 9 | if __name__ == '__main__': 10 | cmd = "bash Image_translation_codes/ASGIT/run_time.sh" 11 | 12 | results = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True) 13 | times = [] 14 | for line in results.stdout.readlines(): 15 | tmp = line.decode("utf-8").strip() 16 | 17 | if 'cost time' in tmp: 18 | times.append((float(tmp.split(": ")[-1])) / num_test) 19 | 20 | print('All cost times:', times) 21 | 22 | avg_time = np.mean(times) 23 | std_time = np.std(times) 24 | 25 | print("Average running time: ", avg_time) 26 | print("Deviation of running time: ", std_time) 27 | 28 | -------------------------------------------------------------------------------- /ASGIT/scripts/.ipynb_checkpoints/train_asgit-checkpoint.sh: -------------------------------------------------------------------------------- 1 | path='./datasets/SEN1-2/10%_train/ROIs1158_spring_10%_train' 2 | 3 | python train_attn.py \ 4 | --netG resnet_9blocks \ 5 | --netD posthoc_attn \ 6 | --model attn_cycle_gan \ 7 | --concat rmult \ 8 | --dataroot $path \ 9 | --name SEN1-2_10%_spring \ 10 | --mask_size 256 -------------------------------------------------------------------------------- /ASGIT/scripts/conda_deps.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | conda install numpy pyyaml mkl mkl-include setuptools cmake cffi typing 3 | conda install pytorch torchvision -c pytorch # add cuda90 if CUDA 9 4 | conda install visdom dominate -c conda-forge # install visdom and dominate 5 | -------------------------------------------------------------------------------- /ASGIT/scripts/download_cyclegan_model.sh: -------------------------------------------------------------------------------- 1 | FILE=$1 2 | 3 | echo "Note: available models are apple2orange, orange2apple, summer2winter_yosemite, winter2summer_yosemite, horse2zebra, zebra2horse, monet2photo, style_monet, style_cezanne, style_ukiyoe, style_vangogh, sat2map, map2sat, cityscapes_photo2label, cityscapes_label2photo, facades_photo2label, facades_label2photo, iphone2dslr_flower" 4 | 5 | echo "Specified [$FILE]" 6 | 7 | mkdir -p ./checkpoints/${FILE}_pretrained 8 | MODEL_FILE=./checkpoints/${FILE}_pretrained/latest_net_G.pth 9 | URL=http://efrosgans.eecs.berkeley.edu/cyclegan/pretrained_models/$FILE.pth 10 | 11 | wget -N $URL -O $MODEL_FILE 12 | -------------------------------------------------------------------------------- /ASGIT/scripts/download_pix2pix_model.sh: -------------------------------------------------------------------------------- 1 | FILE=$1 2 | 3 | echo "Note: available models are edges2shoes, sat2map, map2sat, facades_label2photo, and day2night" 4 | echo "Specified [$FILE]" 5 | 6 | mkdir -p ./checkpoints/${FILE}_pretrained 7 | MODEL_FILE=./checkpoints/${FILE}_pretrained/latest_net_G.pth 8 | URL=http://efrosgans.eecs.berkeley.edu/pix2pix/models-pytorch/$FILE.pth 9 | 10 | wget -N $URL -O $MODEL_FILE 11 | -------------------------------------------------------------------------------- /ASGIT/scripts/install_deps.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | pip install visdom 3 | pip install dominate 4 | -------------------------------------------------------------------------------- /ASGIT/scripts/test_colorization.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | python test.py --dataroot ./datasets/colorization --name color_pix2pix --model colorization 3 | -------------------------------------------------------------------------------- /ASGIT/scripts/test_cyclegan.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | python test.py --dataroot ./datasets/maps --name maps_cyclegan --model cycle_gan --phase test --no_dropout 3 | -------------------------------------------------------------------------------- /ASGIT/scripts/test_pix2pix.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | python test.py --dataroot ./datasets/facades --name facades_pix2pix --model pix2pix --netG unet_256 --direction BtoA --dataset_mode aligned --norm batch 3 | -------------------------------------------------------------------------------- /ASGIT/scripts/test_single.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | python test.py --dataroot ./datasets/facades/testB/ --name facades_pix2pix --model test --netG unet_256 --direction BtoA --dataset_mode single --norm batch 3 | -------------------------------------------------------------------------------- /ASGIT/scripts/train_asgit.sh: -------------------------------------------------------------------------------- 1 | path='./datasets/SEN1-2/10%_train/ROIs1158_spring_10%_train' 2 | 3 | # train_attn_v2.py中修复了使用visdom存储数据时,训练到epoch=20就自动中断的bug 4 | python train_attn_v2.py \ 5 | --netG resnet_9blocks \ 6 | --netD posthoc_attn \ 7 | --model attn_cycle_gan \ 8 | --concat rmult \ 9 | --dataroot $path \ 10 | --name SEN1-2_10%_spring \ 11 | --mask_size 256 -------------------------------------------------------------------------------- /ASGIT/scripts/train_asgit_v2.sh: -------------------------------------------------------------------------------- 1 | # 修改此处数据集路径 2 | path='./datasets/SEN1-2/10%_train/ROIs1158_spring_10%_train' 3 | 4 | 5 | # 参数中需要修改的: 6 | # (1)--name:实验结果存储路径 7 | # (2)--data_length:数据集长度 8 | # 10%: spring: 1948 summer: 1220 fall: 2028 winter: 1012 9 | # 20%: spring: 3898 summer: 2440 fall: 4058 winter: 2025 10 | # 30%: spring: 9746 summer: 6102 fall: 10145 winter: 5064 11 | 12 | python train_attn_v2.py \ 13 | --netG resnet_9blocks \ 14 | --netD posthoc_attn \ 15 | --model attn_cycle_gan \ 16 | --concat rmult \ 17 | --dataroot $path \ 18 | --name SEN1-2_10%_spring \ 19 | --mask_size 256 \ 20 | --data_length 1948 -------------------------------------------------------------------------------- /ASGIT/scripts/train_colorization.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | python train.py --dataroot ./datasets/colorization --name color_pix2pix --model colorization 3 | -------------------------------------------------------------------------------- /ASGIT/scripts/train_cyclegan.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | python train.py --dataroot ./datasets/maps --name maps_cyclegan --model cycle_gan --pool_size 50 --no_dropout 3 | -------------------------------------------------------------------------------- /ASGIT/scripts/train_pix2pix.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | python train.py --dataroot ./datasets/facades --name facades_pix2pix --model pix2pix --netG unet_256 --direction BtoA --lambda_L1 100 --dataset_mode aligned --norm batch --pool_size 0 3 | -------------------------------------------------------------------------------- /ASGIT/util/__init__.py: -------------------------------------------------------------------------------- 1 | """This package includes a miscellaneous collection of useful helper functions.""" 2 | -------------------------------------------------------------------------------- /ASGIT/util/image_pool.py: -------------------------------------------------------------------------------- 1 | import random 2 | import torch 3 | 4 | 5 | class ImagePool(): 6 | """This class implements an image buffer that stores previously generated images. 7 | 8 | This buffer enables us to update discriminators using a history of generated images 9 | rather than the ones produced by the latest generators. 10 | """ 11 | 12 | def __init__(self, pool_size): 13 | """Initialize the ImagePool class 14 | 15 | Parameters: 16 | pool_size (int) -- the size of image buffer, if pool_size=0, no buffer will be created 17 | """ 18 | self.pool_size = pool_size 19 | if self.pool_size > 0: # create an empty pool 20 | self.num_imgs = 0 21 | self.images = [] 22 | 23 | def query(self, images): 24 | """Return an image from the pool. 25 | 26 | Parameters: 27 | images: the latest generated images from the generator 28 | 29 | Returns images from the buffer. 30 | 31 | By 50/100, the buffer will return input images. 32 | By 50/100, the buffer will return images previously stored in the buffer, 33 | and insert the current images to the buffer. 34 | """ 35 | if self.pool_size == 0: # if the buffer size is 0, do nothing 36 | return images 37 | return_images = [] 38 | for image in images: 39 | image = torch.unsqueeze(image.data, 0) 40 | if self.num_imgs < self.pool_size: # if the buffer is not full; keep inserting current images to the buffer 41 | self.num_imgs = self.num_imgs + 1 42 | self.images.append(image) 43 | return_images.append(image) 44 | else: 45 | p = random.uniform(0, 1) 46 | if p > 0.5: # by 50% chance, the buffer will return a previously stored image, and insert the current image into the buffer 47 | random_id = random.randint(0, self.pool_size - 1) # randint is inclusive 48 | tmp = self.images[random_id].clone() 49 | self.images[random_id] = image 50 | return_images.append(tmp) 51 | else: # by another 50% chance, the buffer will return the current image 52 | return_images.append(image) 53 | return_images = torch.cat(return_images, 0) # collect all the images and return 54 | return return_images 55 | -------------------------------------------------------------------------------- /BicycleGAN/.ipynb_checkpoints/test-checkpoint.py: -------------------------------------------------------------------------------- 1 | import os 2 | from options.test_options import TestOptions 3 | from data import create_dataset 4 | from models import create_model 5 | from util.visualizer import save_images 6 | from itertools import islice 7 | from util import html 8 | 9 | 10 | # options 11 | opt = TestOptions().parse() 12 | opt.num_threads = 1 # test code only supports num_threads=1 13 | opt.batch_size = 1 # test code only supports batch_size=1 14 | opt.serial_batches = True # no shuffle 15 | 16 | # create dataset 17 | dataset = create_dataset(opt) 18 | model = create_model(opt) 19 | model.setup(opt) 20 | model.eval() 21 | print('Loading model %s' % opt.model) 22 | 23 | # create website 24 | web_dir = os.path.join(opt.results_dir, opt.phase + '_sync' if opt.sync else opt.phase) 25 | webpage = html.HTML(web_dir, 'Training = %s, Phase = %s, Class =%s' % (opt.name, opt.phase, opt.name)) 26 | 27 | # sample random z 28 | if opt.sync: 29 | z_samples = model.get_z_random(opt.n_samples + 1, opt.nz) 30 | 31 | # test stage 32 | for i, data in enumerate(islice(dataset, opt.num_test)): 33 | model.set_input(data) 34 | print('process input image %3.3d/%3.3d' % (i, opt.num_test)) 35 | if not opt.sync: 36 | z_samples = model.get_z_random(opt.n_samples + 1, opt.nz) 37 | for nn in range(opt.n_samples + 1): 38 | encode = nn == 0 and not opt.no_encode 39 | real_A, fake_B, real_B = model.test(z_samples[[nn]], encode=encode) 40 | if nn == 0: 41 | images = [real_A, real_B, fake_B] 42 | names = ['input', 'ground truth', 'encoded'] 43 | else: 44 | images.append(fake_B) 45 | names.append('random_sample%2.2d' % nn) 46 | 47 | img_path = 'input_%3.3d' % i 48 | save_images(webpage, images, names, img_path, aspect_ratio=opt.aspect_ratio, width=opt.crop_size) 49 | 50 | webpage.save() 51 | -------------------------------------------------------------------------------- /BicycleGAN/data/.ipynb_checkpoints/image_folder-checkpoint.py: -------------------------------------------------------------------------------- 1 | """A modified image folder class 2 | 3 | We modify the official PyTorch image folder (https://github.com/pytorch/vision/blob/master/torchvision/datasets/folder.py) 4 | so that this class can load images from both current directory and its subdirectories. 5 | """ 6 | 7 | import torch.utils.data as data 8 | 9 | from PIL import Image 10 | import os 11 | import os.path 12 | 13 | IMG_EXTENSIONS = [ 14 | '.jpg', '.JPG', '.jpeg', '.JPEG', 15 | '.png', '.PNG', '.ppm', '.PPM', '.bmp', '.BMP', 16 | ] 17 | 18 | 19 | def is_image_file(filename): 20 | return any(filename.endswith(extension) for extension in IMG_EXTENSIONS) 21 | 22 | 23 | def make_dataset(dir, max_dataset_size=float("inf")): 24 | images = [] 25 | assert os.path.isdir(dir), '%s is not a valid directory' % dir 26 | 27 | for root, _, fnames in sorted(os.walk(dir)): 28 | for fname in fnames: 29 | if is_image_file(fname): 30 | path = os.path.join(root, fname) 31 | images.append(path) 32 | return images[:min(max_dataset_size, len(images))] 33 | 34 | 35 | def default_loader(path): 36 | return Image.open(path).convert('RGB') 37 | 38 | 39 | class ImageFolder(data.Dataset): 40 | 41 | def __init__(self, root, transform=None, return_paths=False, 42 | loader=default_loader): 43 | imgs = make_dataset(root) 44 | if len(imgs) == 0: 45 | raise(RuntimeError("Found 0 images in: " + root + "\n" 46 | "Supported image extensions are: " + 47 | ",".join(IMG_EXTENSIONS))) 48 | 49 | self.root = root 50 | self.imgs = imgs 51 | self.transform = transform 52 | self.return_paths = return_paths 53 | self.loader = loader 54 | 55 | def __getitem__(self, index): 56 | path = self.imgs[index] 57 | img = self.loader(path) 58 | if self.transform is not None: 59 | img = self.transform(img) 60 | if self.return_paths: 61 | return img, path 62 | else: 63 | return img 64 | 65 | def __len__(self): 66 | return len(self.imgs) 67 | -------------------------------------------------------------------------------- /BicycleGAN/data/.ipynb_checkpoints/single_dataset-checkpoint.py: -------------------------------------------------------------------------------- 1 | from data.base_dataset import BaseDataset, get_transform 2 | from data.image_folder import make_dataset 3 | from PIL import Image 4 | 5 | 6 | class SingleDataset(BaseDataset): 7 | """This dataset class can load a set of images specified by the path --dataroot /path/to/data. 8 | 9 | It can be used for generating CycleGAN results only for one side with the model option '-model test'. 10 | """ 11 | 12 | def __init__(self, opt): 13 | """Initialize this dataset class. 14 | 15 | Parameters: 16 | opt (Option class) -- stores all the experiment flags; needs to be a subclass of BaseOptions 17 | """ 18 | BaseDataset.__init__(self, opt) 19 | self.A_paths = sorted(make_dataset(opt.dataroot, opt.max_dataset_size)) 20 | input_nc = self.opt.output_nc if self.opt.direction == 'BtoA' else self.opt.input_nc 21 | self.transform = get_transform(opt, grayscale=(input_nc == 1)) 22 | 23 | def __getitem__(self, index): 24 | """Return a data point and its metadata information. 25 | 26 | Parameters: 27 | index - - a random integer for data indexing 28 | 29 | Returns a dictionary that contains A and A_paths 30 | A(tensor) - - an image in one domain 31 | A_paths(str) - - the path of the image 32 | """ 33 | A_path = self.A_paths[index] 34 | A_img = Image.open(A_path).convert('RGB') 35 | A = self.transform(A_img) 36 | return {'A': A, 'A_paths': A_path} 37 | 38 | def __len__(self): 39 | """Return the total number of images in the dataset.""" 40 | return len(self.A_paths) 41 | -------------------------------------------------------------------------------- /BicycleGAN/data/image_folder.py: -------------------------------------------------------------------------------- 1 | """A modified image folder class 2 | 3 | We modify the official PyTorch image folder (https://github.com/pytorch/vision/blob/master/torchvision/datasets/folder.py) 4 | so that this class can load images from both current directory and its subdirectories. 5 | """ 6 | 7 | import torch.utils.data as data 8 | 9 | from PIL import Image 10 | import os 11 | import os.path 12 | 13 | IMG_EXTENSIONS = [ 14 | '.jpg', '.JPG', '.jpeg', '.JPEG', 15 | '.png', '.PNG', '.ppm', '.PPM', '.bmp', '.BMP', 16 | ] 17 | 18 | 19 | def is_image_file(filename): 20 | return any(filename.endswith(extension) for extension in IMG_EXTENSIONS) 21 | 22 | 23 | def make_dataset(dir, max_dataset_size=float("inf")): 24 | images = [] 25 | assert os.path.isdir(dir), '%s is not a valid directory' % dir 26 | 27 | for root, _, fnames in sorted(os.walk(dir)): 28 | for fname in fnames: 29 | if is_image_file(fname): 30 | path = os.path.join(root, fname) 31 | images.append(path) 32 | return images[:min(max_dataset_size, len(images))] 33 | 34 | 35 | def default_loader(path): 36 | return Image.open(path).convert('RGB') 37 | 38 | 39 | class ImageFolder(data.Dataset): 40 | 41 | def __init__(self, root, transform=None, return_paths=False, 42 | loader=default_loader): 43 | imgs = make_dataset(root) 44 | if len(imgs) == 0: 45 | raise(RuntimeError("Found 0 images in: " + root + "\n" 46 | "Supported image extensions are: " + 47 | ",".join(IMG_EXTENSIONS))) 48 | 49 | self.root = root 50 | self.imgs = imgs 51 | self.transform = transform 52 | self.return_paths = return_paths 53 | self.loader = loader 54 | 55 | def __getitem__(self, index): 56 | path = self.imgs[index] 57 | img = self.loader(path) 58 | if self.transform is not None: 59 | img = self.transform(img) 60 | if self.return_paths: 61 | return img, path 62 | else: 63 | return img 64 | 65 | def __len__(self): 66 | return len(self.imgs) 67 | -------------------------------------------------------------------------------- /BicycleGAN/data/single_dataset.py: -------------------------------------------------------------------------------- 1 | from data.base_dataset import BaseDataset, get_transform 2 | from data.image_folder import make_dataset 3 | from PIL import Image 4 | 5 | 6 | class SingleDataset(BaseDataset): 7 | """This dataset class can load a set of images specified by the path --dataroot /path/to/data. 8 | 9 | It can be used for generating CycleGAN results only for one side with the model option '-model test'. 10 | """ 11 | 12 | def __init__(self, opt): 13 | """Initialize this dataset class. 14 | 15 | Parameters: 16 | opt (Option class) -- stores all the experiment flags; needs to be a subclass of BaseOptions 17 | """ 18 | BaseDataset.__init__(self, opt) 19 | self.A_paths = sorted(make_dataset(opt.dataroot, opt.max_dataset_size)) 20 | input_nc = self.opt.output_nc if self.opt.direction == 'BtoA' else self.opt.input_nc 21 | self.transform = get_transform(opt, grayscale=(input_nc == 1)) 22 | 23 | def __getitem__(self, index): 24 | """Return a data point and its metadata information. 25 | 26 | Parameters: 27 | index - - a random integer for data indexing 28 | 29 | Returns a dictionary that contains A and A_paths 30 | A(tensor) - - an image in one domain 31 | A_paths(str) - - the path of the image 32 | """ 33 | A_path = self.A_paths[index] 34 | A_img = Image.open(A_path).convert('RGB') 35 | A = self.transform(A_img) 36 | return {'A': A, 'A_paths': A_path} 37 | 38 | def __len__(self): 39 | """Return the total number of images in the dataset.""" 40 | return len(self.A_paths) 41 | -------------------------------------------------------------------------------- /BicycleGAN/datasets/.ipynb_checkpoints/download_dataset-checkpoint.sh: -------------------------------------------------------------------------------- 1 | FILE=$1 2 | 3 | if [[ $FILE != "cityscapes" && $FILE != "night2day" && $FILE != "edges2handbags" && $FILE != "edges2shoes" && $FILE != "facades" && $FILE != "maps" ]]; then 4 | echo "Available datasets are cityscapes, night2day, edges2handbags, edges2shoes, facades, maps" 5 | exit 1 6 | fi 7 | 8 | echo "Specified [$FILE]" 9 | 10 | URL=http://efrosgans.eecs.berkeley.edu/pix2pix/datasets/$FILE.tar.gz 11 | TAR_FILE=./datasets/$FILE.tar.gz 12 | TARGET_DIR=./datasets/$FILE/ 13 | wget -N $URL -O $TAR_FILE 14 | mkdir -p $TARGET_DIR 15 | tar -zxvf $TAR_FILE -C ./datasets/ 16 | rm $TAR_FILE 17 | -------------------------------------------------------------------------------- /BicycleGAN/datasets/download_dataset.sh: -------------------------------------------------------------------------------- 1 | FILE=$1 2 | 3 | if [[ $FILE != "cityscapes" && $FILE != "night2day" && $FILE != "edges2handbags" && $FILE != "edges2shoes" && $FILE != "facades" && $FILE != "maps" ]]; then 4 | echo "Available datasets are cityscapes, night2day, edges2handbags, edges2shoes, facades, maps" 5 | exit 1 6 | fi 7 | 8 | echo "Specified [$FILE]" 9 | 10 | URL=http://efrosgans.eecs.berkeley.edu/pix2pix/datasets/$FILE.tar.gz 11 | TAR_FILE=./datasets/$FILE.tar.gz 12 | TARGET_DIR=./datasets/$FILE/ 13 | wget -N $URL -O $TAR_FILE 14 | mkdir -p $TARGET_DIR 15 | tar -zxvf $TAR_FILE -C ./datasets/ 16 | rm $TAR_FILE 17 | -------------------------------------------------------------------------------- /BicycleGAN/datasets/download_mini_dataset.sh: -------------------------------------------------------------------------------- 1 | FILE=$1 2 | echo "Specified [$FILE]" 3 | URL=https://people.eecs.berkeley.edu/~taesung_park/CycleGAN/datasets/$FILE.zip 4 | ZIP_FILE=./datasets/$FILE.zip 5 | TARGET_DIR=./datasets/$FILE/ 6 | wget -N $URL -O $ZIP_FILE 7 | mkdir $TARGET_DIR 8 | unzip $ZIP_FILE -d ./datasets/ 9 | rm $ZIP_FILE 10 | -------------------------------------------------------------------------------- /BicycleGAN/datasets/download_testset.sh: -------------------------------------------------------------------------------- 1 | FILE=$1 2 | URL=http://efrosgans.eecs.berkeley.edu/BicycleGAN/testset/${FILE}.tar.gz 3 | TAR_FILE=./datasets/$FILE.tar.gz 4 | TARGET_DIR=./datasets/$FILE/ 5 | wget -N $URL -O $TAR_FILE 6 | mkdir $TARGET_DIR 7 | tar -zxvf $TAR_FILE -C ./datasets/ 8 | rm $TAR_FILE 9 | -------------------------------------------------------------------------------- /BicycleGAN/metric/.ipynb_checkpoints/psnr_ssim-checkpoint.py: -------------------------------------------------------------------------------- 1 | """ 2 | # > Script for measuring quantitative performances in terms of 3 | # - Structural Similarity Metric (SSIM) 4 | # - Peak Signal to Noise Ratio (PSNR) 5 | # > Maintainer: https://github.com/xahidbuffon 6 | """ 7 | ## python libs 8 | import numpy as np 9 | from PIL import Image 10 | from glob import glob 11 | from os.path import join 12 | from ntpath import basename 13 | ## local libs 14 | from imqual_utils import getSSIM, getPSNR 15 | import cv2 16 | 17 | 18 | ## compares avg ssim and psnr 19 | def SSIMs_PSNRs(gtr_dir, gen_dir, im_res=(256, 256)): 20 | """ 21 | - gtr_dir contain ground-truths 22 | - gen_dir contain generated images 23 | """ 24 | gtr_paths = sorted(glob(join(gtr_dir, "*.*"))) 25 | gen_paths = sorted(glob(join(gen_dir, "*.*"))) 26 | ssims, psnrs = [], [] 27 | for gtr_path, gen_path in zip(gtr_paths, gen_paths): 28 | gtr_f = basename(gtr_path).split('.')[0] 29 | gen_f = basename(gen_path).split('.')[0] 30 | 31 | # read images from two datasets 32 | r_im = Image.open(gtr_path).resize(im_res) 33 | g_im = Image.open(gen_path).resize(im_res) 34 | 35 | # get ssim on RGB channels 36 | ssim = getSSIM(np.array(r_im), np.array(g_im)) 37 | ssims.append(ssim) 38 | # get psnt on L channel (SOTA norm) 39 | r_im = r_im.convert("L"); g_im = g_im.convert("L") 40 | psnr = getPSNR(np.array(r_im), np.array(g_im)) 41 | psnrs.append(psnr) 42 | return np.array(ssims), np.array(psnrs) 43 | 44 | 45 | """ 46 | Get datasets from 47 | - http://irvlab.cs.umn.edu/resources/euvp-dataset 48 | - http://irvlab.cs.umn.edu/resources/ufo-120-dataset 49 | """ 50 | gtr_dir = "Image_translation_codes/pytorch-CycleGAN-and-pix2pix/metric/testA" 51 | 52 | ## generated im paths 53 | gen_dir = "Image_translation_codes/pytorch-CycleGAN-and-pix2pix/metric/testB" 54 | 55 | 56 | ### compute SSIM and PSNR 57 | SSIM_measures, PSNR_measures = SSIMs_PSNRs(gtr_dir, gen_dir) 58 | print ("SSIM on {0} samples".format(len(SSIM_measures))) 59 | print ("Mean: {0} std: {1}".format(np.mean(SSIM_measures), np.std(SSIM_measures))) 60 | 61 | print ("PSNR on {0} samples".format(len(PSNR_measures))) 62 | print ("Mean: {0} std: {1}".format(np.mean(PSNR_measures), np.std(PSNR_measures))) 63 | 64 | 65 | -------------------------------------------------------------------------------- /BicycleGAN/metric/SSIM.py: -------------------------------------------------------------------------------- 1 | from skimage.metrics import structural_similarity as ssim 2 | from PIL import Image 3 | import os 4 | import numpy as np 5 | from glob import glob 6 | from tqdm import tqdm 7 | import argparse 8 | 9 | 10 | def cal_SSIM(x, y): 11 | return ssim(x, y, multichannel=True) 12 | 13 | 14 | def get_dirImage(real_dir, fake_dir): 15 | real_image_paths = sorted(glob(os.path.join(real_dir, "*.*"))) 16 | fake_image_paths = sorted(glob(os.path.join(fake_dir, "*.*"))) 17 | return real_image_paths, fake_image_paths 18 | 19 | 20 | def cal_batch_SSIM(real_paths, fake_paths): 21 | assert len(real_paths) == len(fake_paths) 22 | 23 | SSIMs = [] 24 | for i in tqdm(range((len(real_paths)))): 25 | 26 | img1 = Image.open(real_paths[i]) 27 | img2 = Image.open(fake_paths[i]) 28 | 29 | tmp_ssim = cal_SSIM(np.array(img1), np.array(img2)) 30 | SSIMs.append(tmp_ssim) 31 | return SSIMs 32 | 33 | 34 | if __name__ == '__main__': 35 | parser = argparse.ArgumentParser() 36 | 37 | # parser.add_argument('--dir_root', type=str, default='/workspace/Image_translation_codes/pytorch-CycleGAN-and-pix2pix/results/SEN1_2_20%_spring_cyclegan/test_latest') 38 | parser.add_argument('--fake_dir', type=str, default='fake') 39 | parser.add_argument('--real_dir', type=str, default='real') 40 | 41 | # 只需要修改此处 42 | # root = r'D:\ZYT\Codes\1.Heterogeneous_CD\Image_Translation\pytorch-CycleGAN-and-pix2pix\results\sar2opt_pix2pix\test_latest' 43 | 44 | opt = parser.parse_args() 45 | # real_dir = opt.dir_root + '/' + opt.real_subdir 46 | # fake_dir = opt.dir_root + '/' + opt.fake_subdir 47 | 48 | real_paths, fake_paths = get_dirImage(opt.real_dir, opt.fake_dir) 49 | print(len(real_paths)) 50 | 51 | SSIMs = cal_batch_SSIM(real_paths, fake_paths) 52 | 53 | print("mean SSIM: ", np.mean(SSIMs)) 54 | 55 | -------------------------------------------------------------------------------- /BicycleGAN/metric/psnr_ssim.py: -------------------------------------------------------------------------------- 1 | """ 2 | # > Script for measuring quantitative performances in terms of 3 | # - Structural Similarity Metric (SSIM) 4 | # - Peak Signal to Noise Ratio (PSNR) 5 | # > Maintainer: https://github.com/xahidbuffon 6 | """ 7 | ## python libs 8 | import numpy as np 9 | from PIL import Image 10 | from glob import glob 11 | from os.path import join 12 | from ntpath import basename 13 | ## local libs 14 | from imqual_utils import getSSIM, getPSNR 15 | import cv2 16 | 17 | 18 | ## compares avg ssim and psnr 19 | def SSIMs_PSNRs(gtr_dir, gen_dir, im_res=(256, 256)): 20 | """ 21 | - gtr_dir contain ground-truths 22 | - gen_dir contain generated images 23 | """ 24 | gtr_paths = sorted(glob(join(gtr_dir, "*.*"))) 25 | gen_paths = sorted(glob(join(gen_dir, "*.*"))) 26 | ssims, psnrs = [], [] 27 | for gtr_path, gen_path in zip(gtr_paths, gen_paths): 28 | gtr_f = basename(gtr_path).split('.')[0] 29 | gen_f = basename(gen_path).split('.')[0] 30 | 31 | # read images from two datasets 32 | r_im = Image.open(gtr_path).resize(im_res) 33 | g_im = Image.open(gen_path).resize(im_res) 34 | 35 | # get ssim on RGB channels 36 | ssim = getSSIM(np.array(r_im), np.array(g_im)) 37 | ssims.append(ssim) 38 | # get psnt on L channel (SOTA norm) 39 | r_im = r_im.convert("L"); g_im = g_im.convert("L") 40 | psnr = getPSNR(np.array(r_im), np.array(g_im)) 41 | psnrs.append(psnr) 42 | return np.array(ssims), np.array(psnrs) 43 | 44 | 45 | """ 46 | Get datasets from 47 | - http://irvlab.cs.umn.edu/resources/euvp-dataset 48 | - http://irvlab.cs.umn.edu/resources/ufo-120-dataset 49 | """ 50 | gtr_dir = "Image_translation_codes/BicycleGAN/results/combined_SEN1_2_20%_spring/test/real" 51 | 52 | ## generated im paths 53 | gen_dir = "Image_translation_codes/BicycleGAN/results/combined_SEN1_2_20%_spring/test/fake" 54 | 55 | 56 | ### compute SSIM and PSNR 57 | SSIM_measures, PSNR_measures = SSIMs_PSNRs(gtr_dir, gen_dir) 58 | print ("SSIM on {0} samples".format(len(SSIM_measures))) 59 | print ("Mean: {0} std: {1}".format(np.mean(SSIM_measures), np.std(SSIM_measures))) 60 | 61 | print ("PSNR on {0} samples".format(len(PSNR_measures))) 62 | print ("Mean: {0} std: {1}".format(np.mean(PSNR_measures), np.std(PSNR_measures))) 63 | 64 | 65 | -------------------------------------------------------------------------------- /BicycleGAN/options/.ipynb_checkpoints/__init__-checkpoint.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MarsZhaoYT/SAR2Opt-Heterogeneous-Dataset/18f28ef26be16a9d0bee069d820fd83e2976f7b6/BicycleGAN/options/.ipynb_checkpoints/__init__-checkpoint.py -------------------------------------------------------------------------------- /BicycleGAN/options/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MarsZhaoYT/SAR2Opt-Heterogeneous-Dataset/18f28ef26be16a9d0bee069d820fd83e2976f7b6/BicycleGAN/options/__init__.py -------------------------------------------------------------------------------- /BicycleGAN/options/test_options.py: -------------------------------------------------------------------------------- 1 | from .base_options import BaseOptions 2 | 3 | 4 | class TestOptions(BaseOptions): 5 | def initialize(self, parser): 6 | BaseOptions.initialize(self, parser) 7 | parser.add_argument('--results_dir', type=str, default='../results/', help='saves results here.') 8 | parser.add_argument('--phase', type=str, default='test', help='train, val, test, etc') 9 | parser.add_argument('--num_test', type=int, default=5000, help='how many test images to run') 10 | parser.add_argument('--n_samples', type=int, default=5, help='#samples') 11 | parser.add_argument('--no_encode', action='store_true', help='do not produce encoded image') 12 | parser.add_argument('--sync', action='store_true', help='use the same latent code for different input images') 13 | parser.add_argument('--aspect_ratio', type=float, default=1.0, help='aspect ratio for the results') 14 | parser.add_argument('--eval', action='store_true', help='use eval mode during test time.') 15 | 16 | self.isTrain = False 17 | return parser 18 | -------------------------------------------------------------------------------- /BicycleGAN/options/video_options.py: -------------------------------------------------------------------------------- 1 | from .base_options import BaseOptions 2 | 3 | 4 | class VideoOptions(BaseOptions): 5 | def initialize(self, parser): 6 | BaseOptions.initialize(self, parser) 7 | parser.add_argument('--results_dir', type=str, default='../video/', help='saves results here.') 8 | parser.add_argument('--phase', type=str, default='val', help='train, val, test, etc') 9 | parser.add_argument('--num_test', type=int, default=50, help='how many test images to run') 10 | parser.add_argument('--n_samples', type=int, default=5, help='#samples') 11 | parser.add_argument('--num_frames', type=int, default=4, help='number of the frames used in the morphing sequence') 12 | parser.add_argument('--align_mode', type=str, default='horizontal', help='ways of aligning the input images') 13 | parser.add_argument('--border', type=int, default='0', help='border between results') 14 | parser.add_argument('--seed', type=int, default=50, help='random seed for latent vectors') 15 | parser.add_argument('--fps', type=int, default=8, help='speed of the generated video') 16 | self.isTrain = False 17 | return parser 18 | -------------------------------------------------------------------------------- /BicycleGAN/pretrained_models/.ipynb_checkpoints/download_model-checkpoint.sh: -------------------------------------------------------------------------------- 1 | FILE=$1 2 | 3 | echo "Note: available models are edges2shoes, edges2handbags, night2day, maps, and facades" 4 | echo "downloading [$FILE]" 5 | MODEL_DIR=./pretrained_models/${FILE} 6 | mkdir -p ${MODEL_DIR} 7 | 8 | 9 | MODEL_FILE_G=${MODEL_DIR}/latest_net_G.pth 10 | URL_G=http://efrosgans.eecs.berkeley.edu/BicycleGAN//models/${FILE}_net_G.pth 11 | wget -N $URL_G -O $MODEL_FILE_G 12 | 13 | 14 | MODEL_FILE_E=${MODEL_DIR}/latest_net_E.pth 15 | URL_E=http://efrosgans.eecs.berkeley.edu/BicycleGAN//models/${FILE}_net_E.pth 16 | wget -N $URL_E -O $MODEL_FILE_E 17 | -------------------------------------------------------------------------------- /BicycleGAN/pretrained_models/download_model.sh: -------------------------------------------------------------------------------- 1 | FILE=$1 2 | 3 | echo "Note: available models are edges2shoes, edges2handbags, night2day, maps, and facades" 4 | echo "downloading [$FILE]" 5 | MODEL_DIR=./pretrained_models/${FILE} 6 | mkdir -p ${MODEL_DIR} 7 | 8 | 9 | MODEL_FILE_G=${MODEL_DIR}/latest_net_G.pth 10 | URL_G=http://efrosgans.eecs.berkeley.edu/BicycleGAN//models/${FILE}_net_G.pth 11 | wget -N $URL_G -O $MODEL_FILE_G 12 | 13 | 14 | MODEL_FILE_E=${MODEL_DIR}/latest_net_E.pth 15 | URL_E=http://efrosgans.eecs.berkeley.edu/BicycleGAN//models/${FILE}_net_E.pth 16 | wget -N $URL_E -O $MODEL_FILE_E 17 | -------------------------------------------------------------------------------- /BicycleGAN/results/split_real_fake.py: -------------------------------------------------------------------------------- 1 | import os 2 | from shutil import copy 3 | 4 | def check_dir_exists(path): 5 | if not os.path.exists(path): 6 | os.mkdir(path) 7 | 8 | def split_by_name(dir_path, real_save_path, fake_save_path): 9 | check_dir_exists(real_save_path) 10 | check_dir_exists(fake_save_path) 11 | 12 | if not os.path.exists(real_save_path): 13 | os.mkdir(real_save_path) 14 | 15 | if not os.path.exists(fake_save_path): 16 | os.mkdir(fake_save_path) 17 | 18 | for file in os.listdir(dir_path): 19 | tmp_file = os.path.join(os.path.abspath(dir_path), file) 20 | if 'ground' in file: 21 | copy(tmp_file, real_save_path + os.sep + file) 22 | elif 'random' in file: 23 | copy(tmp_file, fake_save_path + os.sep + file) 24 | 25 | if __name__ == '__main__': 26 | 27 | percent_list = ['10%', '20%'] 28 | season_list = ['spring', 'summer', 'fall', 'winter'] 29 | 30 | for percent in percent_list: 31 | for season in season_list: 32 | dir_name = percent + '_' + season 33 | 34 | dir_path = 'Image_translation_codes/BicycleGAN/results/combined_SEN1_2_' + dir_name + '/test/images' 35 | real_path = 'Image_translation_codes/BicycleGAN/results/combined_SEN1_2_' + dir_name + '/test/real' 36 | fake_path = 'Image_translation_codes/BicycleGAN/results/combined_SEN1_2_' + dir_name + '/test/fake' 37 | split_by_name(dir_path, real_path, fake_path) 38 | -------------------------------------------------------------------------------- /BicycleGAN/run_time.sh: -------------------------------------------------------------------------------- 1 | # set -ex 2 | # models 3 | RESULTS_DIR='Image_translation_codes/BicycleGAN/results/combined_SEN1_2_20%_summer' 4 | 5 | # dataset 6 | CLASS='combined_SEN1_2_20%_summer_bicycle_gan' 7 | DIRECTION='AtoB' # from domain A to domain B 8 | LOAD_SIZE=286 # scale images to this size 9 | CROP_SIZE=256 # then crop to this size 10 | INPUT_NC=3 # number of channels in the input image 11 | 12 | # misc 13 | GPU_ID=0 # gpu id 14 | NUM_TEST=610 # number of input images duirng test 15 | NUM_SAMPLES=1 # number of samples per input images 16 | 17 | 18 | for ((i=1; i<=5; i++)); 19 | do 20 | echo "Processing"$i"th epoch..." 21 | 22 | # command 23 | CUDA_VISIBLE_DEVICES=${GPU_ID} python /workspace/Image_translation_codes/BicycleGAN/test.py \ 24 | --dataroot Image_translation_codes/BicycleGAN/datasets/combined_SEN1_2/20%_train/ROIs1868_summer \ 25 | --results_dir ${RESULTS_DIR} \ 26 | --checkpoints_dir Image_translation_codes/BicycleGAN/checkpoint/combined_SEN1_2_20%_summer/ \ 27 | --name ${CLASS} \ 28 | --direction ${DIRECTION} \ 29 | --load_size ${LOAD_SIZE} \ 30 | --crop_size ${CROP_SIZE} \ 31 | --input_nc ${INPUT_NC} \ 32 | --num_test ${NUM_TEST} \ 33 | --n_samples ${NUM_SAMPLES} \ 34 | --center_crop \ 35 | --no_flip 36 | 37 | done -------------------------------------------------------------------------------- /BicycleGAN/runtime.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | from subprocess import run 3 | import numpy as np 4 | 5 | # spring: 974 summer: 610 fall: 1015 winter: 506 sar2opt: 587/ 627 6 | num_test = 610 7 | 8 | 9 | if __name__ == '__main__': 10 | cmd = "bash Image_translation_codes/BicycleGAN/run_time.sh" 11 | 12 | results = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True) 13 | times = [] 14 | for line in results.stdout.readlines(): 15 | tmp = line.decode("utf-8").strip() 16 | 17 | if 'cost time' in tmp: 18 | times.append((float(tmp.split(": ")[-1])) / num_test) 19 | 20 | print('All cost times:', times) 21 | 22 | avg_time = np.mean(times) 23 | std_time = np.std(times) 24 | 25 | print("Average running time: ", avg_time) 26 | print("Deviation of running time: ", std_time) 27 | 28 | -------------------------------------------------------------------------------- /BicycleGAN/scripts/.ipynb_checkpoints/test_sar2opt-checkpoint.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | # models 3 | RESULTS_DIR='./results/combined_sar2opt' 4 | 5 | # dataset 6 | CLASS='combined_sar2opt' 7 | DIRECTION='BtoA' # from domain A to domain B 8 | LOAD_SIZE=286 # scale images to this size 9 | CROP_SIZE=256 # then crop to this size 10 | INPUT_NC=3 # number of channels in the input image 11 | 12 | # misc 13 | GPU_ID=0 # gpu id 14 | NUM_TEST=627 # number of input images duirng test 15 | NUM_SAMPLES=1 # number of samples per input images 16 | 17 | 18 | # command 19 | CUDA_VISIBLE_DEVICES=${GPU_ID} python ./test.py \ 20 | --dataroot ./datasets/${CLASS} \ 21 | --results_dir ${RESULTS_DIR} \ 22 | --checkpoints_dir checkpoint/ \ 23 | --name ${CLASS} \ 24 | --direction ${DIRECTION} \ 25 | --load_size ${LOAD_SIZE} \ 26 | --crop_size ${CROP_SIZE} \ 27 | --input_nc ${INPUT_NC} \ 28 | --num_test ${NUM_TEST} \ 29 | --n_samples ${NUM_SAMPLES} \ 30 | --center_crop \ 31 | --no_flip 32 | -------------------------------------------------------------------------------- /BicycleGAN/scripts/.ipynb_checkpoints/train-checkpoint.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | CLASS='edges2shoes' # facades, day2night, edges2shoes, edges2handbags, maps 3 | MODEL='bicycle_gan' 4 | CLASS=${1} 5 | GPU_ID=${2} 6 | DISPLAY_ID=$((GPU_ID*10+1)) 7 | PORT=2005 8 | NZ=8 9 | 10 | 11 | CHECKPOINTS_DIR=../checkpoints/${CLASS}/ 12 | DATE=`date '+%d_%m_%Y_%H'` 13 | NAME=${CLASS}_${MODEL}_${DATE} 14 | 15 | 16 | # dataset 17 | NO_FLIP='' 18 | DIRECTION='AtoB' 19 | LOAD_SIZE=286 20 | CROP_SIZE=256 21 | INPUT_NC=3 22 | 23 | # dataset parameters 24 | case ${CLASS} in 25 | 'facades') 26 | NITER=200 27 | NITER_DECAY=200 28 | SAVE_EPOCH=25 29 | DIRECTION='BtoA' 30 | ;; 31 | 'edges2shoes') 32 | NITER=30 33 | NITER_DECAY=30 34 | LOAD_SIZE=256 35 | SAVE_EPOCH=5 36 | INPUT_NC=1 37 | NO_FLIP='--no_flip' 38 | ;; 39 | 'edges2handbags') 40 | NITER=15 41 | NITER_DECAY=15 42 | LOAD_SIZE=256 43 | SAVE_EPOCH=5 44 | INPUT_NC=1 45 | ;; 46 | 'maps') 47 | NITER=200 48 | NITER_DECAY=200 49 | LOAD_SIZE=600 50 | SAVE_EPOCH=25 51 | DIRECTION='BtoA' 52 | ;; 53 | 'day2night') 54 | NITER=50 55 | NITER_DECAY=50 56 | SAVE_EPOCH=10 57 | ;; 58 | *) 59 | echo 'WRONG category'${CLASS} 60 | ;; 61 | esac 62 | 63 | 64 | 65 | # command 66 | CUDA_VISIBLE_DEVICES=${GPU_ID} python ./train.py \ 67 | --display_id ${DISPLAY_ID} \ 68 | --dataroot ./datasets/${CLASS} \ 69 | --name ${NAME} \ 70 | --model ${MODEL} \ 71 | --display_port ${PORT} \ 72 | --direction ${DIRECTION} \ 73 | --checkpoints_dir ${CHECKPOINTS_DIR} \ 74 | --load_size ${LOAD_SIZE} \ 75 | --crop_size ${CROP_SIZE} \ 76 | --nz ${NZ} \ 77 | --save_epoch_freq ${SAVE_EPOCH} \ 78 | --input_nc ${INPUT_NC} \ 79 | --niter ${NITER} \ 80 | --niter_decay ${NITER_DECAY} \ 81 | --use_dropout 82 | -------------------------------------------------------------------------------- /BicycleGAN/scripts/.ipynb_checkpoints/train_combined_SEN1_2_10%_fall-checkpoint.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | MODEL='bicycle_gan' 3 | # dataset details 4 | CLASS='combined_SEN1_2_10%_fall' # facades, day2night, edges2shoes, edges2handbags, maps 5 | NZ=8 6 | NO_FLIP='--no_flip' 7 | DIRECTION='AtoB' 8 | LOAD_SIZE=256 9 | CROP_SIZE=256 10 | INPUT_NC=3 11 | NITER=40 12 | NITER_DECAY=40 13 | 14 | # training 15 | GPU_ID=0 16 | DISPLAY_ID=$((GPU_ID*10+1)) 17 | CHECKPOINTS_DIR=./checkpoint/${CLASS}/ 18 | NAME=${CLASS}_${MODEL} 19 | 20 | # command 21 | CUDA_VISIBLE_DEVICES=${GPU_ID} python ./train.py \ 22 | --display_id ${DISPLAY_ID} \ 23 | --dataroot ./datasets/combined_SEN1_2/10%_train/ROIs1970_fall \ 24 | --name ${NAME} \ 25 | --model ${MODEL} \ 26 | --direction ${DIRECTION} \ 27 | --checkpoints_dir ${CHECKPOINTS_DIR} \ 28 | --load_size ${LOAD_SIZE} \ 29 | --crop_size ${CROP_SIZE} \ 30 | --nz ${NZ} \ 31 | --input_nc ${INPUT_NC} \ 32 | --niter ${NITER} \ 33 | --niter_decay ${NITER_DECAY} \ 34 | --use_dropout 35 | -------------------------------------------------------------------------------- /BicycleGAN/scripts/.ipynb_checkpoints/train_combined_SEN1_2_10%_spring-checkpoint.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | MODEL='bicycle_gan' 3 | # dataset details 4 | CLASS='combined_SEN1_2_10%_spring' # facades, day2night, edges2shoes, edges2handbags, maps 5 | NZ=8 6 | NO_FLIP='--no_flip' 7 | DIRECTION='AtoB' 8 | LOAD_SIZE=256 9 | CROP_SIZE=256 10 | INPUT_NC=3 11 | NITER=40 12 | NITER_DECAY=40 13 | 14 | # training 15 | GPU_ID=1 16 | DISPLAY_ID=$((GPU_ID*10+1)) 17 | CHECKPOINTS_DIR=./checkpoint/${CLASS}/ 18 | NAME=${CLASS}_${MODEL} 19 | 20 | # command 21 | CUDA_VISIBLE_DEVICES=${GPU_ID} python ./train.py \ 22 | --display_id ${DISPLAY_ID} \ 23 | --dataroot ./datasets/combined_SEN1_2/10%_train/ROIs1158_spring \ 24 | --name ${NAME} \ 25 | --model ${MODEL} \ 26 | --direction ${DIRECTION} \ 27 | --checkpoints_dir ${CHECKPOINTS_DIR} \ 28 | --load_size ${LOAD_SIZE} \ 29 | --crop_size ${CROP_SIZE} \ 30 | --nz ${NZ} \ 31 | --input_nc ${INPUT_NC} \ 32 | --niter ${NITER} \ 33 | --niter_decay ${NITER_DECAY} \ 34 | --use_dropout 35 | -------------------------------------------------------------------------------- /BicycleGAN/scripts/.ipynb_checkpoints/train_combined_SEN1_2_10%_summer-checkpoint.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | MODEL='bicycle_gan' 3 | # dataset details 4 | CLASS='combined_SEN1_2_10%_summer' # facades, day2night, edges2shoes, edges2handbags, maps 5 | NZ=8 6 | NO_FLIP='--no_flip' 7 | DIRECTION='AtoB' 8 | LOAD_SIZE=256 9 | CROP_SIZE=256 10 | INPUT_NC=3 11 | NITER=40 12 | NITER_DECAY=40 13 | 14 | # training 15 | GPU_ID=0 16 | DISPLAY_ID=$((GPU_ID*10+1)) 17 | CHECKPOINTS_DIR=./checkpoint/${CLASS}/ 18 | NAME=${CLASS}_${MODEL} 19 | 20 | # command 21 | CUDA_VISIBLE_DEVICES=${GPU_ID} python ./train.py \ 22 | --display_id ${DISPLAY_ID} \ 23 | --dataroot ./datasets/combined_SEN1_2/10%_train/ROIs1868_summer \ 24 | --name ${NAME} \ 25 | --model ${MODEL} \ 26 | --direction ${DIRECTION} \ 27 | --checkpoints_dir ${CHECKPOINTS_DIR} \ 28 | --load_size ${LOAD_SIZE} \ 29 | --crop_size ${CROP_SIZE} \ 30 | --nz ${NZ} \ 31 | --input_nc ${INPUT_NC} \ 32 | --niter ${NITER} \ 33 | --niter_decay ${NITER_DECAY} \ 34 | --use_dropout 35 | -------------------------------------------------------------------------------- /BicycleGAN/scripts/.ipynb_checkpoints/train_combined_SEN1_2_10%_winter-checkpoint.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | MODEL='bicycle_gan' 3 | # dataset details 4 | CLASS='combined_SEN1_2_10%_winter' # facades, day2night, edges2shoes, edges2handbags, maps 5 | NZ=8 6 | NO_FLIP='--no_flip' 7 | DIRECTION='AtoB' 8 | LOAD_SIZE=256 9 | CROP_SIZE=256 10 | INPUT_NC=3 11 | NITER=40 12 | NITER_DECAY=40 13 | 14 | # training 15 | GPU_ID=0 16 | DISPLAY_ID=$((GPU_ID*10+1)) 17 | CHECKPOINTS_DIR=./checkpoint/${CLASS}/ 18 | NAME=${CLASS}_${MODEL} 19 | 20 | # command 21 | CUDA_VISIBLE_DEVICES=${GPU_ID} python ./train.py \ 22 | --display_id ${DISPLAY_ID} \ 23 | --dataroot ./datasets/combined_SEN1_2/10%_train/ROIs2017_winter \ 24 | --name ${NAME} \ 25 | --model ${MODEL} \ 26 | --direction ${DIRECTION} \ 27 | --checkpoints_dir ${CHECKPOINTS_DIR} \ 28 | --load_size ${LOAD_SIZE} \ 29 | --crop_size ${CROP_SIZE} \ 30 | --nz ${NZ} \ 31 | --input_nc ${INPUT_NC} \ 32 | --niter ${NITER} \ 33 | --niter_decay ${NITER_DECAY} \ 34 | --use_dropout 35 | -------------------------------------------------------------------------------- /BicycleGAN/scripts/.ipynb_checkpoints/train_combined_SEN1_2_20%_fall-checkpoint.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | MODEL='bicycle_gan' 3 | # dataset details 4 | CLASS='combined_SEN1_2_20%_fall' # facades, day2night, edges2shoes, edges2handbags, maps 5 | NZ=8 6 | NO_FLIP='--no_flip' 7 | DIRECTION='AtoB' 8 | LOAD_SIZE=256 9 | CROP_SIZE=256 10 | INPUT_NC=3 11 | NITER=40 12 | NITER_DECAY=40 13 | 14 | # training 15 | GPU_ID=0 16 | DISPLAY_ID=$((GPU_ID*10+1)) 17 | CHECKPOINTS_DIR=./checkpoint/${CLASS}/ 18 | NAME=${CLASS}_${MODEL} 19 | 20 | # command 21 | CUDA_VISIBLE_DEVICES=${GPU_ID} python ./train.py \ 22 | --display_id ${DISPLAY_ID} \ 23 | --dataroot ./datasets/combined_SEN1_2/20%_train/ROIs1970_fall \ 24 | --name ${NAME} \ 25 | --model ${MODEL} \ 26 | --direction ${DIRECTION} \ 27 | --checkpoints_dir ${CHECKPOINTS_DIR} \ 28 | --load_size ${LOAD_SIZE} \ 29 | --crop_size ${CROP_SIZE} \ 30 | --nz ${NZ} \ 31 | --input_nc ${INPUT_NC} \ 32 | --niter ${NITER} \ 33 | --niter_decay ${NITER_DECAY} \ 34 | --use_dropout 35 | -------------------------------------------------------------------------------- /BicycleGAN/scripts/.ipynb_checkpoints/train_combined_SEN1_2_20%_spring-checkpoint.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | MODEL='bicycle_gan' 3 | # dataset details 4 | CLASS='combined_SEN1_2_20%_spring' # facades, day2night, edges2shoes, edges2handbags, maps 5 | NZ=8 6 | NO_FLIP='--no_flip' 7 | DIRECTION='AtoB' 8 | LOAD_SIZE=256 9 | CROP_SIZE=256 10 | INPUT_NC=3 11 | NITER=40 12 | NITER_DECAY=40 13 | 14 | # training 15 | GPU_ID=0 16 | DISPLAY_ID=$((GPU_ID*10+1)) 17 | CHECKPOINTS_DIR=./checkpoint/${CLASS}/ 18 | NAME=${CLASS}_${MODEL} 19 | 20 | # command 21 | CUDA_VISIBLE_DEVICES=${GPU_ID} python ./train.py \ 22 | --display_id ${DISPLAY_ID} \ 23 | --dataroot ./datasets/combined_SEN1_2/20%_train/ROIs1158_spring \ 24 | --name ${NAME} \ 25 | --model ${MODEL} \ 26 | --direction ${DIRECTION} \ 27 | --checkpoints_dir ${CHECKPOINTS_DIR} \ 28 | --load_size ${LOAD_SIZE} \ 29 | --crop_size ${CROP_SIZE} \ 30 | --nz ${NZ} \ 31 | --input_nc ${INPUT_NC} \ 32 | --niter ${NITER} \ 33 | --niter_decay ${NITER_DECAY} \ 34 | --use_dropout 35 | -------------------------------------------------------------------------------- /BicycleGAN/scripts/.ipynb_checkpoints/train_combined_SEN1_2_20%_summer-checkpoint.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | MODEL='bicycle_gan' 3 | # dataset details 4 | CLASS='combined_SEN1_2_20%_summer' # facades, day2night, edges2shoes, edges2handbags, maps 5 | NZ=8 6 | NO_FLIP='--no_flip' 7 | DIRECTION='AtoB' 8 | LOAD_SIZE=256 9 | CROP_SIZE=256 10 | INPUT_NC=3 11 | NITER=40 12 | NITER_DECAY=40 13 | 14 | # training 15 | GPU_ID=0 16 | DISPLAY_ID=$((GPU_ID*10+1)) 17 | CHECKPOINTS_DIR=./checkpoint/${CLASS}/ 18 | NAME=${CLASS}_${MODEL} 19 | 20 | # command 21 | CUDA_VISIBLE_DEVICES=${GPU_ID} python ./train.py \ 22 | --display_id ${DISPLAY_ID} \ 23 | --dataroot ./datasets/combined_SEN1_2/20%_train/ROIs1868_summer \ 24 | --name ${NAME} \ 25 | --model ${MODEL} \ 26 | --direction ${DIRECTION} \ 27 | --checkpoints_dir ${CHECKPOINTS_DIR} \ 28 | --load_size ${LOAD_SIZE} \ 29 | --crop_size ${CROP_SIZE} \ 30 | --nz ${NZ} \ 31 | --input_nc ${INPUT_NC} \ 32 | --niter ${NITER} \ 33 | --niter_decay ${NITER_DECAY} \ 34 | --use_dropout 35 | -------------------------------------------------------------------------------- /BicycleGAN/scripts/.ipynb_checkpoints/train_combined_SEN1_2_20%_winter-checkpoint.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | MODEL='bicycle_gan' 3 | # dataset details 4 | CLASS='combined_SEN1_2_20%_winter' # facades, day2night, edges2shoes, edges2handbags, maps 5 | NZ=8 6 | NO_FLIP='--no_flip' 7 | DIRECTION='AtoB' 8 | LOAD_SIZE=256 9 | CROP_SIZE=256 10 | INPUT_NC=3 11 | NITER=40 12 | NITER_DECAY=40 13 | 14 | # training 15 | GPU_ID=0 16 | DISPLAY_ID=$((GPU_ID*10+1)) 17 | CHECKPOINTS_DIR=./checkpoint/${CLASS}/ 18 | NAME=${CLASS}_${MODEL} 19 | 20 | # command 21 | CUDA_VISIBLE_DEVICES=${GPU_ID} python ./train.py \ 22 | --display_id ${DISPLAY_ID} \ 23 | --dataroot ./datasets/combined_SEN1_2/20%_train/ROIs2017_winter \ 24 | --name ${NAME} \ 25 | --model ${MODEL} \ 26 | --direction ${DIRECTION} \ 27 | --checkpoints_dir ${CHECKPOINTS_DIR} \ 28 | --load_size ${LOAD_SIZE} \ 29 | --crop_size ${CROP_SIZE} \ 30 | --nz ${NZ} \ 31 | --input_nc ${INPUT_NC} \ 32 | --niter ${NITER} \ 33 | --niter_decay ${NITER_DECAY} \ 34 | --use_dropout 35 | -------------------------------------------------------------------------------- /BicycleGAN/scripts/.ipynb_checkpoints/train_opt2sar-checkpoint.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | MODEL='bicycle_gan' 3 | # dataset details 4 | CLASS='opt2sar' # facades, day2night, edges2shoes, edges2handbags, maps 5 | NZ=8 6 | NO_FLIP='--no_flip' 7 | DIRECTION='AtoB' 8 | LOAD_SIZE=256 9 | CROP_SIZE=256 10 | INPUT_NC=3 11 | NITER=30 12 | NITER_DECAY=30 13 | 14 | # training 15 | GPU_ID=0 16 | DISPLAY_ID=$((GPU_ID*10+1)) 17 | CHECKPOINTS_DIR=../checkpoint/${CLASS}/ 18 | NAME=${CLASS}_${MODEL} 19 | 20 | # command 21 | CUDA_VISIBLE_DEVICES=${GPU_ID} python ./train.py \ 22 | --display_id ${DISPLAY_ID} \ 23 | --dataroot ./datasets/combined_opt-sar \ 24 | --name ${NAME} \ 25 | --model ${MODEL} \ 26 | --direction ${DIRECTION} \ 27 | --checkpoints_dir ${CHECKPOINTS_DIR} \ 28 | --load_size ${LOAD_SIZE} \ 29 | --crop_size ${CROP_SIZE} \ 30 | --nz ${NZ} \ 31 | --input_nc ${INPUT_NC} \ 32 | --niter ${NITER} \ 33 | --niter_decay ${NITER_DECAY} \ 34 | --use_dropout 35 | -------------------------------------------------------------------------------- /BicycleGAN/scripts/demo/.ipynb_checkpoints/train_edges2shoes-checkpoint.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | MODEL='bicycle_gan' 3 | # dataset details 4 | CLASS='edges2shoes' # facades, day2night, edges2shoes, edges2handbags, maps 5 | NZ=8 6 | NO_FLIP='--no_flip' 7 | DIRECTION='AtoB' 8 | LOAD_SIZE=256 9 | CROP_SIZE=256 10 | INPUT_NC=1 11 | NITER=30 12 | NITER_DECAY=30 13 | 14 | # training 15 | GPU_ID=0 16 | DISPLAY_ID=$((GPU_ID*10+1)) 17 | CHECKPOINTS_DIR=../checkpoint/${CLASS}/ 18 | NAME=${CLASS}_${MODEL} 19 | 20 | # command 21 | CUDA_VISIBLE_DEVICES=${GPU_ID} python ./train.py \ 22 | --display_id ${DISPLAY_ID} \ 23 | --dataroot ./datasets/${CLASS} \ 24 | --name ${NAME} \ 25 | --model ${MODEL} \ 26 | --direction ${DIRECTION} \ 27 | --checkpoints_dir ${CHECKPOINTS_DIR} \ 28 | --load_size ${LOAD_SIZE} \ 29 | --crop_size ${CROP_SIZE} \ 30 | --nz ${NZ} \ 31 | --input_nc ${INPUT_NC} \ 32 | --niter ${NITER} \ 33 | --niter_decay ${NITER_DECAY} \ 34 | --use_dropout 35 | -------------------------------------------------------------------------------- /BicycleGAN/scripts/demo/.ipynb_checkpoints/train_facades-checkpoint.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | MODEL='bicycle_gan' 3 | # dataset details 4 | CLASS='facades' # facades, day2night, edges2shoes, edges2handbags, maps 5 | NZ=8 6 | NO_FLIP='' 7 | DIRECTION='BtoA' 8 | LOAD_SIZE=256 9 | CROP_SIZE=256 10 | INPUT_NC=3 11 | NITER=200 12 | NITER_DECAY=200 13 | SAVE_EPOCH=25 14 | 15 | # training 16 | GPU_ID=0 17 | DISPLAY_ID=$((GPU_ID*10+1)) 18 | CHECKPOINTS_DIR=../checkpoints/${CLASS}/ 19 | NAME=${CLASS}_${MODEL} 20 | 21 | # command 22 | python ./train.py \ 23 | --display_id ${DISPLAY_ID} \ 24 | --dataroot ./datasets/${CLASS} \ 25 | --name ${NAME} \ 26 | --model ${MODEL} \ 27 | --direction ${DIRECTION} \ 28 | --checkpoints_dir ${CHECKPOINTS_DIR} \ 29 | --load_size ${LOAD_SIZE} \ 30 | --crop_size ${CROP_SIZE} \ 31 | --nz ${NZ} \ 32 | --input_nc ${INPUT_NC} \ 33 | --niter ${NITER} \ 34 | --niter_decay ${NITER_DECAY} \ 35 | --save_epoch_freq ${SAVE_EPOCH} \ 36 | --use_dropout 37 | -------------------------------------------------------------------------------- /BicycleGAN/scripts/demo/check_all.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | DOWNLOAD_MODEL=${1} 3 | # test code 4 | echo 'test edges2handbags' 5 | if [ ${DOWNLOAD_MODEL} -eq 1 ] 6 | then 7 | bash ./pretrained_models/download_model.sh edges2handbags 8 | fi 9 | bash ./datasets/download_testset.sh edges2handbags 10 | bash ./scripts/test_edges2handbags.sh 11 | 12 | echo 'test edges2shoes' 13 | if [ ${DOWNLOAD_MODEL} -eq 1 ] 14 | then 15 | bash ./pretrained_models/download_model.sh edges2shoes 16 | fi 17 | bash ./datasets/download_testset.sh edges2shoes 18 | bash ./scripts/test_edges2shoes.sh 19 | 20 | echo 'test facades_label2image' 21 | if [ ${DOWNLOAD_MODEL} -eq 1 ] 22 | then 23 | bash ./pretrained_models/download_model.sh night2day 24 | fi 25 | bash ./datasets/download_testset.sh night2day 26 | bash ./scripts/test_night2day.sh 27 | 28 | echo 'test maps' 29 | if [ ${DOWNLOAD_MODEL} -eq 1 ] 30 | then 31 | bash ./pretrained_models/download_model.sh maps 32 | fi 33 | bash ./datasets/download_testset.sh maps 34 | bash ./scripts/test_maps.sh 35 | 36 | echo 'test facades' 37 | if [ ${DOWNLOAD_MODEL} -eq 1 ] 38 | then 39 | bash ./pretrained_models/download_model.sh facades 40 | fi 41 | bash ./datasets/download_testset.sh facades 42 | bash ./scripts/test_facades.sh 43 | 44 | echo 'test night2day' 45 | if [ ${DOWNLOAD_MODEL} -eq 1 ] 46 | then 47 | bash ./pretrained_models/download_model.sh night2day 48 | fi 49 | bash ./datasets/download_testset.sh night2day 50 | bash ./scripts/test_night2day.sh 51 | 52 | echo 'video edges2shoes' 53 | bash ./scripts/video_edges2shoes.sh 54 | 55 | echo "train a pix2pix model" 56 | bash ./datasets/download_dataset.sh facades 57 | python train.py --dataroot ./datasets/facades --name facades_pix2pix --model pix2pix \ 58 | --netG unet_256 --direction BtoA --lambda_L1 10 --dataset_mode aligned \ 59 | --gan_mode lsgan --norm batch --niter 1 --niter_decay 0 --save_epoch_freq 1 60 | echo "train a bicyclegan model" 61 | python train.py --dataroot ./datasets/facades --name facades_bicycle --model bicycle_gan \ 62 | --direction BtoA --dataset_mode aligned \ 63 | --gan_mode lsgan --norm batch --niter 1 --niter_decay 0 --save_epoch_freq 1 64 | -------------------------------------------------------------------------------- /BicycleGAN/scripts/demo/install_conda.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | conda install -c conda-forge visdom 3 | conda install -c conda-forge dominate 4 | conda install -c conda-forge moviepy 5 | -------------------------------------------------------------------------------- /BicycleGAN/scripts/demo/install_pip.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | sudo -H pip install visdom 3 | sudo -H pip install dominate 4 | sudo -H pip install moviepy 5 | -------------------------------------------------------------------------------- /BicycleGAN/scripts/demo/test_before_push.py: -------------------------------------------------------------------------------- 1 | """Simple script to make sure basic usage such as training, testing, saving and loading runs without errors.""" 2 | import os 3 | 4 | 5 | def run(command): 6 | print(command) 7 | exit_status = os.system(command) 8 | if exit_status > 0: 9 | exit(1) 10 | 11 | 12 | if __name__ == '__main__': 13 | if not os.path.exists('./datasets/mini_pix2pix'): 14 | run('bash ./datasets/download_mini_dataset.sh mini_pix2pix') 15 | 16 | # pix2pix train/test 17 | run('python train.py --model pix2pix --name temp_pix2pix --dataroot ./datasets/mini_pix2pix --niter 1 --niter_decay 0 --save_latest_freq 10 --display_id -1') 18 | 19 | # template train/test 20 | run('python train.py --model template --name temp2 --dataroot ./datasets/mini_pix2pix --niter 1 --niter_decay 0 --save_latest_freq 10 --display_id -1') 21 | 22 | run('bash ./scripts/test_edges2shoes.sh') 23 | run('bash ./scripts/test_edges2shoes.sh --sync') 24 | run('bash ./scripts/video_edges2shoes.sh') 25 | # run('bash ./scripts/train_facades.sh') 26 | -------------------------------------------------------------------------------- /BicycleGAN/scripts/demo/test_edges2handbags.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | # models 3 | RESULTS_DIR='./results/edges2handbags' 4 | G_PATH='./pretrained_models/edges2handbags_net_G.pth' 5 | E_PATH='./pretrained_models/edges2handbags_net_E.pth' 6 | 7 | # dataset 8 | CLASS='edges2handbags' 9 | DIRECTION='AtoB' # from domain A to domain B 10 | LOAD_SIZE=256 # scale images to this size 11 | CROP_SIZE=256 # then crop to this size 12 | INPUT_NC=1 # number of channels in the input image 13 | 14 | # misc 15 | GPU_ID=0 # gpu id 16 | NUM_TEST=10 # number of input images duirng test 17 | NUM_SAMPLES=10 # number of samples per input images 18 | 19 | # command 20 | CUDA_VISIBLE_DEVICES=${GPU_ID} python ./test.py \ 21 | --dataroot ./datasets/${CLASS} \ 22 | --results_dir ${RESULTS_DIR} \ 23 | --checkpoints_dir ./pretrained_models/ \ 24 | --name ${CLASS} \ 25 | --direction ${DIRECTION} \ 26 | --load_size ${LOAD_SIZE} \ 27 | --crop_size ${CROP_SIZE} \ 28 | --input_nc ${INPUT_NC} \ 29 | --num_test ${NUM_TEST} \ 30 | --n_samples ${NUM_SAMPLES} \ 31 | --center_crop \ 32 | --no_flip 33 | -------------------------------------------------------------------------------- /BicycleGAN/scripts/demo/test_edges2shoes.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | # models 3 | RESULTS_DIR='./results/edges2shoes' 4 | G_PATH='./pretrained_models/edges2shoes_net_G.pth' 5 | E_PATH='./pretrained_models/edges2shoes_net_E.pth' 6 | 7 | # dataset 8 | CLASS='edges2shoes' 9 | DIRECTION='AtoB' # from domain A to domain B 10 | LOAD_SIZE=256 # scale images to this size 11 | CROP_SIZE=256 # then crop to this size 12 | INPUT_NC=1 # number of channels in the input image 13 | 14 | # misc 15 | GPU_ID=0 # gpu id 16 | NUM_TEST=10 # number of input images duirng test 17 | NUM_SAMPLES=10 # number of samples per input images 18 | 19 | # command 20 | CUDA_VISIBLE_DEVICES=${GPU_ID} python ./test.py \ 21 | --dataroot ./datasets/${CLASS} \ 22 | --results_dir ${RESULTS_DIR} \ 23 | --checkpoints_dir ./pretrained_models/ \ 24 | --name ${CLASS} \ 25 | --direction ${DIRECTION} \ 26 | --load_size ${LOAD_SIZE} \ 27 | --crop_size ${CROP_SIZE} \ 28 | --input_nc ${INPUT_NC} \ 29 | --num_test ${NUM_TEST} \ 30 | --n_samples ${NUM_SAMPLES} \ 31 | --center_crop \ 32 | --no_flip 33 | -------------------------------------------------------------------------------- /BicycleGAN/scripts/demo/test_facades.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | # models 3 | RESULTS_DIR='./results/facades' 4 | 5 | # dataset 6 | CLASS='facades' 7 | DIRECTION='BtoA' # from domain A to domain B 8 | LOAD_SIZE=286 # scale images to this size 9 | CROP_SIZE=256 # then crop to this size 10 | INPUT_NC=3 # number of channels in the input image 11 | 12 | # misc 13 | GPU_ID=0 # gpu id 14 | NUM_TEST=10 # number of input images duirng test 15 | NUM_SAMPLES=10 # number of samples per input images 16 | 17 | 18 | # command 19 | CUDA_VISIBLE_DEVICES=${GPU_ID} python ./test.py \ 20 | --dataroot ./datasets/${CLASS} \ 21 | --results_dir ${RESULTS_DIR} \ 22 | --checkpoints_dir ./pretrained_models/ \ 23 | --name ${CLASS} \ 24 | --direction ${DIRECTION} \ 25 | --load_size ${LOAD_SIZE} \ 26 | --crop_size ${CROP_SIZE} \ 27 | --input_nc ${INPUT_NC} \ 28 | --num_test ${NUM_TEST} \ 29 | --n_samples ${NUM_SAMPLES} \ 30 | --center_crop \ 31 | --no_flip 32 | -------------------------------------------------------------------------------- /BicycleGAN/scripts/demo/test_night2day.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | # models 3 | RESULTS_DIR='./results/night2day' 4 | G_PATH='./pretrained_models/night2day_net_G.pth' 5 | E_PATH='./pretrained_models/night2day_net_E.pth' 6 | 7 | # dataset 8 | CLASS='night2day' 9 | DIRECTION='AtoB' # from domain A to domain B 10 | LOAD_SIZE=286 # scale images to this size 11 | CROP_SIZE=256 # then crop to this size 12 | INPUT_NC=3 # number of channels in the input image 13 | ASPECT_RATIO=1.4 14 | # change aspect ratio for the test images 15 | 16 | # misc 17 | GPU_ID=0 # gpu id 18 | NUM_TEST=10 # number of input images duirng test 19 | NUM_SAMPLES=10 # number of samples per input images 20 | 21 | 22 | # command 23 | CUDA_VISIBLE_DEVICES=${GPU_ID} python ./test.py \ 24 | --dataroot ./datasets/${CLASS} \ 25 | --results_dir ${RESULTS_DIR} \ 26 | --checkpoints_dir ./pretrained_models/ \ 27 | --name ${CLASS} \ 28 | --direction ${DIRECTION} \ 29 | --load_size ${LOAD_SIZE} \ 30 | --crop_size ${CROP_SIZE} \ 31 | --input_nc ${INPUT_NC} \ 32 | --num_test ${NUM_TEST} \ 33 | --n_samples ${NUM_SAMPLES} \ 34 | --aspect_ratio ${ASPECT_RATIO} \ 35 | --center_crop \ 36 | --no_flip 37 | -------------------------------------------------------------------------------- /BicycleGAN/scripts/demo/train_edges2shoes.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | MODEL='bicycle_gan' 3 | # dataset details 4 | CLASS='edges2shoes' # facades, day2night, edges2shoes, edges2handbags, maps 5 | NZ=8 6 | NO_FLIP='--no_flip' 7 | DIRECTION='AtoB' 8 | LOAD_SIZE=256 9 | CROP_SIZE=256 10 | INPUT_NC=1 11 | NITER=30 12 | NITER_DECAY=30 13 | 14 | # training 15 | GPU_ID=0 16 | DISPLAY_ID=$((GPU_ID*10+1)) 17 | CHECKPOINTS_DIR=../checkpoints/${CLASS}/ 18 | NAME=${CLASS}_${MODEL} 19 | 20 | # command 21 | CUDA_VISIBLE_DEVICES=${GPU_ID} python ./train.py \ 22 | --display_id ${DISPLAY_ID} \ 23 | --dataroot ./datasets/${CLASS} \ 24 | --name ${NAME} \ 25 | --model ${MODEL} \ 26 | --direction ${DIRECTION} \ 27 | --checkpoints_dir ${CHECKPOINTS_DIR} \ 28 | --load_size ${LOAD_SIZE} \ 29 | --crop_size ${CROP_SIZE} \ 30 | --nz ${NZ} \ 31 | --input_nc ${INPUT_NC} \ 32 | --niter ${NITER} \ 33 | --niter_decay ${NITER_DECAY} \ 34 | --use_dropout 35 | -------------------------------------------------------------------------------- /BicycleGAN/scripts/demo/train_facades.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | MODEL='bicycle_gan' 3 | # dataset details 4 | CLASS='facades' # facades, day2night, edges2shoes, edges2handbags, maps 5 | NZ=8 6 | NO_FLIP='' 7 | DIRECTION='BtoA' 8 | LOAD_SIZE=256 9 | CROP_SIZE=256 10 | INPUT_NC=3 11 | NITER=200 12 | NITER_DECAY=200 13 | SAVE_EPOCH=25 14 | 15 | # training 16 | GPU_ID=0 17 | DISPLAY_ID=$((GPU_ID*10+1)) 18 | CHECKPOINTS_DIR=../checkpoints/${CLASS}/ 19 | NAME=${CLASS}_${MODEL} 20 | 21 | # command 22 | python ./train.py \ 23 | --display_id ${DISPLAY_ID} \ 24 | --dataroot ./datasets/${CLASS} \ 25 | --name ${NAME} \ 26 | --model ${MODEL} \ 27 | --direction ${DIRECTION} \ 28 | --checkpoints_dir ${CHECKPOINTS_DIR} \ 29 | --load_size ${LOAD_SIZE} \ 30 | --crop_size ${CROP_SIZE} \ 31 | --nz ${NZ} \ 32 | --input_nc ${INPUT_NC} \ 33 | --niter ${NITER} \ 34 | --niter_decay ${NITER_DECAY} \ 35 | --save_epoch_freq ${SAVE_EPOCH} \ 36 | --use_dropout 37 | -------------------------------------------------------------------------------- /BicycleGAN/scripts/demo/video_edges2shoes.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | # models 3 | RESULTS_DIR='./videos/edges2shoes' 4 | G_PATH='./pretrained_models/edges2shoes_net_G.pth' 5 | 6 | # dataset 7 | CLASS='edges2shoes' 8 | DIRECTION='AtoB' 9 | LOAD_SIZE=256 10 | CROP_SIZE=256 11 | INPUT_NC=1 12 | 13 | # misc 14 | GPU_ID=0 15 | NUM_TEST=5 # number of input images duirng test 16 | NUM_SAMPLES=20 # number of samples per input images 17 | 18 | # command 19 | CUDA_VISIBLE_DEVICES=${GPU_ID} python ./video.py \ 20 | --dataroot ./datasets/${CLASS} \ 21 | --results_dir ${RESULTS_DIR} \ 22 | --checkpoints_dir ./pretrained_models/ \ 23 | --name ${CLASS} \ 24 | --direction ${DIRECTION} \ 25 | --load_size ${LOAD_SIZE} --crop_size ${CROP_SIZE} \ 26 | --input_nc ${INPUT_NC} \ 27 | --num_test ${NUM_TEST} \ 28 | --n_samples ${NUM_SAMPLES} \ 29 | --center_crop \ 30 | --no_flip 31 | -------------------------------------------------------------------------------- /BicycleGAN/scripts/test_scripts/.ipynb_checkpoints/test_combined_SEN1_2_10%_spring-checkpoint.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | # models 3 | RESULTS_DIR='results/combined_SEN1_2_10%_spring' 4 | 5 | # dataset 6 | CLASS='combined_SEN1_2_10%_spring_bicycle_gan' 7 | DIRECTION='AtoB' # from domain A to domain B 8 | LOAD_SIZE=286 # scale images to this size 9 | CROP_SIZE=256 # then crop to this size 10 | INPUT_NC=3 # number of channels in the input image 11 | 12 | # misc 13 | GPU_ID=0 # gpu id 14 | NUM_TEST=627 # number of input images duirng test 15 | NUM_SAMPLES=1 # number of samples per input images 16 | 17 | 18 | # command 19 | CUDA_VISIBLE_DEVICES=${GPU_ID} python ./test.py \ 20 | --dataroot ./datasets/combined_SEN1_2/10%_train/ROIs1158_spring \ 21 | --results_dir ${RESULTS_DIR} \ 22 | --checkpoints_dir /workspace/Image_translation_codes/BicycleGAN/checkpoint/combined_SEN1_2_10%_spring/ \ 23 | --name ${CLASS} \ 24 | --direction ${DIRECTION} \ 25 | --load_size ${LOAD_SIZE} \ 26 | --crop_size ${CROP_SIZE} \ 27 | --input_nc ${INPUT_NC} \ 28 | --num_test ${NUM_TEST} \ 29 | --n_samples ${NUM_SAMPLES} \ 30 | --center_crop \ 31 | --no_flip 32 | -------------------------------------------------------------------------------- /BicycleGAN/scripts/test_scripts/.ipynb_checkpoints/test_maps-checkpoint.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | # models 3 | RESULTS_DIR='./results/maps' 4 | G_PATH='./pretrained_models/map2aerial_net_G.pth' 5 | E_PATH='./pretrained_models/map2aerial_net_E.pth' 6 | 7 | # dataset 8 | CLASS='maps' 9 | DIRECTION='BtoA' # from domain A to domain B 10 | LOAD_SIZE=512 # scale images to this size 11 | CROP_SIZE=512 # then crop to this size 12 | INPUT_NC=3 # number of channels in the input image 13 | ASPECT_RATIO=1.0 14 | # change aspect ratio for the test images 15 | 16 | # misc 17 | GPU_ID=0 # gpu id 18 | NUM_TEST=10 # number of input images duirng test 19 | NUM_SAMPLES=10 # number of samples per input images 20 | 21 | 22 | # command 23 | CUDA_VISIBLE_DEVICES=${GPU_ID} python ./test.py \ 24 | --dataroot ./datasets/${CLASS} \ 25 | --results_dir ${RESULTS_DIR} \ 26 | --checkpoints_dir ./pretrained_models/ \ 27 | --name ${CLASS} \ 28 | --direction ${DIRECTION} \ 29 | --load_size ${LOAD_SIZE} \ 30 | --crop_size ${CROP_SIZE} \ 31 | --input_nc ${INPUT_NC} \ 32 | --num_test ${NUM_TEST} \ 33 | --n_samples ${NUM_SAMPLES} \ 34 | --aspect_ratio ${ASPECT_RATIO} \ 35 | --center_crop \ 36 | --no_flip \ 37 | --no_encode 38 | -------------------------------------------------------------------------------- /BicycleGAN/scripts/test_scripts/.ipynb_checkpoints/test_sar2opt-checkpoint.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | # models 3 | RESULTS_DIR='results/combined_sar2opt' 4 | 5 | # dataset 6 | CLASS='combined_sar2opt' 7 | DIRECTION='AtoB' # from domain A to domain B 8 | LOAD_SIZE=286 # scale images to this size 9 | CROP_SIZE=256 # then crop to this size 10 | INPUT_NC=3 # number of channels in the input image 11 | 12 | # misc 13 | GPU_ID=0 # gpu id 14 | NUM_TEST=627 # number of input images duirng test 15 | NUM_SAMPLES=1 # number of samples per input images 16 | 17 | 18 | # command 19 | CUDA_VISIBLE_DEVICES=${GPU_ID} python ./test.py \ 20 | --dataroot ./datasets/${CLASS} \ 21 | --results_dir ${RESULTS_DIR} \ 22 | --checkpoints_dir checkpoint/ \ 23 | --name ${CLASS} \ 24 | --direction ${DIRECTION} \ 25 | --load_size ${LOAD_SIZE} \ 26 | --crop_size ${CROP_SIZE} \ 27 | --input_nc ${INPUT_NC} \ 28 | --num_test ${NUM_TEST} \ 29 | --n_samples ${NUM_SAMPLES} \ 30 | --center_crop \ 31 | --no_flip 32 | -------------------------------------------------------------------------------- /BicycleGAN/scripts/test_scripts/test_combined_SEN1_2_10%_fall.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | # models 3 | RESULTS_DIR='results/combined_SEN1_2_10%_fall' 4 | 5 | # dataset 6 | CLASS='combined_SEN1_2_10%_fall_bicycle_gan' 7 | DIRECTION='AtoB' # from domain A to domain B 8 | LOAD_SIZE=286 # scale images to this size 9 | CROP_SIZE=256 # then crop to this size 10 | INPUT_NC=3 # number of channels in the input image 11 | 12 | # misc 13 | GPU_ID=0 # gpu id 14 | NUM_TEST=508 # number of input images duirng test 15 | NUM_SAMPLES=1 # number of samples per input images 16 | 17 | 18 | # command 19 | CUDA_VISIBLE_DEVICES=${GPU_ID} python ./test.py \ 20 | --dataroot ./datasets/combined_SEN1_2/10%_train/ROIs1970_fall \ 21 | --results_dir ${RESULTS_DIR} \ 22 | --checkpoints_dir /workspace/Image_translation_codes/BicycleGAN/checkpoint/combined_SEN1_2_10%_fall/ \ 23 | --name ${CLASS} \ 24 | --direction ${DIRECTION} \ 25 | --load_size ${LOAD_SIZE} \ 26 | --crop_size ${CROP_SIZE} \ 27 | --input_nc ${INPUT_NC} \ 28 | --num_test ${NUM_TEST} \ 29 | --n_samples ${NUM_SAMPLES} \ 30 | --center_crop \ 31 | --no_flip 32 | -------------------------------------------------------------------------------- /BicycleGAN/scripts/test_scripts/test_combined_SEN1_2_10%_spring.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | # models 3 | RESULTS_DIR='results/combined_SEN1_2_10%_spring' 4 | 5 | # dataset 6 | CLASS='combined_SEN1_2_10%_spring_bicycle_gan' 7 | DIRECTION='AtoB' # from domain A to domain B 8 | LOAD_SIZE=286 # scale images to this size 9 | CROP_SIZE=256 # then crop to this size 10 | INPUT_NC=3 # number of channels in the input image 11 | 12 | # misc 13 | GPU_ID=0 # gpu id 14 | NUM_TEST=488 # number of input images duirng test 15 | NUM_SAMPLES=1 # number of samples per input images 16 | 17 | 18 | # command 19 | CUDA_VISIBLE_DEVICES=${GPU_ID} python ./test.py \ 20 | --dataroot ./datasets/combined_SEN1_2/10%_train/ROIs1158_spring \ 21 | --results_dir ${RESULTS_DIR} \ 22 | --checkpoints_dir /workspace/Image_translation_codes/BicycleGAN/checkpoint/combined_SEN1_2_10%_spring/ \ 23 | --name ${CLASS} \ 24 | --direction ${DIRECTION} \ 25 | --load_size ${LOAD_SIZE} \ 26 | --crop_size ${CROP_SIZE} \ 27 | --input_nc ${INPUT_NC} \ 28 | --num_test ${NUM_TEST} \ 29 | --n_samples ${NUM_SAMPLES} \ 30 | --center_crop \ 31 | --no_flip 32 | -------------------------------------------------------------------------------- /BicycleGAN/scripts/test_scripts/test_combined_SEN1_2_10%_summer.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | # models 3 | RESULTS_DIR='results/combined_SEN1_2_10%_summer' 4 | 5 | # dataset 6 | CLASS='combined_SEN1_2_10%_summer_bicycle_gan' 7 | DIRECTION='AtoB' # from domain A to domain B 8 | LOAD_SIZE=286 # scale images to this size 9 | CROP_SIZE=256 # then crop to this size 10 | INPUT_NC=3 # number of channels in the input image 11 | 12 | # misc 13 | GPU_ID=0 # gpu id 14 | NUM_TEST=305 # number of input images duirng test 15 | NUM_SAMPLES=1 # number of samples per input images 16 | 17 | 18 | # command 19 | CUDA_VISIBLE_DEVICES=${GPU_ID} python ./test.py \ 20 | --dataroot ./datasets/combined_SEN1_2/10%_train/ROIs1868_summer \ 21 | --results_dir ${RESULTS_DIR} \ 22 | --checkpoints_dir /workspace/Image_translation_codes/BicycleGAN/checkpoint/combined_SEN1_2_10%_summer/ \ 23 | --name ${CLASS} \ 24 | --direction ${DIRECTION} \ 25 | --load_size ${LOAD_SIZE} \ 26 | --crop_size ${CROP_SIZE} \ 27 | --input_nc ${INPUT_NC} \ 28 | --num_test ${NUM_TEST} \ 29 | --n_samples ${NUM_SAMPLES} \ 30 | --center_crop \ 31 | --no_flip 32 | -------------------------------------------------------------------------------- /BicycleGAN/scripts/test_scripts/test_combined_SEN1_2_10%_winter.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | # models 3 | RESULTS_DIR='results/combined_SEN1_2_10%_winter' 4 | 5 | # dataset 6 | CLASS='combined_SEN1_2_10%_winter_bicycle_gan' 7 | DIRECTION='AtoB' # from domain A to domain B 8 | LOAD_SIZE=286 # scale images to this size 9 | CROP_SIZE=256 # then crop to this size 10 | INPUT_NC=3 # number of channels in the input image 11 | 12 | # misc 13 | GPU_ID=0 # gpu id 14 | NUM_TEST=253 # number of input images duirng test 15 | NUM_SAMPLES=1 # number of samples per input images 16 | 17 | 18 | # command 19 | CUDA_VISIBLE_DEVICES=${GPU_ID} python ./test.py \ 20 | --dataroot ./datasets/combined_SEN1_2/10%_train/ROIs2017_winter \ 21 | --results_dir ${RESULTS_DIR} \ 22 | --checkpoints_dir /workspace/Image_translation_codes/BicycleGAN/checkpoint/combined_SEN1_2_10%_winter/ \ 23 | --name ${CLASS} \ 24 | --direction ${DIRECTION} \ 25 | --load_size ${LOAD_SIZE} \ 26 | --crop_size ${CROP_SIZE} \ 27 | --input_nc ${INPUT_NC} \ 28 | --num_test ${NUM_TEST} \ 29 | --n_samples ${NUM_SAMPLES} \ 30 | --center_crop \ 31 | --no_flip 32 | -------------------------------------------------------------------------------- /BicycleGAN/scripts/test_scripts/test_combined_SEN1_2_20%_fall.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | # models 3 | RESULTS_DIR='/workspace/Image_translation_codes/BicycleGAN/results/combined_SEN1_2_20%_fall' 4 | 5 | # dataset 6 | CLASS='combined_SEN1_2_20%_fall_bicycle_gan' 7 | DIRECTION='AtoB' # from domain A to domain B 8 | LOAD_SIZE=286 # scale images to this size 9 | CROP_SIZE=256 # then crop to this size 10 | INPUT_NC=3 # number of channels in the input image 11 | 12 | # misc 13 | GPU_ID=0 # gpu id 14 | NUM_TEST=1015 # number of input images duirng test 15 | NUM_SAMPLES=1 # number of samples per input images 16 | 17 | 18 | # command 19 | CUDA_VISIBLE_DEVICES=${GPU_ID} python ./test.py \ 20 | --dataroot ./datasets/combined_SEN1_2/20%_train/ROIs1970_fall \ 21 | --results_dir ${RESULTS_DIR} \ 22 | --checkpoints_dir /workspace/Image_translation_codes/BicycleGAN/checkpoint/combined_SEN1_2_20%_fall/ \ 23 | --name ${CLASS} \ 24 | --direction ${DIRECTION} \ 25 | --load_size ${LOAD_SIZE} \ 26 | --crop_size ${CROP_SIZE} \ 27 | --input_nc ${INPUT_NC} \ 28 | --num_test ${NUM_TEST} \ 29 | --n_samples ${NUM_SAMPLES} \ 30 | --center_crop \ 31 | --no_flip 32 | -------------------------------------------------------------------------------- /BicycleGAN/scripts/test_scripts/test_combined_SEN1_2_20%_spring.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | # models 3 | RESULTS_DIR='/workspace/Image_translation_codes/BicycleGAN/results/combined_SEN1_2_20%_spring' 4 | 5 | # dataset 6 | CLASS='combined_SEN1_2_20%_spring_bicycle_gan' 7 | DIRECTION='AtoB' # from domain A to domain B 8 | LOAD_SIZE=286 # scale images to this size 9 | CROP_SIZE=256 # then crop to this size 10 | INPUT_NC=3 # number of channels in the input image 11 | 12 | # misc 13 | GPU_ID=0 # gpu id 14 | NUM_TEST=974 # number of input images duirng test 15 | NUM_SAMPLES=1 # number of samples per input images 16 | 17 | 18 | # command 19 | CUDA_VISIBLE_DEVICES=${GPU_ID} python ./test.py \ 20 | --dataroot ./datasets/combined_SEN1_2/20%_train/ROIs1158_spring \ 21 | --results_dir ${RESULTS_DIR} \ 22 | --checkpoints_dir /workspace/Image_translation_codes/BicycleGAN/checkpoint/combined_SEN1_2_20%_spring/ \ 23 | --name ${CLASS} \ 24 | --direction ${DIRECTION} \ 25 | --load_size ${LOAD_SIZE} \ 26 | --crop_size ${CROP_SIZE} \ 27 | --input_nc ${INPUT_NC} \ 28 | --num_test ${NUM_TEST} \ 29 | --n_samples ${NUM_SAMPLES} \ 30 | --center_crop \ 31 | --no_flip 32 | -------------------------------------------------------------------------------- /BicycleGAN/scripts/test_scripts/test_combined_SEN1_2_20%_summer.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | # models 3 | RESULTS_DIR='results/combined_SEN1_2_20%_summer' 4 | 5 | # dataset 6 | CLASS='combined_SEN1_2_20%_summer_bicycle_gan' 7 | DIRECTION='AtoB' # from domain A to domain B 8 | LOAD_SIZE=286 # scale images to this size 9 | CROP_SIZE=256 # then crop to this size 10 | INPUT_NC=3 # number of channels in the input image 11 | 12 | # misc 13 | GPU_ID=0 # gpu id 14 | NUM_TEST=610 # number of input images duirng test 15 | NUM_SAMPLES=1 # number of samples per input images 16 | 17 | 18 | # command 19 | CUDA_VISIBLE_DEVICES=${GPU_ID} python ./test.py \ 20 | --dataroot ./datasets/combined_SEN1_2/20%_train/ROIs1868_summer \ 21 | --results_dir ${RESULTS_DIR} \ 22 | --checkpoints_dir /workspace/Image_translation_codes/BicycleGAN/checkpoint/combined_SEN1_2_20%_summer/ \ 23 | --name ${CLASS} \ 24 | --direction ${DIRECTION} \ 25 | --load_size ${LOAD_SIZE} \ 26 | --crop_size ${CROP_SIZE} \ 27 | --input_nc ${INPUT_NC} \ 28 | --num_test ${NUM_TEST} \ 29 | --n_samples ${NUM_SAMPLES} \ 30 | --center_crop \ 31 | --no_flip 32 | -------------------------------------------------------------------------------- /BicycleGAN/scripts/test_scripts/test_combined_SEN1_2_20%_winter.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | # models 3 | RESULTS_DIR='results/combined_SEN1_2_20%_winter' 4 | 5 | # dataset 6 | CLASS='combined_SEN1_2_20%_winter_bicycle_gan' 7 | DIRECTION='AtoB' # from domain A to domain B 8 | LOAD_SIZE=286 # scale images to this size 9 | CROP_SIZE=256 # then crop to this size 10 | INPUT_NC=3 # number of channels in the input image 11 | 12 | # misc 13 | GPU_ID=0 # gpu id 14 | NUM_TEST=506 # number of input images duirng test 15 | NUM_SAMPLES=1 # number of samples per input images 16 | 17 | 18 | # command 19 | CUDA_VISIBLE_DEVICES=${GPU_ID} python ./test.py \ 20 | --dataroot ./datasets/combined_SEN1_2/20%_train/ROIs2017_winter \ 21 | --results_dir ${RESULTS_DIR} \ 22 | --checkpoints_dir /workspace/Image_translation_codes/BicycleGAN/checkpoint/combined_SEN1_2_20%_winter/ \ 23 | --name ${CLASS} \ 24 | --direction ${DIRECTION} \ 25 | --load_size ${LOAD_SIZE} \ 26 | --crop_size ${CROP_SIZE} \ 27 | --input_nc ${INPUT_NC} \ 28 | --num_test ${NUM_TEST} \ 29 | --n_samples ${NUM_SAMPLES} \ 30 | --center_crop \ 31 | --no_flip 32 | -------------------------------------------------------------------------------- /BicycleGAN/scripts/test_scripts/test_combined_sar2opt.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | # models 3 | RESULTS_DIR='results/combined_sar2opt' 4 | 5 | # dataset 6 | CLASS='combined_sar2opt' 7 | DIRECTION='AtoB' # from domain A to domain B 8 | LOAD_SIZE=286 # scale images to this size 9 | CROP_SIZE=256 # then crop to this size 10 | INPUT_NC=3 # number of channels in the input image 11 | 12 | # misc 13 | GPU_ID=0 # gpu id 14 | NUM_TEST=587 # number of input images duirng test 15 | NUM_SAMPLES=1 # number of samples per input images 16 | 17 | 18 | # command 19 | CUDA_VISIBLE_DEVICES=${GPU_ID} python ./test.py \ 20 | --dataroot ./datasets/${CLASS} \ 21 | --results_dir ${RESULTS_DIR} \ 22 | --checkpoints_dir /workspace/Image_translation_codes/BicycleGAN/checkpoint/ \ 23 | --name ${CLASS} \ 24 | --direction ${DIRECTION} \ 25 | --load_size ${LOAD_SIZE} \ 26 | --crop_size ${CROP_SIZE} \ 27 | --input_nc ${INPUT_NC} \ 28 | --num_test ${NUM_TEST} \ 29 | --n_samples ${NUM_SAMPLES} \ 30 | --center_crop \ 31 | --no_flip 32 | -------------------------------------------------------------------------------- /BicycleGAN/scripts/test_scripts/test_maps.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | # models 3 | RESULTS_DIR='./results/maps' 4 | G_PATH='./pretrained_models/map2aerial_net_G.pth' 5 | E_PATH='./pretrained_models/map2aerial_net_E.pth' 6 | 7 | # dataset 8 | CLASS='maps' 9 | DIRECTION='BtoA' # from domain A to domain B 10 | LOAD_SIZE=512 # scale images to this size 11 | CROP_SIZE=512 # then crop to this size 12 | INPUT_NC=3 # number of channels in the input image 13 | ASPECT_RATIO=1.0 14 | # change aspect ratio for the test images 15 | 16 | # misc 17 | GPU_ID=0 # gpu id 18 | NUM_TEST=10 # number of input images duirng test 19 | NUM_SAMPLES=10 # number of samples per input images 20 | 21 | 22 | # command 23 | CUDA_VISIBLE_DEVICES=${GPU_ID} python ./test.py \ 24 | --dataroot ./datasets/${CLASS} \ 25 | --results_dir ${RESULTS_DIR} \ 26 | --checkpoints_dir ./pretrained_models/ \ 27 | --name ${CLASS} \ 28 | --direction ${DIRECTION} \ 29 | --load_size ${LOAD_SIZE} \ 30 | --crop_size ${CROP_SIZE} \ 31 | --input_nc ${INPUT_NC} \ 32 | --num_test ${NUM_TEST} \ 33 | --n_samples ${NUM_SAMPLES} \ 34 | --aspect_ratio ${ASPECT_RATIO} \ 35 | --center_crop \ 36 | --no_flip \ 37 | --no_encode 38 | -------------------------------------------------------------------------------- /BicycleGAN/scripts/train.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | CLASS='edges2shoes' # facades, day2night, edges2shoes, edges2handbags, maps 3 | MODEL='bicycle_gan' 4 | CLASS=${1} 5 | GPU_ID=${2} 6 | DISPLAY_ID=$((GPU_ID*10+1)) 7 | PORT=2005 8 | NZ=8 9 | 10 | 11 | CHECKPOINTS_DIR=../checkpoints/${CLASS}/ 12 | DATE=`date '+%d_%m_%Y_%H'` 13 | NAME=${CLASS}_${MODEL}_${DATE} 14 | 15 | 16 | # dataset 17 | NO_FLIP='' 18 | DIRECTION='AtoB' 19 | LOAD_SIZE=286 20 | CROP_SIZE=256 21 | INPUT_NC=3 22 | 23 | # dataset parameters 24 | case ${CLASS} in 25 | 'facades') 26 | NITER=200 27 | NITER_DECAY=200 28 | SAVE_EPOCH=25 29 | DIRECTION='BtoA' 30 | ;; 31 | 'edges2shoes') 32 | NITER=30 33 | NITER_DECAY=30 34 | LOAD_SIZE=256 35 | SAVE_EPOCH=5 36 | INPUT_NC=1 37 | NO_FLIP='--no_flip' 38 | ;; 39 | 'edges2handbags') 40 | NITER=15 41 | NITER_DECAY=15 42 | LOAD_SIZE=256 43 | SAVE_EPOCH=5 44 | INPUT_NC=1 45 | ;; 46 | 'maps') 47 | NITER=200 48 | NITER_DECAY=200 49 | LOAD_SIZE=600 50 | SAVE_EPOCH=25 51 | DIRECTION='BtoA' 52 | ;; 53 | 'day2night') 54 | NITER=50 55 | NITER_DECAY=50 56 | SAVE_EPOCH=10 57 | ;; 58 | *) 59 | echo 'WRONG category'${CLASS} 60 | ;; 61 | esac 62 | 63 | 64 | 65 | # command 66 | CUDA_VISIBLE_DEVICES=${GPU_ID} python ./train.py \ 67 | --display_id ${DISPLAY_ID} \ 68 | --dataroot ./datasets/${CLASS} \ 69 | --name ${NAME} \ 70 | --model ${MODEL} \ 71 | --display_port ${PORT} \ 72 | --direction ${DIRECTION} \ 73 | --checkpoints_dir ${CHECKPOINTS_DIR} \ 74 | --load_size ${LOAD_SIZE} \ 75 | --crop_size ${CROP_SIZE} \ 76 | --nz ${NZ} \ 77 | --save_epoch_freq ${SAVE_EPOCH} \ 78 | --input_nc ${INPUT_NC} \ 79 | --niter ${NITER} \ 80 | --niter_decay ${NITER_DECAY} \ 81 | --use_dropout 82 | -------------------------------------------------------------------------------- /BicycleGAN/scripts/train_scripts/.ipynb_checkpoints/train_combined_SEN1_2_10%_spring-checkpoint.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | MODEL='bicycle_gan' 3 | # dataset details 4 | CLASS='combined_SEN1_2_10%_spring' # facades, day2night, edges2shoes, edges2handbags, maps 5 | NZ=8 6 | NO_FLIP='--no_flip' 7 | DIRECTION='AtoB' 8 | LOAD_SIZE=256 9 | CROP_SIZE=256 10 | INPUT_NC=3 11 | NITER=50 12 | NITER_DECAY=50 13 | 14 | # training 15 | GPU_ID=1 16 | DISPLAY_ID=$((GPU_ID*10+1)) 17 | CHECKPOINTS_DIR=./checkpoint/${CLASS}/ 18 | NAME=${CLASS}_${MODEL} 19 | 20 | # command 21 | CUDA_VISIBLE_DEVICES=${GPU_ID} python ./train.py \ 22 | --display_id ${DISPLAY_ID} \ 23 | --dataroot ./datasets/combined_SEN1_2/10%_train/ROIs1158_spring \ 24 | --name ${NAME} \ 25 | --model ${MODEL} \ 26 | --direction ${DIRECTION} \ 27 | --checkpoints_dir ${CHECKPOINTS_DIR} \ 28 | --load_size ${LOAD_SIZE} \ 29 | --crop_size ${CROP_SIZE} \ 30 | --nz ${NZ} \ 31 | --input_nc ${INPUT_NC} \ 32 | --niter ${NITER} \ 33 | --niter_decay ${NITER_DECAY} \ 34 | --use_dropout 35 | -------------------------------------------------------------------------------- /BicycleGAN/scripts/train_scripts/train_combined_SEN1_2_10%_fall.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | MODEL='bicycle_gan' 3 | # dataset details 4 | CLASS='combined_SEN1_2_10%_fall' # facades, day2night, edges2shoes, edges2handbags, maps 5 | NZ=8 6 | NO_FLIP='--no_flip' 7 | DIRECTION='AtoB' 8 | LOAD_SIZE=256 9 | CROP_SIZE=256 10 | INPUT_NC=3 11 | NITER=40 12 | NITER_DECAY=40 13 | 14 | # training 15 | GPU_ID=0 16 | DISPLAY_ID=$((GPU_ID*10+1)) 17 | CHECKPOINTS_DIR=./checkpoint/${CLASS}/ 18 | NAME=${CLASS}_${MODEL} 19 | 20 | # command 21 | CUDA_VISIBLE_DEVICES=${GPU_ID} python ./train.py \ 22 | --display_id ${DISPLAY_ID} \ 23 | --dataroot ./datasets/combined_SEN1_2/10%_train/ROIs1970_fall \ 24 | --name ${NAME} \ 25 | --model ${MODEL} \ 26 | --direction ${DIRECTION} \ 27 | --checkpoints_dir ${CHECKPOINTS_DIR} \ 28 | --load_size ${LOAD_SIZE} \ 29 | --crop_size ${CROP_SIZE} \ 30 | --nz ${NZ} \ 31 | --input_nc ${INPUT_NC} \ 32 | --niter ${NITER} \ 33 | --niter_decay ${NITER_DECAY} \ 34 | --use_dropout 35 | -------------------------------------------------------------------------------- /BicycleGAN/scripts/train_scripts/train_combined_SEN1_2_10%_spring.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | MODEL='bicycle_gan' 3 | # dataset details 4 | CLASS='combined_SEN1_2_10%_spring' # facades, day2night, edges2shoes, edges2handbags, maps 5 | NZ=8 6 | NO_FLIP='--no_flip' 7 | DIRECTION='AtoB' 8 | LOAD_SIZE=256 9 | CROP_SIZE=256 10 | INPUT_NC=3 11 | NITER=50 12 | NITER_DECAY=50 13 | 14 | # training 15 | GPU_ID=0 16 | DISPLAY_ID=$((GPU_ID*10+1)) 17 | CHECKPOINTS_DIR=./checkpoint/${CLASS}/ 18 | NAME=${CLASS}_${MODEL} 19 | 20 | # command 21 | CUDA_VISIBLE_DEVICES=${GPU_ID} python ./train.py \ 22 | --display_id ${DISPLAY_ID} \ 23 | --dataroot ./datasets/combined_SEN1_2/10%_train/ROIs1158_spring \ 24 | --name ${NAME} \ 25 | --model ${MODEL} \ 26 | --direction ${DIRECTION} \ 27 | --checkpoints_dir ${CHECKPOINTS_DIR} \ 28 | --load_size ${LOAD_SIZE} \ 29 | --crop_size ${CROP_SIZE} \ 30 | --nz ${NZ} \ 31 | --input_nc ${INPUT_NC} \ 32 | --niter ${NITER} \ 33 | --niter_decay ${NITER_DECAY} \ 34 | --use_dropout 35 | -------------------------------------------------------------------------------- /BicycleGAN/scripts/train_scripts/train_combined_SEN1_2_10%_summer.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | MODEL='bicycle_gan' 3 | # dataset details 4 | CLASS='combined_SEN1_2_10%_summer' # facades, day2night, edges2shoes, edges2handbags, maps 5 | NZ=8 6 | NO_FLIP='--no_flip' 7 | DIRECTION='AtoB' 8 | LOAD_SIZE=256 9 | CROP_SIZE=256 10 | INPUT_NC=3 11 | NITER=40 12 | NITER_DECAY=40 13 | 14 | # training 15 | GPU_ID=0 16 | DISPLAY_ID=$((GPU_ID*10+1)) 17 | CHECKPOINTS_DIR=./checkpoint/${CLASS}/ 18 | NAME=${CLASS}_${MODEL} 19 | 20 | # command 21 | CUDA_VISIBLE_DEVICES=${GPU_ID} python ./train.py \ 22 | --display_id ${DISPLAY_ID} \ 23 | --dataroot ./datasets/combined_SEN1_2/10%_train/ROIs1868_summer \ 24 | --name ${NAME} \ 25 | --model ${MODEL} \ 26 | --direction ${DIRECTION} \ 27 | --checkpoints_dir ${CHECKPOINTS_DIR} \ 28 | --load_size ${LOAD_SIZE} \ 29 | --crop_size ${CROP_SIZE} \ 30 | --nz ${NZ} \ 31 | --input_nc ${INPUT_NC} \ 32 | --niter ${NITER} \ 33 | --niter_decay ${NITER_DECAY} \ 34 | --use_dropout 35 | -------------------------------------------------------------------------------- /BicycleGAN/scripts/train_scripts/train_combined_SEN1_2_10%_winter.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | MODEL='bicycle_gan' 3 | # dataset details 4 | CLASS='combined_SEN1_2_10%_winter' # facades, day2night, edges2shoes, edges2handbags, maps 5 | NZ=8 6 | NO_FLIP='--no_flip' 7 | DIRECTION='AtoB' 8 | LOAD_SIZE=256 9 | CROP_SIZE=256 10 | INPUT_NC=3 11 | NITER=40 12 | NITER_DECAY=40 13 | 14 | # training 15 | GPU_ID=0 16 | DISPLAY_ID=$((GPU_ID*10+1)) 17 | CHECKPOINTS_DIR=./checkpoint/${CLASS}/ 18 | NAME=${CLASS}_${MODEL} 19 | 20 | # command 21 | CUDA_VISIBLE_DEVICES=${GPU_ID} python ./train.py \ 22 | --display_id ${DISPLAY_ID} \ 23 | --dataroot ./datasets/combined_SEN1_2/10%_train/ROIs2017_winter \ 24 | --name ${NAME} \ 25 | --model ${MODEL} \ 26 | --direction ${DIRECTION} \ 27 | --checkpoints_dir ${CHECKPOINTS_DIR} \ 28 | --load_size ${LOAD_SIZE} \ 29 | --crop_size ${CROP_SIZE} \ 30 | --nz ${NZ} \ 31 | --input_nc ${INPUT_NC} \ 32 | --niter ${NITER} \ 33 | --niter_decay ${NITER_DECAY} \ 34 | --use_dropout 35 | -------------------------------------------------------------------------------- /BicycleGAN/scripts/train_scripts/train_combined_SEN1_2_20%_fall.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | MODEL='bicycle_gan' 3 | # dataset details 4 | CLASS='combined_SEN1_2_20%_fall' # facades, day2night, edges2shoes, edges2handbags, maps 5 | NZ=8 6 | NO_FLIP='--no_flip' 7 | DIRECTION='AtoB' 8 | LOAD_SIZE=256 9 | CROP_SIZE=256 10 | INPUT_NC=3 11 | NITER=40 12 | NITER_DECAY=40 13 | 14 | # training 15 | GPU_ID=0 16 | DISPLAY_ID=$((GPU_ID*10+1)) 17 | CHECKPOINTS_DIR=./checkpoint/${CLASS}/ 18 | NAME=${CLASS}_${MODEL} 19 | 20 | # command 21 | CUDA_VISIBLE_DEVICES=${GPU_ID} python ./train.py \ 22 | --display_id ${DISPLAY_ID} \ 23 | --dataroot ./datasets/combined_SEN1_2/20%_train/ROIs1970_fall \ 24 | --name ${NAME} \ 25 | --model ${MODEL} \ 26 | --direction ${DIRECTION} \ 27 | --checkpoints_dir ${CHECKPOINTS_DIR} \ 28 | --load_size ${LOAD_SIZE} \ 29 | --crop_size ${CROP_SIZE} \ 30 | --nz ${NZ} \ 31 | --input_nc ${INPUT_NC} \ 32 | --niter ${NITER} \ 33 | --niter_decay ${NITER_DECAY} \ 34 | --use_dropout 35 | -------------------------------------------------------------------------------- /BicycleGAN/scripts/train_scripts/train_combined_SEN1_2_20%_spring.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | MODEL='bicycle_gan' 3 | # dataset details 4 | CLASS='combined_SEN1_2_20%_spring' # facades, day2night, edges2shoes, edges2handbags, maps 5 | NZ=8 6 | NO_FLIP='--no_flip' 7 | DIRECTION='AtoB' 8 | LOAD_SIZE=256 9 | CROP_SIZE=256 10 | INPUT_NC=3 11 | NITER=40 12 | NITER_DECAY=40 13 | 14 | # training 15 | GPU_ID=0 16 | DISPLAY_ID=$((GPU_ID*10+1)) 17 | CHECKPOINTS_DIR=./checkpoint/${CLASS}/ 18 | NAME=${CLASS}_${MODEL} 19 | 20 | # command 21 | CUDA_VISIBLE_DEVICES=${GPU_ID} python ./train.py \ 22 | --display_id ${DISPLAY_ID} \ 23 | --dataroot ./datasets/combined_SEN1_2/20%_train/ROIs1158_spring \ 24 | --name ${NAME} \ 25 | --model ${MODEL} \ 26 | --direction ${DIRECTION} \ 27 | --checkpoints_dir ${CHECKPOINTS_DIR} \ 28 | --load_size ${LOAD_SIZE} \ 29 | --crop_size ${CROP_SIZE} \ 30 | --nz ${NZ} \ 31 | --input_nc ${INPUT_NC} \ 32 | --niter ${NITER} \ 33 | --niter_decay ${NITER_DECAY} \ 34 | --use_dropout 35 | -------------------------------------------------------------------------------- /BicycleGAN/scripts/train_scripts/train_combined_SEN1_2_20%_summer.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | MODEL='bicycle_gan' 3 | # dataset details 4 | CLASS='combined_SEN1_2_20%_summer' # facades, day2night, edges2shoes, edges2handbags, maps 5 | NZ=8 6 | NO_FLIP='--no_flip' 7 | DIRECTION='AtoB' 8 | LOAD_SIZE=256 9 | CROP_SIZE=256 10 | INPUT_NC=3 11 | NITER=40 12 | NITER_DECAY=40 13 | 14 | # training 15 | GPU_ID=0 16 | DISPLAY_ID=$((GPU_ID*10+1)) 17 | CHECKPOINTS_DIR=./checkpoint/${CLASS}/ 18 | NAME=${CLASS}_${MODEL} 19 | 20 | # command 21 | CUDA_VISIBLE_DEVICES=${GPU_ID} python ./train.py \ 22 | --display_id ${DISPLAY_ID} \ 23 | --dataroot ./datasets/combined_SEN1_2/20%_train/ROIs1868_summer \ 24 | --name ${NAME} \ 25 | --model ${MODEL} \ 26 | --direction ${DIRECTION} \ 27 | --checkpoints_dir ${CHECKPOINTS_DIR} \ 28 | --load_size ${LOAD_SIZE} \ 29 | --crop_size ${CROP_SIZE} \ 30 | --nz ${NZ} \ 31 | --input_nc ${INPUT_NC} \ 32 | --niter ${NITER} \ 33 | --niter_decay ${NITER_DECAY} \ 34 | --use_dropout 35 | -------------------------------------------------------------------------------- /BicycleGAN/scripts/train_scripts/train_combined_SEN1_2_20%_winter.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | MODEL='bicycle_gan' 3 | # dataset details 4 | CLASS='combined_SEN1_2_20%_winter' # facades, day2night, edges2shoes, edges2handbags, maps 5 | NZ=8 6 | NO_FLIP='--no_flip' 7 | DIRECTION='AtoB' 8 | LOAD_SIZE=256 9 | CROP_SIZE=256 10 | INPUT_NC=3 11 | NITER=40 12 | NITER_DECAY=40 13 | 14 | # training 15 | GPU_ID=0 16 | DISPLAY_ID=$((GPU_ID*10+1)) 17 | CHECKPOINTS_DIR=./checkpoint/${CLASS}/ 18 | NAME=${CLASS}_${MODEL} 19 | 20 | # command 21 | CUDA_VISIBLE_DEVICES=${GPU_ID} python ./train.py \ 22 | --display_id ${DISPLAY_ID} \ 23 | --dataroot ./datasets/combined_SEN1_2/20%_train/ROIs2017_winter \ 24 | --name ${NAME} \ 25 | --model ${MODEL} \ 26 | --direction ${DIRECTION} \ 27 | --checkpoints_dir ${CHECKPOINTS_DIR} \ 28 | --load_size ${LOAD_SIZE} \ 29 | --crop_size ${CROP_SIZE} \ 30 | --nz ${NZ} \ 31 | --input_nc ${INPUT_NC} \ 32 | --niter ${NITER} \ 33 | --niter_decay ${NITER_DECAY} \ 34 | --use_dropout 35 | -------------------------------------------------------------------------------- /BicycleGAN/scripts/train_scripts/train_combined_sar2opt.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | MODEL='bicycle_gan' 3 | # dataset details 4 | CLASS='sar2opt' # facades, day2night, edges2shoes, edges2handbags, maps 5 | NZ=8 6 | NO_FLIP='--no_flip' 7 | DIRECTION='AtoB' 8 | LOAD_SIZE=256 9 | CROP_SIZE=256 10 | INPUT_NC=3 11 | NITER=200 12 | NITER_DECAY=200 13 | 14 | # training 15 | GPU_ID=1 16 | DISPLAY_ID=$((GPU_ID*10+1)) 17 | CHECKPOINTS_DIR=./checkpoint/${CLASS}/ 18 | NAME=${CLASS}_${MODEL} 19 | 20 | # command 21 | CUDA_VISIBLE_DEVICES=${GPU_ID} python ./train.py \ 22 | --display_id ${DISPLAY_ID} \ 23 | --dataroot ./datasets/combined_opt-sar \ 24 | --name ${NAME} \ 25 | --model ${MODEL} \ 26 | --direction ${DIRECTION} \ 27 | --checkpoints_dir ${CHECKPOINTS_DIR} \ 28 | --load_size ${LOAD_SIZE} \ 29 | --crop_size ${CROP_SIZE} \ 30 | --nz ${NZ} \ 31 | --input_nc ${INPUT_NC} \ 32 | --niter ${NITER} \ 33 | --niter_decay ${NITER_DECAY} \ 34 | --use_dropout 35 | -------------------------------------------------------------------------------- /BicycleGAN/test.py: -------------------------------------------------------------------------------- 1 | import os 2 | from options.test_options import TestOptions 3 | from data import create_dataset 4 | from models import create_model 5 | from util.visualizer import save_images 6 | from itertools import islice 7 | from util import html 8 | import time 9 | 10 | start_time = time.time() 11 | 12 | # options 13 | opt = TestOptions().parse() 14 | opt.num_threads = 1 # test code only supports num_threads=1 15 | opt.batch_size = 1 # test code only supports batch_size=1 16 | opt.serial_batches = True # no shuffle 17 | 18 | # create dataset 19 | dataset = create_dataset(opt) 20 | model = create_model(opt) 21 | model.setup(opt) 22 | model.eval() 23 | print('Loading model %s' % opt.model) 24 | 25 | # create website 26 | web_dir = os.path.join(opt.results_dir, opt.phase + '_sync' if opt.sync else opt.phase) 27 | webpage = html.HTML(web_dir, 'Training = %s, Phase = %s, Class =%s' % (opt.name, opt.phase, opt.name)) 28 | 29 | # sample random z 30 | if opt.sync: 31 | z_samples = model.get_z_random(opt.n_samples + 1, opt.nz) 32 | 33 | # test stage 34 | for i, data in enumerate(islice(dataset, opt.num_test)): 35 | model.set_input(data) 36 | print('process input image %3.3d/%3.3d' % (i, opt.num_test)) 37 | if not opt.sync: 38 | z_samples = model.get_z_random(opt.n_samples + 1, opt.nz) 39 | for nn in range(opt.n_samples + 1): 40 | encode = nn == 0 and not opt.no_encode 41 | real_A, fake_B, real_B = model.test(z_samples[[nn]], encode=encode) 42 | if nn == 0: 43 | images = [real_A, real_B, fake_B] 44 | names = ['input', 'ground truth', 'encoded'] 45 | else: 46 | images.append(fake_B) 47 | names.append('random_sample%2.2d' % nn) 48 | 49 | img_path = 'input_%3.3d' % i 50 | save_images(webpage, images, names, img_path, aspect_ratio=opt.aspect_ratio, width=opt.crop_size) 51 | 52 | webpage.save() 53 | 54 | end_time = time.time() 55 | cost_time = end_time - start_time 56 | print('cost time: ', cost_time) -------------------------------------------------------------------------------- /BicycleGAN/util/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MarsZhaoYT/SAR2Opt-Heterogeneous-Dataset/18f28ef26be16a9d0bee069d820fd83e2976f7b6/BicycleGAN/util/__init__.py -------------------------------------------------------------------------------- /CUT/data/.ipynb_checkpoints/image_folder-checkpoint.py: -------------------------------------------------------------------------------- 1 | """A modified image folder class 2 | 3 | We modify the official PyTorch image folder (https://github.com/pytorch/vision/blob/master/torchvision/datasets/folder.py) 4 | so that this class can load images from both current directory and its subdirectories. 5 | """ 6 | 7 | import torch.utils.data as data 8 | 9 | from PIL import Image 10 | import os 11 | import os.path 12 | 13 | IMG_EXTENSIONS = [ 14 | '.jpg', '.JPG', '.jpeg', '.JPEG', 15 | '.png', '.PNG', '.ppm', '.PPM', '.bmp', '.BMP', 16 | '.tif', '.TIF', '.tiff', '.TIFF', 17 | ] 18 | 19 | 20 | def is_image_file(filename): 21 | return any(filename.endswith(extension) for extension in IMG_EXTENSIONS) 22 | 23 | 24 | def make_dataset(dir, max_dataset_size=float("inf")): 25 | images = [] 26 | assert os.path.isdir(dir) or os.path.islink(dir), '%s is not a valid directory' % dir 27 | 28 | for root, _, fnames in sorted(os.walk(dir, followlinks=True)): 29 | for fname in fnames: 30 | if is_image_file(fname): 31 | path = os.path.join(root, fname) 32 | images.append(path) 33 | return images[:min(max_dataset_size, len(images))] 34 | 35 | 36 | def default_loader(path): 37 | return Image.open(path).convert('RGB') 38 | 39 | 40 | class ImageFolder(data.Dataset): 41 | 42 | def __init__(self, root, transform=None, return_paths=False, 43 | loader=default_loader): 44 | imgs = make_dataset(root) 45 | if len(imgs) == 0: 46 | raise(RuntimeError("Found 0 images in: " + root + "\n" 47 | "Supported image extensions are: " + 48 | ",".join(IMG_EXTENSIONS))) 49 | 50 | self.root = root 51 | self.imgs = imgs 52 | self.transform = transform 53 | self.return_paths = return_paths 54 | self.loader = loader 55 | 56 | def __getitem__(self, index): 57 | path = self.imgs[index] 58 | img = self.loader(path) 59 | if self.transform is not None: 60 | img = self.transform(img) 61 | if self.return_paths: 62 | return img, path 63 | else: 64 | return img 65 | 66 | def __len__(self): 67 | return len(self.imgs) 68 | -------------------------------------------------------------------------------- /CUT/data/image_folder.py: -------------------------------------------------------------------------------- 1 | """A modified image folder class 2 | 3 | We modify the official PyTorch image folder (https://github.com/pytorch/vision/blob/master/torchvision/datasets/folder.py) 4 | so that this class can load images from both current directory and its subdirectories. 5 | """ 6 | 7 | import torch.utils.data as data 8 | 9 | from PIL import Image 10 | import os 11 | import os.path 12 | 13 | IMG_EXTENSIONS = [ 14 | '.jpg', '.JPG', '.jpeg', '.JPEG', 15 | '.png', '.PNG', '.ppm', '.PPM', '.bmp', '.BMP', 16 | '.tif', '.TIF', '.tiff', '.TIFF', 17 | ] 18 | 19 | 20 | def is_image_file(filename): 21 | return any(filename.endswith(extension) for extension in IMG_EXTENSIONS) 22 | 23 | 24 | def make_dataset(dir, max_dataset_size=float("inf")): 25 | images = [] 26 | assert os.path.isdir(dir) or os.path.islink(dir), '%s is not a valid directory' % dir 27 | 28 | for root, _, fnames in sorted(os.walk(dir, followlinks=True)): 29 | for fname in fnames: 30 | if is_image_file(fname): 31 | path = os.path.join(root, fname) 32 | images.append(path) 33 | return images[:min(max_dataset_size, len(images))] 34 | 35 | 36 | def default_loader(path): 37 | return Image.open(path).convert('RGB') 38 | 39 | 40 | class ImageFolder(data.Dataset): 41 | 42 | def __init__(self, root, transform=None, return_paths=False, 43 | loader=default_loader): 44 | imgs = make_dataset(root) 45 | if len(imgs) == 0: 46 | raise(RuntimeError("Found 0 images in: " + root + "\n" 47 | "Supported image extensions are: " + 48 | ",".join(IMG_EXTENSIONS))) 49 | 50 | self.root = root 51 | self.imgs = imgs 52 | self.transform = transform 53 | self.return_paths = return_paths 54 | self.loader = loader 55 | 56 | def __getitem__(self, index): 57 | path = self.imgs[index] 58 | img = self.loader(path) 59 | if self.transform is not None: 60 | img = self.transform(img) 61 | if self.return_paths: 62 | return img, path 63 | else: 64 | return img 65 | 66 | def __len__(self): 67 | return len(self.imgs) 68 | -------------------------------------------------------------------------------- /CUT/data/single_dataset.py: -------------------------------------------------------------------------------- 1 | from data.base_dataset import BaseDataset, get_transform 2 | from data.image_folder import make_dataset 3 | from PIL import Image 4 | 5 | 6 | class SingleDataset(BaseDataset): 7 | """This dataset class can load a set of images specified by the path --dataroot /path/to/data. 8 | 9 | It can be used for generating CycleGAN results only for one side with the model option '-model test'. 10 | """ 11 | 12 | def __init__(self, opt): 13 | """Initialize this dataset class. 14 | 15 | Parameters: 16 | opt (Option class) -- stores all the experiment flags; needs to be a subclass of BaseOptions 17 | """ 18 | BaseDataset.__init__(self, opt) 19 | self.A_paths = sorted(make_dataset(opt.dataroot, opt.max_dataset_size)) 20 | input_nc = self.opt.output_nc if self.opt.direction == 'BtoA' else self.opt.input_nc 21 | self.transform = get_transform(opt, grayscale=(input_nc == 1)) 22 | 23 | def __getitem__(self, index): 24 | """Return a data point and its metadata information. 25 | 26 | Parameters: 27 | index - - a random integer for data indexing 28 | 29 | Returns a dictionary that contains A and A_paths 30 | A(tensor) - - an image in one domain 31 | A_paths(str) - - the path of the image 32 | """ 33 | A_path = self.A_paths[index] 34 | A_img = Image.open(A_path).convert('RGB') 35 | A = self.transform(A_img) 36 | return {'A': A, 'A_paths': A_path} 37 | 38 | def __len__(self): 39 | """Return the total number of images in the dataset.""" 40 | return len(self.A_paths) 41 | -------------------------------------------------------------------------------- /CUT/datasets/combine_A_and_B.py: -------------------------------------------------------------------------------- 1 | import os 2 | import numpy as np 3 | import cv2 4 | import argparse 5 | 6 | parser = argparse.ArgumentParser('create image pairs') 7 | parser.add_argument('--fold_A', dest='fold_A', help='input directory for image A', type=str, default='../dataset/50kshoes_edges') 8 | parser.add_argument('--fold_B', dest='fold_B', help='input directory for image B', type=str, default='../dataset/50kshoes_jpg') 9 | parser.add_argument('--fold_AB', dest='fold_AB', help='output directory', type=str, default='../dataset/test_AB') 10 | parser.add_argument('--num_imgs', dest='num_imgs', help='number of images', type=int, default=1000000) 11 | parser.add_argument('--use_AB', dest='use_AB', help='if true: (0001_A, 0001_B) to (0001_AB)', action='store_true') 12 | args = parser.parse_args() 13 | 14 | for arg in vars(args): 15 | print('[%s] = ' % arg, getattr(args, arg)) 16 | 17 | splits = os.listdir(args.fold_A) 18 | 19 | for sp in splits: 20 | img_fold_A = os.path.join(args.fold_A, sp) 21 | img_fold_B = os.path.join(args.fold_B, sp) 22 | img_list = os.listdir(img_fold_A) 23 | if args.use_AB: 24 | img_list = [img_path for img_path in img_list if '_A.' in img_path] 25 | 26 | num_imgs = min(args.num_imgs, len(img_list)) 27 | print('split = %s, use %d/%d images' % (sp, num_imgs, len(img_list))) 28 | img_fold_AB = os.path.join(args.fold_AB, sp) 29 | if not os.path.isdir(img_fold_AB): 30 | os.makedirs(img_fold_AB) 31 | print('split = %s, number of images = %d' % (sp, num_imgs)) 32 | for n in range(num_imgs): 33 | name_A = img_list[n] 34 | path_A = os.path.join(img_fold_A, name_A) 35 | if args.use_AB: 36 | name_B = name_A.replace('_A.', '_B.') 37 | else: 38 | name_B = name_A 39 | path_B = os.path.join(img_fold_B, name_B) 40 | if os.path.isfile(path_A) and os.path.isfile(path_B): 41 | name_AB = name_A 42 | if args.use_AB: 43 | name_AB = name_AB.replace('_A.', '.') # remove _A 44 | path_AB = os.path.join(img_fold_AB, name_AB) 45 | im_A = cv2.imread(path_A, 1) # python2: cv2.CV_LOAD_IMAGE_COLOR; python3: cv2.IMREAD_COLOR 46 | im_B = cv2.imread(path_B, 1) # python2: cv2.CV_LOAD_IMAGE_COLOR; python3: cv2.IMREAD_COLOR 47 | im_AB = np.concatenate([im_A, im_B], 1) 48 | cv2.imwrite(path_AB, im_AB) 49 | -------------------------------------------------------------------------------- /CUT/datasets/download_cut_dataset.sh: -------------------------------------------------------------------------------- 1 | FILE=$1 2 | 3 | if [[ $FILE != "ae_photos" && $FILE != "apple2orange" && $FILE != "summer2winter_yosemite" && $FILE != "horse2zebra" && $FILE != "monet2photo" && $FILE != "cezanne2photo" && $FILE != "ukiyoe2photo" && $FILE != "vangogh2photo" && $FILE != "maps" && $FILE != "cityscapes" && $FILE != "facades" && $FILE != "iphone2dslr_flower" && $FILE != "mini" && $FILE != "mini_pix2pix" && $FILE != "mini_colorization" && $FILE != "grumpifycat" ]]; then 4 | echo "Available datasets are: apple2orange, summer2winter_yosemite, horse2zebra, monet2photo, cezanne2photo, ukiyoe2photo, vangogh2photo, maps, cityscapes, facades, iphone2dslr_flower, ae_photos, grumpifycat" 5 | exit 1 6 | fi 7 | 8 | if [[ $FILE == "cityscapes" ]]; then 9 | echo "Due to license issue, we cannot provide the Cityscapes dataset from our repository. Please download the Cityscapes dataset from https://cityscapes-dataset.com, and use the script ./datasets/prepare_cityscapes_dataset.py." 10 | echo "You need to download gtFine_trainvaltest.zip and leftImg8bit_trainvaltest.zip. For further instruction, please read ./datasets/prepare_cityscapes_dataset.py" 11 | exit 1 12 | fi 13 | 14 | echo "Specified [$FILE]" 15 | URL=https://people.eecs.berkeley.edu/~taesung_park/CycleGAN/datasets/$FILE.zip 16 | ZIP_FILE=./datasets/$FILE.zip 17 | TARGET_DIR=./datasets/$FILE/ 18 | wget --no-check-certificate -N $URL -O $ZIP_FILE 19 | mkdir $TARGET_DIR 20 | unzip $ZIP_FILE -d ./datasets/ 21 | rm $ZIP_FILE 22 | -------------------------------------------------------------------------------- /CUT/datasets/make_dataset_aligned.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | from PIL import Image 4 | 5 | 6 | def get_file_paths(folder): 7 | image_file_paths = [] 8 | for root, dirs, filenames in os.walk(folder): 9 | filenames = sorted(filenames) 10 | for filename in filenames: 11 | input_path = os.path.abspath(root) 12 | file_path = os.path.join(input_path, filename) 13 | if filename.endswith('.png') or filename.endswith('.jpg'): 14 | image_file_paths.append(file_path) 15 | 16 | break # prevent descending into subfolders 17 | return image_file_paths 18 | 19 | 20 | def align_images(a_file_paths, b_file_paths, target_path): 21 | if not os.path.exists(target_path): 22 | os.makedirs(target_path) 23 | 24 | for i in range(len(a_file_paths)): 25 | img_a = Image.open(a_file_paths[i]) 26 | img_b = Image.open(b_file_paths[i]) 27 | assert(img_a.size == img_b.size) 28 | 29 | aligned_image = Image.new("RGB", (img_a.size[0] * 2, img_a.size[1])) 30 | aligned_image.paste(img_a, (0, 0)) 31 | aligned_image.paste(img_b, (img_a.size[0], 0)) 32 | aligned_image.save(os.path.join(target_path, '{:04d}.jpg'.format(i))) 33 | 34 | 35 | if __name__ == '__main__': 36 | import argparse 37 | parser = argparse.ArgumentParser() 38 | parser.add_argument( 39 | '--dataset-path', 40 | dest='dataset_path', 41 | help='Which folder to process (it should have subfolders testA, testB, trainA and trainB' 42 | ) 43 | args = parser.parse_args() 44 | 45 | dataset_folder = args.dataset_path 46 | print(dataset_folder) 47 | 48 | test_a_path = os.path.join(dataset_folder, 'testA') 49 | test_b_path = os.path.join(dataset_folder, 'testB') 50 | test_a_file_paths = get_file_paths(test_a_path) 51 | test_b_file_paths = get_file_paths(test_b_path) 52 | assert(len(test_a_file_paths) == len(test_b_file_paths)) 53 | test_path = os.path.join(dataset_folder, 'test') 54 | 55 | train_a_path = os.path.join(dataset_folder, 'trainA') 56 | train_b_path = os.path.join(dataset_folder, 'trainB') 57 | train_a_file_paths = get_file_paths(train_a_path) 58 | train_b_file_paths = get_file_paths(train_b_path) 59 | assert(len(train_a_file_paths) == len(train_b_file_paths)) 60 | train_path = os.path.join(dataset_folder, 'train') 61 | 62 | align_images(test_a_file_paths, test_b_file_paths, test_path) 63 | align_images(train_a_file_paths, train_b_file_paths, train_path) 64 | -------------------------------------------------------------------------------- /CUT/experiments/__init__.py: -------------------------------------------------------------------------------- 1 | import os 2 | import importlib 3 | import glob 4 | 5 | 6 | def find_launcher_using_name(launcher_name): 7 | # cur_dir = os.path.dirname(os.path.abspath(__file__)) 8 | # pythonfiles = glob.glob(cur_dir + '/**/*.py') 9 | launcher_filename = "experiments.{}_launcher".format(launcher_name) 10 | launcherlib = importlib.import_module(launcher_filename) 11 | 12 | # In the file, the class called LauncherNameLauncher() will 13 | # be instantiated. It has to be a subclass of BaseLauncher, 14 | # and it is case-insensitive. 15 | launcher = None 16 | target_launcher_name = launcher_name.replace('_', '') + 'launcher' 17 | for name, cls in launcherlib.__dict__.items(): 18 | if name.lower() == target_launcher_name.lower(): 19 | launcher = cls 20 | 21 | if launcher is None: 22 | raise ValueError("In %s.py, there should be a subclass of BaseLauncher " 23 | "with class name that matches %s in lowercase." % 24 | (launcher_filename, target_launcher_name)) 25 | 26 | return launcher 27 | 28 | 29 | if __name__ == "__main__": 30 | import sys 31 | import pickle 32 | 33 | assert len(sys.argv) >= 3 34 | 35 | name = sys.argv[1] 36 | Launcher = find_launcher_using_name(name) 37 | 38 | cache = "/tmp/tmux_launcher/{}".format(name) 39 | if os.path.isfile(cache): 40 | instance = pickle.load(open(cache, 'r')) 41 | else: 42 | instance = Launcher() 43 | 44 | cmd = sys.argv[2] 45 | if cmd == "launch": 46 | instance.launch() 47 | elif cmd == "stop": 48 | instance.stop() 49 | elif cmd == "send": 50 | expid = int(sys.argv[3]) 51 | cmd = int(sys.argv[4]) 52 | instance.send_command(expid, cmd) 53 | 54 | os.makedirs("/tmp/tmux_launcher/", exist_ok=True) 55 | pickle.dump(instance, open(cache, 'w')) 56 | -------------------------------------------------------------------------------- /CUT/experiments/grumpifycat_launcher.py: -------------------------------------------------------------------------------- 1 | from .tmux_launcher import Options, TmuxLauncher 2 | 3 | 4 | class Launcher(TmuxLauncher): 5 | def common_options(self): 6 | return [ 7 | # Command 0 8 | Options( 9 | dataroot="./datasets/grumpifycat", 10 | name="grumpifycat_CUT", 11 | CUT_mode="CUT" 12 | ), 13 | 14 | # Command 1 15 | Options( 16 | dataroot="./datasets/grumpifycat", 17 | name="grumpifycat_FastCUT", 18 | CUT_mode="FastCUT", 19 | ) 20 | ] 21 | 22 | def commands(self): 23 | return ["python train.py " + str(opt) for opt in self.common_options()] 24 | 25 | def test_commands(self): 26 | # RussianBlue -> Grumpy Cats dataset does not have test split. 27 | # Therefore, let's set the test split to be the "train" set. 28 | return ["python test.py " + str(opt.set(phase='train')) for opt in self.common_options()] 29 | -------------------------------------------------------------------------------- /CUT/metric/SSIM.py: -------------------------------------------------------------------------------- 1 | from skimage.metrics import structural_similarity as ssim 2 | from PIL import Image 3 | import os 4 | import numpy as np 5 | from glob import glob 6 | from tqdm import tqdm 7 | import argparse 8 | 9 | 10 | def cal_SSIM(x, y): 11 | return ssim(x, y, multichannel=True) 12 | 13 | 14 | def get_dirImage(real_dir, fake_dir): 15 | real_image_paths = sorted(glob(os.path.join(real_dir, "*.*"))) 16 | fake_image_paths = sorted(glob(os.path.join(fake_dir, "*.*"))) 17 | return real_image_paths, fake_image_paths 18 | 19 | 20 | def cal_batch_SSIM(real_paths, fake_paths): 21 | assert len(real_paths) == len(fake_paths) 22 | 23 | SSIMs = [] 24 | for i in tqdm(range((len(real_paths)))): 25 | 26 | img1 = Image.open(real_paths[i]).resize((256, 256)) 27 | img2 = Image.open(fake_paths[i]).resize((256, 256)) 28 | 29 | tmp_ssim = cal_SSIM(np.array(img1), np.array(img2)) 30 | SSIMs.append(tmp_ssim) 31 | return SSIMs 32 | 33 | 34 | if __name__ == '__main__': 35 | parser = argparse.ArgumentParser() 36 | 37 | # parser.add_argument('--dir_root', type=str, default='/workspace/Image_translation_codes/pytorch-CycleGAN-and-pix2pix/results/SEN1_2_20%_spring_cyclegan/test_latest') 38 | parser.add_argument('--fake_dir', type=str, default='fake') 39 | parser.add_argument('--real_dir', type=str, default='real') 40 | 41 | # 只需要修改此处 42 | # root = r'D:\ZYT\Codes\1.Heterogeneous_CD\Image_Translation\pytorch-CycleGAN-and-pix2pix\results\sar2opt_pix2pix\test_latest' 43 | 44 | opt = parser.parse_args() 45 | # real_dir = opt.dir_root + '/' + opt.real_subdir 46 | # fake_dir = opt.dir_root + '/' + opt.fake_subdir 47 | 48 | real_paths, fake_paths = get_dirImage(opt.real_dir, opt.fake_dir) 49 | print(len(real_paths)) 50 | 51 | SSIMs = cal_batch_SSIM(real_paths, fake_paths) 52 | 53 | print("mean SSIM: ", np.mean(SSIMs)) 54 | 55 | -------------------------------------------------------------------------------- /CUT/metric/imqual_utils.py: -------------------------------------------------------------------------------- 1 | """ 2 | # > Implementation of the classic paper by Zhou Wang et. al.: 3 | # - Image quality assessment: from error visibility to structural similarity 4 | # - https://ieeexplore.ieee.org/stamp/stamp.jsp?arnumber=1284395 5 | # > Maintainer: https://github.com/xahidbuffon 6 | """ 7 | from __future__ import division 8 | import numpy as np 9 | import math 10 | from scipy.ndimage import gaussian_filter 11 | 12 | 13 | def getSSIM(X, Y): 14 | """ 15 | Computes the mean structural similarity between two images. 16 | """ 17 | # assert (X.shape[0] == Y.shape[0]), "Image-patche provided have different dimensions" 18 | assert (X.shape[0] == Y.shape[0] & X.shape[1] == Y.shape[1]), "Image-patche provided have different dimensions" 19 | nch = 1 if X.ndim==2 else X.shape[-1] 20 | mssim = [] 21 | for ch in range(nch): 22 | Xc, Yc = X[...,ch].astype(np.float64), Y[...,ch].astype(np.float64) 23 | mssim.append(compute_ssim(Xc, Yc)) 24 | return np.mean(mssim) 25 | 26 | 27 | def compute_ssim(X, Y): 28 | """ 29 | Compute the structural similarity per single channel (given two images) 30 | """ 31 | # variables are initialized as suggested in the paper 32 | K1 = 0.01 33 | K2 = 0.03 34 | sigma = 1.5 35 | win_size = 5 36 | 37 | # means 38 | ux = gaussian_filter(X, sigma) 39 | uy = gaussian_filter(Y, sigma) 40 | 41 | # variances and covariances 42 | uxx = gaussian_filter(X * X, sigma) 43 | uyy = gaussian_filter(Y * Y, sigma) 44 | uxy = gaussian_filter(X * Y, sigma) 45 | 46 | # normalize by unbiased estimate of std dev 47 | N = win_size ** X.ndim 48 | unbiased_norm = N / (N - 1) # eq. 4 of the paper 49 | vx = (uxx - ux * ux) * unbiased_norm 50 | vy = (uyy - uy * uy) * unbiased_norm 51 | vxy = (uxy - ux * uy) * unbiased_norm 52 | 53 | R = 255 54 | C1 = (K1 * R) ** 2 55 | C2 = (K2 * R) ** 2 56 | # compute SSIM (eq. 13 of the paper) 57 | sim = (2 * ux * uy + C1) * (2 * vxy + C2) 58 | D = (ux ** 2 + uy ** 2 + C1) * (vx + vy + C2) 59 | SSIM = sim/D 60 | mssim = SSIM.mean() 61 | 62 | return mssim 63 | 64 | 65 | 66 | def getPSNR(X, Y): 67 | #assume RGB image 68 | target_data = np.array(X, dtype=np.float64) 69 | ref_data = np.array(Y, dtype=np.float64) 70 | diff = ref_data - target_data 71 | diff = diff.flatten('C') 72 | rmse = math.sqrt(np.mean(diff ** 2.) ) 73 | if rmse == 0: return 100 74 | else: return 20*math.log10(255.0/rmse) 75 | 76 | 77 | -------------------------------------------------------------------------------- /CUT/metric/psnr_ssim.py: -------------------------------------------------------------------------------- 1 | """ 2 | # > Script for measuring quantitative performances in terms of 3 | # - Structural Similarity Metric (SSIM) 4 | # - Peak Signal to Noise Ratio (PSNR) 5 | # > Maintainer: https://github.com/xahidbuffon 6 | """ 7 | ## python libs 8 | import numpy as np 9 | from PIL import Image 10 | from glob import glob 11 | from os.path import join 12 | from ntpath import basename 13 | ## local libs 14 | from imqual_utils import getSSIM, getPSNR 15 | import cv2 16 | 17 | 18 | ## compares avg ssim and psnr 19 | def SSIMs_PSNRs(gtr_dir, gen_dir, im_res=(256, 256)): 20 | """ 21 | - gtr_dir contain ground-truths 22 | - gen_dir contain generated images 23 | """ 24 | gtr_paths = sorted(glob(join(gtr_dir, "*.*"))) 25 | gen_paths = sorted(glob(join(gen_dir, "*.*"))) 26 | ssims, psnrs = [], [] 27 | for gtr_path, gen_path in zip(gtr_paths, gen_paths): 28 | gtr_f = basename(gtr_path).split('.')[0] 29 | gen_f = basename(gen_path).split('.')[0] 30 | 31 | # read images from two datasets 32 | r_im = Image.open(gtr_path).resize(im_res) 33 | g_im = Image.open(gen_path).resize(im_res) 34 | 35 | # get ssim on RGB channels 36 | ssim = getSSIM(np.array(r_im), np.array(g_im)) 37 | ssims.append(ssim) 38 | # get psnt on L channel (SOTA norm) 39 | r_im = r_im.convert("L"); g_im = g_im.convert("L") 40 | psnr = getPSNR(np.array(r_im), np.array(g_im)) 41 | psnrs.append(psnr) 42 | return np.array(ssims), np.array(psnrs) 43 | 44 | 45 | """ 46 | Get datasets from 47 | - http://irvlab.cs.umn.edu/resources/euvp-dataset 48 | - http://irvlab.cs.umn.edu/resources/ufo-120-dataset 49 | """ 50 | gtr_dir = "Image_translation_codes/contrastive-unpaired-translation/results/sar2opt_FastCUT/test_latest/images/real_B" 51 | 52 | ## generated im paths 53 | gen_dir = "Image_translation_codes/contrastive-unpaired-translation/results/sar2opt_FastCUT/test_latest/images/fake_B" 54 | 55 | 56 | ### compute SSIM and PSNR 57 | SSIM_measures, PSNR_measures = SSIMs_PSNRs(gtr_dir, gen_dir) 58 | print ("SSIM on {0} samples".format(len(SSIM_measures))) 59 | print ("Mean: {0} std: {1}".format(np.mean(SSIM_measures), np.std(SSIM_measures))) 60 | 61 | print ("PSNR on {0} samples".format(len(PSNR_measures))) 62 | print ("Mean: {0} std: {1}".format(np.mean(PSNR_measures), np.std(PSNR_measures))) 63 | 64 | 65 | -------------------------------------------------------------------------------- /CUT/models/nce.py: -------------------------------------------------------------------------------- 1 | from packaging import version 2 | import torch 3 | from torch import nn 4 | 5 | 6 | class PatchNCELoss(nn.Module): 7 | def __init__(self, opt): 8 | super().__init__() 9 | self.opt = opt 10 | self.cross_entropy_loss = torch.nn.CrossEntropyLoss(reduction='none') 11 | self.mask_dtype = torch.uint8 if version.parse(torch.__version__) < version.parse('1.2.0') else torch.bool 12 | 13 | def forward(self, feat_q, feat_k): 14 | batchSize = feat_q.shape[0] 15 | dim = feat_q.shape[1] 16 | feat_k = feat_k.detach() 17 | 18 | # pos logit 19 | l_pos = torch.bmm(feat_q.view(batchSize, 1, -1), feat_k.view(batchSize, -1, 1)) 20 | l_pos = l_pos.view(batchSize, 1) 21 | 22 | # neg logit -- current batch 23 | # reshape features to batch size 24 | feat_q = feat_q.view(self.opt.batch_size, -1, dim) 25 | feat_k = feat_k.view(self.opt.batch_size, -1, dim) 26 | npatches = feat_q.size(1) 27 | l_neg_curbatch = torch.bmm(feat_q, feat_k.transpose(2, 1)) 28 | 29 | # diagonal entries are similarity between same features, and hence meaningless. 30 | # just fill the diagonal with very small number, which is exp(-10) and almost zero 31 | diagonal = torch.eye(npatches, device=feat_q.device, dtype=self.mask_dtype)[None, :, :] 32 | l_neg_curbatch.masked_fill_(diagonal, -10.0) 33 | l_neg = l_neg_curbatch.view(-1, npatches) 34 | 35 | out = torch.cat((l_pos, l_neg), dim=1) / self.opt.nce_T 36 | 37 | loss = self.cross_entropy_loss(out, torch.zeros(out.size(0), dtype=torch.long, 38 | device=feat_q.device)) 39 | 40 | return loss 41 | -------------------------------------------------------------------------------- /CUT/options/.ipynb_checkpoints/test_options-checkpoint.py: -------------------------------------------------------------------------------- 1 | from .base_options import BaseOptions 2 | 3 | 4 | class TestOptions(BaseOptions): 5 | """This class includes test options. 6 | 7 | It also includes shared options defined in BaseOptions. 8 | """ 9 | 10 | def initialize(self, parser): 11 | parser = BaseOptions.initialize(self, parser) # define shared options 12 | parser.add_argument('--results_dir', type=str, default='./results/', help='saves results here.') 13 | parser.add_argument('--phase', type=str, default='test', help='train, val, test, etc') 14 | # Dropout and Batchnorm has different behavioir during training and test. 15 | parser.add_argument('--eval', action='store_true', help='use eval mode during test time.') 16 | parser.add_argument('--num_test', type=int, default=50, help='how many test images to run') 17 | 18 | # To avoid cropping, the load_size should be the same as crop_size 19 | parser.set_defaults(load_size=parser.get_default('crop_size')) 20 | self.isTrain = False 21 | return parser 22 | -------------------------------------------------------------------------------- /CUT/options/__init__.py: -------------------------------------------------------------------------------- 1 | """This package options includes option modules: training options, test options, and basic options (used in both training and test).""" 2 | -------------------------------------------------------------------------------- /CUT/options/test_options.py: -------------------------------------------------------------------------------- 1 | from .base_options import BaseOptions 2 | 3 | 4 | class TestOptions(BaseOptions): 5 | """This class includes test options. 6 | 7 | It also includes shared options defined in BaseOptions. 8 | """ 9 | 10 | def initialize(self, parser): 11 | parser = BaseOptions.initialize(self, parser) # define shared options 12 | parser.add_argument('--results_dir', type=str, default='./results/', help='saves results here.') 13 | parser.add_argument('--phase', type=str, default='test', help='train, val, test, etc') 14 | # Dropout and Batchnorm has different behavioir during training and test. 15 | parser.add_argument('--eval', action='store_true', help='use eval mode during test time.') 16 | parser.add_argument('--num_test', type=int, default=1000, help='how many test images to run') 17 | 18 | # To avoid cropping, the load_size should be the same as crop_size 19 | parser.set_defaults(load_size=parser.get_default('crop_size')) 20 | self.isTrain = False 21 | return parser 22 | -------------------------------------------------------------------------------- /CUT/run_time.sh: -------------------------------------------------------------------------------- 1 | dataroot='Image_translation_codes/contrastive-unpaired-translation/datasets/sar2opt' 2 | name='sar2opt_CUT' 3 | 4 | 5 | for ((i=1; i<=5; i++)); 6 | do 7 | echo "Processing"$i"th epoch..." 8 | 9 | python test.py --dataroot $dataroot \ 10 | --name $name \ 11 | --model cut 12 | 13 | done -------------------------------------------------------------------------------- /CUT/runtime.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | from subprocess import run 3 | import numpy as np 4 | 5 | # spring: 974 summer: 610 fall: 1015 winter: 506 sar2opt: 587/ 627 6 | num_test = 627 7 | 8 | 9 | if __name__ == '__main__': 10 | cmd = "bash Image_translation_codes/contrastive-unpaired-translation/run_time.sh" 11 | 12 | results = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True) 13 | times = [] 14 | for line in results.stdout.readlines(): 15 | tmp = line.decode("utf-8").strip() 16 | 17 | if 'cost time' in tmp: 18 | times.append((float(tmp.split(": ")[-1])) / num_test) 19 | 20 | print('All cost times:', times) 21 | 22 | avg_time = np.mean(times) 23 | std_time = np.std(times) 24 | 25 | print("Average running time: ", avg_time) 26 | print("Deviation of running time: ", std_time) 27 | 28 | -------------------------------------------------------------------------------- /CUT/scripts/conda_deps.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | conda install numpy pyyaml mkl mkl-include setuptools cmake cffi typing 3 | conda install pytorch torchvision -c pytorch # add cuda90 if CUDA 9 4 | conda install visdom dominate -c conda-forge # install visdom and dominate 5 | -------------------------------------------------------------------------------- /CUT/scripts/install_deps.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | pip install visdom 3 | pip install dominate 4 | -------------------------------------------------------------------------------- /CUT/util/.ipynb_checkpoints/__init__-checkpoint.py: -------------------------------------------------------------------------------- 1 | """This package includes a miscellaneous collection of useful helper functions.""" 2 | from util import * 3 | -------------------------------------------------------------------------------- /CUT/util/.ipynb_checkpoints/image_pool-checkpoint.py: -------------------------------------------------------------------------------- 1 | import random 2 | import torch 3 | 4 | 5 | class ImagePool(): 6 | """This class implements an image buffer that stores previously generated images. 7 | 8 | This buffer enables us to update discriminators using a history of generated images 9 | rather than the ones produced by the latest generators. 10 | """ 11 | 12 | def __init__(self, pool_size): 13 | """Initialize the ImagePool class 14 | 15 | Parameters: 16 | pool_size (int) -- the size of image buffer, if pool_size=0, no buffer will be created 17 | """ 18 | self.pool_size = pool_size 19 | if self.pool_size > 0: # create an empty pool 20 | self.num_imgs = 0 21 | self.images = [] 22 | 23 | def query(self, images): 24 | """Return an image from the pool. 25 | 26 | Parameters: 27 | images: the latest generated images from the generator 28 | 29 | Returns images from the buffer. 30 | 31 | By 50/100, the buffer will return input images. 32 | By 50/100, the buffer will return images previously stored in the buffer, 33 | and insert the current images to the buffer. 34 | """ 35 | if self.pool_size == 0: # if the buffer size is 0, do nothing 36 | return images 37 | return_images = [] 38 | for image in images: 39 | image = torch.unsqueeze(image.data, 0) 40 | if self.num_imgs < self.pool_size: # if the buffer is not full; keep inserting current images to the buffer 41 | self.num_imgs = self.num_imgs + 1 42 | self.images.append(image) 43 | return_images.append(image) 44 | else: 45 | p = random.uniform(0, 1) 46 | if p > 0.5: # by 50% chance, the buffer will return a previously stored image, and insert the current image into the buffer 47 | random_id = random.randint(0, self.pool_size - 1) # randint is inclusive 48 | tmp = self.images[random_id].clone() 49 | self.images[random_id] = image 50 | return_images.append(tmp) 51 | else: # by another 50% chance, the buffer will return the current image 52 | return_images.append(image) 53 | return_images = torch.cat(return_images, 0) # collect all the images and return 54 | return return_images 55 | -------------------------------------------------------------------------------- /CUT/util/__init__.py: -------------------------------------------------------------------------------- 1 | """This package includes a miscellaneous collection of useful helper functions.""" 2 | from util import * 3 | -------------------------------------------------------------------------------- /CUT/util/image_pool.py: -------------------------------------------------------------------------------- 1 | import random 2 | import torch 3 | 4 | 5 | class ImagePool(): 6 | """This class implements an image buffer that stores previously generated images. 7 | 8 | This buffer enables us to update discriminators using a history of generated images 9 | rather than the ones produced by the latest generators. 10 | """ 11 | 12 | def __init__(self, pool_size): 13 | """Initialize the ImagePool class 14 | 15 | Parameters: 16 | pool_size (int) -- the size of image buffer, if pool_size=0, no buffer will be created 17 | """ 18 | self.pool_size = pool_size 19 | if self.pool_size > 0: # create an empty pool 20 | self.num_imgs = 0 21 | self.images = [] 22 | 23 | def query(self, images): 24 | """Return an image from the pool. 25 | 26 | Parameters: 27 | images: the latest generated images from the generator 28 | 29 | Returns images from the buffer. 30 | 31 | By 50/100, the buffer will return input images. 32 | By 50/100, the buffer will return images previously stored in the buffer, 33 | and insert the current images to the buffer. 34 | """ 35 | if self.pool_size == 0: # if the buffer size is 0, do nothing 36 | return images 37 | return_images = [] 38 | for image in images: 39 | image = torch.unsqueeze(image.data, 0) 40 | if self.num_imgs < self.pool_size: # if the buffer is not full; keep inserting current images to the buffer 41 | self.num_imgs = self.num_imgs + 1 42 | self.images.append(image) 43 | return_images.append(image) 44 | else: 45 | p = random.uniform(0, 1) 46 | if p > 0.5: # by 50% chance, the buffer will return a previously stored image, and insert the current image into the buffer 47 | random_id = random.randint(0, self.pool_size - 1) # randint is inclusive 48 | tmp = self.images[random_id].clone() 49 | self.images[random_id] = image 50 | return_images.append(tmp) 51 | else: # by another 50% chance, the buffer will return the current image 52 | return_images.append(image) 53 | return_images = torch.cat(return_images, 0) # collect all the images and return 54 | return return_images 55 | -------------------------------------------------------------------------------- /CycleGAN-Pix2Pix/data/image_folder.py: -------------------------------------------------------------------------------- 1 | """A modified image folder class 2 | 3 | We modify the official PyTorch image folder (https://github.com/pytorch/vision/blob/master/torchvision/datasets/folder.py) 4 | so that this class can load images from both current directory and its subdirectories. 5 | """ 6 | 7 | import torch.utils.data as data 8 | 9 | from PIL import Image 10 | import os 11 | 12 | IMG_EXTENSIONS = [ 13 | '.jpg', '.JPG', '.jpeg', '.JPEG', 14 | '.png', '.PNG', '.ppm', '.PPM', '.bmp', '.BMP', 15 | '.tif', '.TIF', '.tiff', '.TIFF', 16 | ] 17 | 18 | 19 | def is_image_file(filename): 20 | return any(filename.endswith(extension) for extension in IMG_EXTENSIONS) 21 | 22 | 23 | def make_dataset(dir, max_dataset_size=float("inf")): 24 | images = [] 25 | assert os.path.isdir(dir), '%s is not a valid directory' % dir 26 | 27 | for root, _, fnames in sorted(os.walk(dir)): 28 | for fname in fnames: 29 | if is_image_file(fname): 30 | path = os.path.join(root, fname) 31 | images.append(path) 32 | return images[:min(max_dataset_size, len(images))] 33 | 34 | 35 | def default_loader(path): 36 | return Image.open(path).convert('RGB') 37 | 38 | 39 | class ImageFolder(data.Dataset): 40 | 41 | def __init__(self, root, transform=None, return_paths=False, 42 | loader=default_loader): 43 | imgs = make_dataset(root) 44 | if len(imgs) == 0: 45 | raise(RuntimeError("Found 0 images in: " + root + "\n" 46 | "Supported image extensions are: " + ",".join(IMG_EXTENSIONS))) 47 | 48 | self.root = root 49 | self.imgs = imgs 50 | self.transform = transform 51 | self.return_paths = return_paths 52 | self.loader = loader 53 | 54 | def __getitem__(self, index): 55 | path = self.imgs[index] 56 | img = self.loader(path) 57 | if self.transform is not None: 58 | img = self.transform(img) 59 | if self.return_paths: 60 | return img, path 61 | else: 62 | return img 63 | 64 | def __len__(self): 65 | return len(self.imgs) 66 | -------------------------------------------------------------------------------- /CycleGAN-Pix2Pix/data/single_dataset.py: -------------------------------------------------------------------------------- 1 | from data.base_dataset import BaseDataset, get_transform 2 | from data.image_folder import make_dataset 3 | from PIL import Image 4 | 5 | 6 | class SingleDataset(BaseDataset): 7 | """This dataset class can load a set of images specified by the path --dataroot /path/to/data. 8 | 9 | It can be used for generating CycleGAN results only for one side with the model option '-model test'. 10 | """ 11 | 12 | def __init__(self, opt): 13 | """Initialize this dataset class. 14 | 15 | Parameters: 16 | opt (Option class) -- stores all the experiment flags; needs to be a subclass of BaseOptions 17 | """ 18 | BaseDataset.__init__(self, opt) 19 | self.A_paths = sorted(make_dataset(opt.dataroot, opt.max_dataset_size)) 20 | input_nc = self.opt.output_nc if self.opt.direction == 'BtoA' else self.opt.input_nc 21 | self.transform = get_transform(opt, grayscale=(input_nc == 1)) 22 | 23 | def __getitem__(self, index): 24 | """Return a data point and its metadata information. 25 | 26 | Parameters: 27 | index - - a random integer for data indexing 28 | 29 | Returns a dictionary that contains A and A_paths 30 | A(tensor) - - an image in one domain 31 | A_paths(str) - - the path of the image 32 | """ 33 | A_path = self.A_paths[index] 34 | A_img = Image.open(A_path).convert('RGB') 35 | A = self.transform(A_img) 36 | return {'A': A, 'A_paths': A_path} 37 | 38 | def __len__(self): 39 | """Return the total number of images in the dataset.""" 40 | return len(self.A_paths) 41 | -------------------------------------------------------------------------------- /CycleGAN-Pix2Pix/datasets/.ipynb_checkpoints/combine_AB-checkpoint.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import os 3 | import numpy as np 4 | 5 | 6 | class Combine_Image(): 7 | def __init__(self, path_AB): 8 | self.path_AB = path_AB 9 | 10 | if not os.path.exists(path_AB): 11 | os.makedirs(path_AB) 12 | 13 | def combine_image(self, path_A, path_B): 14 | img_A = cv2.imread(path_A) 15 | img_B = cv2.imread(path_B) 16 | img_AB = np.concatenate([img_A, img_B], 1) 17 | filename = path_A.split('/')[-1] 18 | save_path = os.path.join(self.path_AB, filename) 19 | 20 | cv2.imwrite(save_path, img_AB) 21 | 22 | def get_subfile_name(self, path): 23 | lst = [] 24 | for file in os.listdir(path): 25 | lst.append(os.path.join(path, file)) 26 | return lst 27 | 28 | def run(self, phase='train'): 29 | dataset_A = os.path.join(dataset_path, phase) + 'A' 30 | dataset_B = os.path.join(dataset_path, phase) + 'B' 31 | 32 | list_A = self.get_subfile_name(dataset_A) 33 | list_B = self.get_subfile_name(dataset_B) 34 | zipped_AB = zip(list_A, list_B) 35 | 36 | for path in list(zipped_AB): 37 | path_A = path[0] 38 | path_B = path[1] 39 | self.combine_image(path_A, path_B) 40 | 41 | 42 | if __name__ == '__main__': 43 | dataset_path = 'SEN1_2/Splited_by_percent/10%_train/ROIs1158_spring_10%_train' 44 | combined_path = 'combined_SEN1_2/10%_train/train' 45 | Combine_Image(combined_path).run(phase='train') 46 | -------------------------------------------------------------------------------- /CycleGAN-Pix2Pix/datasets/combine_AB.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import os 3 | import numpy as np 4 | 5 | 6 | class Combine_Image(): 7 | def __init__(self, path_AB): 8 | self.path_AB = path_AB 9 | 10 | if not os.path.exists(path_AB): 11 | os.makedirs(path_AB) 12 | 13 | def combine_image(self, path_A, path_B): 14 | img_A = cv2.imread(path_A) 15 | img_B = cv2.imread(path_B) 16 | img_AB = np.concatenate([img_A, img_B], 1) 17 | filename = path_A.split('/')[-1] 18 | save_path = os.path.join(self.path_AB, filename) 19 | 20 | cv2.imwrite(save_path, img_AB) 21 | 22 | def get_subfile_name(self, path): 23 | lst = [] 24 | for file in os.listdir(path): 25 | lst.append(os.path.join(path, file)) 26 | return lst 27 | 28 | def run(self, phase='train'): 29 | dataset_A = os.path.join(dataset_path, phase) + 'A' 30 | dataset_B = os.path.join(dataset_path, phase) + 'B' 31 | 32 | list_A = self.get_subfile_name(dataset_A) 33 | list_B = self.get_subfile_name(dataset_B) 34 | list_A.sort(key=lambda x: (int(x.split('_')[-2]), int(x.split('_p')[-1].split('.')[0]))) 35 | list_B.sort(key=lambda x: (int(x.split('_')[-2]), int(x.split('_p')[-1].split('.')[0]))) 36 | zipped_AB = zip(list_A, list_B) 37 | 38 | for path in list(zipped_AB): 39 | path_A = path[0] 40 | path_B = path[1] 41 | print(path_A) 42 | print(path_B) 43 | print('------------') 44 | self.combine_image(path_A, path_B) 45 | 46 | 47 | if __name__ == '__main__': 48 | dataset_path = 'Image_translation_codes/pytorch-CycleGAN-and-pix2pix/datasets/SEN1_2/Splited_by_percent/10%_train/ROIs1158_spring_10%_train' 49 | combined_path = 'Image_translation_codes/pytorch-CycleGAN-and-pix2pix/datasets/combined_SEN1_2/10%_train/ROIs1158_spring_10%/test' 50 | Combine_Image(combined_path).run(phase='test') 51 | -------------------------------------------------------------------------------- /CycleGAN-Pix2Pix/datasets/download_cyclegan_dataset.sh: -------------------------------------------------------------------------------- 1 | FILE=$1 2 | 3 | if [[ $FILE != "ae_photos" && $FILE != "apple2orange" && $FILE != "summer2winter_yosemite" && $FILE != "horse2zebra" && $FILE != "monet2photo" && $FILE != "cezanne2photo" && $FILE != "ukiyoe2photo" && $FILE != "vangogh2photo" && $FILE != "maps" && $FILE != "cityscapes" && $FILE != "facades" && $FILE != "iphone2dslr_flower" && $FILE != "mini" && $FILE != "mini_pix2pix" && $FILE != "mini_colorization" ]]; then 4 | echo "Available datasets are: apple2orange, summer2winter_yosemite, horse2zebra, monet2photo, cezanne2photo, ukiyoe2photo, vangogh2photo, maps, cityscapes, facades, iphone2dslr_flower, ae_photos" 5 | exit 1 6 | fi 7 | 8 | if [[ $FILE == "cityscapes" ]]; then 9 | echo "Due to license issue, we cannot provide the Cityscapes dataset from our repository. Please download the Cityscapes dataset from https://cityscapes-dataset.com, and use the script ./datasets/prepare_cityscapes_dataset.py." 10 | echo "You need to download gtFine_trainvaltest.zip and leftImg8bit_trainvaltest.zip. For further instruction, please read ./datasets/prepare_cityscapes_dataset.py" 11 | exit 1 12 | fi 13 | 14 | echo "Specified [$FILE]" 15 | URL=https://people.eecs.berkeley.edu/~taesung_park/CycleGAN/datasets/$FILE.zip 16 | ZIP_FILE=./datasets/$FILE.zip 17 | TARGET_DIR=./datasets/$FILE/ 18 | wget -N $URL -O $ZIP_FILE 19 | mkdir $TARGET_DIR 20 | unzip $ZIP_FILE -d ./datasets/ 21 | rm $ZIP_FILE 22 | -------------------------------------------------------------------------------- /CycleGAN-Pix2Pix/datasets/download_pix2pix_dataset.sh: -------------------------------------------------------------------------------- 1 | FILE=$1 2 | 3 | if [[ $FILE != "cityscapes" && $FILE != "night2day" && $FILE != "edges2handbags" && $FILE != "edges2shoes" && $FILE != "facades" && $FILE != "maps" ]]; then 4 | echo "Available datasets are cityscapes, night2day, edges2handbags, edges2shoes, facades, maps" 5 | exit 1 6 | fi 7 | 8 | if [[ $FILE == "cityscapes" ]]; then 9 | echo "Due to license issue, we cannot provide the Cityscapes dataset from our repository. Please download the Cityscapes dataset from https://cityscapes-dataset.com, and use the script ./datasets/prepare_cityscapes_dataset.py." 10 | echo "You need to download gtFine_trainvaltest.zip and leftImg8bit_trainvaltest.zip. For further instruction, please read ./datasets/prepare_cityscapes_dataset.py" 11 | exit 1 12 | fi 13 | 14 | echo "Specified [$FILE]" 15 | 16 | URL=http://efrosgans.eecs.berkeley.edu/pix2pix/datasets/$FILE.tar.gz 17 | TAR_FILE=./datasets/$FILE.tar.gz 18 | TARGET_DIR=./datasets/$FILE/ 19 | wget -N $URL -O $TAR_FILE 20 | mkdir -p $TARGET_DIR 21 | tar -zxvf $TAR_FILE -C ./datasets/ 22 | rm $TAR_FILE 23 | -------------------------------------------------------------------------------- /CycleGAN-Pix2Pix/datasets/make_dataset_aligned.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | from PIL import Image 4 | 5 | 6 | def get_file_paths(folder): 7 | image_file_paths = [] 8 | for root, dirs, filenames in os.walk(folder): 9 | filenames = sorted(filenames) 10 | for filename in filenames: 11 | input_path = os.path.abspath(root) 12 | file_path = os.path.join(input_path, filename) 13 | if filename.endswith('.png') or filename.endswith('.jpg'): 14 | image_file_paths.append(file_path) 15 | 16 | break # prevent descending into subfolders 17 | return image_file_paths 18 | 19 | 20 | def align_images(a_file_paths, b_file_paths, target_path): 21 | if not os.path.exists(target_path): 22 | os.makedirs(target_path) 23 | 24 | for i in range(len(a_file_paths)): 25 | img_a = Image.open(a_file_paths[i]) 26 | img_b = Image.open(b_file_paths[i]) 27 | assert(img_a.size == img_b.size) 28 | 29 | aligned_image = Image.new("RGB", (img_a.size[0] * 2, img_a.size[1])) 30 | aligned_image.paste(img_a, (0, 0)) 31 | aligned_image.paste(img_b, (img_a.size[0], 0)) 32 | aligned_image.save(os.path.join(target_path, '{:04d}.jpg'.format(i))) 33 | 34 | 35 | if __name__ == '__main__': 36 | import argparse 37 | parser = argparse.ArgumentParser() 38 | parser.add_argument( 39 | '--dataset-path', 40 | dest='dataset_path', 41 | help='Which folder to process (it should have subfolders testA, testB, trainA and trainB' 42 | ) 43 | args = parser.parse_args() 44 | 45 | dataset_folder = args.dataset_path 46 | print(dataset_folder) 47 | 48 | test_a_path = os.path.join(dataset_folder, 'testA') 49 | test_b_path = os.path.join(dataset_folder, 'testB') 50 | test_a_file_paths = get_file_paths(test_a_path) 51 | test_b_file_paths = get_file_paths(test_b_path) 52 | assert(len(test_a_file_paths) == len(test_b_file_paths)) 53 | test_path = os.path.join(dataset_folder, 'test') 54 | 55 | train_a_path = os.path.join(dataset_folder, 'trainA') 56 | train_b_path = os.path.join(dataset_folder, 'trainB') 57 | train_a_file_paths = get_file_paths(train_a_path) 58 | train_b_file_paths = get_file_paths(train_b_path) 59 | assert(len(train_a_file_paths) == len(train_b_file_paths)) 60 | train_path = os.path.join(dataset_folder, 'train') 61 | 62 | align_images(test_a_file_paths, test_b_file_paths, test_path) 63 | align_images(train_a_file_paths, train_b_file_paths, train_path) 64 | -------------------------------------------------------------------------------- /CycleGAN-Pix2Pix/metric/SSIM.py: -------------------------------------------------------------------------------- 1 | from skimage.metrics import structural_similarity as ssim 2 | from PIL import Image 3 | import os 4 | import numpy as np 5 | from glob import glob 6 | from tqdm import tqdm 7 | import argparse 8 | 9 | 10 | def cal_SSIM(x, y): 11 | return ssim(x, y, multichannel=True) 12 | 13 | 14 | def get_dirImage(real_dir, fake_dir): 15 | real_image_paths = sorted(glob(os.path.join(real_dir, "*.*"))) 16 | fake_image_paths = sorted(glob(os.path.join(fake_dir, "*.*"))) 17 | return real_image_paths, fake_image_paths 18 | 19 | 20 | def cal_batch_SSIM(real_paths, fake_paths): 21 | assert len(real_paths) == len(fake_paths) 22 | 23 | SSIMs = [] 24 | for i in tqdm(range((len(real_paths)))): 25 | 26 | img1 = Image.open(real_paths[i]) 27 | img2 = Image.open(fake_paths[i]) 28 | 29 | tmp_ssim = cal_SSIM(np.array(img1), np.array(img2)) 30 | SSIMs.append(tmp_ssim) 31 | return SSIMs 32 | 33 | 34 | if __name__ == '__main__': 35 | parser = argparse.ArgumentParser() 36 | 37 | parser.add_argument('--dir_root', type=str, default='/workspace/Image_translation_codes/pytorch-CycleGAN-and-pix2pix/results/SEN1_2_20%_spring_cyclegan/test_latest') 38 | parser.add_argument('--fake_subdir', type=str, default='fake') 39 | parser.add_argument('--real_subdir', type=str, default='real') 40 | 41 | # 只需要修改此处 42 | # root = r'D:\ZYT\Codes\1.Heterogeneous_CD\Image_Translation\pytorch-CycleGAN-and-pix2pix\results\sar2opt_pix2pix\test_latest' 43 | 44 | opt = parser.parse_args() 45 | real_dir = opt.dir_root + '/' + opt.real_subdir 46 | fake_dir = opt.dir_root + '/' + opt.fake_subdir 47 | 48 | real_paths, fake_paths = get_dirImage(real_dir, fake_dir) 49 | print(len(real_paths)) 50 | 51 | SSIMs = cal_batch_SSIM(real_paths, fake_paths) 52 | 53 | print("mean SSIM: ", np.mean(SSIMs)) 54 | 55 | -------------------------------------------------------------------------------- /CycleGAN-Pix2Pix/metric/psnr_ssim.py: -------------------------------------------------------------------------------- 1 | """ 2 | # > Script for measuring quantitative performances in terms of 3 | # - Structural Similarity Metric (SSIM) 4 | # - Peak Signal to Noise Ratio (PSNR) 5 | # > Maintainer: https://github.com/xahidbuffon 6 | """ 7 | ## python libs 8 | import numpy as np 9 | from PIL import Image 10 | from glob import glob 11 | from os.path import join 12 | from ntpath import basename 13 | ## local libs 14 | from imqual_utils import getSSIM, getPSNR 15 | import cv2 16 | 17 | 18 | ## compares avg ssim and psnr 19 | def SSIMs_PSNRs(gtr_dir, gen_dir, im_res=(256, 256)): 20 | """ 21 | - gtr_dir contain ground-truths 22 | - gen_dir contain generated images 23 | """ 24 | gtr_paths = sorted(glob(join(gtr_dir, "*.*"))) 25 | gen_paths = sorted(glob(join(gen_dir, "*.*"))) 26 | ssims, psnrs = [], [] 27 | for gtr_path, gen_path in zip(gtr_paths, gen_paths): 28 | gtr_f = basename(gtr_path).split('.')[0] 29 | gen_f = basename(gen_path).split('.')[0] 30 | 31 | # read images from two datasets 32 | r_im = Image.open(gtr_path).resize(im_res) 33 | g_im = Image.open(gen_path).resize(im_res) 34 | 35 | # get ssim on RGB channels 36 | ssim = getSSIM(np.array(r_im), np.array(g_im)) 37 | ssims.append(ssim) 38 | # get psnt on L channel (SOTA norm) 39 | r_im = r_im.convert("L"); g_im = g_im.convert("L") 40 | psnr = getPSNR(np.array(r_im), np.array(g_im)) 41 | psnrs.append(psnr) 42 | return np.array(ssims), np.array(psnrs) 43 | 44 | 45 | """ 46 | Get datasets from 47 | - http://irvlab.cs.umn.edu/resources/euvp-dataset 48 | - http://irvlab.cs.umn.edu/resources/ufo-120-dataset 49 | """ 50 | gtr_dir = "Image_translation_codes/pytorch-CycleGAN-and-pix2pix/results/sar2opt_pix2pix_A2B/test_latest/fake" 51 | 52 | ## generated im paths 53 | gen_dir = "Image_translation_codes/pytorch-CycleGAN-and-pix2pix/results/sar2opt_pix2pix_A2B/test_latest/real" 54 | 55 | 56 | ### compute SSIM and PSNR 57 | SSIM_measures, PSNR_measures = SSIMs_PSNRs(gtr_dir, gen_dir) 58 | print ("SSIM on {0} samples".format(len(SSIM_measures))) 59 | print ("Mean: {0} std: {1}".format(np.mean(SSIM_measures), np.std(SSIM_measures))) 60 | 61 | print ("PSNR on {0} samples".format(len(PSNR_measures))) 62 | print ("Mean: {0} std: {1}".format(np.mean(PSNR_measures), np.std(PSNR_measures))) 63 | 64 | 65 | -------------------------------------------------------------------------------- /CycleGAN-Pix2Pix/options/.ipynb_checkpoints/test_options-checkpoint.py: -------------------------------------------------------------------------------- 1 | from .base_options import BaseOptions 2 | 3 | 4 | class TestOptions(BaseOptions): 5 | """This class includes test options. 6 | 7 | It also includes shared options defined in BaseOptions. 8 | """ 9 | 10 | def initialize(self, parser): 11 | parser = BaseOptions.initialize(self, parser) # define shared options 12 | parser.add_argument('--results_dir', type=str, default='./results/', help='saves results here.') 13 | parser.add_argument('--aspect_ratio', type=float, default=1.0, help='aspect ratio of result images') 14 | parser.add_argument('--phase', type=str, default='test', help='train, val, test, etc') 15 | # Dropout and Batchnorm has different behavioir during training and test. 16 | parser.add_argument('--eval', action='store_true', help='use eval mode during test time.') 17 | parser.add_argument('--num_test', type=int, default=627, help='how many test images to run') 18 | # rewrite devalue values 19 | parser.set_defaults(model='test') 20 | # To avoid cropping, the load_size should be the same as crop_size 21 | parser.set_defaults(load_size=parser.get_default('crop_size')) 22 | self.isTrain = False 23 | return parser 24 | -------------------------------------------------------------------------------- /CycleGAN-Pix2Pix/options/__init__.py: -------------------------------------------------------------------------------- 1 | """This package options includes option modules: training options, test options, and basic options (used in both training and test).""" 2 | -------------------------------------------------------------------------------- /CycleGAN-Pix2Pix/options/test_options.py: -------------------------------------------------------------------------------- 1 | from .base_options import BaseOptions 2 | 3 | 4 | class TestOptions(BaseOptions): 5 | """This class includes test options. 6 | 7 | It also includes shared options defined in BaseOptions. 8 | """ 9 | 10 | def initialize(self, parser): 11 | parser = BaseOptions.initialize(self, parser) # define shared options 12 | parser.add_argument('--results_dir', type=str, default='./results/', help='saves results here.') 13 | parser.add_argument('--aspect_ratio', type=float, default=1.0, help='aspect ratio of result images') 14 | parser.add_argument('--phase', type=str, default='test', help='train, val, test, etc') 15 | # Dropout and Batchnorm has different behavioir during training and test. 16 | parser.add_argument('--eval', action='store_true', help='use eval mode during test time.') 17 | parser.add_argument('--num_test', type=int, default=5000, help='how many test images to run') 18 | # rewrite devalue values 19 | parser.set_defaults(model='test') 20 | # To avoid cropping, the load_size should be the same as crop_size 21 | parser.set_defaults(load_size=parser.get_default('crop_size')) 22 | self.isTrain = False 23 | return parser 24 | -------------------------------------------------------------------------------- /CycleGAN-Pix2Pix/results/sar2opt_cyclegan/test_latest/.ipynb_checkpoints/split_real_fake-checkpoint.py: -------------------------------------------------------------------------------- 1 | import os 2 | from shutil import copy 3 | 4 | def check_dir_exists(path): 5 | if not os.path.exists(path): 6 | os.mkdir(path) 7 | 8 | def split_by_name(dir_path, real_save_path, fake_save_path): 9 | check_dir_exists(real_save_path) 10 | check_dir_exists(fake_save_path) 11 | 12 | if not os.path.exists(real_save_path): 13 | os.mkdir(real_save_path) 14 | 15 | if not os.path.exists(fake_save_path): 16 | os.mkdir(fake_save_path) 17 | 18 | for file in os.listdir(dir_path): 19 | tmp_file = os.path.join(os.path.abspath(dir_path), file) 20 | if 'real_B' in file: 21 | copy(tmp_file, real_save_path + os.sep + file) 22 | elif 'fake_B' in file: 23 | copy(tmp_file, fake_save_path + os.sep + file) 24 | 25 | if __name__ == '__main__': 26 | dir_path = 'Image_translation_codes/pytorch-CycleGAN-and-pix2pix/results/sar2opt_pix2pix_A2B/test_latest/images' 27 | real_path = 'Image_translation_codes/pytorch-CycleGAN-and-pix2pix/results/sar2opt_pix2pix_A2B/test_latest/real' 28 | fake_path = 'Image_translation_codes/pytorch-CycleGAN-and-pix2pix/results/sar2opt_pix2pix_A2B/test_latest/fake' 29 | split_by_name(dir_path, real_path, fake_path) 30 | -------------------------------------------------------------------------------- /CycleGAN-Pix2Pix/results/sar2opt_cyclegan/test_latest/split_real_fake.py: -------------------------------------------------------------------------------- 1 | import os 2 | from shutil import copy 3 | 4 | def check_dir_exists(path): 5 | if not os.path.exists(path): 6 | os.mkdir(path) 7 | 8 | def split_by_name(dir_path, fakeA_save_path, fakeB_save_path): 9 | check_dir_exists(fakeA_save_path) 10 | check_dir_exists(fakeB_save_path) 11 | 12 | if not os.path.exists(fakeA_save_path): 13 | os.mkdir(fakeA_save_path) 14 | 15 | if not os.path.exists(fakeB_save_path): 16 | os.mkdir(fakeB_save_path) 17 | 18 | for file in os.listdir(dir_path): 19 | tmp_file = os.path.join(os.path.abspath(dir_path), file) 20 | if 'fake_A' in file: 21 | copy(tmp_file, fakeA_save_path + os.sep + file) 22 | elif 'fake_B' in file: 23 | copy(tmp_file, fakeB_save_path + os.sep + file) 24 | 25 | if __name__ == '__main__': 26 | dir_path = 'Image_translation_codes/pytorch-CycleGAN-and-pix2pix/results/sar2opt_cyclegan/test_latest/images' 27 | fakeA_path = 'Image_translation_codes/pytorch-CycleGAN-and-pix2pix/results/sar2opt_cyclegan/test_latest/fakeA' 28 | fakeB_path = 'Image_translation_codes/pytorch-CycleGAN-and-pix2pix/results/sar2opt_cyclegan/test_latest/fakeB' 29 | split_by_name(dir_path, fakeA_path, fakeB_path) 30 | -------------------------------------------------------------------------------- /CycleGAN-Pix2Pix/results/sar2opt_pix2pix_1/test_latest/.ipynb_checkpoints/split_real_fake-checkpoint.py: -------------------------------------------------------------------------------- 1 | import os 2 | from shutil import copyfile 3 | 4 | def check_dir_exists(path): 5 | if not os.path.exists(path): 6 | os.mkdir(path) 7 | 8 | def split_by_name(dir_path, real_save_path, fake_save_path): 9 | # check_dir_exists(real_save_path) 10 | # check_dir_exists(fake_save_path) 11 | 12 | if not os.path.exists(real_save_path): 13 | os.mkdir(real_save_path) 14 | 15 | if not os.path.exists(fake_save_path): 16 | os.mkdir(fake_save_path) 17 | 18 | for file in os.listdir(dir_path): 19 | tmp_file = os.path.join(dir_path, file) 20 | print(tmp_file) 21 | if 'real' in str(file): 22 | copyfile(tmp_file, real_save_path) 23 | else: 24 | copyfile(tmp_file, fake_save_path) 25 | 26 | 27 | if __name__ == '__main__': 28 | dir_path = '/workspace/Image_translation_codes/pytorch-CycleGAN-and-pix2pix/results/sar2opt_pix2pix/test_latest/images' 29 | real_path = 'real' 30 | fake_path = 'fake' 31 | split_by_name(dir_path, real_path, fake_path) -------------------------------------------------------------------------------- /CycleGAN-Pix2Pix/results/sar2opt_pix2pix_1/test_latest/split_real_fake.py: -------------------------------------------------------------------------------- 1 | import os 2 | from shutil import copy 3 | 4 | def check_dir_exists(path): 5 | if not os.path.exists(path): 6 | os.mkdir(path) 7 | 8 | def split_by_name(dir_path, real_save_path, fake_save_path): 9 | check_dir_exists(real_save_path) 10 | check_dir_exists(fake_save_path) 11 | 12 | if not os.path.exists(real_save_path): 13 | os.mkdir(real_save_path) 14 | 15 | if not os.path.exists(fake_save_path): 16 | os.mkdir(fake_save_path) 17 | 18 | for file in os.listdir(dir_path): 19 | tmp_file = os.path.join(os.path.abspath(dir_path), file) 20 | if 'real_B' in file: 21 | copy(tmp_file, real_save_path + os.sep + file) 22 | elif 'fake_B' in file: 23 | copy(tmp_file, fake_save_path + os.sep + file) 24 | 25 | if __name__ == '__main__': 26 | dir_path = 'Image_translation_codes/pytorch-CycleGAN-and-pix2pix/results/sar2opt_pix2pix_A2B/test_latest/images' 27 | real_path = 'Image_translation_codes/pytorch-CycleGAN-and-pix2pix/results/sar2opt_pix2pix_A2B/test_latest/real' 28 | fake_path = 'Image_translation_codes/pytorch-CycleGAN-and-pix2pix/results/sar2opt_pix2pix_A2B/test_latest/fake' 29 | split_by_name(dir_path, real_path, fake_path) -------------------------------------------------------------------------------- /CycleGAN-Pix2Pix/results/sar2opt_pix2pix_A2B/test_latest/.ipynb_checkpoints/split_real_fake-checkpoint.py: -------------------------------------------------------------------------------- 1 | import os 2 | from shutil import copy 3 | 4 | def check_dir_exists(path): 5 | if not os.path.exists(path): 6 | os.mkdir(path) 7 | 8 | def split_by_name(dir_path, real_save_path, fake_save_path): 9 | check_dir_exists(real_save_path) 10 | check_dir_exists(fake_save_path) 11 | 12 | if not os.path.exists(real_save_path): 13 | os.mkdir(real_save_path) 14 | 15 | if not os.path.exists(fake_save_path): 16 | os.mkdir(fake_save_path) 17 | 18 | for file in os.listdir(dir_path): 19 | tmp_file = os.path.join(os.path.abspath(dir_path), file) 20 | if 'real_B' in file: 21 | copy(tmp_file, real_save_path + os.sep + file) 22 | elif 'fake_B' in file: 23 | copy(tmp_file, fake_save_path + os.sep + file) 24 | 25 | if __name__ == '__main__': 26 | dir_path = './images' 27 | real_path = './real' 28 | fake_path = './fake' 29 | split_by_name(dir_path, real_path, fake_path) 30 | -------------------------------------------------------------------------------- /CycleGAN-Pix2Pix/results/sar2opt_pix2pix_A2B/test_latest/split_real_fake.py: -------------------------------------------------------------------------------- 1 | import os 2 | from shutil import copy 3 | 4 | def check_dir_exists(path): 5 | if not os.path.exists(path): 6 | os.mkdir(path) 7 | 8 | def split_by_name(dir_path, real_save_path, fake_save_path): 9 | check_dir_exists(real_save_path) 10 | check_dir_exists(fake_save_path) 11 | 12 | if not os.path.exists(real_save_path): 13 | os.mkdir(real_save_path) 14 | 15 | if not os.path.exists(fake_save_path): 16 | os.mkdir(fake_save_path) 17 | 18 | for file in os.listdir(dir_path): 19 | tmp_file = os.path.join(os.path.abspath(dir_path), file) 20 | if 'real_B' in file: 21 | copy(tmp_file, real_save_path + os.sep + file) 22 | elif 'fake_B' in file: 23 | copy(tmp_file, fake_save_path + os.sep + file) 24 | 25 | if __name__ == '__main__': 26 | dir_path = './images' 27 | real_path = './real' 28 | fake_path = './fake' 29 | split_by_name(dir_path, real_path, fake_path) 30 | -------------------------------------------------------------------------------- /CycleGAN-Pix2Pix/results/split_real_fake.py: -------------------------------------------------------------------------------- 1 | import os 2 | from shutil import copy 3 | 4 | def check_dir_exists(path): 5 | if not os.path.exists(path): 6 | os.mkdir(path) 7 | 8 | def split_by_name(dir_path, real_save_path, fake_save_path): 9 | check_dir_exists(real_save_path) 10 | check_dir_exists(fake_save_path) 11 | 12 | if not os.path.exists(real_save_path): 13 | os.mkdir(real_save_path) 14 | 15 | if not os.path.exists(fake_save_path): 16 | os.mkdir(fake_save_path) 17 | 18 | for file in os.listdir(dir_path): 19 | tmp_file = os.path.join(os.path.abspath(dir_path), file) 20 | if 'real_B' in file: 21 | copy(tmp_file, real_save_path + os.sep + file) 22 | elif 'fake_B' in file: 23 | copy(tmp_file, fake_save_path + os.sep + file) 24 | 25 | if __name__ == '__main__': 26 | dir_path = 'Image_translation_codes/pytorch-CycleGAN-and-pix2pix/results/sar2opt_cyclegan/test_latest/images' 27 | real_path = 'Image_translation_codes/pytorch-CycleGAN-and-pix2pix/results/sar2opt_cyclegan/test_latest/real' 28 | fake_path = 'Image_translation_codes/pytorch-CycleGAN-and-pix2pix/results/sar2opt_cyclegan/test_latest/fake' 29 | split_by_name(dir_path, real_path, fake_path) 30 | -------------------------------------------------------------------------------- /CycleGAN-Pix2Pix/run_time.sh: -------------------------------------------------------------------------------- 1 | # set -ex 2 | 3 | 4 | for ((i=1; i<=5; i++)); 5 | do 6 | echo "Processing"$i"th epoch..." 7 | # --- pix2pix --- 8 | # python Image_translation_codes/pytorch-CycleGAN-and-pix2pix/test.py \ 9 | # --dataroot Image_translation_codes/pytorch-CycleGAN-and-pix2pix/datasets/combined_sar2opt \ 10 | # --name sar2opt_pix2pix \ 11 | # --model pix2pix \ 12 | # --direction AtoB 13 | 14 | # --- cyclegan --- 15 | python Image_translation_codes/pytorch-CycleGAN-and-pix2pix/test.py \ 16 | --dataroot Image_translation_codes/pytorch-CycleGAN-and-pix2pix/datasets/sar2opt \ 17 | --name sar2opt_cyclegan \ 18 | --model cycle_gan 19 | 20 | done -------------------------------------------------------------------------------- /CycleGAN-Pix2Pix/runtime.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | from subprocess import run 3 | import numpy as np 4 | 5 | # spring: 974 summer: 610 fall: 1015 winter: 506 sar2opt: 587/ 627 6 | num_test = 627 7 | 8 | 9 | if __name__ == '__main__': 10 | cmd = "bash Image_translation_codes/pytorch-CycleGAN-and-pix2pix/run_time.sh" 11 | 12 | results = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True) 13 | times = [] 14 | for line in results.stdout.readlines(): 15 | tmp = line.decode("utf-8").strip() 16 | 17 | if 'cost time' in tmp: 18 | times.append((float(tmp.split(": ")[-1])) / num_test) 19 | 20 | print('All cost times:', times) 21 | 22 | avg_time = np.mean(times) 23 | std_time = np.std(times) 24 | 25 | print("Average running time: ", avg_time) 26 | print("Deviation of running time: ", std_time) 27 | 28 | -------------------------------------------------------------------------------- /CycleGAN-Pix2Pix/scripts/conda_deps.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | conda install numpy pyyaml mkl mkl-include setuptools cmake cffi typing 3 | conda install pytorch torchvision -c pytorch # add cuda90 if CUDA 9 4 | conda install visdom dominate -c conda-forge # install visdom and dominate 5 | -------------------------------------------------------------------------------- /CycleGAN-Pix2Pix/scripts/download_cyclegan_model.sh: -------------------------------------------------------------------------------- 1 | FILE=$1 2 | 3 | echo "Note: available models are apple2orange, orange2apple, summer2winter_yosemite, winter2summer_yosemite, horse2zebra, zebra2horse, monet2photo, style_monet, style_cezanne, style_ukiyoe, style_vangogh, sat2map, map2sat, cityscapes_photo2label, cityscapes_label2photo, facades_photo2label, facades_label2photo, iphone2dslr_flower" 4 | 5 | echo "Specified [$FILE]" 6 | 7 | mkdir -p ./checkpoints/${FILE}_pretrained 8 | MODEL_FILE=./checkpoints/${FILE}_pretrained/latest_net_G.pth 9 | URL=http://efrosgans.eecs.berkeley.edu/cyclegan/pretrained_models/$FILE.pth 10 | 11 | wget -N $URL -O $MODEL_FILE 12 | -------------------------------------------------------------------------------- /CycleGAN-Pix2Pix/scripts/download_pix2pix_model.sh: -------------------------------------------------------------------------------- 1 | FILE=$1 2 | 3 | echo "Note: available models are edges2shoes, sat2map, map2sat, facades_label2photo, and day2night" 4 | echo "Specified [$FILE]" 5 | 6 | mkdir -p ./checkpoints/${FILE}_pretrained 7 | MODEL_FILE=./checkpoints/${FILE}_pretrained/latest_net_G.pth 8 | URL=http://efrosgans.eecs.berkeley.edu/pix2pix/models-pytorch/$FILE.pth 9 | 10 | wget -N $URL -O $MODEL_FILE 11 | -------------------------------------------------------------------------------- /CycleGAN-Pix2Pix/scripts/eval_cityscapes/download_fcn8s.sh: -------------------------------------------------------------------------------- 1 | URL=http://people.eecs.berkeley.edu/~tinghuiz/projects/pix2pix/fcn-8s-cityscapes/fcn-8s-cityscapes.caffemodel 2 | OUTPUT_FILE=./scripts/eval_cityscapes/caffemodel/fcn-8s-cityscapes.caffemodel 3 | wget -N $URL -O $OUTPUT_FILE 4 | -------------------------------------------------------------------------------- /CycleGAN-Pix2Pix/scripts/eval_cityscapes/util.py: -------------------------------------------------------------------------------- 1 | # The following code is modified from https://github.com/shelhamer/clockwork-fcn 2 | import numpy as np 3 | 4 | 5 | def get_out_scoremap(net): 6 | return net.blobs['score'].data[0].argmax(axis=0).astype(np.uint8) 7 | 8 | 9 | def feed_net(net, in_): 10 | """ 11 | Load prepared input into net. 12 | """ 13 | net.blobs['data'].reshape(1, *in_.shape) 14 | net.blobs['data'].data[...] = in_ 15 | 16 | 17 | def segrun(net, in_): 18 | feed_net(net, in_) 19 | net.forward() 20 | return get_out_scoremap(net) 21 | 22 | 23 | def fast_hist(a, b, n): 24 | k = np.where((a >= 0) & (a < n))[0] 25 | bc = np.bincount(n * a[k].astype(int) + b[k], minlength=n**2) 26 | if len(bc) != n**2: 27 | # ignore this example if dimension mismatch 28 | return 0 29 | return bc.reshape(n, n) 30 | 31 | 32 | def get_scores(hist): 33 | # Mean pixel accuracy 34 | acc = np.diag(hist).sum() / (hist.sum() + 1e-12) 35 | 36 | # Per class accuracy 37 | cl_acc = np.diag(hist) / (hist.sum(1) + 1e-12) 38 | 39 | # Per class IoU 40 | iu = np.diag(hist) / (hist.sum(1) + hist.sum(0) - np.diag(hist) + 1e-12) 41 | 42 | return acc, np.nanmean(cl_acc), np.nanmean(iu), cl_acc, iu 43 | -------------------------------------------------------------------------------- /CycleGAN-Pix2Pix/scripts/install_deps.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | pip install visdom 3 | pip install dominate 4 | -------------------------------------------------------------------------------- /CycleGAN-Pix2Pix/scripts/test_colorization.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | python test.py --dataroot ./datasets/colorization --name color_pix2pix --model colorization 3 | -------------------------------------------------------------------------------- /CycleGAN-Pix2Pix/scripts/test_cyclegan.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | python test.py --dataroot ./datasets/maps --name maps_cyclegan --model cycle_gan --phase test --no_dropout 3 | -------------------------------------------------------------------------------- /CycleGAN-Pix2Pix/scripts/test_pix2pix.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | python test.py --dataroot ./datasets/facades --name facades_pix2pix --model pix2pix --netG unet_256 --direction BtoA --dataset_mode aligned --norm batch 3 | -------------------------------------------------------------------------------- /CycleGAN-Pix2Pix/scripts/test_single.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | python test.py --dataroot ./datasets/facades/testB/ --name facades_pix2pix --model test --netG unet_256 --direction BtoA --dataset_mode single --norm batch 3 | -------------------------------------------------------------------------------- /CycleGAN-Pix2Pix/scripts/train_colorization.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | python train.py --dataroot ./datasets/colorization --name color_pix2pix --model colorization 3 | -------------------------------------------------------------------------------- /CycleGAN-Pix2Pix/scripts/train_cyclegan.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | python train.py --dataroot ./datasets/maps --name maps_cyclegan --model cycle_gan --pool_size 50 --no_dropout 3 | -------------------------------------------------------------------------------- /CycleGAN-Pix2Pix/scripts/train_pix2pix.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | python train.py --dataroot ./datasets/facades --name facades_pix2pix --model pix2pix --netG unet_256 --direction BtoA --lambda_L1 100 --dataset_mode aligned --norm batch --pool_size 0 3 | -------------------------------------------------------------------------------- /CycleGAN-Pix2Pix/util/__init__.py: -------------------------------------------------------------------------------- 1 | """This package includes a miscellaneous collection of useful helper functions.""" 2 | -------------------------------------------------------------------------------- /CycleGAN-Pix2Pix/util/image_pool.py: -------------------------------------------------------------------------------- 1 | import random 2 | import torch 3 | 4 | 5 | class ImagePool(): 6 | """This class implements an image buffer that stores previously generated images. 7 | 8 | This buffer enables us to update discriminators using a history of generated images 9 | rather than the ones produced by the latest generators. 10 | """ 11 | 12 | def __init__(self, pool_size): 13 | """Initialize the ImagePool class 14 | 15 | Parameters: 16 | pool_size (int) -- the size of image buffer, if pool_size=0, no buffer will be created 17 | """ 18 | self.pool_size = pool_size 19 | if self.pool_size > 0: # create an empty pool 20 | self.num_imgs = 0 21 | self.images = [] 22 | 23 | def query(self, images): 24 | """Return an image from the pool. 25 | 26 | Parameters: 27 | images: the latest generated images from the generator 28 | 29 | Returns images from the buffer. 30 | 31 | By 50/100, the buffer will return input images. 32 | By 50/100, the buffer will return images previously stored in the buffer, 33 | and insert the current images to the buffer. 34 | """ 35 | if self.pool_size == 0: # if the buffer size is 0, do nothing 36 | return images 37 | return_images = [] 38 | for image in images: 39 | image = torch.unsqueeze(image.data, 0) 40 | if self.num_imgs < self.pool_size: # if the buffer is not full; keep inserting current images to the buffer 41 | self.num_imgs = self.num_imgs + 1 42 | self.images.append(image) 43 | return_images.append(image) 44 | else: 45 | p = random.uniform(0, 1) 46 | if p > 0.5: # by 50% chance, the buffer will return a previously stored image, and insert the current image into the buffer 47 | random_id = random.randint(0, self.pool_size - 1) # randint is inclusive 48 | tmp = self.images[random_id].clone() 49 | self.images[random_id] = image 50 | return_images.append(tmp) 51 | else: # by another 50% chance, the buffer will return the current image 52 | return_images.append(image) 53 | return_images = torch.cat(return_images, 0) # collect all the images and return 54 | return return_images 55 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 MarsZhaoYT 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /MUNIT/metric/SSIM.py: -------------------------------------------------------------------------------- 1 | from skimage.metrics import structural_similarity as ssim 2 | from PIL import Image 3 | import os 4 | import numpy as np 5 | from glob import glob 6 | from tqdm import tqdm 7 | import argparse 8 | 9 | 10 | def cal_SSIM(x, y): 11 | return ssim(x, y, multichannel=True) 12 | 13 | 14 | def get_dirImage(real_dir, fake_dir): 15 | real_image_paths = sorted(glob(os.path.join(real_dir, "*.*"))) 16 | fake_image_paths = sorted(glob(os.path.join(fake_dir, "*.*"))) 17 | return real_image_paths, fake_image_paths 18 | 19 | 20 | def cal_batch_SSIM(real_paths, fake_paths): 21 | assert len(real_paths) == len(fake_paths) 22 | 23 | SSIMs = [] 24 | for i in tqdm(range((len(real_paths)))): 25 | 26 | img1 = Image.open(real_paths[i]).resize((256, 256)) 27 | img2 = Image.open(fake_paths[i]).resize((256, 256)) 28 | 29 | tmp_ssim = cal_SSIM(np.array(img1), np.array(img2)) 30 | SSIMs.append(tmp_ssim) 31 | return SSIMs 32 | 33 | 34 | if __name__ == '__main__': 35 | parser = argparse.ArgumentParser() 36 | 37 | # parser.add_argument('--dir_root', type=str, default='/workspace/Image_translation_codes/pytorch-CycleGAN-and-pix2pix/results/SEN1_2_20%_spring_cyclegan/test_latest') 38 | parser.add_argument('--fake_dir', type=str, default='fake') 39 | parser.add_argument('--real_dir', type=str, default='real') 40 | 41 | # 只需要修改此处 42 | # root = r'D:\ZYT\Codes\1.Heterogeneous_CD\Image_Translation\pytorch-CycleGAN-and-pix2pix\results\sar2opt_pix2pix\test_latest' 43 | 44 | opt = parser.parse_args() 45 | # real_dir = opt.dir_root + '/' + opt.real_subdir 46 | # fake_dir = opt.dir_root + '/' + opt.fake_subdir 47 | 48 | real_paths, fake_paths = get_dirImage(opt.real_dir, opt.fake_dir) 49 | print(len(real_paths)) 50 | 51 | SSIMs = cal_batch_SSIM(real_paths, fake_paths) 52 | 53 | print("mean SSIM: ", np.mean(SSIMs)) 54 | 55 | -------------------------------------------------------------------------------- /MUNIT/metric/imqual_utils.py: -------------------------------------------------------------------------------- 1 | """ 2 | # > Implementation of the classic paper by Zhou Wang et. al.: 3 | # - Image quality assessment: from error visibility to structural similarity 4 | # - https://ieeexplore.ieee.org/stamp/stamp.jsp?arnumber=1284395 5 | # > Maintainer: https://github.com/xahidbuffon 6 | """ 7 | from __future__ import division 8 | import numpy as np 9 | import math 10 | from scipy.ndimage import gaussian_filter 11 | 12 | 13 | def getSSIM(X, Y): 14 | """ 15 | Computes the mean structural similarity between two images. 16 | """ 17 | # assert (X.shape[0] == Y.shape[0]), "Image-patche provided have different dimensions" 18 | assert (X.shape[0] == Y.shape[0] & X.shape[1] == Y.shape[1]), "Image-patche provided have different dimensions" 19 | nch = 1 if X.ndim==2 else X.shape[-1] 20 | mssim = [] 21 | for ch in range(nch): 22 | Xc, Yc = X[...,ch].astype(np.float64), Y[...,ch].astype(np.float64) 23 | mssim.append(compute_ssim(Xc, Yc)) 24 | return np.mean(mssim) 25 | 26 | 27 | def compute_ssim(X, Y): 28 | """ 29 | Compute the structural similarity per single channel (given two images) 30 | """ 31 | # variables are initialized as suggested in the paper 32 | K1 = 0.01 33 | K2 = 0.03 34 | sigma = 1.5 35 | win_size = 5 36 | 37 | # means 38 | ux = gaussian_filter(X, sigma) 39 | uy = gaussian_filter(Y, sigma) 40 | 41 | # variances and covariances 42 | uxx = gaussian_filter(X * X, sigma) 43 | uyy = gaussian_filter(Y * Y, sigma) 44 | uxy = gaussian_filter(X * Y, sigma) 45 | 46 | # normalize by unbiased estimate of std dev 47 | N = win_size ** X.ndim 48 | unbiased_norm = N / (N - 1) # eq. 4 of the paper 49 | vx = (uxx - ux * ux) * unbiased_norm 50 | vy = (uyy - uy * uy) * unbiased_norm 51 | vxy = (uxy - ux * uy) * unbiased_norm 52 | 53 | R = 255 54 | C1 = (K1 * R) ** 2 55 | C2 = (K2 * R) ** 2 56 | # compute SSIM (eq. 13 of the paper) 57 | sim = (2 * ux * uy + C1) * (2 * vxy + C2) 58 | D = (ux ** 2 + uy ** 2 + C1) * (vx + vy + C2) 59 | SSIM = sim/D 60 | mssim = SSIM.mean() 61 | 62 | return mssim 63 | 64 | 65 | 66 | def getPSNR(X, Y): 67 | #assume RGB image 68 | target_data = np.array(X, dtype=np.float64) 69 | ref_data = np.array(Y, dtype=np.float64) 70 | diff = ref_data - target_data 71 | diff = diff.flatten('C') 72 | rmse = math.sqrt(np.mean(diff ** 2.) ) 73 | if rmse == 0: return 100 74 | else: return 20*math.log10(255.0/rmse) 75 | 76 | 77 | -------------------------------------------------------------------------------- /MUNIT/metric/psnr_ssim.py: -------------------------------------------------------------------------------- 1 | """ 2 | # > Script for measuring quantitative performances in terms of 3 | # - Structural Similarity Metric (SSIM) 4 | # - Peak Signal to Noise Ratio (PSNR) 5 | # > Maintainer: https://github.com/xahidbuffon 6 | """ 7 | ## python libs 8 | import numpy as np 9 | from PIL import Image 10 | from glob import glob 11 | from os.path import join 12 | from ntpath import basename 13 | ## local libs 14 | from imqual_utils import getSSIM, getPSNR 15 | import cv2 16 | 17 | 18 | ## compares avg ssim and psnr 19 | def SSIMs_PSNRs(gtr_dir, gen_dir, im_res=(256, 256)): 20 | """ 21 | - gtr_dir contain ground-truths 22 | - gen_dir contain generated images 23 | """ 24 | gtr_paths = sorted(glob(join(gtr_dir, "*.*"))) 25 | gen_paths = sorted(glob(join(gen_dir, "*.*"))) 26 | ssims, psnrs = [], [] 27 | for gtr_path, gen_path in zip(gtr_paths, gen_paths): 28 | gtr_f = basename(gtr_path).split('.')[0] 29 | gen_f = basename(gen_path).split('.')[0] 30 | 31 | # read images from two datasets 32 | r_im = Image.open(gtr_path).resize(im_res) 33 | g_im = Image.open(gen_path).resize(im_res) 34 | 35 | # get ssim on RGB channels 36 | ssim = getSSIM(np.array(r_im), np.array(g_im)) 37 | ssims.append(ssim) 38 | # get psnt on L channel (SOTA norm) 39 | r_im = r_im.convert("L"); g_im = g_im.convert("L") 40 | psnr = getPSNR(np.array(r_im), np.array(g_im)) 41 | psnrs.append(psnr) 42 | return np.array(ssims), np.array(psnrs) 43 | 44 | 45 | """ 46 | Get datasets from 47 | - http://irvlab.cs.umn.edu/resources/euvp-dataset 48 | - http://irvlab.cs.umn.edu/resources/ufo-120-dataset 49 | """ 50 | gtr_dir = "Image_translation_codes/MUNIT/datasets/SEN1_2/20%_train/ROIs2017_winter_20%_train/testB" 51 | 52 | ## generated im paths 53 | gen_dir = "Image_translation_codes/MUNIT/results/SEN1_2_20%_winter/fake" 54 | 55 | 56 | ### compute SSIM and PSNR 57 | SSIM_measures, PSNR_measures = SSIMs_PSNRs(gtr_dir, gen_dir) 58 | print ("SSIM on {0} samples".format(len(SSIM_measures))) 59 | print ("Mean: {0} std: {1}".format(np.mean(SSIM_measures), np.std(SSIM_measures))) 60 | 61 | print ("PSNR on {0} samples".format(len(PSNR_measures))) 62 | print ("Mean: {0} std: {1}".format(np.mean(PSNR_measures), np.std(PSNR_measures))) 63 | 64 | 65 | -------------------------------------------------------------------------------- /MUNIT/results/split_real_fake.py: -------------------------------------------------------------------------------- 1 | import os 2 | from shutil import copy 3 | 4 | def check_dir_exists(path): 5 | if not os.path.exists(path): 6 | os.mkdir(path) 7 | 8 | def split_by_name(dir_path, real_save_path, fake_save_path): 9 | check_dir_exists(real_save_path) 10 | check_dir_exists(fake_save_path) 11 | 12 | if not os.path.exists(real_save_path): 13 | os.mkdir(real_save_path) 14 | 15 | if not os.path.exists(fake_save_path): 16 | os.mkdir(fake_save_path) 17 | 18 | for file in os.listdir(dir_path): 19 | tmp_file = os.path.join(os.path.abspath(dir_path), file) 20 | if 'output' in file: 21 | copy(tmp_file, fake_save_path + os.sep + file) 22 | else: 23 | copy(tmp_file, real_save_path + os.sep + file) 24 | 25 | if __name__ == '__main__': 26 | dir_path = 'Image_translation_codes/MUNIT/results/SEN1_2_20%_winter/images/' 27 | real_path = '/workspace/Image_translation_codes/MUNIT/results/SEN1_2_20%_winter/real' 28 | fake_path = '/workspace/Image_translation_codes/MUNIT/results/SEN1_2_20%_winter/fake' 29 | split_by_name(dir_path, real_path, fake_path) 30 | -------------------------------------------------------------------------------- /MUNIT/run_time.sh: -------------------------------------------------------------------------------- 1 | # set -ex 2 | 3 | config_file='Image_translation_codes/MUNIT/configs/SEN1_2_20%_summer.yaml' 4 | input_folder='Image_translation_codes/MUNIT/datasets/SEN1_2/20%_train/ROIs1868_summer_20%_train/testA/' 5 | output_folder='Image_translation_codes/MUNIT/results/SEN1_2_20%_summer/images/' 6 | checkpoint='Image_translation_codes/MUNIT/outputs/SEN1_2_20%_summer/checkpoint/gen_00190000.pt' 7 | 8 | for ((i=1; i<=5; i++)); 9 | do 10 | echo "Processing"$i"th epoch..." 11 | 12 | python Image_translation_codes/MUNIT/test.py \ 13 | --config $config_file \ 14 | --input_folder $input_folder \ 15 | --output_folder $output_folder \ 16 | --checkpoint $checkpoint \ 17 | --a2b 1 18 | 19 | done -------------------------------------------------------------------------------- /MUNIT/runtime.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | from subprocess import run 3 | import numpy as np 4 | 5 | # spring: 974 summer: 610 fall: 1015 winter: 506 sar2opt: 587/ 627 6 | num_test = 610 7 | 8 | 9 | if __name__ == '__main__': 10 | cmd = "bash Image_translation_codes/MUNIT/run_time.sh" 11 | 12 | results = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True) 13 | times = [] 14 | for line in results.stdout.readlines(): 15 | tmp = line.decode("utf-8").strip() 16 | 17 | if 'cost time' in tmp: 18 | times.append((float(tmp.split(": ")[-1])) / num_test) 19 | 20 | print('All cost times:', times) 21 | 22 | avg_time = np.mean(times) 23 | std_time = np.std(times) 24 | 25 | print("Average running time: ", avg_time) 26 | print("Deviation of running time: ", std_time) 27 | 28 | -------------------------------------------------------------------------------- /MUNIT/scripts/demo_train_edges2handbags.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | rm datasets/edges2handbags -rf 3 | mkdir datasets/edges2handbags -p 4 | axel -n 1 https://people.eecs.berkeley.edu/~tinghuiz/projects/pix2pix/datasets/edges2handbags.tar.gz --output=datasets/edges2handbags/edges2handbags.tar.gz 5 | tar -zxvf datasets/edges2handbags/edges2handbags.tar.gz -C datasets/ 6 | mkdir datasets/edges2handbags/train1 -p 7 | mkdir datasets/edges2handbags/train0 -p 8 | mkdir datasets/edges2handbags/test1 -p 9 | mkdir datasets/edges2handbags/test0 -p 10 | for f in datasets/edges2handbags/train/*; do convert -quality 100 -crop 50%x100% +repage $f datasets/edges2handbags/train%d/${f##*/}; done; 11 | for f in datasets/edges2handbags/val/*; do convert -quality 100 -crop 50%x100% +repage $f datasets/edges2handbags/test%d/${f##*/}; done; 12 | mv datasets/edges2handbags/train0 datasets/edges2handbags/trainA 13 | mv datasets/edges2handbags/train1 datasets/edges2handbags/trainB 14 | mv datasets/edges2handbags/test0 datasets/edges2handbags/testA 15 | mv datasets/edges2handbags/test1 datasets/edges2handbags/testB 16 | python train.py --config configs/edges2handbags_folder.yaml 17 | 18 | -------------------------------------------------------------------------------- /MUNIT/scripts/demo_train_edges2shoes.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | rm datasets/edges2shoes -rf 3 | mkdir datasets/edges2shoes -p 4 | axel -n 1 https://people.eecs.berkeley.edu/~tinghuiz/projects/pix2pix/datasets/edges2shoes.tar.gz --output=datasets/edges2shoes/edges2shoes.tar.gz 5 | tar -zxvf datasets/edges2shoes/edges2shoes.tar.gz -C datasets 6 | mkdir datasets/edges2shoes/train1 -p 7 | mkdir datasets/edges2shoes/train0 -p 8 | mkdir datasets/edges2shoes/test1 -p 9 | mkdir datasets/edges2shoes/test0 -p 10 | for f in datasets/edges2shoes/train/*; do convert -quality 100 -crop 50%x100% +repage $f datasets/edges2shoes/train%d/${f##*/}; done; 11 | for f in datasets/edges2shoes/val/*; do convert -quality 100 -crop 50%x100% +repage $f datasets/edges2shoes/test%d/${f##*/}; done; 12 | mv datasets/edges2shoes/train0 datasets/edges2shoes/trainA 13 | mv datasets/edges2shoes/train1 datasets/edges2shoes/trainB 14 | mv datasets/edges2shoes/test0 datasets/edges2shoes/testA 15 | mv datasets/edges2shoes/test1 datasets/edges2shoes/testB 16 | python train.py --config configs/edges2shoes_folder.yaml 17 | -------------------------------------------------------------------------------- /MUNIT/scripts/demo_train_summer2winter_yosemite256.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | rm datasets/summer2winter_yosemite256 -p 3 | mkdir datasets/summer2winter_yosemite256 -p 4 | axel -n 1 https://people.eecs.berkeley.edu/~taesung_park/CycleGAN/datasets/summer2winter_yosemite.zip --output=datasets/summer2winter_yosemite256/summer2winter_yosemite.zip 5 | unzip datasets/summer2winter_yosemite256/summer2winter_yosemite.zip -d datasets/summer2winter_yosemite256 6 | python train.py --config configs/summer2winter_yosemite256_folder.yaml 7 | -------------------------------------------------------------------------------- /NICE-GAN/.ipynb_checkpoints/utils-checkpoint.py: -------------------------------------------------------------------------------- 1 | from scipy import misc 2 | import os, cv2, torch 3 | import numpy as np 4 | 5 | def load_test_data(image_path, size=256): 6 | img = misc.imread(image_path, mode='RGB') 7 | img = misc.imresize(img, [size, size]) 8 | img = np.expand_dims(img, axis=0) 9 | img = preprocessing(img) 10 | 11 | return img 12 | 13 | def preprocessing(x): 14 | x = x/127.5 - 1 # -1 ~ 1 15 | return x 16 | 17 | def save_images(images, size, image_path): 18 | return imsave(inverse_transform(images), size, image_path) 19 | 20 | def inverse_transform(images): 21 | return (images+1.) / 2 22 | 23 | def imsave(images, size, path): 24 | return misc.imsave(path, merge(images, size)) 25 | 26 | def merge(images, size): 27 | h, w = images.shape[1], images.shape[2] 28 | img = np.zeros((h * size[0], w * size[1], 3)) 29 | for idx, image in enumerate(images): 30 | i = idx % size[1] 31 | j = idx // size[1] 32 | img[h*j:h*(j+1), w*i:w*(i+1), :] = image 33 | 34 | return img 35 | 36 | def check_folder(log_dir): 37 | if not os.path.exists(log_dir): 38 | os.makedirs(log_dir) 39 | return log_dir 40 | 41 | def str2bool(x): 42 | return x.lower() in ('true') 43 | 44 | def cam(x, size = 256): 45 | x = x - np.min(x) 46 | cam_img = x / np.max(x) 47 | cam_img = np.uint8(255 * cam_img) 48 | cam_img = cv2.resize(cam_img, (size, size)) 49 | cam_img = cv2.applyColorMap(cam_img, cv2.COLORMAP_JET) 50 | return cam_img / 255.0 51 | 52 | def imagenet_norm(x): 53 | mean = [0.485, 0.456, 0.406] 54 | std = [0.299, 0.224, 0.225] 55 | mean = torch.FloatTensor(mean).unsqueeze(0).unsqueeze(2).unsqueeze(3).to(x.device) 56 | std = torch.FloatTensor(std).unsqueeze(0).unsqueeze(2).unsqueeze(3).to(x.device) 57 | return (x - mean) / std 58 | 59 | def denorm(x): 60 | return x * 0.5 + 0.5 61 | 62 | def tensor2numpy(x): 63 | return x.detach().cpu().numpy().transpose(1,2,0) 64 | 65 | def RGB2BGR(x): 66 | return cv2.cvtColor(x, cv2.COLOR_RGB2BGR) -------------------------------------------------------------------------------- /NICE-GAN/metric/SSIM.py: -------------------------------------------------------------------------------- 1 | from skimage.metrics import structural_similarity as ssim 2 | from PIL import Image 3 | import os 4 | import numpy as np 5 | from glob import glob 6 | from tqdm import tqdm 7 | import argparse 8 | 9 | 10 | def cal_SSIM(x, y): 11 | return ssim(x, y, multichannel=True) 12 | 13 | 14 | def get_dirImage(real_dir, fake_dir): 15 | real_image_paths = sorted(glob(os.path.join(real_dir, "*.*"))) 16 | fake_image_paths = sorted(glob(os.path.join(fake_dir, "*.*"))) 17 | return real_image_paths, fake_image_paths 18 | 19 | 20 | def cal_batch_SSIM(real_paths, fake_paths): 21 | assert len(real_paths) == len(fake_paths) 22 | 23 | SSIMs = [] 24 | for i in tqdm(range((len(real_paths)))): 25 | 26 | img1 = Image.open(real_paths[i]).resize((256, 256)) 27 | img2 = Image.open(fake_paths[i]).resize((256, 256)) 28 | 29 | tmp_ssim = cal_SSIM(np.array(img1), np.array(img2)) 30 | SSIMs.append(tmp_ssim) 31 | return SSIMs 32 | 33 | 34 | if __name__ == '__main__': 35 | parser = argparse.ArgumentParser() 36 | 37 | # parser.add_argument('--dir_root', type=str, default='/workspace/Image_translation_codes/pytorch-CycleGAN-and-pix2pix/results/SEN1_2_20%_spring_cyclegan/test_latest') 38 | parser.add_argument('--fake_dir', type=str, default='fake') 39 | parser.add_argument('--real_dir', type=str, default='real') 40 | 41 | # 只需要修改此处 42 | # root = r'D:\ZYT\Codes\1.Heterogeneous_CD\Image_Translation\pytorch-CycleGAN-and-pix2pix\results\sar2opt_pix2pix\test_latest' 43 | 44 | opt = parser.parse_args() 45 | # real_dir = opt.dir_root + '/' + opt.real_subdir 46 | # fake_dir = opt.dir_root + '/' + opt.fake_subdir 47 | 48 | real_paths, fake_paths = get_dirImage(opt.real_dir, opt.fake_dir) 49 | print(len(real_paths)) 50 | 51 | SSIMs = cal_batch_SSIM(real_paths, fake_paths) 52 | 53 | print("mean SSIM: ", np.mean(SSIMs)) 54 | 55 | -------------------------------------------------------------------------------- /NICE-GAN/metric/imqual_utils.py: -------------------------------------------------------------------------------- 1 | """ 2 | # > Implementation of the classic paper by Zhou Wang et. al.: 3 | # - Image quality assessment: from error visibility to structural similarity 4 | # - https://ieeexplore.ieee.org/stamp/stamp.jsp?arnumber=1284395 5 | # > Maintainer: https://github.com/xahidbuffon 6 | """ 7 | from __future__ import division 8 | import numpy as np 9 | import math 10 | from scipy.ndimage import gaussian_filter 11 | 12 | 13 | def getSSIM(X, Y): 14 | """ 15 | Computes the mean structural similarity between two images. 16 | """ 17 | # assert (X.shape[0] == Y.shape[0]), "Image-patche provided have different dimensions" 18 | assert (X.shape[0] == Y.shape[0] & X.shape[1] == Y.shape[1]), "Image-patche provided have different dimensions" 19 | nch = 1 if X.ndim==2 else X.shape[-1] 20 | mssim = [] 21 | for ch in range(nch): 22 | Xc, Yc = X[...,ch].astype(np.float64), Y[...,ch].astype(np.float64) 23 | mssim.append(compute_ssim(Xc, Yc)) 24 | return np.mean(mssim) 25 | 26 | 27 | def compute_ssim(X, Y): 28 | """ 29 | Compute the structural similarity per single channel (given two images) 30 | """ 31 | # variables are initialized as suggested in the paper 32 | K1 = 0.01 33 | K2 = 0.03 34 | sigma = 1.5 35 | win_size = 5 36 | 37 | # means 38 | ux = gaussian_filter(X, sigma) 39 | uy = gaussian_filter(Y, sigma) 40 | 41 | # variances and covariances 42 | uxx = gaussian_filter(X * X, sigma) 43 | uyy = gaussian_filter(Y * Y, sigma) 44 | uxy = gaussian_filter(X * Y, sigma) 45 | 46 | # normalize by unbiased estimate of std dev 47 | N = win_size ** X.ndim 48 | unbiased_norm = N / (N - 1) # eq. 4 of the paper 49 | vx = (uxx - ux * ux) * unbiased_norm 50 | vy = (uyy - uy * uy) * unbiased_norm 51 | vxy = (uxy - ux * uy) * unbiased_norm 52 | 53 | R = 255 54 | C1 = (K1 * R) ** 2 55 | C2 = (K2 * R) ** 2 56 | # compute SSIM (eq. 13 of the paper) 57 | sim = (2 * ux * uy + C1) * (2 * vxy + C2) 58 | D = (ux ** 2 + uy ** 2 + C1) * (vx + vy + C2) 59 | SSIM = sim/D 60 | mssim = SSIM.mean() 61 | 62 | return mssim 63 | 64 | 65 | 66 | def getPSNR(X, Y): 67 | #assume RGB image 68 | target_data = np.array(X, dtype=np.float64) 69 | ref_data = np.array(Y, dtype=np.float64) 70 | diff = ref_data - target_data 71 | diff = diff.flatten('C') 72 | rmse = math.sqrt(np.mean(diff ** 2.) ) 73 | if rmse == 0: return 100 74 | else: return 20*math.log10(255.0/rmse) 75 | 76 | 77 | -------------------------------------------------------------------------------- /NICE-GAN/metric/psnr_ssim.py: -------------------------------------------------------------------------------- 1 | """ 2 | # > Script for measuring quantitative performances in terms of 3 | # - Structural Similarity Metric (SSIM) 4 | # - Peak Signal to Noise Ratio (PSNR) 5 | # > Maintainer: https://github.com/xahidbuffon 6 | """ 7 | ## python libs 8 | import numpy as np 9 | from PIL import Image 10 | from glob import glob 11 | from os.path import join 12 | from ntpath import basename 13 | ## local libs 14 | from imqual_utils import getSSIM, getPSNR 15 | import cv2 16 | 17 | 18 | ## compares avg ssim and psnr 19 | def SSIMs_PSNRs(gtr_dir, gen_dir, im_res=(256, 256)): 20 | """ 21 | - gtr_dir contain ground-truths 22 | - gen_dir contain generated images 23 | """ 24 | gtr_paths = sorted(glob(join(gtr_dir, "*.*"))) 25 | gen_paths = sorted(glob(join(gen_dir, "*.*"))) 26 | ssims, psnrs = [], [] 27 | for gtr_path, gen_path in zip(gtr_paths, gen_paths): 28 | gtr_f = basename(gtr_path).split('.')[0] 29 | gen_f = basename(gen_path).split('.')[0] 30 | 31 | # read images from two datasets 32 | r_im = Image.open(gtr_path).resize(im_res) 33 | g_im = Image.open(gen_path).resize(im_res) 34 | 35 | # get ssim on RGB channels 36 | ssim = getSSIM(np.array(r_im), np.array(g_im)) 37 | ssims.append(ssim) 38 | # get psnt on L channel (SOTA norm) 39 | r_im = r_im.convert("L"); g_im = g_im.convert("L") 40 | psnr = getPSNR(np.array(r_im), np.array(g_im)) 41 | psnrs.append(psnr) 42 | return np.array(ssims), np.array(psnrs) 43 | 44 | 45 | """ 46 | Get datasets from 47 | - http://irvlab.cs.umn.edu/resources/euvp-dataset 48 | - http://irvlab.cs.umn.edu/resources/ufo-120-dataset 49 | """ 50 | # test img path 51 | gtr_dir = "/workspace/Image_translation_codes/NICE-GAN-pytorch/dataset/SEN1_2/Splited_by_percent/20%_train/ROIs1970_fall_20%_train/testB" 52 | 53 | # generated img path 54 | gen_dir = "/workspace/Image_translation_codes/NICE-GAN-pytorch/results/ROIs1970_fall_20%_train/fakeB" 55 | 56 | 57 | ### compute SSIM and PSNR 58 | SSIM_measures, PSNR_measures = SSIMs_PSNRs(gtr_dir, gen_dir) 59 | print ("SSIM on {0} samples".format(len(SSIM_measures))) 60 | print ("Mean: {0} std: {1}".format(np.mean(SSIM_measures), np.std(SSIM_measures))) 61 | 62 | print ("PSNR on {0} samples".format(len(PSNR_measures))) 63 | print ("Mean: {0} std: {1}".format(np.mean(PSNR_measures), np.std(PSNR_measures))) 64 | 65 | 66 | -------------------------------------------------------------------------------- /NICE-GAN/run_time.sh: -------------------------------------------------------------------------------- 1 | 2 | dataset_root='Image_translation_codes/NICE-GAN-pytorch/dataset/' 3 | dataset_name='SEN1_2/Splited_by_percent/20%_train/ROIs2017_winter_20%_train' 4 | 5 | dataset=$dataset_root$dataset_name 6 | 7 | for ((i=1; i<=5; i++)); 8 | do 9 | echo "Processing"$i"th epoch..." 10 | 11 | python Image_translation_codes/NICE-GAN-pytorch/main.py \ 12 | --dataset $dataset \ 13 | --phase test 14 | 15 | done -------------------------------------------------------------------------------- /NICE-GAN/runtime.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | from subprocess import run 3 | import numpy as np 4 | 5 | # spring: 974 summer: 610 fall: 1015 winter: 506 sar2opt: 587/ 627 6 | num_test = 506 7 | 8 | 9 | if __name__ == '__main__': 10 | cmd = "bash Image_translation_codes/NICE-GAN-pytorch/run_time.sh" 11 | 12 | results = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True) 13 | times = [] 14 | for line in results.stdout.readlines(): 15 | tmp = line.decode("utf-8").strip() 16 | 17 | if 'cost time' in tmp: 18 | times.append((float(tmp.split(": ")[-1])) / num_test) 19 | 20 | print('All cost times:', times) 21 | 22 | avg_time = np.mean(times) 23 | std_time = np.std(times) 24 | 25 | print("Average running time: ", avg_time) 26 | print("Deviation of running time: ", std_time) 27 | 28 | -------------------------------------------------------------------------------- /NICE-GAN/utils.py: -------------------------------------------------------------------------------- 1 | from scipy import misc 2 | import os, cv2, torch 3 | import numpy as np 4 | 5 | def load_test_data(image_path, size=256): 6 | img = misc.imread(image_path, mode='RGB') 7 | img = misc.imresize(img, [size, size]) 8 | img = np.expand_dims(img, axis=0) 9 | img = preprocessing(img) 10 | 11 | return img 12 | 13 | def preprocessing(x): 14 | x = x/127.5 - 1 # -1 ~ 1 15 | return x 16 | 17 | def save_images(images, size, image_path): 18 | return imsave(inverse_transform(images), size, image_path) 19 | 20 | def inverse_transform(images): 21 | return (images+1.) / 2 22 | 23 | def imsave(images, size, path): 24 | return misc.imsave(path, merge(images, size)) 25 | 26 | def merge(images, size): 27 | h, w = images.shape[1], images.shape[2] 28 | img = np.zeros((h * size[0], w * size[1], 3)) 29 | for idx, image in enumerate(images): 30 | i = idx % size[1] 31 | j = idx // size[1] 32 | img[h*j:h*(j+1), w*i:w*(i+1), :] = image 33 | 34 | return img 35 | 36 | def check_folder(log_dir): 37 | if not os.path.exists(log_dir): 38 | os.makedirs(log_dir) 39 | return log_dir 40 | 41 | def str2bool(x): 42 | return x.lower() in ('true') 43 | 44 | def cam(x, size = 256): 45 | x = x - np.min(x) 46 | cam_img = x / np.max(x) 47 | cam_img = np.uint8(255 * cam_img) 48 | cam_img = cv2.resize(cam_img, (size, size)) 49 | cam_img = cv2.applyColorMap(cam_img, cv2.COLORMAP_JET) 50 | return cam_img / 255.0 51 | 52 | def imagenet_norm(x): 53 | mean = [0.485, 0.456, 0.406] 54 | std = [0.299, 0.224, 0.225] 55 | mean = torch.FloatTensor(mean).unsqueeze(0).unsqueeze(2).unsqueeze(3).to(x.device) 56 | std = torch.FloatTensor(std).unsqueeze(0).unsqueeze(2).unsqueeze(3).to(x.device) 57 | return (x - mean) / std 58 | 59 | def denorm(x): 60 | return x * 0.5 + 0.5 61 | 62 | def tensor2numpy(x): 63 | return x.detach().cpu().numpy().transpose(1,2,0) 64 | 65 | def RGB2BGR(x): 66 | return cv2.cvtColor(x, cv2.COLOR_RGB2BGR) -------------------------------------------------------------------------------- /imgs/Honeyview_sar2opt.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MarsZhaoYT/SAR2Opt-Heterogeneous-Dataset/18f28ef26be16a9d0bee069d820fd83e2976f7b6/imgs/Honeyview_sar2opt.png -------------------------------------------------------------------------------- /imgs/Honeyview_translated_results.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MarsZhaoYT/SAR2Opt-Heterogeneous-Dataset/18f28ef26be16a9d0bee069d820fd83e2976f7b6/imgs/Honeyview_translated_results.png -------------------------------------------------------------------------------- /imgs/overview.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MarsZhaoYT/SAR2Opt-Heterogeneous-Dataset/18f28ef26be16a9d0bee069d820fd83e2976f7b6/imgs/overview.png -------------------------------------------------------------------------------- /imgs/results: -------------------------------------------------------------------------------- 1 | 2 | --------------------------------------------------------------------------------