├── LICENSE.md ├── README.md ├── cross_view_translation ├── README.md ├── data │ ├── __init__.py │ ├── __init__.pyc │ ├── __pycache__ │ │ ├── __init__.cpython-36.pyc │ │ ├── __init__.cpython-37.pyc │ │ ├── aligned_dataset.cpython-36.pyc │ │ ├── aligned_dataset.cpython-37.pyc │ │ ├── base_data_loader.cpython-36.pyc │ │ ├── base_data_loader.cpython-37.pyc │ │ ├── base_dataset.cpython-36.pyc │ │ ├── base_dataset.cpython-37.pyc │ │ ├── image_folder.cpython-36.pyc │ │ ├── image_folder.cpython-37.pyc │ │ └── single_dataset.cpython-36.pyc │ ├── aligned_dataset.py │ ├── aligned_dataset.pyc │ ├── base_data_loader.py │ ├── base_data_loader.pyc │ ├── base_dataset.py │ ├── base_dataset.pyc │ ├── image_folder.py │ ├── image_folder.pyc │ ├── single_dataset.py │ └── unaligned_dataset.py ├── datasets │ ├── download_lggan_dataset.sh │ ├── samples │ │ ├── cvusa │ │ │ ├── test │ │ │ │ ├── 0000002.jpg │ │ │ │ ├── 0000005.jpg │ │ │ │ ├── 0000014.jpg │ │ │ │ ├── 0000023.jpg │ │ │ │ ├── 0000027.jpg │ │ │ │ ├── 0000035.jpg │ │ │ │ ├── 0000037.jpg │ │ │ │ ├── 0000038.jpg │ │ │ │ ├── 0000049.jpg │ │ │ │ ├── 0000052.jpg │ │ │ │ ├── 0000066.jpg │ │ │ │ └── 0000067.jpg │ │ │ └── train │ │ │ │ ├── 0000001.jpg │ │ │ │ ├── 0000003.jpg │ │ │ │ ├── 0000004.jpg │ │ │ │ ├── 0000006.jpg │ │ │ │ ├── 0000007.jpg │ │ │ │ ├── 0000008.jpg │ │ │ │ ├── 0000009.jpg │ │ │ │ ├── 0000010.jpg │ │ │ │ ├── 0000011.jpg │ │ │ │ ├── 0000012.jpg │ │ │ │ └── 0000013.jpg │ │ ├── dayton │ │ │ ├── test │ │ │ │ ├── --oXLKfmtepqJ5OHQ84jZg.x832.y508.a-86.a2g.png │ │ │ │ ├── --oXLKfmtepqJ5OHQ84jZg.x851.y452.a-82.a2g.png │ │ │ │ ├── -0_Hxmk_U8TWBEu0uD7dug.x1295.y448.a-84.a2g.png │ │ │ │ ├── -0_Hxmk_U8TWBEu0uD7dug.x365.y470.a75.a2g.png │ │ │ │ ├── -0n6gItL08Q522U_x0UKaA.x1183.y455.a-106.a2g.png │ │ │ │ ├── -0n6gItL08Q522U_x0UKaA.x309.y405.a64.a2g.png │ │ │ │ ├── -0n6gItL08Q522U_x0UKaA.x8.y481.a0.a2g.png │ │ │ │ ├── -0n6gItL08Q522U_x0UKaA.x837.y509.a179.a2g.png │ │ │ │ ├── -19hmUez9cLpI3Sq1-HXJw.x107.y483.a-42.a2g.png │ │ │ │ ├── -1kaR7iId-fJdN1A1OS6FA.x1007.y473.a9.a2g.png │ │ │ │ ├── -1kaR7iId-fJdN1A1OS6FA.x1216.y470.a54.a2g.png │ │ │ │ └── -1kaR7iId-fJdN1A1OS6FA.x684.y491.a-60.a2g.png │ │ │ └── train │ │ │ │ ├── --oXLKfmtepqJ5OHQ84jZg.x1066.y499.a-36.a2g.png │ │ │ │ ├── --oXLKfmtepqJ5OHQ84jZg.x1281.y482.a10.a2g.png │ │ │ │ ├── --oXLKfmtepqJ5OHQ84jZg.x1428.y496.a41.a2g.png │ │ │ │ ├── --oXLKfmtepqJ5OHQ84jZg.x1631.y494.a85.a2g.png │ │ │ │ ├── --oXLKfmtepqJ5OHQ84jZg.x261.y478.a150.a2g.png │ │ │ │ ├── --oXLKfmtepqJ5OHQ84jZg.x317.y421.a162.a2g.png │ │ │ │ ├── --oXLKfmtepqJ5OHQ84jZg.x62.y463.a107.a2g.png │ │ │ │ ├── --oXLKfmtepqJ5OHQ84jZg.x892.y481.a-74.a2g.png │ │ │ │ ├── -0_Hxmk_U8TWBEu0uD7dug.x1172.y440.a-111.a2g.png │ │ │ │ └── -0_Hxmk_U8TWBEu0uD7dug.x168.y467.a32.a2g.png │ │ └── sva │ │ │ ├── test │ │ │ ├── 000_002540.png │ │ │ ├── 000_002550.png │ │ │ ├── 000_002560.png │ │ │ ├── 000_002570.png │ │ │ ├── 000_002580.png │ │ │ ├── 000_002590.png │ │ │ ├── 000_002600.png │ │ │ ├── 000_002610.png │ │ │ ├── 000_002620.png │ │ │ └── 000_002630.png │ │ │ └── train │ │ │ ├── 001_000010.png │ │ │ ├── 001_000020.png │ │ │ ├── 001_000030.png │ │ │ ├── 001_000040.png │ │ │ ├── 001_000050.png │ │ │ ├── 001_000060.png │ │ │ ├── 001_000070.png │ │ │ ├── 001_000080.png │ │ │ ├── 001_000090.png │ │ │ ├── 001_000100.png │ │ │ └── 001_000110.png │ └── sva_split │ │ ├── sva_lggan_test.txt │ │ └── sva_lggan_train.txt ├── environment.yml ├── models │ ├── __init__.py │ ├── __init__.pyc │ ├── __pycache__ │ │ ├── __init__.cpython-36.pyc │ │ ├── __init__.cpython-37.pyc │ │ ├── base_model.cpython-36.pyc │ │ ├── base_model.cpython-37.pyc │ │ ├── cycle_gan_model.cpython-36.pyc │ │ ├── networks.cpython-36.pyc │ │ ├── networks.cpython-37.pyc │ │ ├── pix2pix_model.cpython-36.pyc │ │ ├── pix2pix_model.cpython-37.pyc │ │ └── test_model.cpython-36.pyc │ ├── base_model.py │ ├── lggan_model.py │ ├── networks.py │ └── test_model.py ├── options │ ├── __init__.py │ ├── __init__.pyc │ ├── __pycache__ │ │ ├── __init__.cpython-36.pyc │ │ ├── __init__.cpython-37.pyc │ │ ├── base_options.cpython-36.pyc │ │ ├── base_options.cpython-37.pyc │ │ ├── test_options.cpython-36.pyc │ │ ├── train_options.cpython-36.pyc │ │ └── train_options.cpython-37.pyc │ ├── base_options.py │ ├── base_options.pyc │ ├── test_options.py │ ├── test_options.pyc │ ├── train_options.py │ └── train_options.pyc ├── requirements.txt ├── scripts │ ├── cityscapes_preprosess.m │ ├── conda_deps.sh │ ├── data_preprocessing.m │ ├── download_lggan_model.sh │ └── install_deps.sh ├── test.py ├── test_cvusa.sh ├── test_dayton_a2g_256.sh ├── test_dayton_a2g_64.sh ├── test_dayton_g2a_256.sh ├── test_dayton_g2a_64.sh ├── test_sva.sh ├── train.py ├── train_cvusa.sh ├── train_dayton_a2g_256.sh ├── train_dayton_a2g_64.sh ├── train_dayton_g2a_256.sh ├── train_dayton_g2a_64.sh ├── train_sva.sh └── util │ ├── __init__.py │ ├── __init__.pyc │ ├── __pycache__ │ ├── __init__.cpython-36.pyc │ ├── __init__.cpython-37.pyc │ ├── html.cpython-36.pyc │ ├── html.cpython-37.pyc │ ├── image_pool.cpython-36.pyc │ ├── image_pool.cpython-37.pyc │ ├── util.cpython-36.pyc │ ├── util.cpython-37.pyc │ ├── visualizer.cpython-36.pyc │ └── visualizer.cpython-37.pyc │ ├── get_data.py │ ├── html.py │ ├── html.pyc │ ├── image_pool.py │ ├── image_pool.pyc │ ├── util.py │ ├── util.pyc │ ├── visualizer.py │ └── visualizer.pyc ├── imgs ├── cross_view_results.jpg ├── feature_map.jpg ├── framework.jpg ├── seg_ade20k.jpg ├── seg_city.jpg └── semantic_results.jpg └── semantic_image_synthesis ├── README.md ├── ade20k ├── models │ ├── networks │ │ └── generator.py │ └── pix2pix_model.py └── trainers │ └── pix2pix_trainer.py ├── data ├── __init__.py ├── __pycache__ │ ├── __init__.cpython-36.pyc │ ├── __init__.cpython-37.pyc │ ├── ade20k_dataset.cpython-37.pyc │ ├── base_dataset.cpython-36.pyc │ ├── base_dataset.cpython-37.pyc │ ├── cityscapes_dataset.cpython-36.pyc │ ├── cityscapes_dataset.cpython-37.pyc │ ├── image_folder.cpython-36.pyc │ ├── image_folder.cpython-37.pyc │ ├── pix2pix_dataset.cpython-36.pyc │ └── pix2pix_dataset.cpython-37.pyc ├── ade20k_dataset.py ├── base_dataset.py ├── cityscapes_dataset.py ├── coco_dataset.py ├── custom_dataset.py ├── facades_dataset.py ├── image_folder.py └── pix2pix_dataset.py ├── models ├── __init__.py ├── __pycache__ │ ├── __init__.cpython-36.pyc │ ├── __init__.cpython-37.pyc │ ├── pix2pix_model.cpython-36.pyc │ └── pix2pix_model.cpython-37.pyc ├── networks │ ├── __init__.py │ ├── __pycache__ │ │ ├── __init__.cpython-36.pyc │ │ ├── __init__.cpython-37.pyc │ │ ├── architecture.cpython-36.pyc │ │ ├── architecture.cpython-37.pyc │ │ ├── base_network.cpython-36.pyc │ │ ├── base_network.cpython-37.pyc │ │ ├── discriminator.cpython-36.pyc │ │ ├── discriminator.cpython-37.pyc │ │ ├── encoder.cpython-36.pyc │ │ ├── encoder.cpython-37.pyc │ │ ├── generator.cpython-36.pyc │ │ ├── generator.cpython-37.pyc │ │ ├── loss.cpython-36.pyc │ │ ├── loss.cpython-37.pyc │ │ ├── normalization.cpython-36.pyc │ │ └── normalization.cpython-37.pyc │ ├── architecture.py │ ├── base_network.py │ ├── discriminator.py │ ├── encoder.py │ ├── generator.py │ ├── loss.py │ └── normalization.py └── pix2pix_model.py ├── options ├── __init__.py ├── __pycache__ │ ├── __init__.cpython-36.pyc │ ├── __init__.cpython-37.pyc │ ├── base_options.cpython-36.pyc │ ├── base_options.cpython-37.pyc │ ├── test_options.cpython-36.pyc │ ├── test_options.cpython-37.pyc │ ├── train_options.cpython-36.pyc │ └── train_options.cpython-37.pyc ├── base_options.py ├── test_options.py └── train_options.py ├── requirements.txt ├── scripts └── download_lggan_model.sh ├── test_ade.py ├── test_ade.sh ├── test_city.py ├── test_cityscapes.sh ├── train_ade.py ├── train_ade.sh ├── train_city.py ├── train_cityscapes.sh ├── trainers ├── __init__.py ├── __pycache__ │ ├── __init__.cpython-36.pyc │ └── pix2pix_trainer.cpython-36.pyc └── pix2pix_trainer.py └── util ├── __init__.py ├── __pycache__ ├── __init__.cpython-36.pyc ├── __init__.cpython-37.pyc ├── coco.cpython-36.pyc ├── coco.cpython-37.pyc ├── html.cpython-36.pyc ├── html.cpython-37.pyc ├── iter_counter.cpython-36.pyc ├── util.cpython-36.pyc ├── util.cpython-37.pyc ├── visualizer.cpython-36.pyc └── visualizer.cpython-37.pyc ├── coco.py ├── html.py ├── iter_counter.py ├── util.py └── visualizer.py /cross_view_translation/data/__init__.py: -------------------------------------------------------------------------------- 1 | import importlib 2 | import torch.utils.data 3 | from data.base_data_loader import BaseDataLoader 4 | from data.base_dataset import BaseDataset 5 | 6 | def find_dataset_using_name(dataset_name): 7 | # Given the option --dataset_mode [datasetname], 8 | # the file "data/datasetname_dataset.py" 9 | # will be imported. 10 | dataset_filename = "data." + dataset_name + "_dataset" 11 | datasetlib = importlib.import_module(dataset_filename) 12 | 13 | # In the file, the class called DatasetNameDataset() will 14 | # be instantiated. It has to be a subclass of BaseDataset, 15 | # and it is case-insensitive. 16 | dataset = None 17 | target_dataset_name = dataset_name.replace('_', '') + 'dataset' 18 | for name, cls in datasetlib.__dict__.items(): 19 | if name.lower() == target_dataset_name.lower() \ 20 | and issubclass(cls, BaseDataset): 21 | dataset = cls 22 | 23 | if dataset is None: 24 | print("In %s.py, there should be a subclass of BaseDataset with class name that matches %s in lowercase." % (dataset_filename, target_dataset_name)) 25 | exit(0) 26 | 27 | return dataset 28 | 29 | 30 | def get_option_setter(dataset_name): 31 | dataset_class = find_dataset_using_name(dataset_name) 32 | return dataset_class.modify_commandline_options 33 | 34 | 35 | def create_dataset(opt): 36 | dataset = find_dataset_using_name(opt.dataset_mode) 37 | instance = dataset() 38 | instance.initialize(opt) 39 | print("dataset [%s] was created" % (instance.name())) 40 | return instance 41 | 42 | 43 | def CreateDataLoader(opt): 44 | data_loader = CustomDatasetDataLoader() 45 | data_loader.initialize(opt) 46 | return data_loader 47 | 48 | 49 | ## Wrapper class of Dataset class that performs 50 | ## multi-threaded data loading 51 | class CustomDatasetDataLoader(BaseDataLoader): 52 | def name(self): 53 | return 'CustomDatasetDataLoader' 54 | 55 | def initialize(self, opt): 56 | BaseDataLoader.initialize(self, opt) 57 | self.dataset = create_dataset(opt) 58 | self.dataloader = torch.utils.data.DataLoader( 59 | self.dataset, 60 | batch_size=opt.batchSize, 61 | shuffle=not opt.serial_batches, 62 | num_workers=int(opt.nThreads)) 63 | 64 | def load_data(self): 65 | return self 66 | 67 | def __len__(self): 68 | return min(len(self.dataset), self.opt.max_dataset_size) 69 | 70 | def __iter__(self): 71 | for i, data in enumerate(self.dataloader): 72 | if i * self.opt.batchSize >= self.opt.max_dataset_size: 73 | break 74 | yield data 75 | -------------------------------------------------------------------------------- /cross_view_translation/data/__init__.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/data/__init__.pyc -------------------------------------------------------------------------------- /cross_view_translation/data/__pycache__/__init__.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/data/__pycache__/__init__.cpython-36.pyc -------------------------------------------------------------------------------- /cross_view_translation/data/__pycache__/__init__.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/data/__pycache__/__init__.cpython-37.pyc -------------------------------------------------------------------------------- /cross_view_translation/data/__pycache__/aligned_dataset.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/data/__pycache__/aligned_dataset.cpython-36.pyc -------------------------------------------------------------------------------- /cross_view_translation/data/__pycache__/aligned_dataset.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/data/__pycache__/aligned_dataset.cpython-37.pyc -------------------------------------------------------------------------------- /cross_view_translation/data/__pycache__/base_data_loader.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/data/__pycache__/base_data_loader.cpython-36.pyc -------------------------------------------------------------------------------- /cross_view_translation/data/__pycache__/base_data_loader.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/data/__pycache__/base_data_loader.cpython-37.pyc -------------------------------------------------------------------------------- /cross_view_translation/data/__pycache__/base_dataset.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/data/__pycache__/base_dataset.cpython-36.pyc -------------------------------------------------------------------------------- /cross_view_translation/data/__pycache__/base_dataset.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/data/__pycache__/base_dataset.cpython-37.pyc -------------------------------------------------------------------------------- /cross_view_translation/data/__pycache__/image_folder.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/data/__pycache__/image_folder.cpython-36.pyc -------------------------------------------------------------------------------- /cross_view_translation/data/__pycache__/image_folder.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/data/__pycache__/image_folder.cpython-37.pyc -------------------------------------------------------------------------------- /cross_view_translation/data/__pycache__/single_dataset.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/data/__pycache__/single_dataset.cpython-36.pyc -------------------------------------------------------------------------------- /cross_view_translation/data/aligned_dataset.py: -------------------------------------------------------------------------------- 1 | import os.path 2 | import random 3 | import torchvision.transforms as transforms 4 | import torch 5 | from data.base_dataset import BaseDataset 6 | from data.image_folder import make_dataset 7 | from PIL import Image 8 | 9 | 10 | class AlignedDataset(BaseDataset): 11 | @staticmethod 12 | def modify_commandline_options(parser, is_train): 13 | return parser 14 | 15 | def initialize(self, opt): 16 | self.opt = opt 17 | self.root = opt.dataroot 18 | self.dir_AB = os.path.join(opt.dataroot, opt.phase) 19 | self.AB_paths = sorted(make_dataset(self.dir_AB)) 20 | assert(opt.resize_or_crop == 'resize_and_crop') 21 | 22 | def __getitem__(self, index): 23 | AB_path = self.AB_paths[index] 24 | ABCD = Image.open(AB_path).convert('RGB') 25 | w, h = ABCD.size 26 | w2 = int(w / 6) 27 | A = ABCD.crop((0, 0, w2, h)).resize((self.opt.loadSize, self.opt.loadSize), Image.BICUBIC) 28 | B = ABCD.crop((w2, 0, w2+w2, h)).resize((self.opt.loadSize, self.opt.loadSize), Image.BICUBIC) 29 | C = ABCD.crop((w2+w2, 0, w2+w2+w2, h)).resize((self.opt.loadSize, self.opt.loadSize), Image.BICUBIC) 30 | D = ABCD.crop((w2+w2+w2, 0, w2+w2+w2+w2, h)).resize((self.opt.loadSize, self.opt.loadSize), Image.BICUBIC) 31 | E = ABCD.crop((w2+w2+w2+w2, 0, w2+w2+w2+w2+w2, h)).resize((self.opt.loadSize, self.opt.loadSize), Image.BICUBIC) 32 | F = ABCD.crop((w2+w2+w2+w2+w2, 0, w, h)).resize((self.opt.loadSize, self.opt.loadSize), Image.BICUBIC) 33 | # A.show() 34 | # B.show() 35 | # C.show() 36 | # D.show() 37 | 38 | A = transforms.ToTensor()(A) 39 | B = transforms.ToTensor()(B) 40 | C = transforms.ToTensor()(C) 41 | D = transforms.ToTensor()(D) 42 | E = transforms.ToTensor()(E) 43 | F = transforms.ToTensor()(F) 44 | w_offset = random.randint(0, max(0, self.opt.loadSize - self.opt.fineSize - 1)) 45 | h_offset = random.randint(0, max(0, self.opt.loadSize - self.opt.fineSize - 1)) 46 | 47 | A = A[:, h_offset:h_offset + self.opt.fineSize, w_offset:w_offset + self.opt.fineSize] 48 | B = B[:, h_offset:h_offset + self.opt.fineSize, w_offset:w_offset + self.opt.fineSize] 49 | C = C[:, h_offset:h_offset + self.opt.fineSize, w_offset:w_offset + self.opt.fineSize] 50 | D = D[:, h_offset:h_offset + self.opt.fineSize, w_offset:w_offset + self.opt.fineSize] 51 | E = E[:, h_offset:h_offset + self.opt.fineSize, w_offset:w_offset + self.opt.fineSize] 52 | F = F[:, h_offset:h_offset + self.opt.fineSize, w_offset:w_offset + self.opt.fineSize] 53 | 54 | A = transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))(A) 55 | B = transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))(B) 56 | C = transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))(C) 57 | D = transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))(D) 58 | E = transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))(E) 59 | F = transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))(F) 60 | 61 | if self.opt.which_direction == 'BtoA': 62 | input_nc = self.opt.output_nc 63 | output_nc = self.opt.input_nc 64 | else: 65 | input_nc = self.opt.input_nc 66 | output_nc = self.opt.output_nc 67 | 68 | if (not self.opt.no_flip) and random.random() < 0.5: 69 | idx = [i for i in range(A.size(2) - 1, -1, -1)] 70 | idx = torch.LongTensor(idx) 71 | A = A.index_select(2, idx) 72 | B = B.index_select(2, idx) 73 | 74 | if input_nc == 1: # RGB to gray 75 | tmp = A[0, ...] * 0.299 + A[1, ...] * 0.587 + A[2, ...] * 0.114 76 | A = tmp.unsqueeze(0) 77 | 78 | if output_nc == 1: # RGB to gray 79 | tmp = B[0, ...] * 0.299 + B[1, ...] * 0.587 + B[2, ...] * 0.114 80 | B = tmp.unsqueeze(0) 81 | 82 | return {'A': A, 'B': B, 'C': C, 'D': D, 'E': E, 'F': F, 83 | 'A_paths': AB_path, 'B_paths': AB_path} 84 | 85 | def __len__(self): 86 | return len(self.AB_paths) 87 | 88 | def name(self): 89 | return 'AlignedDataset' 90 | -------------------------------------------------------------------------------- /cross_view_translation/data/aligned_dataset.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/data/aligned_dataset.pyc -------------------------------------------------------------------------------- /cross_view_translation/data/base_data_loader.py: -------------------------------------------------------------------------------- 1 | class BaseDataLoader(): 2 | def __init__(self): 3 | pass 4 | 5 | def initialize(self, opt): 6 | self.opt = opt 7 | pass 8 | 9 | def load_data(): 10 | return None 11 | -------------------------------------------------------------------------------- /cross_view_translation/data/base_data_loader.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/data/base_data_loader.pyc -------------------------------------------------------------------------------- /cross_view_translation/data/base_dataset.py: -------------------------------------------------------------------------------- 1 | import torch.utils.data as data 2 | from PIL import Image 3 | import torchvision.transforms as transforms 4 | 5 | 6 | class BaseDataset(data.Dataset): 7 | def __init__(self): 8 | super(BaseDataset, self).__init__() 9 | 10 | def name(self): 11 | return 'BaseDataset' 12 | 13 | @staticmethod 14 | def modify_commandline_options(parser, is_train): 15 | return parser 16 | 17 | def initialize(self, opt): 18 | pass 19 | 20 | def __len__(self): 21 | return 0 22 | 23 | 24 | def get_transform(opt): 25 | transform_list = [] 26 | if opt.resize_or_crop == 'resize_and_crop': 27 | osize = [opt.loadSize, opt.loadSize] 28 | transform_list.append(transforms.Resize(osize, Image.BICUBIC)) 29 | transform_list.append(transforms.RandomCrop(opt.fineSize)) 30 | elif opt.resize_or_crop == 'crop': 31 | transform_list.append(transforms.RandomCrop(opt.fineSize)) 32 | elif opt.resize_or_crop == 'scale_width': 33 | transform_list.append(transforms.Lambda( 34 | lambda img: __scale_width(img, opt.fineSize))) 35 | elif opt.resize_or_crop == 'scale_width_and_crop': 36 | transform_list.append(transforms.Lambda( 37 | lambda img: __scale_width(img, opt.loadSize))) 38 | transform_list.append(transforms.RandomCrop(opt.fineSize)) 39 | elif opt.resize_or_crop == 'none': 40 | transform_list.append(transforms.Lambda( 41 | lambda img: __adjust(img))) 42 | else: 43 | raise ValueError('--resize_or_crop %s is not a valid option.' % opt.resize_or_crop) 44 | 45 | if opt.isTrain and not opt.no_flip: 46 | transform_list.append(transforms.RandomHorizontalFlip()) 47 | 48 | transform_list += [transforms.ToTensor(), 49 | transforms.Normalize((0.5, 0.5, 0.5), 50 | (0.5, 0.5, 0.5))] 51 | return transforms.Compose(transform_list) 52 | 53 | # just modify the width and height to be multiple of 4 54 | def __adjust(img): 55 | ow, oh = img.size 56 | 57 | # the size needs to be a multiple of this number, 58 | # because going through generator network may change img size 59 | # and eventually cause size mismatch error 60 | mult = 4 61 | if ow % mult == 0 and oh % mult == 0: 62 | return img 63 | w = (ow - 1) // mult 64 | w = (w + 1) * mult 65 | h = (oh - 1) // mult 66 | h = (h + 1) * mult 67 | 68 | if ow != w or oh != h: 69 | __print_size_warning(ow, oh, w, h) 70 | 71 | return img.resize((w, h), Image.BICUBIC) 72 | 73 | 74 | def __scale_width(img, target_width): 75 | ow, oh = img.size 76 | 77 | # the size needs to be a multiple of this number, 78 | # because going through generator network may change img size 79 | # and eventually cause size mismatch error 80 | mult = 4 81 | assert target_width % mult == 0, "the target width needs to be multiple of %d." % mult 82 | if (ow == target_width and oh % mult == 0): 83 | return img 84 | w = target_width 85 | target_height = int(target_width * oh / ow) 86 | m = (target_height - 1) // mult 87 | h = (m + 1) * mult 88 | 89 | if target_height != h: 90 | __print_size_warning(target_width, target_height, w, h) 91 | 92 | return img.resize((w, h), Image.BICUBIC) 93 | 94 | 95 | def __print_size_warning(ow, oh, w, h): 96 | if not hasattr(__print_size_warning, 'has_printed'): 97 | print("The image size needs to be a multiple of 4. " 98 | "The loaded image size was (%d, %d), so it was adjusted to " 99 | "(%d, %d). This adjustment will be done to all images " 100 | "whose sizes are not multiples of 4" % (ow, oh, w, h)) 101 | __print_size_warning.has_printed = True 102 | 103 | 104 | -------------------------------------------------------------------------------- /cross_view_translation/data/base_dataset.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/data/base_dataset.pyc -------------------------------------------------------------------------------- /cross_view_translation/data/image_folder.py: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Code from 3 | # https://github.com/pytorch/vision/blob/master/torchvision/datasets/folder.py 4 | # Modified the original code so that it also loads images from the current 5 | # directory as well as the subdirectories 6 | ############################################################################### 7 | 8 | import torch.utils.data as data 9 | 10 | from PIL import Image 11 | import os 12 | import os.path 13 | 14 | IMG_EXTENSIONS = [ 15 | '.jpg', '.JPG', '.jpeg', '.JPEG', 16 | '.png', '.PNG', '.ppm', '.PPM', '.bmp', '.BMP', 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): 25 | images = [] 26 | assert os.path.isdir(dir), '%s is not a valid directory' % dir 27 | 28 | for root, _, fnames in sorted(os.walk(dir)): 29 | for fname in fnames: 30 | if is_image_file(fname): 31 | path = os.path.join(root, fname) 32 | images.append(path) 33 | 34 | return images 35 | 36 | 37 | def default_loader(path): 38 | return Image.open(path).convert('RGB') 39 | 40 | 41 | class ImageFolder(data.Dataset): 42 | 43 | def __init__(self, root, transform=None, return_paths=False, 44 | loader=default_loader): 45 | imgs = make_dataset(root) 46 | if len(imgs) == 0: 47 | raise(RuntimeError("Found 0 images in: " + root + "\n" 48 | "Supported image extensions are: " + 49 | ",".join(IMG_EXTENSIONS))) 50 | 51 | self.root = root 52 | self.imgs = imgs 53 | self.transform = transform 54 | self.return_paths = return_paths 55 | self.loader = loader 56 | 57 | def __getitem__(self, index): 58 | path = self.imgs[index] 59 | img = self.loader(path) 60 | if self.transform is not None: 61 | img = self.transform(img) 62 | if self.return_paths: 63 | return img, path 64 | else: 65 | return img 66 | 67 | def __len__(self): 68 | return len(self.imgs) 69 | -------------------------------------------------------------------------------- /cross_view_translation/data/image_folder.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/data/image_folder.pyc -------------------------------------------------------------------------------- /cross_view_translation/data/single_dataset.py: -------------------------------------------------------------------------------- 1 | import os.path 2 | from data.base_dataset import BaseDataset, get_transform 3 | from data.image_folder import make_dataset 4 | from PIL import Image 5 | 6 | 7 | class SingleDataset(BaseDataset): 8 | @staticmethod 9 | def modify_commandline_options(parser, is_train): 10 | return parser 11 | 12 | def initialize(self, opt): 13 | self.opt = opt 14 | self.root = opt.dataroot 15 | self.dir_A = os.path.join(opt.dataroot) 16 | 17 | self.A_paths = make_dataset(self.dir_A) 18 | 19 | self.A_paths = sorted(self.A_paths) 20 | 21 | self.transform = get_transform(opt) 22 | 23 | def __getitem__(self, index): 24 | A_path = self.A_paths[index] 25 | A_img = Image.open(A_path).convert('RGB') 26 | A = self.transform(A_img) 27 | if self.opt.which_direction == 'BtoA': 28 | input_nc = self.opt.output_nc 29 | else: 30 | input_nc = self.opt.input_nc 31 | 32 | if input_nc == 1: # RGB to gray 33 | tmp = A[0, ...] * 0.299 + A[1, ...] * 0.587 + A[2, ...] * 0.114 34 | A = tmp.unsqueeze(0) 35 | 36 | return {'A': A, 'A_paths': A_path} 37 | 38 | def __len__(self): 39 | return len(self.A_paths) 40 | 41 | def name(self): 42 | return 'SingleImageDataset' 43 | -------------------------------------------------------------------------------- /cross_view_translation/data/unaligned_dataset.py: -------------------------------------------------------------------------------- 1 | import os.path 2 | from data.base_dataset import BaseDataset, get_transform 3 | from data.image_folder import make_dataset 4 | from PIL import Image 5 | import random 6 | 7 | 8 | class UnalignedDataset(BaseDataset): 9 | @staticmethod 10 | def modify_commandline_options(parser, is_train): 11 | return parser 12 | 13 | def initialize(self, opt): 14 | self.opt = opt 15 | self.root = opt.dataroot 16 | self.dir_A = os.path.join(opt.dataroot, opt.phase + 'A') 17 | self.dir_B = os.path.join(opt.dataroot, opt.phase + 'B') 18 | 19 | self.A_paths = make_dataset(self.dir_A) 20 | self.B_paths = make_dataset(self.dir_B) 21 | 22 | self.A_paths = sorted(self.A_paths) 23 | self.B_paths = sorted(self.B_paths) 24 | self.A_size = len(self.A_paths) 25 | self.B_size = len(self.B_paths) 26 | self.transform = get_transform(opt) 27 | 28 | def __getitem__(self, index): 29 | A_path = self.A_paths[index % self.A_size] 30 | if self.opt.serial_batches: 31 | index_B = index % self.B_size 32 | else: 33 | index_B = random.randint(0, self.B_size - 1) 34 | B_path = self.B_paths[index_B] 35 | # print('(A, B) = (%d, %d)' % (index_A, index_B)) 36 | A_img = Image.open(A_path).convert('RGB') 37 | B_img = Image.open(B_path).convert('RGB') 38 | 39 | A = self.transform(A_img) 40 | B = self.transform(B_img) 41 | if self.opt.which_direction == 'BtoA': 42 | input_nc = self.opt.output_nc 43 | output_nc = self.opt.input_nc 44 | else: 45 | input_nc = self.opt.input_nc 46 | output_nc = self.opt.output_nc 47 | 48 | if input_nc == 1: # RGB to gray 49 | tmp = A[0, ...] * 0.299 + A[1, ...] * 0.587 + A[2, ...] * 0.114 50 | A = tmp.unsqueeze(0) 51 | 52 | if output_nc == 1: # RGB to gray 53 | tmp = B[0, ...] * 0.299 + B[1, ...] * 0.587 + B[2, ...] * 0.114 54 | B = tmp.unsqueeze(0) 55 | return {'A': A, 'B': B, 56 | 'A_paths': A_path, 'B_paths': B_path} 57 | 58 | def __len__(self): 59 | return max(self.A_size, self.B_size) 60 | 61 | def name(self): 62 | return 'UnalignedDataset' 63 | -------------------------------------------------------------------------------- /cross_view_translation/datasets/download_lggan_dataset.sh: -------------------------------------------------------------------------------- 1 | FILE=$1 2 | 3 | if [[ $FILE != "cvusa_lggan" && $FILE != "dayton_lggan" && $FILE != "dayton_ablation_lggan" && $FILE != "sva_lggan" ]]; 4 | then echo "Available datasets are cvusa_lggan, dayton_lggan, dayton_ablation_lggan, sva_lggan" 5 | exit 1 6 | fi 7 | 8 | 9 | echo "Specified [$FILE]" 10 | 11 | URL=http://disi.unitn.it/~hao.tang/uploads/datasets/LGGAN/$FILE.tar.gz 12 | TAR_FILE=./datasets/$FILE.tar.gz 13 | TARGET_DIR=./datasets/$FILE/ 14 | wget -N $URL -O $TAR_FILE 15 | mkdir -p $TARGET_DIR 16 | tar -zxvf $TAR_FILE -C ./datasets/ 17 | rm $TAR_FILE 18 | -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/cvusa/test/0000002.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/cvusa/test/0000002.jpg -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/cvusa/test/0000005.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/cvusa/test/0000005.jpg -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/cvusa/test/0000014.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/cvusa/test/0000014.jpg -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/cvusa/test/0000023.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/cvusa/test/0000023.jpg -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/cvusa/test/0000027.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/cvusa/test/0000027.jpg -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/cvusa/test/0000035.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/cvusa/test/0000035.jpg -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/cvusa/test/0000037.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/cvusa/test/0000037.jpg -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/cvusa/test/0000038.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/cvusa/test/0000038.jpg -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/cvusa/test/0000049.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/cvusa/test/0000049.jpg -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/cvusa/test/0000052.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/cvusa/test/0000052.jpg -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/cvusa/test/0000066.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/cvusa/test/0000066.jpg -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/cvusa/test/0000067.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/cvusa/test/0000067.jpg -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/cvusa/train/0000001.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/cvusa/train/0000001.jpg -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/cvusa/train/0000003.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/cvusa/train/0000003.jpg -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/cvusa/train/0000004.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/cvusa/train/0000004.jpg -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/cvusa/train/0000006.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/cvusa/train/0000006.jpg -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/cvusa/train/0000007.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/cvusa/train/0000007.jpg -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/cvusa/train/0000008.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/cvusa/train/0000008.jpg -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/cvusa/train/0000009.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/cvusa/train/0000009.jpg -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/cvusa/train/0000010.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/cvusa/train/0000010.jpg -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/cvusa/train/0000011.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/cvusa/train/0000011.jpg -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/cvusa/train/0000012.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/cvusa/train/0000012.jpg -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/cvusa/train/0000013.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/cvusa/train/0000013.jpg -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/dayton/test/--oXLKfmtepqJ5OHQ84jZg.x832.y508.a-86.a2g.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/dayton/test/--oXLKfmtepqJ5OHQ84jZg.x832.y508.a-86.a2g.png -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/dayton/test/--oXLKfmtepqJ5OHQ84jZg.x851.y452.a-82.a2g.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/dayton/test/--oXLKfmtepqJ5OHQ84jZg.x851.y452.a-82.a2g.png -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/dayton/test/-0_Hxmk_U8TWBEu0uD7dug.x1295.y448.a-84.a2g.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/dayton/test/-0_Hxmk_U8TWBEu0uD7dug.x1295.y448.a-84.a2g.png -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/dayton/test/-0_Hxmk_U8TWBEu0uD7dug.x365.y470.a75.a2g.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/dayton/test/-0_Hxmk_U8TWBEu0uD7dug.x365.y470.a75.a2g.png -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/dayton/test/-0n6gItL08Q522U_x0UKaA.x1183.y455.a-106.a2g.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/dayton/test/-0n6gItL08Q522U_x0UKaA.x1183.y455.a-106.a2g.png -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/dayton/test/-0n6gItL08Q522U_x0UKaA.x309.y405.a64.a2g.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/dayton/test/-0n6gItL08Q522U_x0UKaA.x309.y405.a64.a2g.png -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/dayton/test/-0n6gItL08Q522U_x0UKaA.x8.y481.a0.a2g.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/dayton/test/-0n6gItL08Q522U_x0UKaA.x8.y481.a0.a2g.png -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/dayton/test/-0n6gItL08Q522U_x0UKaA.x837.y509.a179.a2g.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/dayton/test/-0n6gItL08Q522U_x0UKaA.x837.y509.a179.a2g.png -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/dayton/test/-19hmUez9cLpI3Sq1-HXJw.x107.y483.a-42.a2g.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/dayton/test/-19hmUez9cLpI3Sq1-HXJw.x107.y483.a-42.a2g.png -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/dayton/test/-1kaR7iId-fJdN1A1OS6FA.x1007.y473.a9.a2g.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/dayton/test/-1kaR7iId-fJdN1A1OS6FA.x1007.y473.a9.a2g.png -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/dayton/test/-1kaR7iId-fJdN1A1OS6FA.x1216.y470.a54.a2g.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/dayton/test/-1kaR7iId-fJdN1A1OS6FA.x1216.y470.a54.a2g.png -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/dayton/test/-1kaR7iId-fJdN1A1OS6FA.x684.y491.a-60.a2g.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/dayton/test/-1kaR7iId-fJdN1A1OS6FA.x684.y491.a-60.a2g.png -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/dayton/train/--oXLKfmtepqJ5OHQ84jZg.x1066.y499.a-36.a2g.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/dayton/train/--oXLKfmtepqJ5OHQ84jZg.x1066.y499.a-36.a2g.png -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/dayton/train/--oXLKfmtepqJ5OHQ84jZg.x1281.y482.a10.a2g.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/dayton/train/--oXLKfmtepqJ5OHQ84jZg.x1281.y482.a10.a2g.png -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/dayton/train/--oXLKfmtepqJ5OHQ84jZg.x1428.y496.a41.a2g.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/dayton/train/--oXLKfmtepqJ5OHQ84jZg.x1428.y496.a41.a2g.png -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/dayton/train/--oXLKfmtepqJ5OHQ84jZg.x1631.y494.a85.a2g.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/dayton/train/--oXLKfmtepqJ5OHQ84jZg.x1631.y494.a85.a2g.png -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/dayton/train/--oXLKfmtepqJ5OHQ84jZg.x261.y478.a150.a2g.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/dayton/train/--oXLKfmtepqJ5OHQ84jZg.x261.y478.a150.a2g.png -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/dayton/train/--oXLKfmtepqJ5OHQ84jZg.x317.y421.a162.a2g.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/dayton/train/--oXLKfmtepqJ5OHQ84jZg.x317.y421.a162.a2g.png -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/dayton/train/--oXLKfmtepqJ5OHQ84jZg.x62.y463.a107.a2g.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/dayton/train/--oXLKfmtepqJ5OHQ84jZg.x62.y463.a107.a2g.png -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/dayton/train/--oXLKfmtepqJ5OHQ84jZg.x892.y481.a-74.a2g.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/dayton/train/--oXLKfmtepqJ5OHQ84jZg.x892.y481.a-74.a2g.png -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/dayton/train/-0_Hxmk_U8TWBEu0uD7dug.x1172.y440.a-111.a2g.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/dayton/train/-0_Hxmk_U8TWBEu0uD7dug.x1172.y440.a-111.a2g.png -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/dayton/train/-0_Hxmk_U8TWBEu0uD7dug.x168.y467.a32.a2g.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/dayton/train/-0_Hxmk_U8TWBEu0uD7dug.x168.y467.a32.a2g.png -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/sva/test/000_002540.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/sva/test/000_002540.png -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/sva/test/000_002550.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/sva/test/000_002550.png -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/sva/test/000_002560.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/sva/test/000_002560.png -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/sva/test/000_002570.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/sva/test/000_002570.png -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/sva/test/000_002580.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/sva/test/000_002580.png -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/sva/test/000_002590.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/sva/test/000_002590.png -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/sva/test/000_002600.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/sva/test/000_002600.png -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/sva/test/000_002610.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/sva/test/000_002610.png -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/sva/test/000_002620.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/sva/test/000_002620.png -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/sva/test/000_002630.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/sva/test/000_002630.png -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/sva/train/001_000010.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/sva/train/001_000010.png -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/sva/train/001_000020.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/sva/train/001_000020.png -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/sva/train/001_000030.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/sva/train/001_000030.png -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/sva/train/001_000040.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/sva/train/001_000040.png -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/sva/train/001_000050.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/sva/train/001_000050.png -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/sva/train/001_000060.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/sva/train/001_000060.png -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/sva/train/001_000070.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/sva/train/001_000070.png -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/sva/train/001_000080.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/sva/train/001_000080.png -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/sva/train/001_000090.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/sva/train/001_000090.png -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/sva/train/001_000100.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/sva/train/001_000100.png -------------------------------------------------------------------------------- /cross_view_translation/datasets/samples/sva/train/001_000110.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/datasets/samples/sva/train/001_000110.png -------------------------------------------------------------------------------- /cross_view_translation/environment.yml: -------------------------------------------------------------------------------- 1 | name: pytorch-CycleGAN-and-pix2pix 2 | channels: 3 | - peterjc123 4 | - defaults 5 | dependencies: 6 | - python=3.5.5 7 | - pytorch=0.3.1 8 | - scipy 9 | - pip: 10 | - dominate==2.3.1 11 | - git+https://github.com/pytorch/vision.git 12 | - Pillow==5.0.0 13 | - numpy==1.14.1 14 | - visdom==0.1.7 15 | -------------------------------------------------------------------------------- /cross_view_translation/models/__init__.py: -------------------------------------------------------------------------------- 1 | import importlib 2 | from models.base_model import BaseModel 3 | 4 | 5 | def find_model_using_name(model_name): 6 | # Given the option --model [modelname], 7 | # the file "models/modelname_model.py" 8 | # will be imported. 9 | model_filename = "models." + model_name + "_model" 10 | modellib = importlib.import_module(model_filename) 11 | 12 | # In the file, the class called ModelNameModel() will 13 | # be instantiated. It has to be a subclass of BaseModel, 14 | # and it is case-insensitive. 15 | model = None 16 | target_model_name = model_name.replace('_', '') + 'model' 17 | for name, cls in modellib.__dict__.items(): 18 | if name.lower() == target_model_name.lower() \ 19 | and issubclass(cls, BaseModel): 20 | model = cls 21 | 22 | if model is None: 23 | print("In %s.py, there should be a subclass of BaseModel with class name that matches %s in lowercase." % (model_filename, target_model_name)) 24 | exit(0) 25 | 26 | return model 27 | 28 | 29 | def get_option_setter(model_name): 30 | model_class = find_model_using_name(model_name) 31 | return model_class.modify_commandline_options 32 | 33 | 34 | def create_model(opt): 35 | model = find_model_using_name(opt.model) 36 | instance = model() 37 | instance.initialize(opt) 38 | print("model [%s] was created" % (instance.name())) 39 | return instance 40 | -------------------------------------------------------------------------------- /cross_view_translation/models/__init__.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/models/__init__.pyc -------------------------------------------------------------------------------- /cross_view_translation/models/__pycache__/__init__.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/models/__pycache__/__init__.cpython-36.pyc -------------------------------------------------------------------------------- /cross_view_translation/models/__pycache__/__init__.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/models/__pycache__/__init__.cpython-37.pyc -------------------------------------------------------------------------------- /cross_view_translation/models/__pycache__/base_model.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/models/__pycache__/base_model.cpython-36.pyc -------------------------------------------------------------------------------- /cross_view_translation/models/__pycache__/base_model.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/models/__pycache__/base_model.cpython-37.pyc -------------------------------------------------------------------------------- /cross_view_translation/models/__pycache__/cycle_gan_model.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/models/__pycache__/cycle_gan_model.cpython-36.pyc -------------------------------------------------------------------------------- /cross_view_translation/models/__pycache__/networks.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/models/__pycache__/networks.cpython-36.pyc -------------------------------------------------------------------------------- /cross_view_translation/models/__pycache__/networks.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/models/__pycache__/networks.cpython-37.pyc -------------------------------------------------------------------------------- /cross_view_translation/models/__pycache__/pix2pix_model.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/models/__pycache__/pix2pix_model.cpython-36.pyc -------------------------------------------------------------------------------- /cross_view_translation/models/__pycache__/pix2pix_model.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/models/__pycache__/pix2pix_model.cpython-37.pyc -------------------------------------------------------------------------------- /cross_view_translation/models/__pycache__/test_model.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/models/__pycache__/test_model.cpython-36.pyc -------------------------------------------------------------------------------- /cross_view_translation/models/base_model.py: -------------------------------------------------------------------------------- 1 | import os 2 | import torch 3 | from collections import OrderedDict 4 | from . import networks 5 | 6 | 7 | class BaseModel(): 8 | 9 | # modify parser to add command line options, 10 | # and also change the default values if needed 11 | @staticmethod 12 | def modify_commandline_options(parser, is_train): 13 | return parser 14 | 15 | def name(self): 16 | return 'BaseModel' 17 | 18 | def initialize(self, opt): 19 | self.opt = opt 20 | self.gpu_ids = opt.gpu_ids 21 | self.isTrain = opt.isTrain 22 | self.device = torch.device('cuda:{}'.format(self.gpu_ids[0])) if self.gpu_ids else torch.device('cpu') 23 | self.save_dir = os.path.join(opt.checkpoints_dir, opt.name) 24 | if opt.resize_or_crop != 'scale_width': 25 | torch.backends.cudnn.benchmark = True 26 | self.loss_names = [] 27 | self.model_names = [] 28 | self.visual_names = [] 29 | self.image_paths = [] 30 | 31 | def set_input(self, input): 32 | self.input = input 33 | 34 | def forward(self): 35 | pass 36 | 37 | # load and print networks; create schedulers 38 | def setup(self, opt, parser=None): 39 | if self.isTrain: 40 | self.schedulers = [networks.get_scheduler(optimizer, opt) for optimizer in self.optimizers] 41 | 42 | if not self.isTrain or opt.continue_train: 43 | self.load_networks(opt.which_epoch) 44 | self.print_networks(opt.verbose) 45 | 46 | # make models eval mode during test time 47 | def eval(self): 48 | for name in self.model_names: 49 | if isinstance(name, str): 50 | net = getattr(self, 'net' + name) 51 | net.eval() 52 | 53 | # used in test time, wrapping `forward` in no_grad() so we don't save 54 | # intermediate steps for backprop 55 | def test(self): 56 | with torch.no_grad(): 57 | self.forward() 58 | 59 | # get image paths 60 | def get_image_paths(self): 61 | return self.image_paths 62 | 63 | def optimize_parameters(self): 64 | pass 65 | 66 | # update learning rate (called once every epoch) 67 | def update_learning_rate(self): 68 | for scheduler in self.schedulers: 69 | scheduler.step() 70 | lr = self.optimizers[0].param_groups[0]['lr'] 71 | print('learning rate = %.7f' % lr) 72 | 73 | # return visualization images. train.py will display these images, and save the images to a html 74 | def get_current_visuals(self): 75 | visual_ret = OrderedDict() 76 | for name in self.visual_names: 77 | if isinstance(name, str): 78 | visual_ret[name] = getattr(self, name) 79 | return visual_ret 80 | 81 | # return traning losses/errors. train.py will print out these errors as debugging information 82 | def get_current_losses(self): 83 | errors_ret = OrderedDict() 84 | for name in self.loss_names: 85 | if isinstance(name, str): 86 | # float(...) works for both scalar tensor and float number 87 | errors_ret[name] = float(getattr(self, 'loss_' + name)) 88 | return errors_ret 89 | 90 | # save models to the disk 91 | def save_networks(self, which_epoch): 92 | for name in self.model_names: 93 | if isinstance(name, str): 94 | save_filename = '%s_net_%s.pth' % (which_epoch, name) 95 | save_path = os.path.join(self.save_dir, save_filename) 96 | net = getattr(self, 'net' + name) 97 | 98 | if len(self.gpu_ids) > 0 and torch.cuda.is_available(): 99 | torch.save(net.module.cpu().state_dict(), save_path) 100 | net.cuda(self.gpu_ids[0]) 101 | else: 102 | torch.save(net.cpu().state_dict(), save_path) 103 | 104 | def __patch_instance_norm_state_dict(self, state_dict, module, keys, i=0): 105 | key = keys[i] 106 | if i + 1 == len(keys): # at the end, pointing to a parameter/buffer 107 | if module.__class__.__name__.startswith('InstanceNorm') and \ 108 | (key == 'running_mean' or key == 'running_var'): 109 | if getattr(module, key) is None: 110 | state_dict.pop('.'.join(keys)) 111 | if module.__class__.__name__.startswith('InstanceNorm') and \ 112 | (key == 'num_batches_tracked'): 113 | state_dict.pop('.'.join(keys)) 114 | else: 115 | self.__patch_instance_norm_state_dict(state_dict, getattr(module, key), keys, i + 1) 116 | 117 | # load models from the disk 118 | def load_networks(self, which_epoch): 119 | for name in self.model_names: 120 | if isinstance(name, str): 121 | load_filename = '%s_net_%s.pth' % (which_epoch, name) 122 | load_path = os.path.join(self.save_dir, load_filename) 123 | net = getattr(self, 'net' + name) 124 | if isinstance(net, torch.nn.DataParallel): 125 | net = net.module 126 | print('loading the model from %s' % load_path) 127 | # if you are using PyTorch newer than 0.4 (e.g., built from 128 | # GitHub source), you can remove str() on self.device 129 | state_dict = torch.load(load_path, map_location=str(self.device)) 130 | if hasattr(state_dict, '_metadata'): 131 | del state_dict._metadata 132 | 133 | # patch InstanceNorm checkpoints prior to 0.4 134 | for key in list(state_dict.keys()): # need to copy keys here because we mutate in loop 135 | self.__patch_instance_norm_state_dict(state_dict, net, key.split('.')) 136 | net.load_state_dict(state_dict) 137 | 138 | # print network information 139 | def print_networks(self, verbose): 140 | print('---------- Networks initialized -------------') 141 | for name in self.model_names: 142 | if isinstance(name, str): 143 | net = getattr(self, 'net' + name) 144 | num_params = 0 145 | for param in net.parameters(): 146 | num_params += param.numel() 147 | if verbose: 148 | print(net) 149 | print('[Network %s] Total number of parameters : %.5f M' % (name, num_params / 1e6)) 150 | print('-----------------------------------------------') 151 | 152 | # set requies_grad=Fasle to avoid computation 153 | def set_requires_grad(self, nets, requires_grad=False): 154 | if not isinstance(nets, list): 155 | nets = [nets] 156 | for net in nets: 157 | if net is not None: 158 | for param in net.parameters(): 159 | param.requires_grad = requires_grad 160 | -------------------------------------------------------------------------------- /cross_view_translation/models/test_model.py: -------------------------------------------------------------------------------- 1 | from .base_model import BaseModel 2 | from . import networks 3 | from .cycle_gan_model import CycleGANModel 4 | 5 | 6 | class TestModel(BaseModel): 7 | def name(self): 8 | return 'TestModel' 9 | 10 | @staticmethod 11 | def modify_commandline_options(parser, is_train=True): 12 | assert not is_train, 'TestModel cannot be used in train mode' 13 | parser = CycleGANModel.modify_commandline_options(parser, is_train=False) 14 | parser.set_defaults(dataset_mode='single') 15 | 16 | parser.add_argument('--model_suffix', type=str, default='', 17 | help='In checkpoints_dir, [which_epoch]_net_G[model_suffix].pth will' 18 | ' be loaded as the generator of TestModel') 19 | 20 | return parser 21 | 22 | def initialize(self, opt): 23 | assert(not opt.isTrain) 24 | BaseModel.initialize(self, opt) 25 | 26 | # specify the training losses you want to print out. The program will call base_model.get_current_losses 27 | self.loss_names = [] 28 | # specify the images you want to save/display. The program will call base_model.get_current_visuals 29 | self.visual_names = ['real_A', 'fake_B'] 30 | # specify the models you want to save to the disk. The program will call base_model.save_networks and base_model.load_networks 31 | self.model_names = ['G' + opt.model_suffix] 32 | 33 | self.netG = networks.define_G(opt.input_nc, opt.output_nc, opt.ngf, opt.which_model_netG, 34 | opt.norm, not opt.no_dropout, opt.init_type, opt.init_gain, self.gpu_ids) 35 | 36 | # assigns the model to self.netG_[suffix] so that it can be loaded 37 | # please see BaseModel.load_networks 38 | setattr(self, 'netG' + opt.model_suffix, self.netG) 39 | 40 | def set_input(self, input): 41 | # we need to use single_dataset mode 42 | self.real_A = input['A'].to(self.device) 43 | self.image_paths = input['A_paths'] 44 | 45 | def forward(self): 46 | self.fake_B = self.netG(self.real_A) -------------------------------------------------------------------------------- /cross_view_translation/options/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/options/__init__.py -------------------------------------------------------------------------------- /cross_view_translation/options/__init__.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/options/__init__.pyc -------------------------------------------------------------------------------- /cross_view_translation/options/__pycache__/__init__.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/options/__pycache__/__init__.cpython-36.pyc -------------------------------------------------------------------------------- /cross_view_translation/options/__pycache__/__init__.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/options/__pycache__/__init__.cpython-37.pyc -------------------------------------------------------------------------------- /cross_view_translation/options/__pycache__/base_options.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/options/__pycache__/base_options.cpython-36.pyc -------------------------------------------------------------------------------- /cross_view_translation/options/__pycache__/base_options.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/options/__pycache__/base_options.cpython-37.pyc -------------------------------------------------------------------------------- /cross_view_translation/options/__pycache__/test_options.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/options/__pycache__/test_options.cpython-36.pyc -------------------------------------------------------------------------------- /cross_view_translation/options/__pycache__/train_options.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/options/__pycache__/train_options.cpython-36.pyc -------------------------------------------------------------------------------- /cross_view_translation/options/__pycache__/train_options.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/options/__pycache__/train_options.cpython-37.pyc -------------------------------------------------------------------------------- /cross_view_translation/options/base_options.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | import os 3 | from util import util 4 | import torch 5 | import models 6 | import data 7 | 8 | 9 | class BaseOptions(): 10 | def __init__(self): 11 | self.initialized = False 12 | 13 | def initialize(self, parser): 14 | parser.add_argument('--dataroot', type=str, default='/home/csdept/projects/pytorch-CycleGAN-and-pix2pix_sg2/datasets/dayton', help='path to images (should have subfolders trainA, trainB, valA, valB, etc)') 15 | parser.add_argument('--batchSize', type=int, default=4, help='input batch size') 16 | parser.add_argument('--loadSize', type=int, default=286, help='scale images to this size') 17 | parser.add_argument('--fineSize', type=int, default=256, help='then crop to this size') 18 | parser.add_argument('--input_nc', type=int, default=3, help='# of input image channels') 19 | parser.add_argument('--output_nc', type=int, default=3, help='# of output image channels') 20 | parser.add_argument('--ngf', type=int, default=64, help='# of gen filters in first conv layer') 21 | parser.add_argument('--ndf', type=int, default=64, help='# of discrim filters in first conv layer') 22 | parser.add_argument('--which_model_netD', type=str, default='basic', help='selects model to use for netD') 23 | parser.add_argument('--which_model_netG', type=str, default='resnet_9blocks', help='selects model to use for netG') 24 | parser.add_argument('--n_layers_D', type=int, default=3, help='only used if which_model_netD==n_layers') 25 | parser.add_argument('--gpu_ids', type=str, default='0', help='gpu ids: e.g. 0 0,1,2, 0,2. use -1 for CPU') 26 | parser.add_argument('--name', type=str, default='ego2top_x_sg2_1', help='name of the experiment. It decides where to store samples and models') 27 | parser.add_argument('--dataset_mode', type=str, default='aligned', help='chooses how datasets are loaded. [unaligned | aligned | single]') 28 | parser.add_argument('--model', type=str, default='pix2pix', 29 | help='chooses which model to use. cycle_gan, pix2pix, test') 30 | parser.add_argument('--which_direction', type=str, default='AtoB', help='AtoB or BtoA') 31 | parser.add_argument('--nThreads', default=4, type=int, help='# threads for loading data') 32 | parser.add_argument('--checkpoints_dir', type=str, default='./checkpoints', help='models are saved here') 33 | parser.add_argument('--norm', type=str, default='batch', help='instance normalization or batch normalization') 34 | parser.add_argument('--serial_batches', action='store_true', help='if true, takes images in order to make batches, otherwise takes them randomly') 35 | parser.add_argument('--display_winsize', type=int, default=256, help='display window size') 36 | parser.add_argument('--display_id', type=int, default=1, help='window id of the web display') 37 | parser.add_argument('--display_server', type=str, default="http://localhost", help='visdom server of the web display') 38 | parser.add_argument('--display_env', type=str, default='main', help='visdom display environment name (default is "main")') 39 | parser.add_argument('--display_port', type=int, default=8097, help='visdom port of the web display') 40 | parser.add_argument('--no_dropout', action='store_true', help='no dropout for the generator') 41 | parser.add_argument('--max_dataset_size', type=int, default=float("inf"), help='Maximum number of samples allowed per dataset. If the dataset directory contains more than max_dataset_size, only a subset is loaded.') 42 | parser.add_argument('--resize_or_crop', type=str, default='resize_and_crop', help='scaling and cropping of images at load time [resize_and_crop|crop|scale_width|scale_width_and_crop]') 43 | parser.add_argument('--no_flip', action='store_true', help='if specified, do not flip the images for data augmentation') 44 | parser.add_argument('--init_type', type=str, default='normal', help='network initialization [normal|xavier|kaiming|orthogonal]') 45 | parser.add_argument('--init_gain', type=float, default=0.02, help='scaling factor for normal, xavier and orthogonal.') 46 | parser.add_argument('--verbose', action='store_true', help='if specified, print more debugging information') 47 | parser.add_argument('--suffix', default='', type=str, help='customized suffix: opt.name = opt.name + suffix: e.g., {model}_{which_model_netG}_size{loadSize}') 48 | self.initialized = True 49 | return parser 50 | 51 | def gather_options(self): 52 | # initialize parser with basic options 53 | if not self.initialized: 54 | parser = argparse.ArgumentParser( 55 | formatter_class=argparse.ArgumentDefaultsHelpFormatter) 56 | parser = self.initialize(parser) 57 | 58 | # get the basic options 59 | opt, _ = parser.parse_known_args() 60 | 61 | # modify model-related parser options 62 | model_name = opt.model 63 | model_option_setter = models.get_option_setter(model_name) 64 | parser = model_option_setter(parser, self.isTrain) 65 | opt, _ = parser.parse_known_args() # parse again with the new defaults 66 | 67 | # modify dataset-related parser options 68 | dataset_name = opt.dataset_mode 69 | dataset_option_setter = data.get_option_setter(dataset_name) 70 | parser = dataset_option_setter(parser, self.isTrain) 71 | 72 | self.parser = parser 73 | 74 | return parser.parse_args() 75 | 76 | def print_options(self, opt): 77 | message = '' 78 | message += '----------------- Options ---------------\n' 79 | for k, v in sorted(vars(opt).items()): 80 | comment = '' 81 | default = self.parser.get_default(k) 82 | if v != default: 83 | comment = '\t[default: %s]' % str(default) 84 | message += '{:>25}: {:<30}{}\n'.format(str(k), str(v), comment) 85 | message += '----------------- End -------------------' 86 | print(message) 87 | 88 | # save to the disk 89 | expr_dir = os.path.join(opt.checkpoints_dir, opt.name) 90 | util.mkdirs(expr_dir) 91 | file_name = os.path.join(expr_dir, 'opt.txt') 92 | with open(file_name, 'wt') as opt_file: 93 | opt_file.write(message) 94 | opt_file.write('\n') 95 | 96 | def parse(self): 97 | 98 | opt = self.gather_options() 99 | opt.isTrain = self.isTrain # train or test 100 | 101 | # process opt.suffix 102 | if opt.suffix: 103 | suffix = ('_' + opt.suffix.format(**vars(opt))) if opt.suffix != '' else '' 104 | opt.name = opt.name + suffix 105 | 106 | self.print_options(opt) 107 | 108 | # set gpu ids 109 | str_ids = opt.gpu_ids.split(',') 110 | opt.gpu_ids = [] 111 | for str_id in str_ids: 112 | id = int(str_id) 113 | if id >= 0: 114 | opt.gpu_ids.append(id) 115 | if len(opt.gpu_ids) > 0: 116 | torch.cuda.set_device(opt.gpu_ids[0]) 117 | 118 | self.opt = opt 119 | return self.opt 120 | -------------------------------------------------------------------------------- /cross_view_translation/options/base_options.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/options/base_options.pyc -------------------------------------------------------------------------------- /cross_view_translation/options/test_options.py: -------------------------------------------------------------------------------- 1 | from .base_options import BaseOptions 2 | 3 | 4 | class TestOptions(BaseOptions): 5 | def initialize(self, parser): 6 | parser = BaseOptions.initialize(self, parser) 7 | parser.add_argument('--ntest', type=int, default=float("inf"), help='# of test examples.') 8 | parser.add_argument('--results_dir', type=str, default='./results/', help='saves results here.') 9 | parser.add_argument('--aspect_ratio', type=float, default=1.0, help='aspect ratio of result images') 10 | parser.add_argument('--phase', type=str, default='test', help='train, val, test, etc') 11 | parser.add_argument('--which_epoch', type=str, default='latest', help='which epoch to load? set to latest to use latest cached model') 12 | # Dropout and Batchnorm has different behavioir during training and test. 13 | parser.add_argument('--eval', action='store_true', help='use eval mode during test time.') 14 | parser.add_argument('--how_many', type=int, default=10000000, help='how many test images to run') 15 | 16 | parser.set_defaults(model='pix2pix') 17 | # To avoid cropping, the loadSize should be the same as fineSize 18 | parser.set_defaults(loadSize=parser.get_default('fineSize')) 19 | 20 | self.isTrain = False 21 | return parser 22 | -------------------------------------------------------------------------------- /cross_view_translation/options/test_options.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/options/test_options.pyc -------------------------------------------------------------------------------- /cross_view_translation/options/train_options.py: -------------------------------------------------------------------------------- 1 | from .base_options import BaseOptions 2 | 3 | 4 | class TrainOptions(BaseOptions): 5 | def initialize(self, parser): 6 | parser = BaseOptions.initialize(self, parser) 7 | parser.add_argument('--display_freq', type=int, default=10, help='frequency of showing training results on screen') 8 | parser.add_argument('--display_ncols', type=int, default=5, help='if positive, display all images in a single visdom web panel with certain number of images per row.') 9 | parser.add_argument('--update_html_freq', type=int, default=100, help='frequency of saving training results to html') 10 | parser.add_argument('--print_freq', type=int, default=100, help='frequency of showing training results on console') 11 | parser.add_argument('--save_latest_freq', type=int, default=5000, help='frequency of saving the latest results') 12 | parser.add_argument('--save_epoch_freq', type=int, default=1, help='frequency of saving checkpoints at the end of epochs') 13 | parser.add_argument('--continue_train', action='store_true', help='continue training: load the latest model') 14 | parser.add_argument('--epoch_count', type=int, default=1, help='the starting epoch count, we save the model by , +, ...') 15 | parser.add_argument('--phase', type=str, default='train', help='train, val, test, etc') 16 | parser.add_argument('--which_epoch', type=str, default='latest', help='which epoch to load? set to latest to use latest cached model') 17 | parser.add_argument('--niter', type=int, default=20, help='# of iter at starting learning rate') 18 | parser.add_argument('--niter_decay', type=int, default=15, help='# of iter to linearly decay learning rate to zero') 19 | parser.add_argument('--beta1', type=float, default=0.5, help='momentum term of adam') 20 | parser.add_argument('--lr', type=float, default=0.0002, help='initial learning rate for adam') 21 | parser.add_argument('--no_lsgan', action='store_true', help='do *not* use least square GAN, if false, use vanilla GAN') 22 | parser.add_argument('--pool_size', type=int, default=50, help='the size of image buffer that stores previously generated images') 23 | parser.add_argument('--no_html', action='store_true', help='do not save intermediate training results to [opt.checkpoints_dir]/[opt.name]/web/') 24 | parser.add_argument('--lr_policy', type=str, default='lambda', help='learning rate policy: lambda|step|plateau') 25 | parser.add_argument('--lr_decay_iters', type=int, default=50, help='multiply by a gamma every lr_decay_iters iterations') 26 | 27 | self.isTrain = True 28 | return parser 29 | -------------------------------------------------------------------------------- /cross_view_translation/options/train_options.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/options/train_options.pyc -------------------------------------------------------------------------------- /cross_view_translation/requirements.txt: -------------------------------------------------------------------------------- 1 | torch>=0.4.0 2 | torchvision>=0.2.1 3 | dominate>=2.3.1 4 | visdom>=0.1.8.3 5 | -------------------------------------------------------------------------------- /cross_view_translation/scripts/cityscapes_preprosess.m: -------------------------------------------------------------------------------- 1 | % Label( 'road' , 7 , 0 , 'flat' , 1 , False , False , (128, 64,128) ), 2 | % Label( 'sidewalk' , 8 , 1 , 'flat' , 1 , False , False , (244, 35,232) ), 3 | % 4 | % Label( 'building' , 11 , 2 , 'construction' , 2 , False , False , ( 70, 70, 70) ), 5 | % Label( 'wall' , 12 , 3 , 'construction' , 2 , False , False , (102,102,156) ), 6 | % Label( 'fence' , 13 , 4 , 'construction' , 2 , False , False , (190,153,153) ), 7 | % 8 | % Label( 'pole' , 17 , 5 , 'object' , 3 , False , False , (153,153,153) ), 9 | % Label( 'traffic light' , 19 , 6 , 'object' , 3 , False , False , (250,170, 30) ), 10 | % Label( 'traffic sign' , 20 , 7 , 'object' , 3 , False , False , (220,220, 0) ), 11 | % 12 | % Label( 'vegetation' , 21 , 8 , 'nature' , 4 , False , False , (107,142, 35) ), 13 | % Label( 'terrain' , 22 , 9 , 'nature' , 4 , False , False , (152,251,152) ), 14 | % 15 | % Label( 'sky' , 23 , 10 , 'sky' , 5 , False , False , ( 70,130,180) ), 16 | % 17 | % Label( 'person' , 24 , 11 , 'human' , 6 , True , False , (220, 20, 60) ), 18 | % Label( 'rider' , 25 , 12 , 'human' , 6 , True , False , (255, 0, 0) ), 19 | % 20 | % Label( 'car' , 26 , 13 , 'vehicle' , 7 , True , False , ( 0, 0,142) ), 21 | % Label( 'truck' , 27 , 14 , 'vehicle' , 7 , True , False , ( 0, 0, 70) ), 22 | % Label( 'bus' , 28 , 15 , 'vehicle' , 7 , True , False , ( 0, 60,100) ), 23 | % Label( 'train' , 31 , 16 , 'vehicle' , 7 , True , False , ( 0, 80,100) ), 24 | % Label( 'motorcycle' , 32 , 17 , 'vehicle' , 7 , True , False , ( 0, 0,230) ), 25 | % Label( 'bicycle' , 33 , 18 , 'vehicle' , 7 , True , False , (119, 11, 32) ), 26 | 27 | % class information can be found https://github.com/mcordts/cityscapesScripts/blob/master/cityscapesscripts/helpers/labels.py 28 | 29 | clear all;close all;clc 30 | % image_folder='/home/htang/projects/LocalGlobalGAN/dataset/Cityscapes/leftImg8bit/val'; 31 | % segmeantaion_folder = '/home/htang/projects/LocalGlobalGAN/dataset/Cityscapes/gtFine/val'; 32 | % save_folder='/home/htang/projects/LocalGlobalGAN/dataset/Cityscapes_for_training/val'; 33 | 34 | image_folder='/home/htang/projects/LocalGlobalGAN/dataset/Cityscapes/leftImg8bit/train'; 35 | segmeantaion_folder = '/home/htang/projects/LocalGlobalGAN/dataset/Cityscapes/gtFine/train'; 36 | save_folder='/home/htang/projects/LocalGlobalGAN/dataset/Cityscapes_for_training/train'; 37 | if ~isfolder(save_folder) 38 | mkdir(save_folder) 39 | end 40 | 41 | Image = dir( image_folder ); 42 | 43 | parfor i = 1 : length( Image ) 44 | % for i = 1 : length( Image ) 45 | fprintf('%d / %d \n', i, length(Image)); 46 | if( isequal( Image( i ).name, '.' ) || isequal( Image( i ).name, '..' )) 47 | continue; 48 | end 49 | image_name=Image( i ).name; 50 | image_path=fullfile(image_folder, image_name); 51 | segmeantion_path = fullfile(segmeantaion_folder, strcat(image_name(1:length(image_name)-length('leftImg8bit.png')), 'gtFine_color.png')); 52 | img=imread(image_path); 53 | seg=imread(segmeantion_path); 54 | % imshow(img) 55 | % imshow(seg) 56 | img=imresize(img,[256,512], 'nearest'); 57 | seg=imresize(seg,[256,512], 'nearest'); % color seg 58 | label=seg; 59 | 60 | for k=1:256 61 | for j=1:512 62 | if all(reshape(seg(k,j,:),[1,3])==[128, 64,128]) || all(reshape(seg(k,j,:),[1,3])==[244, 35,232]) 63 | label(k,j,:) = [10,10,10]; 64 | elseif all(reshape(seg(k,j,:),[1,3])==[70, 70, 70]) || all(reshape(seg(k,j,:),[1,3])==[102,102,156]) || all(reshape(seg(k,j,:),[1,3])==[190,153,153]) 65 | label(k,j,:) = [20,20,20]; 66 | elseif all(reshape(seg(k,j,:),[1,3])==[153,153,153]) || all(reshape(seg(k,j,:),[1,3])==[250,170, 30]) || all(reshape(seg(k,j,:),[1,3])==[220,220, 0]) 67 | label(k,j,:) = [30,30,30]; 68 | elseif all(reshape(seg(k,j,:),[1,3])==[107,142, 35]) || all(reshape(seg(k,j,:),[1,3])==[152,251,152]) 69 | label(k,j,:) = [40,40,40]; 70 | elseif all(reshape(seg(k,j,:),[1,3])==[70,130,180]) 71 | label(k,j,:) = [50,50,50]; 72 | elseif all(reshape(seg(k,j,:),[1,3])==[220, 20, 60]) || all(reshape(seg(k,j,:),[1,3])==[255, 0, 0]) 73 | label(k,j,:) = [60,60,60]; 74 | elseif all(reshape(seg(k,j,:),[1,3])==[0, 0,142]) || all(reshape(seg(k,j,:),[1,3])==[ 0, 0, 70]) || all(reshape(seg(k,j,:),[1,3])==[0, 60,100]) || all(reshape(seg(k,j,:),[1,3])==[0, 80,100]) || all(reshape(seg(k,j,:),[1,3])==[0, 0,230]) || all(reshape(seg(k,j,:),[1,3])==[119, 11, 32]) 75 | label(k,j,:) = [70,70,70]; 76 | else 77 | label(k,j,:) = [80,80,80]; 78 | end 79 | 80 | end 81 | end 82 | 83 | im=[seg,img,label]; 84 | % imshow(im) 85 | imwrite(im, fullfile(save_folder, image_name)); 86 | end -------------------------------------------------------------------------------- /cross_view_translation/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 | -------------------------------------------------------------------------------- /cross_view_translation/scripts/data_preprocessing.m: -------------------------------------------------------------------------------- 1 | % class_names={'road', 'sidewalk', 'building', 'wall', 'fence', 'pole', 'trafficlight',... 2 | % 'trafficsign', 'vegetation', 'terrain', 'sky', 'person', 'rider', 'car', ... 3 | % 'truck', 'bus', 'train', 'motorcycle', 'bicycle', 'void'}; 4 | 5 | % class information can be found https://github.com/guosheng/refinenet/blob/3d09d311f447b3592f8f820e52629ddcdd10c539/main/gen_class_info_cityscapes.m 6 | 7 | % 1 ‘Car’0,0,142;, 'truck'0,0,70;, 'bus'0,60,100;, 'train'0,80,100;, 'motorcycle'0,0,230;, 'bicycle'119,11,32; 8 | % 2. 'road'128,64,128;, 'sidewalk'244,35,232;, 'terrain'152,251,152; 9 | % 3. 'pole'153,153,153;, 'trafficlight'250,170,30;, 'trafficsign'220,220,0;, 'person'220,20,60;, ‘rider’255,0,0; 10 | % 4. 'building'70,70,70;, 'wall'102,102,156;, 'fence'190,153,153; 11 | % 5. Sky 70,130,180; 12 | % 6. 'vegetation'107,142,35; 13 | % 7. Void 0,0,0 14 | 15 | clear all;close all;clc 16 | 17 | image_folder='/YOUR_PATH/LocalGlobalGAN/datasets/others/sva/test'; 18 | save_folder='/YOUR_PATH/LocalGlobalGAN/datasets/samples/sva/test'; 19 | 20 | % image_folder='/YOUR_PATH/LocalGlobalGAN/datasets/others/sva/train'; 21 | % save_folder='/YOUR_PATH/LocalGlobalGAN/datasets/samples/sva/train'; 22 | 23 | if ~isfolder(save_folder) 24 | mkdir(save_folder) 25 | end 26 | Image = dir( image_folder ); 27 | 28 | parfor i = 1 : length( Image ) 29 | fprintf('%d / %d \n', i, length(Image)); 30 | if( isequal( Image( i ).name, '.' ) || isequal( Image( i ).name, '..' )) 31 | continue; 32 | end 33 | image_name=Image( i ).name; 34 | if exist(fullfile(save_folder, image_name), 'file') == 0 35 | image_path=fullfile(image_folder, image_name); 36 | img=imread(image_path); 37 | % imshow(img) 38 | image1=img(1:256,1:256,:); 39 | image2=img(1:256,257:512,:); 40 | image3=img(1:256,513:768,:); 41 | image4=img(1:256,769:1024,:); 42 | image5=image3; 43 | image6=image4; 44 | 45 | for k=1:256 46 | for j=1:256 47 | if all(reshape(image4(k,j,:),[1,3])==[0,0,142]) || all(reshape(image4(k,j,:),[1,3])==[0,0,70]) || all(reshape(image4(k,j,:),[1,3])==[0,60,100]) || all(reshape(image4(k,j,:),[1,3])==[0,80,100]) || all(reshape(image4(k,j,:),[1,3])==[0,0,230]) || all(reshape(image4(k,j,:),[1,3])==[119,11,32]) 48 | image6(k,j,:) = [10,10,10]; 49 | elseif all(reshape(image4(k,j,:),[1,3])==[128,64,128]) || all(reshape(image4(k,j,:),[1,3])==[244,35,232]) || all(reshape(image4(k,j,:),[1,3])==[152,251,152]) 50 | image6(k,j,:) = [20,20,20]; 51 | elseif all(reshape(image4(k,j,:),[1,3])==[153,153,153]) || all(reshape(image4(k,j,:),[1,3])==[250,170,30]) || all(reshape(image4(k,j,:),[1,3])==[220,220,0]) || all(reshape(image4(k,j,:),[1,3])==[220,20,60]) || all(reshape(image4(k,j,:),[1,3])==[255,0,0]) 52 | image6(k,j,:) = [30,30,30]; 53 | elseif all(reshape(image4(k,j,:),[1,3])==[70,70,70]) || all(reshape(image4(k,j,:),[1,3])==[102,102,156]) || all(reshape(image4(k,j,:),[1,3])==[190,153,153]) 54 | image6(k,j,:) = [40,40,40]; 55 | elseif all(reshape(image4(k,j,:),[1,3])==[70,130,180]) 56 | image6(k,j,:) = [50,50,50]; 57 | elseif all(reshape(image4(k,j,:),[1,3])==[107,142,35]) 58 | image6(k,j,:) = [60,60,60]; 59 | % elseif all(reshape(image4(k,j,:),[1,3])==[0,0,0]) 60 | % image3(k,j,:) = [70,70,70]; 61 | end 62 | 63 | if all(reshape(image3(k,j,:),[1,3])==[0,0,142]) || all(reshape(image3(k,j,:),[1,3])==[0,0,70]) || all(reshape(image3(k,j,:),[1,3])==[0,60,100]) || all(reshape(image3(k,j,:),[1,3])==[0,80,100]) || all(reshape(image3(k,j,:),[1,3])==[0,0,230]) || all(reshape(image3(k,j,:),[1,3])==[119,11,32]) 64 | image5(k,j,:) = [10,10,10]; 65 | elseif all(reshape(image3(k,j,:),[1,3])==[128,64,128]) || all(reshape(image3(k,j,:),[1,3])==[244,35,232]) || all(reshape(image3(k,j,:),[1,3])==[152,251,152]) 66 | image5(k,j,:) = [20,20,20]; 67 | elseif all(reshape(image3(k,j,:),[1,3])==[153,153,153]) || all(reshape(image3(k,j,:),[1,3])==[250,170,30]) || all(reshape(image3(k,j,:),[1,3])==[220,220,0]) || all(reshape(image3(k,j,:),[1,3])==[220,20,60]) || all(reshape(image3(k,j,:),[1,3])==[255,0,0]) 68 | image5(k,j,:) = [30,30,30]; 69 | elseif all(reshape(image3(k,j,:),[1,3])==[70,70,70]) || all(reshape(image3(k,j,:),[1,3])==[102,102,156]) || all(reshape(image3(k,j,:),[1,3])==[190,153,153]) 70 | image5(k,j,:) = [40,40,40]; 71 | elseif all(reshape(image3(k,j,:),[1,3])==[70,130,180]) 72 | image5(k,j,:) = [50,50,50]; 73 | elseif all(reshape(image3(k,j,:),[1,3])==[107,142,35]) 74 | image5(k,j,:) = [60,60,60]; 75 | % elseif all(reshape(image4(k,j,:),[1,3])==[0,0,0]) 76 | % image3(k,j,:) = [70,70,70]; 77 | end 78 | 79 | end 80 | end 81 | 82 | im=[image1,image2,image3,image4,image5,image6]; 83 | % imshow(im) 84 | imwrite(im, fullfile(save_folder, image_name)); 85 | end 86 | end 87 | -------------------------------------------------------------------------------- /cross_view_translation/scripts/download_lggan_model.sh: -------------------------------------------------------------------------------- 1 | FILE=$1 2 | 3 | echo "Note: available models are dayton_a2g, dayton_g2a, cvusa, dayton_a2g_64, dayton_g2a_64, and sva" 4 | echo "Specified [$FILE]" 5 | 6 | URL=http://disi.unitn.it/~hao.tang/uploads/models/LGGAN/${FILE}_pretrained.tar.gz 7 | TAR_FILE=./checkpoints/${FILE}_pretrained.tar.gz 8 | TARGET_DIR=./checkpoints/${FILE}_pretrained/ 9 | 10 | wget -N $URL -O $TAR_FILE 11 | 12 | mkdir -p $TARGET_DIR 13 | tar -zxvf $TAR_FILE -C ./checkpoints/ 14 | rm $TAR_FILE -------------------------------------------------------------------------------- /cross_view_translation/scripts/install_deps.sh: -------------------------------------------------------------------------------- 1 | set -ex 2 | pip install visdom 3 | pip install dominate 4 | -------------------------------------------------------------------------------- /cross_view_translation/test.py: -------------------------------------------------------------------------------- 1 | import os 2 | from options.test_options import TestOptions 3 | from data import CreateDataLoader 4 | from models import create_model 5 | from util.visualizer import save_images 6 | from util import html 7 | 8 | 9 | if __name__ == '__main__': 10 | opt = TestOptions().parse() 11 | opt.nThreads = 1 # test code only supports nThreads = 1 12 | opt.batchSize = 1 # test code only supports batchSize = 1 13 | opt.serial_batches = True # no shuffle 14 | opt.no_flip = True # no flip 15 | opt.display_id = -1 # no visdom display 16 | data_loader = CreateDataLoader(opt) 17 | dataset = data_loader.load_data() 18 | model = create_model(opt) 19 | model.setup(opt) 20 | # create website 21 | web_dir = os.path.join(opt.results_dir, opt.name, '%s_%s' % (opt.phase, opt.which_epoch)) 22 | webpage = html.HTML(web_dir, 'Experiment = %s, Phase = %s, Epoch = %s' % (opt.name, opt.phase, opt.which_epoch)) 23 | # test 24 | 25 | # Set eval mode. 26 | # This only affects layers like batch norm and drop out. But we do use batch norm in pix2pix. 27 | if opt.eval: 28 | model.eval() 29 | 30 | for i, data in enumerate(dataset): 31 | if i >= opt.how_many: 32 | break 33 | model.set_input(data) 34 | model.test() 35 | visuals = model.get_current_visuals() 36 | img_path = model.get_image_paths() 37 | if i % 5 == 0: 38 | print('processing (%04d)-th image... %s' % (i, img_path)) 39 | save_images(webpage, visuals, img_path, aspect_ratio=opt.aspect_ratio, width=opt.display_winsize) 40 | 41 | webpage.save() -------------------------------------------------------------------------------- /cross_view_translation/test_cvusa.sh: -------------------------------------------------------------------------------- 1 | python test.py --dataroot ./datasets/cvusa_lggan \ 2 | --name cvusa_pretrained \ 3 | --model lggan \ 4 | --which_model_netG resnet_9blocks \ 5 | --which_direction AtoB \ 6 | --dataset_mode aligned \ 7 | --norm instance \ 8 | --gpu_ids 0 \ 9 | --batchSize 4 \ 10 | --loadSize 286 \ 11 | --fineSize 256 \ 12 | --no_flip \ 13 | --eval 14 | -------------------------------------------------------------------------------- /cross_view_translation/test_dayton_a2g_256.sh: -------------------------------------------------------------------------------- 1 | export CUDA_VISIBLE_DEVICES=0; 2 | python test.py --dataroot ./datasets/dayton_lggan \ 3 | --name dayton_a2g_pretrained \ 4 | --model lggan \ 5 | --which_model_netG resnet_9blocks \ 6 | --which_direction AtoB \ 7 | --dataset_mode aligned \ 8 | --norm instance \ 9 | --gpu_ids 0 \ 10 | --batchSize 4 \ 11 | --loadSize 286 \ 12 | --fineSize 256 \ 13 | --no_flip \ 14 | --eval 15 | -------------------------------------------------------------------------------- /cross_view_translation/test_dayton_a2g_64.sh: -------------------------------------------------------------------------------- 1 | export CUDA_VISIBLE_DEVICES=0; 2 | python test.py --dataroot ./datasets/dayton_lggan \ 3 | --name dayton_a2g_64_pretrained \ 4 | --model lggan \ 5 | --which_model_netG resnet_9blocks \ 6 | --which_direction AtoB \ 7 | --dataset_mode aligned \ 8 | --norm instance \ 9 | --gpu_ids 0 \ 10 | --batchSize 16 \ 11 | --loadSize 72 \ 12 | --fineSize 64 \ 13 | --no_flip \ 14 | --eval 15 | -------------------------------------------------------------------------------- /cross_view_translation/test_dayton_g2a_256.sh: -------------------------------------------------------------------------------- 1 | export CUDA_VISIBLE_DEVICES=0; 2 | python test.py --dataroot ./datasets/dayton_g2a_lggan \ 3 | --name dayton_g2a_pretrained \ 4 | --model lggan \ 5 | --which_model_netG resnet_9blocks \ 6 | --which_direction AtoB \ 7 | --dataset_mode aligned \ 8 | --norm instance \ 9 | --gpu_ids 0 \ 10 | --batchSize 4 \ 11 | --loadSize 286 \ 12 | --fineSize 256 \ 13 | --no_flip \ 14 | --eval 15 | -------------------------------------------------------------------------------- /cross_view_translation/test_dayton_g2a_64.sh: -------------------------------------------------------------------------------- 1 | export CUDA_VISIBLE_DEVICES=0; 2 | python test.py --dataroot ./datasets/dayton_g2a_lggan \ 3 | --name dayton_g2a_64_pretrained \ 4 | --model lggan \ 5 | --which_model_netG resnet_9blocks \ 6 | --which_direction AtoB \ 7 | --dataset_mode aligned \ 8 | --norm instance \ 9 | --gpu_ids 0 \ 10 | --batchSize 16 \ 11 | --loadSize 72 \ 12 | --fineSize 64 \ 13 | --no_flip \ 14 | --eval 15 | -------------------------------------------------------------------------------- /cross_view_translation/test_sva.sh: -------------------------------------------------------------------------------- 1 | export CUDA_VISIBLE_DEVICES=0; 2 | python test.py --dataroot ./datasets/sva_lggan \ 3 | --name sva_pretrained \ 4 | --model lggan \ 5 | --which_model_netG resnet_9blocks \ 6 | --which_direction AtoB \ 7 | --dataset_mode aligned \ 8 | --norm instance \ 9 | --gpu_ids 0 \ 10 | --batchSize 4 \ 11 | --loadSize 286 \ 12 | --fineSize 256 \ 13 | --no_flip \ 14 | --eval 15 | -------------------------------------------------------------------------------- /cross_view_translation/train.py: -------------------------------------------------------------------------------- 1 | import time 2 | from options.train_options import TrainOptions 3 | from data import CreateDataLoader 4 | from models import create_model 5 | from util.visualizer import Visualizer 6 | 7 | if __name__ == '__main__': 8 | opt = TrainOptions().parse() 9 | data_loader = CreateDataLoader(opt) 10 | dataset = data_loader.load_data() 11 | dataset_size = len(data_loader) 12 | print('#training images = %d' % dataset_size) 13 | 14 | model = create_model(opt) 15 | model.setup(opt) 16 | visualizer = Visualizer(opt) 17 | total_steps = 0 18 | 19 | for epoch in range(opt.epoch_count, opt.niter + opt.niter_decay + 1): 20 | epoch_start_time = time.time() 21 | iter_data_time = time.time() 22 | epoch_iter = 0 23 | 24 | for i, data in enumerate(dataset): 25 | iter_start_time = time.time() 26 | if total_steps % opt.print_freq == 0: 27 | t_data = iter_start_time - iter_data_time 28 | visualizer.reset() 29 | total_steps += opt.batchSize 30 | epoch_iter += opt.batchSize 31 | model.set_input(data) 32 | model.optimize_parameters() 33 | 34 | if total_steps % opt.display_freq == 0: 35 | save_result = total_steps % opt.update_html_freq == 0 36 | visualizer.display_current_results(model.get_current_visuals(), epoch, save_result) 37 | 38 | if total_steps % opt.print_freq == 0: 39 | losses = model.get_current_losses() 40 | t = (time.time() - iter_start_time) / opt.batchSize 41 | visualizer.print_current_losses(epoch, epoch_iter, losses, t, t_data) 42 | if opt.display_id > 0: 43 | visualizer.plot_current_losses(epoch, float(epoch_iter) / dataset_size, opt, losses) 44 | 45 | if total_steps % opt.save_latest_freq == 0: 46 | print('saving the latest model (epoch %d, total_steps %d)' % 47 | (epoch, total_steps)) 48 | model.save_networks('latest') 49 | 50 | iter_data_time = time.time() 51 | if epoch % opt.save_epoch_freq == 0: 52 | print('saving the model at the end of epoch %d, iters %d' % 53 | (epoch, total_steps)) 54 | model.save_networks('latest') 55 | model.save_networks(epoch) 56 | 57 | print('End of epoch %d / %d \t Time Taken: %d sec' % 58 | (epoch, opt.niter + opt.niter_decay, time.time() - epoch_start_time)) 59 | model.update_learning_rate() 60 | -------------------------------------------------------------------------------- /cross_view_translation/train_cvusa.sh: -------------------------------------------------------------------------------- 1 | export CUDA_VISIBLE_DEVICES=0; 2 | python train.py --dataroot ./datasets/cvusa_lggan \ 3 | --name cvusa_lggan \ 4 | --model lggan \ 5 | --which_model_netG resnet_9blocks \ 6 | --which_direction AtoB \ 7 | --dataset_mode aligned \ 8 | --norm instance \ 9 | --gpu_ids 0 \ 10 | --batchSize 4 \ 11 | --loadSize 286 \ 12 | --fineSize 256 \ 13 | --no_flip \ 14 | --niter 15 \ 15 | --niter_decay 15 \ 16 | --display_id 0 17 | -------------------------------------------------------------------------------- /cross_view_translation/train_dayton_a2g_256.sh: -------------------------------------------------------------------------------- 1 | export CUDA_VISIBLE_DEVICES=0; 2 | python train.py --dataroot ./datasets/dayton_lggan \ 3 | --name dayton_a2g_lggan \ 4 | --model lggan \ 5 | --which_model_netG resnet_9blocks \ 6 | --which_direction AtoB \ 7 | --dataset_mode aligned \ 8 | --norm instance \ 9 | --gpu_ids 0 \ 10 | --batchSize 4 \ 11 | --loadSize 286 \ 12 | --fineSize 256 \ 13 | --no_flip \ 14 | --display_id 0 \ 15 | --niter 20 \ 16 | --niter_decay 15 17 | -------------------------------------------------------------------------------- /cross_view_translation/train_dayton_a2g_64.sh: -------------------------------------------------------------------------------- 1 | export CUDA_VISIBLE_DEVICES=0; 2 | python train.py --dataroot ./datasets/dayton_lggan \ 3 | --name dayton_a2g_64_lggan \ 4 | --model lggan \ 5 | --which_model_netG resnet_9blocks \ 6 | --which_direction AtoB \ 7 | --dataset_mode aligned \ 8 | --norm instance \ 9 | --gpu_ids 0 \ 10 | --batchSize 16 \ 11 | --loadSize 72 \ 12 | --fineSize 64 \ 13 | --no_flip \ 14 | --display_id 0 \ 15 | --niter 50 \ 16 | --niter_decay 50 17 | -------------------------------------------------------------------------------- /cross_view_translation/train_dayton_g2a_256.sh: -------------------------------------------------------------------------------- 1 | export CUDA_VISIBLE_DEVICES=0; 2 | python train.py --dataroot ./datasets/dayton_g2a_lggan \ 3 | --name dayton_g2a_lggan \ 4 | --model lggan \ 5 | --which_model_netG resnet_9blocks \ 6 | --which_direction AtoB \ 7 | --dataset_mode aligned \ 8 | --norm instance \ 9 | --gpu_ids 0 \ 10 | --batchSize 4 \ 11 | --loadSize 286 \ 12 | --fineSize 256 \ 13 | --no_flip \ 14 | --display_id 0 \ 15 | --niter 20 \ 16 | --niter_decay 15 17 | -------------------------------------------------------------------------------- /cross_view_translation/train_dayton_g2a_64.sh: -------------------------------------------------------------------------------- 1 | export CUDA_VISIBLE_DEVICES=0; 2 | python train.py --dataroot ./datasets/dayton_g2a_lggan \ 3 | --name dayton_g2a_64_lggan \ 4 | --model lggan \ 5 | --which_model_netG resnet_9blocks \ 6 | --which_direction AtoB \ 7 | --dataset_mode aligned \ 8 | --norm instance \ 9 | --gpu_ids 0 \ 10 | --batchSize 16 \ 11 | --loadSize 72 \ 12 | --fineSize 64 \ 13 | --no_flip \ 14 | --display_id 0 \ 15 | --niter 50 \ 16 | --niter_decay 50 17 | -------------------------------------------------------------------------------- /cross_view_translation/train_sva.sh: -------------------------------------------------------------------------------- 1 | export CUDA_VISIBLE_DEVICES=0; 2 | python train.py --dataroot ./datasets/sva_lggan \ 3 | --name sva_lggan \ 4 | --model lggan \ 5 | --which_model_netG resnet_9blocks \ 6 | --which_direction AtoB \ 7 | --dataset_mode aligned \ 8 | --norm instance \ 9 | --gpu_ids 0 \ 10 | --batchSize 4 \ 11 | --loadSize 286 \ 12 | --fineSize 256 \ 13 | --no_flip \ 14 | --niter 10 \ 15 | --niter_decay 10 \ 16 | --display_id 0 17 | -------------------------------------------------------------------------------- /cross_view_translation/util/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/util/__init__.py -------------------------------------------------------------------------------- /cross_view_translation/util/__init__.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/util/__init__.pyc -------------------------------------------------------------------------------- /cross_view_translation/util/__pycache__/__init__.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/util/__pycache__/__init__.cpython-36.pyc -------------------------------------------------------------------------------- /cross_view_translation/util/__pycache__/__init__.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/util/__pycache__/__init__.cpython-37.pyc -------------------------------------------------------------------------------- /cross_view_translation/util/__pycache__/html.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/util/__pycache__/html.cpython-36.pyc -------------------------------------------------------------------------------- /cross_view_translation/util/__pycache__/html.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/util/__pycache__/html.cpython-37.pyc -------------------------------------------------------------------------------- /cross_view_translation/util/__pycache__/image_pool.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/util/__pycache__/image_pool.cpython-36.pyc -------------------------------------------------------------------------------- /cross_view_translation/util/__pycache__/image_pool.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/util/__pycache__/image_pool.cpython-37.pyc -------------------------------------------------------------------------------- /cross_view_translation/util/__pycache__/util.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/util/__pycache__/util.cpython-36.pyc -------------------------------------------------------------------------------- /cross_view_translation/util/__pycache__/util.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/util/__pycache__/util.cpython-37.pyc -------------------------------------------------------------------------------- /cross_view_translation/util/__pycache__/visualizer.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/util/__pycache__/visualizer.cpython-36.pyc -------------------------------------------------------------------------------- /cross_view_translation/util/__pycache__/visualizer.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/util/__pycache__/visualizer.cpython-37.pyc -------------------------------------------------------------------------------- /cross_view_translation/util/get_data.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import os 3 | import tarfile 4 | import requests 5 | from warnings import warn 6 | from zipfile import ZipFile 7 | from bs4 import BeautifulSoup 8 | from os.path import abspath, isdir, join, basename 9 | 10 | 11 | class GetData(object): 12 | """ 13 | 14 | Download CycleGAN or Pix2Pix Data. 15 | 16 | Args: 17 | technique : str 18 | One of: 'cyclegan' or 'pix2pix'. 19 | verbose : bool 20 | If True, print additional information. 21 | 22 | Examples: 23 | >>> from util.get_data import GetData 24 | >>> gd = GetData(technique='cyclegan') 25 | >>> new_data_path = gd.get(save_path='./datasets') # options will be displayed. 26 | 27 | """ 28 | 29 | def __init__(self, technique='cyclegan', verbose=True): 30 | url_dict = { 31 | 'pix2pix': 'https://people.eecs.berkeley.edu/~tinghuiz/projects/pix2pix/datasets', 32 | 'cyclegan': 'https://people.eecs.berkeley.edu/~taesung_park/CycleGAN/datasets' 33 | } 34 | self.url = url_dict.get(technique.lower()) 35 | self._verbose = verbose 36 | 37 | def _print(self, text): 38 | if self._verbose: 39 | print(text) 40 | 41 | @staticmethod 42 | def _get_options(r): 43 | soup = BeautifulSoup(r.text, 'lxml') 44 | options = [h.text for h in soup.find_all('a', href=True) 45 | if h.text.endswith(('.zip', 'tar.gz'))] 46 | return options 47 | 48 | def _present_options(self): 49 | r = requests.get(self.url) 50 | options = self._get_options(r) 51 | print('Options:\n') 52 | for i, o in enumerate(options): 53 | print("{0}: {1}".format(i, o)) 54 | choice = input("\nPlease enter the number of the " 55 | "dataset above you wish to download:") 56 | return options[int(choice)] 57 | 58 | def _download_data(self, dataset_url, save_path): 59 | if not isdir(save_path): 60 | os.makedirs(save_path) 61 | 62 | base = basename(dataset_url) 63 | temp_save_path = join(save_path, base) 64 | 65 | with open(temp_save_path, "wb") as f: 66 | r = requests.get(dataset_url) 67 | f.write(r.content) 68 | 69 | if base.endswith('.tar.gz'): 70 | obj = tarfile.open(temp_save_path) 71 | elif base.endswith('.zip'): 72 | obj = ZipFile(temp_save_path, 'r') 73 | else: 74 | raise ValueError("Unknown File Type: {0}.".format(base)) 75 | 76 | self._print("Unpacking Data...") 77 | obj.extractall(save_path) 78 | obj.close() 79 | os.remove(temp_save_path) 80 | 81 | def get(self, save_path, dataset=None): 82 | """ 83 | 84 | Download a dataset. 85 | 86 | Args: 87 | save_path : str 88 | A directory to save the data to. 89 | dataset : str, optional 90 | A specific dataset to download. 91 | Note: this must include the file extension. 92 | If None, options will be presented for you 93 | to choose from. 94 | 95 | Returns: 96 | save_path_full : str 97 | The absolute path to the downloaded data. 98 | 99 | """ 100 | if dataset is None: 101 | selected_dataset = self._present_options() 102 | else: 103 | selected_dataset = dataset 104 | 105 | save_path_full = join(save_path, selected_dataset.split('.')[0]) 106 | 107 | if isdir(save_path_full): 108 | warn("\n'{0}' already exists. Voiding Download.".format( 109 | save_path_full)) 110 | else: 111 | self._print('Downloading Data...') 112 | url = "{0}/{1}".format(self.url, selected_dataset) 113 | self._download_data(url, save_path=save_path) 114 | 115 | return abspath(save_path_full) 116 | -------------------------------------------------------------------------------- /cross_view_translation/util/html.py: -------------------------------------------------------------------------------- 1 | import dominate 2 | from dominate.tags import * 3 | import os 4 | 5 | 6 | class HTML: 7 | def __init__(self, web_dir, title, reflesh=0): 8 | self.title = title 9 | self.web_dir = web_dir 10 | self.img_dir = os.path.join(self.web_dir, 'images') 11 | if not os.path.exists(self.web_dir): 12 | os.makedirs(self.web_dir) 13 | if not os.path.exists(self.img_dir): 14 | os.makedirs(self.img_dir) 15 | # print(self.img_dir) 16 | 17 | self.doc = dominate.document(title=title) 18 | if reflesh > 0: 19 | with self.doc.head: 20 | meta(http_equiv="reflesh", content=str(reflesh)) 21 | 22 | def get_image_dir(self): 23 | return self.img_dir 24 | 25 | def add_header(self, str): 26 | with self.doc: 27 | h3(str) 28 | 29 | def add_table(self, border=1): 30 | self.t = table(border=border, style="table-layout: fixed;") 31 | self.doc.add(self.t) 32 | 33 | def add_images(self, ims, txts, links, width=400): 34 | self.add_table() 35 | with self.t: 36 | with tr(): 37 | for im, txt, link in zip(ims, txts, links): 38 | with td(style="word-wrap: break-word;", halign="center", valign="top"): 39 | with p(): 40 | with a(href=os.path.join('images', link)): 41 | img(style="width:%dpx" % width, src=os.path.join('images', im)) 42 | br() 43 | p(txt) 44 | 45 | def save(self): 46 | html_file = '%s/index.html' % self.web_dir 47 | f = open(html_file, 'wt') 48 | f.write(self.doc.render()) 49 | f.close() 50 | 51 | 52 | if __name__ == '__main__': 53 | html = HTML('web/', 'test_html') 54 | html.add_header('hello world') 55 | 56 | ims = [] 57 | txts = [] 58 | links = [] 59 | for n in range(4): 60 | ims.append('image_%d.png' % n) 61 | txts.append('text_%d' % n) 62 | links.append('image_%d.png' % n) 63 | html.add_images(ims, txts, links) 64 | html.save() 65 | -------------------------------------------------------------------------------- /cross_view_translation/util/html.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/util/html.pyc -------------------------------------------------------------------------------- /cross_view_translation/util/image_pool.py: -------------------------------------------------------------------------------- 1 | import random 2 | import torch 3 | 4 | 5 | class ImagePool(): 6 | def __init__(self, pool_size): 7 | self.pool_size = pool_size 8 | if self.pool_size > 0: 9 | self.num_imgs = 0 10 | self.images = [] 11 | 12 | def query(self, images): 13 | if self.pool_size == 0: 14 | return images 15 | return_images = [] 16 | for image in images: 17 | image = torch.unsqueeze(image.data, 0) 18 | if self.num_imgs < self.pool_size: 19 | self.num_imgs = self.num_imgs + 1 20 | self.images.append(image) 21 | return_images.append(image) 22 | else: 23 | p = random.uniform(0, 1) 24 | if p > 0.5: 25 | random_id = random.randint(0, self.pool_size - 1) # randint is inclusive 26 | tmp = self.images[random_id].clone() 27 | self.images[random_id] = image 28 | return_images.append(tmp) 29 | else: 30 | return_images.append(image) 31 | return_images = torch.cat(return_images, 0) 32 | return return_images 33 | -------------------------------------------------------------------------------- /cross_view_translation/util/image_pool.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/util/image_pool.pyc -------------------------------------------------------------------------------- /cross_view_translation/util/util.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import torch 3 | import numpy as np 4 | from PIL import Image 5 | import os 6 | 7 | 8 | # Converts a Tensor into an image array (numpy) 9 | # |imtype|: the desired type of the converted numpy array 10 | def tensor2im(input_image, imtype=np.uint8): 11 | if isinstance(input_image, torch.Tensor): 12 | image_tensor = input_image.data 13 | else: 14 | return input_image 15 | image_numpy = image_tensor[0].cpu().float().numpy() 16 | if image_numpy.shape[0] == 1: 17 | image_numpy = np.tile(image_numpy, (3, 1, 1)) 18 | image_numpy = (np.transpose(image_numpy, (1, 2, 0)) + 1) / 2.0 * 255.0 19 | return image_numpy.astype(imtype) 20 | 21 | 22 | def diagnose_network(net, name='network'): 23 | mean = 0.0 24 | count = 0 25 | for param in net.parameters(): 26 | if param.grad is not None: 27 | mean += torch.mean(torch.abs(param.grad.data)) 28 | count += 1 29 | if count > 0: 30 | mean = mean / count 31 | print(name) 32 | print(mean) 33 | 34 | 35 | def save_image(image_numpy, image_path): 36 | image_pil = Image.fromarray(image_numpy) 37 | image_pil.save(image_path) 38 | 39 | 40 | def print_numpy(x, val=True, shp=False): 41 | x = x.astype(np.float64) 42 | if shp: 43 | print('shape,', x.shape) 44 | if val: 45 | x = x.flatten() 46 | print('mean = %3.3f, min = %3.3f, max = %3.3f, median = %3.3f, std=%3.3f' % ( 47 | np.mean(x), np.min(x), np.max(x), np.median(x), np.std(x))) 48 | 49 | 50 | def mkdirs(paths): 51 | if isinstance(paths, list) and not isinstance(paths, str): 52 | for path in paths: 53 | mkdir(path) 54 | else: 55 | mkdir(paths) 56 | 57 | 58 | def mkdir(path): 59 | if not os.path.exists(path): 60 | os.makedirs(path) 61 | -------------------------------------------------------------------------------- /cross_view_translation/util/util.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/util/util.pyc -------------------------------------------------------------------------------- /cross_view_translation/util/visualizer.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/cross_view_translation/util/visualizer.pyc -------------------------------------------------------------------------------- /imgs/cross_view_results.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/imgs/cross_view_results.jpg -------------------------------------------------------------------------------- /imgs/feature_map.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/imgs/feature_map.jpg -------------------------------------------------------------------------------- /imgs/framework.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/imgs/framework.jpg -------------------------------------------------------------------------------- /imgs/seg_ade20k.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/imgs/seg_ade20k.jpg -------------------------------------------------------------------------------- /imgs/seg_city.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/imgs/seg_city.jpg -------------------------------------------------------------------------------- /imgs/semantic_results.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/imgs/semantic_results.jpg -------------------------------------------------------------------------------- /semantic_image_synthesis/README.md: -------------------------------------------------------------------------------- 1 | ## Contents 2 | 3 | - [Installation](#Installation) 4 | - [Dataset Preparation](#Dataset-Preparation) 5 | - [Generating Images Using Pretrained Model](#Generating-Images-Using-Pretrained-Model) 6 | - [Train and Test New Models](#Train-and-Test-New-Models) 7 | - [Evaluation](#Evaluation) 8 | 9 | ## Installation 10 | 11 | Clone this repo. 12 | ```bash 13 | git clone https://github.com/Ha0Tang/LGGAN 14 | cd LGGAN/ 15 | ``` 16 | 17 | This code requires PyTorch 1.0 and python 3+. Please install dependencies by 18 | ```bash 19 | pip install -r requirements.txt 20 | ``` 21 | 22 | This code also requires the Synchronized-BatchNorm-PyTorch rep. 23 | ``` 24 | cd semantic_image_synthesis/; 25 | cd models/networks/; 26 | git clone https://github.com/vacancy/Synchronized-BatchNorm-PyTorch; 27 | cp -rf Synchronized-BatchNorm-PyTorch/sync_batchnorm .; 28 | cd ../../; 29 | ``` 30 | 31 | To reproduce the results reported in the paper, you would need an NVIDIA DGX1 machine with 8 V100 GPUs. 32 | 33 | ## Dataset Preparation 34 | Please follow our [DAGAN](https://github.com/Ha0Tang/DAGAN#dataset-preparation) to directly download both Cityscapes and ADE20K datasets. This repository use the same dataset format as [DAGAN](https://github.com/Ha0Tang/DAGAN), [EdgeGAN](https://github.com/Ha0Tang/EdgeGAN) and [SelectionGAN](https://github.com/Ha0Tang/SelectionGAN/tree/master/semantic_synthesis). so you can use the same data for all these methods. 35 | 36 | ## Generating Images Using Pretrained Model 37 | 1. Download the pretrained models using the following script, 38 | ``` 39 | sh scripts/download_lggan_model.sh LGGAN_[dataset] 40 | ``` 41 | where `[dataset]` can be one of `cityscapes`, or `ade`. 42 | 43 | ***If the first download is unsuccessful, please try the second time!!!*** 44 | 45 | 2. Change several parameter and then generate images using `test_[dataset].sh`. If you are running on CPU mode, append `--gpu_ids -1`. 46 | 3. The outputs images are stored at `./results/[type]_pretrained/` by default. You can view them using the autogenerated HTML file in the directory. 47 | 48 | ***Replace three `.py` files in the [ade20k](https://github.com/Ha0Tang/LGGAN/tree/master/semantic_image_synthesis/ade20k) folder to run experiments on ADE20K.*** 49 | 50 | ## Train and Test New Models 51 | 1. Prepare dataset. 52 | 2. Change several parameters and then run `train_[dataset].sh` for training. 53 | There are many options you can specify. To specify the number of GPUs to utilize, use `--gpu_ids`. If you want to use the second and third GPUs for example, use `--gpu_ids 1,2`. 54 | 3. Testing is similar to testing pretrained models. Use `--results_dir` to specify the output directory. `--how_many` will specify the maximum number of images to generate. By default, it loads the latest checkpoint. It can be changed using `--which_epoch`. 55 | 56 | ## Evaluation 57 | Please follow our [DAGAN](https://github.com/Ha0Tang/DAGAN#evaluation) to evalute the trained models. -------------------------------------------------------------------------------- /semantic_image_synthesis/data/__init__.py: -------------------------------------------------------------------------------- 1 | """ 2 | Copyright (C) 2019 NVIDIA Corporation. All rights reserved. 3 | Licensed under the CC BY-NC-SA 4.0 license (https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode). 4 | """ 5 | 6 | import importlib 7 | import torch.utils.data 8 | from data.base_dataset import BaseDataset 9 | 10 | 11 | def find_dataset_using_name(dataset_name): 12 | # Given the option --dataset [datasetname], 13 | # the file "datasets/datasetname_dataset.py" 14 | # will be imported. 15 | dataset_filename = "data." + dataset_name + "_dataset" 16 | datasetlib = importlib.import_module(dataset_filename) 17 | 18 | # In the file, the class called DatasetNameDataset() will 19 | # be instantiated. It has to be a subclass of BaseDataset, 20 | # and it is case-insensitive. 21 | dataset = None 22 | target_dataset_name = dataset_name.replace('_', '') + 'dataset' 23 | for name, cls in datasetlib.__dict__.items(): 24 | if name.lower() == target_dataset_name.lower() \ 25 | and issubclass(cls, BaseDataset): 26 | dataset = cls 27 | 28 | if dataset is None: 29 | raise ValueError("In %s.py, there should be a subclass of BaseDataset " 30 | "with class name that matches %s in lowercase." % 31 | (dataset_filename, target_dataset_name)) 32 | 33 | return dataset 34 | 35 | 36 | def get_option_setter(dataset_name): 37 | dataset_class = find_dataset_using_name(dataset_name) 38 | return dataset_class.modify_commandline_options 39 | 40 | 41 | def create_dataloader(opt): 42 | dataset = find_dataset_using_name(opt.dataset_mode) 43 | instance = dataset() 44 | instance.initialize(opt) 45 | print("dataset [%s] of size %d was created" % 46 | (type(instance).__name__, len(instance))) 47 | dataloader = torch.utils.data.DataLoader( 48 | instance, 49 | batch_size=opt.batchSize, 50 | shuffle=not opt.serial_batches, 51 | num_workers=int(opt.nThreads), 52 | drop_last=opt.isTrain 53 | ) 54 | return dataloader 55 | -------------------------------------------------------------------------------- /semantic_image_synthesis/data/__pycache__/__init__.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/semantic_image_synthesis/data/__pycache__/__init__.cpython-36.pyc -------------------------------------------------------------------------------- /semantic_image_synthesis/data/__pycache__/__init__.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/semantic_image_synthesis/data/__pycache__/__init__.cpython-37.pyc -------------------------------------------------------------------------------- /semantic_image_synthesis/data/__pycache__/ade20k_dataset.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/semantic_image_synthesis/data/__pycache__/ade20k_dataset.cpython-37.pyc -------------------------------------------------------------------------------- /semantic_image_synthesis/data/__pycache__/base_dataset.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/semantic_image_synthesis/data/__pycache__/base_dataset.cpython-36.pyc -------------------------------------------------------------------------------- /semantic_image_synthesis/data/__pycache__/base_dataset.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/semantic_image_synthesis/data/__pycache__/base_dataset.cpython-37.pyc -------------------------------------------------------------------------------- /semantic_image_synthesis/data/__pycache__/cityscapes_dataset.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/semantic_image_synthesis/data/__pycache__/cityscapes_dataset.cpython-36.pyc -------------------------------------------------------------------------------- /semantic_image_synthesis/data/__pycache__/cityscapes_dataset.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/semantic_image_synthesis/data/__pycache__/cityscapes_dataset.cpython-37.pyc -------------------------------------------------------------------------------- /semantic_image_synthesis/data/__pycache__/image_folder.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/semantic_image_synthesis/data/__pycache__/image_folder.cpython-36.pyc -------------------------------------------------------------------------------- /semantic_image_synthesis/data/__pycache__/image_folder.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/semantic_image_synthesis/data/__pycache__/image_folder.cpython-37.pyc -------------------------------------------------------------------------------- /semantic_image_synthesis/data/__pycache__/pix2pix_dataset.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/semantic_image_synthesis/data/__pycache__/pix2pix_dataset.cpython-36.pyc -------------------------------------------------------------------------------- /semantic_image_synthesis/data/__pycache__/pix2pix_dataset.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/semantic_image_synthesis/data/__pycache__/pix2pix_dataset.cpython-37.pyc -------------------------------------------------------------------------------- /semantic_image_synthesis/data/ade20k_dataset.py: -------------------------------------------------------------------------------- 1 | """ 2 | Copyright (C) 2019 NVIDIA Corporation. All rights reserved. 3 | Licensed under the CC BY-NC-SA 4.0 license (https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode). 4 | """ 5 | 6 | from data.pix2pix_dataset import Pix2pixDataset 7 | from data.image_folder import make_dataset 8 | 9 | 10 | class ADE20KDataset(Pix2pixDataset): 11 | 12 | @staticmethod 13 | def modify_commandline_options(parser, is_train): 14 | parser = Pix2pixDataset.modify_commandline_options(parser, is_train) 15 | parser.set_defaults(preprocess_mode='resize_and_crop') 16 | if is_train: 17 | parser.set_defaults(load_size=286) 18 | else: 19 | parser.set_defaults(load_size=256) 20 | parser.set_defaults(crop_size=256) 21 | parser.set_defaults(display_winsize=256) 22 | parser.set_defaults(label_nc=150) 23 | parser.set_defaults(contain_dontcare_label=True) 24 | parser.set_defaults(cache_filelist_read=False) 25 | parser.set_defaults(cache_filelist_write=False) 26 | parser.set_defaults(no_instance=True) 27 | return parser 28 | 29 | def get_paths(self, opt): 30 | root = opt.dataroot 31 | phase = 'val' if opt.phase == 'test' else 'train' 32 | 33 | all_images = make_dataset(root, recursive=True, read_cache=False, write_cache=False) 34 | image_paths = [] 35 | label_paths = [] 36 | for p in all_images: 37 | if '_%s_' % phase not in p: 38 | continue 39 | if p.endswith('.jpg'): 40 | image_paths.append(p) 41 | elif p.endswith('.png'): 42 | label_paths.append(p) 43 | 44 | instance_paths = [] # don't use instance map for ade20k 45 | 46 | return label_paths, image_paths, instance_paths 47 | 48 | # In ADE20k, 'unknown' label is of value 0. 49 | # Change the 'unknown' label to the last label to match other datasets. 50 | def postprocess(self, input_dict): 51 | label = input_dict['label'] 52 | label = label - 1 53 | label[label == -1] = self.opt.label_nc 54 | -------------------------------------------------------------------------------- /semantic_image_synthesis/data/base_dataset.py: -------------------------------------------------------------------------------- 1 | """ 2 | Copyright (C) 2019 NVIDIA Corporation. All rights reserved. 3 | Licensed under the CC BY-NC-SA 4.0 license (https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode). 4 | """ 5 | 6 | import torch.utils.data as data 7 | from PIL import Image 8 | import torchvision.transforms as transforms 9 | import numpy as np 10 | import random 11 | 12 | 13 | class BaseDataset(data.Dataset): 14 | def __init__(self): 15 | super(BaseDataset, self).__init__() 16 | 17 | @staticmethod 18 | def modify_commandline_options(parser, is_train): 19 | return parser 20 | 21 | def initialize(self, opt): 22 | pass 23 | 24 | 25 | def get_params(opt, size): 26 | w, h = size 27 | new_h = h 28 | new_w = w 29 | if opt.preprocess_mode == 'resize_and_crop': 30 | new_h = new_w = opt.load_size 31 | elif opt.preprocess_mode == 'scale_width_and_crop': 32 | new_w = opt.load_size 33 | new_h = opt.load_size * h // w 34 | elif opt.preprocess_mode == 'scale_shortside_and_crop': 35 | ss, ls = min(w, h), max(w, h) # shortside and longside 36 | width_is_shorter = w == ss 37 | ls = int(opt.load_size * ls / ss) 38 | new_w, new_h = (ss, ls) if width_is_shorter else (ls, ss) 39 | 40 | x = random.randint(0, np.maximum(0, new_w - opt.crop_size)) 41 | y = random.randint(0, np.maximum(0, new_h - opt.crop_size)) 42 | 43 | flip = random.random() > 0.5 44 | return {'crop_pos': (x, y), 'flip': flip} 45 | 46 | 47 | def get_transform(opt, params, method=Image.BICUBIC, normalize=True, toTensor=True): 48 | transform_list = [] 49 | if 'resize' in opt.preprocess_mode: 50 | osize = [opt.load_size, opt.load_size] 51 | transform_list.append(transforms.Resize(osize, interpolation=method)) 52 | elif 'scale_width' in opt.preprocess_mode: 53 | transform_list.append(transforms.Lambda(lambda img: __scale_width(img, opt.load_size, method))) 54 | elif 'scale_shortside' in opt.preprocess_mode: 55 | transform_list.append(transforms.Lambda(lambda img: __scale_shortside(img, opt.load_size, method))) 56 | 57 | if 'crop' in opt.preprocess_mode: 58 | transform_list.append(transforms.Lambda(lambda img: __crop(img, params['crop_pos'], opt.crop_size))) 59 | 60 | if opt.preprocess_mode == 'none': 61 | base = 32 62 | transform_list.append(transforms.Lambda(lambda img: __make_power_2(img, base, method))) 63 | 64 | if opt.preprocess_mode == 'fixed': 65 | w = opt.crop_size 66 | h = round(opt.crop_size / opt.aspect_ratio) 67 | transform_list.append(transforms.Lambda(lambda img: __resize(img, w, h, method))) 68 | 69 | if opt.isTrain and not opt.no_flip: 70 | transform_list.append(transforms.Lambda(lambda img: __flip(img, params['flip']))) 71 | 72 | if toTensor: 73 | transform_list += [transforms.ToTensor()] 74 | 75 | if normalize: 76 | transform_list += [transforms.Normalize((0.5, 0.5, 0.5), 77 | (0.5, 0.5, 0.5))] 78 | return transforms.Compose(transform_list) 79 | 80 | 81 | def normalize(): 82 | return transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) 83 | 84 | 85 | def __resize(img, w, h, method=Image.BICUBIC): 86 | return img.resize((w, h), method) 87 | 88 | 89 | def __make_power_2(img, base, method=Image.BICUBIC): 90 | ow, oh = img.size 91 | h = int(round(oh / base) * base) 92 | w = int(round(ow / base) * base) 93 | if (h == oh) and (w == ow): 94 | return img 95 | return img.resize((w, h), method) 96 | 97 | 98 | def __scale_width(img, target_width, method=Image.BICUBIC): 99 | ow, oh = img.size 100 | if (ow == target_width): 101 | return img 102 | w = target_width 103 | h = int(target_width * oh / ow) 104 | return img.resize((w, h), method) 105 | 106 | 107 | def __scale_shortside(img, target_width, method=Image.BICUBIC): 108 | ow, oh = img.size 109 | ss, ls = min(ow, oh), max(ow, oh) # shortside and longside 110 | width_is_shorter = ow == ss 111 | if (ss == target_width): 112 | return img 113 | ls = int(target_width * ls / ss) 114 | nw, nh = (ss, ls) if width_is_shorter else (ls, ss) 115 | return img.resize((nw, nh), method) 116 | 117 | 118 | def __crop(img, pos, size): 119 | ow, oh = img.size 120 | x1, y1 = pos 121 | tw = th = size 122 | return img.crop((x1, y1, x1 + tw, y1 + th)) 123 | 124 | 125 | def __flip(img, flip): 126 | if flip: 127 | return img.transpose(Image.FLIP_LEFT_RIGHT) 128 | return img 129 | -------------------------------------------------------------------------------- /semantic_image_synthesis/data/cityscapes_dataset.py: -------------------------------------------------------------------------------- 1 | """ 2 | Copyright (C) 2019 NVIDIA Corporation. All rights reserved. 3 | Licensed under the CC BY-NC-SA 4.0 license (https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode). 4 | """ 5 | 6 | import os.path 7 | from data.pix2pix_dataset import Pix2pixDataset 8 | from data.image_folder import make_dataset 9 | 10 | 11 | class CityscapesDataset(Pix2pixDataset): 12 | 13 | @staticmethod 14 | def modify_commandline_options(parser, is_train): 15 | parser = Pix2pixDataset.modify_commandline_options(parser, is_train) 16 | parser.set_defaults(preprocess_mode='fixed') 17 | parser.set_defaults(load_size=512) 18 | parser.set_defaults(crop_size=512) 19 | parser.set_defaults(display_winsize=512) 20 | parser.set_defaults(label_nc=35) 21 | parser.set_defaults(aspect_ratio=2.0) 22 | opt, _ = parser.parse_known_args() 23 | if hasattr(opt, 'num_upsampling_layers'): 24 | parser.set_defaults(num_upsampling_layers='more') 25 | return parser 26 | 27 | def get_paths(self, opt): 28 | root = opt.dataroot 29 | phase = 'val' if opt.phase == 'test' else 'train' 30 | 31 | label_dir = os.path.join(root, 'gtFine', phase) 32 | label_paths_all = make_dataset(label_dir, recursive=True) 33 | label_paths = [p for p in label_paths_all if p.endswith('_labelIds.png')] 34 | 35 | image_dir = os.path.join(root, 'leftImg8bit', phase) 36 | image_paths = make_dataset(image_dir, recursive=True) 37 | 38 | if not opt.no_instance: 39 | instance_paths = [p for p in label_paths_all if p.endswith('_instanceIds.png')] 40 | else: 41 | instance_paths = [] 42 | 43 | return label_paths, image_paths, instance_paths 44 | 45 | def paths_match(self, path1, path2): 46 | name1 = os.path.basename(path1) 47 | name2 = os.path.basename(path2) 48 | # compare the first 3 components, [city]_[id1]_[id2] 49 | return '_'.join(name1.split('_')[:3]) == \ 50 | '_'.join(name2.split('_')[:3]) 51 | -------------------------------------------------------------------------------- /semantic_image_synthesis/data/coco_dataset.py: -------------------------------------------------------------------------------- 1 | """ 2 | Copyright (C) 2019 NVIDIA Corporation. All rights reserved. 3 | Licensed under the CC BY-NC-SA 4.0 license (https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode). 4 | """ 5 | 6 | import os.path 7 | from data.pix2pix_dataset import Pix2pixDataset 8 | from data.image_folder import make_dataset 9 | 10 | 11 | class CocoDataset(Pix2pixDataset): 12 | 13 | @staticmethod 14 | def modify_commandline_options(parser, is_train): 15 | parser = Pix2pixDataset.modify_commandline_options(parser, is_train) 16 | parser.add_argument('--coco_no_portraits', action='store_true') 17 | parser.set_defaults(preprocess_mode='resize_and_crop') 18 | if is_train: 19 | parser.set_defaults(load_size=286) 20 | else: 21 | parser.set_defaults(load_size=256) 22 | parser.set_defaults(crop_size=256) 23 | parser.set_defaults(display_winsize=256) 24 | parser.set_defaults(label_nc=182) 25 | parser.set_defaults(contain_dontcare_label=True) 26 | parser.set_defaults(cache_filelist_read=True) 27 | parser.set_defaults(cache_filelist_write=True) 28 | return parser 29 | 30 | def get_paths(self, opt): 31 | root = opt.dataroot 32 | phase = 'val' if opt.phase == 'test' else opt.phase 33 | 34 | label_dir = os.path.join(root, '%s_label' % phase) 35 | label_paths = make_dataset(label_dir, recursive=False, read_cache=True) 36 | 37 | if not opt.coco_no_portraits and opt.isTrain: 38 | label_portrait_dir = os.path.join(root, '%s_label_portrait' % phase) 39 | if os.path.isdir(label_portrait_dir): 40 | label_portrait_paths = make_dataset(label_portrait_dir, recursive=False, read_cache=True) 41 | label_paths += label_portrait_paths 42 | 43 | image_dir = os.path.join(root, '%s_img' % phase) 44 | image_paths = make_dataset(image_dir, recursive=False, read_cache=True) 45 | 46 | if not opt.coco_no_portraits and opt.isTrain: 47 | image_portrait_dir = os.path.join(root, '%s_img_portrait' % phase) 48 | if os.path.isdir(image_portrait_dir): 49 | image_portrait_paths = make_dataset(image_portrait_dir, recursive=False, read_cache=True) 50 | image_paths += image_portrait_paths 51 | 52 | if not opt.no_instance: 53 | instance_dir = os.path.join(root, '%s_inst' % phase) 54 | instance_paths = make_dataset(instance_dir, recursive=False, read_cache=True) 55 | 56 | if not opt.coco_no_portraits and opt.isTrain: 57 | instance_portrait_dir = os.path.join(root, '%s_inst_portrait' % phase) 58 | if os.path.isdir(instance_portrait_dir): 59 | instance_portrait_paths = make_dataset(instance_portrait_dir, recursive=False, read_cache=True) 60 | instance_paths += instance_portrait_paths 61 | 62 | else: 63 | instance_paths = [] 64 | 65 | return label_paths, image_paths, instance_paths 66 | -------------------------------------------------------------------------------- /semantic_image_synthesis/data/custom_dataset.py: -------------------------------------------------------------------------------- 1 | """ 2 | Copyright (C) 2019 NVIDIA Corporation. All rights reserved. 3 | Licensed under the CC BY-NC-SA 4.0 license (https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode). 4 | """ 5 | 6 | from data.pix2pix_dataset import Pix2pixDataset 7 | from data.image_folder import make_dataset 8 | 9 | 10 | class CustomDataset(Pix2pixDataset): 11 | """ Dataset that loads images from directories 12 | Use option --label_dir, --image_dir, --instance_dir to specify the directories. 13 | The images in the directories are sorted in alphabetical order and paired in order. 14 | """ 15 | 16 | @staticmethod 17 | def modify_commandline_options(parser, is_train): 18 | parser = Pix2pixDataset.modify_commandline_options(parser, is_train) 19 | parser.set_defaults(preprocess_mode='resize_and_crop') 20 | load_size = 286 if is_train else 256 21 | parser.set_defaults(load_size=load_size) 22 | parser.set_defaults(crop_size=256) 23 | parser.set_defaults(display_winsize=256) 24 | parser.set_defaults(label_nc=13) 25 | parser.set_defaults(contain_dontcare_label=False) 26 | 27 | parser.add_argument('--label_dir', type=str, required=True, 28 | help='path to the directory that contains label images') 29 | parser.add_argument('--image_dir', type=str, required=True, 30 | help='path to the directory that contains photo images') 31 | parser.add_argument('--instance_dir', type=str, default='', 32 | help='path to the directory that contains instance maps. Leave black if not exists') 33 | return parser 34 | 35 | def get_paths(self, opt): 36 | label_dir = opt.label_dir 37 | label_paths = make_dataset(label_dir, recursive=False, read_cache=True) 38 | 39 | image_dir = opt.image_dir 40 | image_paths = make_dataset(image_dir, recursive=False, read_cache=True) 41 | 42 | if len(opt.instance_dir) > 0: 43 | instance_dir = opt.instance_dir 44 | instance_paths = make_dataset(instance_dir, recursive=False, read_cache=True) 45 | else: 46 | instance_paths = [] 47 | 48 | assert len(label_paths) == len(image_paths), "The #images in %s and %s do not match. Is there something wrong?" 49 | 50 | return label_paths, image_paths, instance_paths 51 | -------------------------------------------------------------------------------- /semantic_image_synthesis/data/facades_dataset.py: -------------------------------------------------------------------------------- 1 | """ 2 | Copyright (C) 2019 NVIDIA Corporation. All rights reserved. 3 | Licensed under the CC BY-NC-SA 4.0 license (https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode). 4 | """ 5 | 6 | import os.path 7 | from data.pix2pix_dataset import Pix2pixDataset 8 | from data.image_folder import make_dataset 9 | 10 | 11 | class FacadesDataset(Pix2pixDataset): 12 | 13 | @staticmethod 14 | def modify_commandline_options(parser, is_train): 15 | parser = Pix2pixDataset.modify_commandline_options(parser, is_train) 16 | parser.set_defaults(dataroot='./dataset/facades/') 17 | parser.set_defaults(preprocess_mode='resize_and_crop') 18 | load_size = 286 if is_train else 256 19 | parser.set_defaults(load_size=load_size) 20 | parser.set_defaults(crop_size=256) 21 | parser.set_defaults(display_winsize=256) 22 | parser.set_defaults(label_nc=13) 23 | parser.set_defaults(contain_dontcare_label=False) 24 | parser.set_defaults(no_instance=True) 25 | return parser 26 | 27 | def get_paths(self, opt): 28 | root = opt.dataroot 29 | phase = 'val' if opt.phase == 'test' else opt.phase 30 | 31 | label_dir = os.path.join(root, '%s_label' % phase) 32 | label_paths = make_dataset(label_dir, recursive=False, read_cache=True) 33 | 34 | image_dir = os.path.join(root, '%s_img' % phase) 35 | image_paths = make_dataset(image_dir, recursive=False, read_cache=True) 36 | 37 | instance_paths = [] 38 | 39 | return label_paths, image_paths, instance_paths 40 | -------------------------------------------------------------------------------- /semantic_image_synthesis/data/image_folder.py: -------------------------------------------------------------------------------- 1 | """ 2 | Copyright (C) 2019 NVIDIA Corporation. All rights reserved. 3 | Licensed under the CC BY-NC-SA 4.0 license (https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode). 4 | """ 5 | 6 | ############################################################################### 7 | # Code from 8 | # https://github.com/pytorch/vision/blob/master/torchvision/datasets/folder.py 9 | # Modified the original code so that it also loads images from the current 10 | # directory as well as the subdirectories 11 | ############################################################################### 12 | import torch.utils.data as data 13 | from PIL import Image 14 | import os 15 | 16 | IMG_EXTENSIONS = [ 17 | '.jpg', '.JPG', '.jpeg', '.JPEG', 18 | '.png', '.PNG', '.ppm', '.PPM', '.bmp', '.BMP', '.tiff', '.webp' 19 | ] 20 | 21 | 22 | def is_image_file(filename): 23 | return any(filename.endswith(extension) for extension in IMG_EXTENSIONS) 24 | 25 | 26 | def make_dataset_rec(dir, images): 27 | assert os.path.isdir(dir), '%s is not a valid directory' % dir 28 | 29 | for root, dnames, fnames in sorted(os.walk(dir, followlinks=True)): 30 | for fname in fnames: 31 | if is_image_file(fname): 32 | path = os.path.join(root, fname) 33 | images.append(path) 34 | 35 | 36 | def make_dataset(dir, recursive=False, read_cache=False, write_cache=False): 37 | images = [] 38 | 39 | if read_cache: 40 | possible_filelist = os.path.join(dir, 'files.list') 41 | if os.path.isfile(possible_filelist): 42 | with open(possible_filelist, 'r') as f: 43 | images = f.read().splitlines() 44 | return images 45 | 46 | if recursive: 47 | make_dataset_rec(dir, images) 48 | else: 49 | assert os.path.isdir(dir) or os.path.islink(dir), '%s is not a valid directory' % dir 50 | 51 | for root, dnames, fnames in sorted(os.walk(dir)): 52 | for fname in fnames: 53 | if is_image_file(fname): 54 | path = os.path.join(root, fname) 55 | images.append(path) 56 | 57 | if write_cache: 58 | filelist_cache = os.path.join(dir, 'files.list') 59 | with open(filelist_cache, 'w') as f: 60 | for path in images: 61 | f.write("%s\n" % path) 62 | print('wrote filelist cache at %s' % filelist_cache) 63 | 64 | return images 65 | 66 | 67 | def default_loader(path): 68 | return Image.open(path).convert('RGB') 69 | 70 | 71 | class ImageFolder(data.Dataset): 72 | 73 | def __init__(self, root, transform=None, return_paths=False, 74 | loader=default_loader): 75 | imgs = make_dataset(root) 76 | if len(imgs) == 0: 77 | raise(RuntimeError("Found 0 images in: " + root + "\n" 78 | "Supported image extensions are: " + 79 | ",".join(IMG_EXTENSIONS))) 80 | 81 | self.root = root 82 | self.imgs = imgs 83 | self.transform = transform 84 | self.return_paths = return_paths 85 | self.loader = loader 86 | 87 | def __getitem__(self, index): 88 | path = self.imgs[index] 89 | img = self.loader(path) 90 | if self.transform is not None: 91 | img = self.transform(img) 92 | if self.return_paths: 93 | return img, path 94 | else: 95 | return img 96 | 97 | def __len__(self): 98 | return len(self.imgs) 99 | -------------------------------------------------------------------------------- /semantic_image_synthesis/data/pix2pix_dataset.py: -------------------------------------------------------------------------------- 1 | """ 2 | Copyright (C) 2019 NVIDIA Corporation. All rights reserved. 3 | Licensed under the CC BY-NC-SA 4.0 license (https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode). 4 | """ 5 | 6 | from data.base_dataset import BaseDataset, get_params, get_transform 7 | from PIL import Image 8 | import util.util as util 9 | import os 10 | 11 | 12 | class Pix2pixDataset(BaseDataset): 13 | @staticmethod 14 | def modify_commandline_options(parser, is_train): 15 | parser.add_argument('--no_pairing_check', action='store_true', 16 | help='If specified, skip sanity check of correct label-image file pairing') 17 | return parser 18 | 19 | def initialize(self, opt): 20 | self.opt = opt 21 | 22 | label_paths, image_paths, instance_paths = self.get_paths(opt) 23 | 24 | util.natural_sort(label_paths) 25 | util.natural_sort(image_paths) 26 | if not opt.no_instance: 27 | util.natural_sort(instance_paths) 28 | 29 | label_paths = label_paths[:opt.max_dataset_size] 30 | image_paths = image_paths[:opt.max_dataset_size] 31 | instance_paths = instance_paths[:opt.max_dataset_size] 32 | 33 | if not opt.no_pairing_check: 34 | for path1, path2 in zip(label_paths, image_paths): 35 | assert self.paths_match(path1, path2), \ 36 | "The label-image pair (%s, %s) do not look like the right pair because the filenames are quite different. Are you sure about the pairing? Please see data/pix2pix_dataset.py to see what is going on, and use --no_pairing_check to bypass this." % (path1, path2) 37 | 38 | self.label_paths = label_paths 39 | self.image_paths = image_paths 40 | self.instance_paths = instance_paths 41 | 42 | size = len(self.label_paths) 43 | self.dataset_size = size 44 | 45 | def get_paths(self, opt): 46 | label_paths = [] 47 | image_paths = [] 48 | instance_paths = [] 49 | assert False, "A subclass of Pix2pixDataset must override self.get_paths(self, opt)" 50 | return label_paths, image_paths, instance_paths 51 | 52 | def paths_match(self, path1, path2): 53 | filename1_without_ext = os.path.splitext(os.path.basename(path1))[0] 54 | filename2_without_ext = os.path.splitext(os.path.basename(path2))[0] 55 | return filename1_without_ext == filename2_without_ext 56 | 57 | def __getitem__(self, index): 58 | # Label Image 59 | label_path = self.label_paths[index] 60 | label = Image.open(label_path) 61 | params = get_params(self.opt, label.size) 62 | transform_label = get_transform(self.opt, params, method=Image.NEAREST, normalize=False) 63 | label_tensor = transform_label(label) * 255.0 64 | label_tensor[label_tensor == 255] = self.opt.label_nc # 'unknown' is opt.label_nc 65 | 66 | # input image (real images) 67 | image_path = self.image_paths[index] 68 | assert self.paths_match(label_path, image_path), \ 69 | "The label_path %s and image_path %s don't match." % \ 70 | (label_path, image_path) 71 | image = Image.open(image_path) 72 | image = image.convert('RGB') 73 | 74 | transform_image = get_transform(self.opt, params) 75 | image_tensor = transform_image(image) 76 | 77 | # if using instance maps 78 | if self.opt.no_instance: 79 | instance_tensor = 0 80 | else: 81 | instance_path = self.instance_paths[index] 82 | instance = Image.open(instance_path) 83 | if instance.mode == 'L': 84 | instance_tensor = transform_label(instance) * 255 85 | instance_tensor = instance_tensor.long() 86 | else: 87 | instance_tensor = transform_label(instance) 88 | 89 | input_dict = {'label': label_tensor, 90 | 'instance': instance_tensor, 91 | 'image': image_tensor, 92 | 'path': image_path, 93 | } 94 | 95 | # Give subclasses a chance to modify the final output 96 | self.postprocess(input_dict) 97 | 98 | return input_dict 99 | 100 | def postprocess(self, input_dict): 101 | return input_dict 102 | 103 | def __len__(self): 104 | return self.dataset_size 105 | -------------------------------------------------------------------------------- /semantic_image_synthesis/models/__init__.py: -------------------------------------------------------------------------------- 1 | """ 2 | Copyright (C) 2019 NVIDIA Corporation. All rights reserved. 3 | Licensed under the CC BY-NC-SA 4.0 license (https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode). 4 | """ 5 | 6 | import importlib 7 | import torch 8 | 9 | 10 | def find_model_using_name(model_name): 11 | # Given the option --model [modelname], 12 | # the file "models/modelname_model.py" 13 | # will be imported. 14 | model_filename = "models." + model_name + "_model" 15 | modellib = importlib.import_module(model_filename) 16 | 17 | # In the file, the class called ModelNameModel() will 18 | # be instantiated. It has to be a subclass of torch.nn.Module, 19 | # and it is case-insensitive. 20 | model = None 21 | target_model_name = model_name.replace('_', '') + 'model' 22 | for name, cls in modellib.__dict__.items(): 23 | if name.lower() == target_model_name.lower() \ 24 | and issubclass(cls, torch.nn.Module): 25 | model = cls 26 | 27 | if model is None: 28 | print("In %s.py, there should be a subclass of torch.nn.Module with class name that matches %s in lowercase." % (model_filename, target_model_name)) 29 | exit(0) 30 | 31 | return model 32 | 33 | 34 | def get_option_setter(model_name): 35 | model_class = find_model_using_name(model_name) 36 | return model_class.modify_commandline_options 37 | 38 | 39 | def create_model(opt): 40 | model = find_model_using_name(opt.model) 41 | instance = model(opt) 42 | print("model [%s] was created" % (type(instance).__name__)) 43 | 44 | return instance 45 | -------------------------------------------------------------------------------- /semantic_image_synthesis/models/__pycache__/__init__.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/semantic_image_synthesis/models/__pycache__/__init__.cpython-36.pyc -------------------------------------------------------------------------------- /semantic_image_synthesis/models/__pycache__/__init__.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/semantic_image_synthesis/models/__pycache__/__init__.cpython-37.pyc -------------------------------------------------------------------------------- /semantic_image_synthesis/models/__pycache__/pix2pix_model.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/semantic_image_synthesis/models/__pycache__/pix2pix_model.cpython-36.pyc -------------------------------------------------------------------------------- /semantic_image_synthesis/models/__pycache__/pix2pix_model.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/semantic_image_synthesis/models/__pycache__/pix2pix_model.cpython-37.pyc -------------------------------------------------------------------------------- /semantic_image_synthesis/models/networks/__init__.py: -------------------------------------------------------------------------------- 1 | """ 2 | Copyright (C) 2019 NVIDIA Corporation. All rights reserved. 3 | Licensed under the CC BY-NC-SA 4.0 license (https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode). 4 | """ 5 | 6 | import torch 7 | from models.networks.base_network import BaseNetwork 8 | from models.networks.loss import * 9 | from models.networks.discriminator import * 10 | from models.networks.generator import * 11 | from models.networks.encoder import * 12 | import util.util as util 13 | 14 | 15 | def find_network_using_name(target_network_name, filename): 16 | target_class_name = target_network_name + filename 17 | module_name = 'models.networks.' + filename 18 | network = util.find_class_in_module(target_class_name, module_name) 19 | 20 | assert issubclass(network, BaseNetwork), \ 21 | "Class %s should be a subclass of BaseNetwork" % network 22 | 23 | return network 24 | 25 | 26 | def modify_commandline_options(parser, is_train): 27 | opt, _ = parser.parse_known_args() 28 | 29 | netG_cls = find_network_using_name(opt.netG, 'generator') 30 | parser = netG_cls.modify_commandline_options(parser, is_train) 31 | if is_train: 32 | netD_cls = find_network_using_name(opt.netD, 'discriminator') 33 | parser = netD_cls.modify_commandline_options(parser, is_train) 34 | netE_cls = find_network_using_name('conv', 'encoder') 35 | parser = netE_cls.modify_commandline_options(parser, is_train) 36 | 37 | return parser 38 | 39 | 40 | def create_network(cls, opt): 41 | net = cls(opt) 42 | net.print_network() 43 | if len(opt.gpu_ids) > 0: 44 | assert(torch.cuda.is_available()) 45 | net.cuda() 46 | net.init_weights(opt.init_type, opt.init_variance) 47 | return net 48 | 49 | 50 | def define_G(opt): 51 | netG_cls = find_network_using_name(opt.netG, 'generator') 52 | return create_network(netG_cls, opt) 53 | 54 | 55 | def define_D(opt): 56 | netD_cls = find_network_using_name(opt.netD, 'discriminator') 57 | return create_network(netD_cls, opt) 58 | 59 | 60 | def define_E(opt): 61 | # there exists only one encoder type 62 | netE_cls = find_network_using_name('conv', 'encoder') 63 | return create_network(netE_cls, opt) 64 | -------------------------------------------------------------------------------- /semantic_image_synthesis/models/networks/__pycache__/__init__.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/semantic_image_synthesis/models/networks/__pycache__/__init__.cpython-36.pyc -------------------------------------------------------------------------------- /semantic_image_synthesis/models/networks/__pycache__/__init__.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/semantic_image_synthesis/models/networks/__pycache__/__init__.cpython-37.pyc -------------------------------------------------------------------------------- /semantic_image_synthesis/models/networks/__pycache__/architecture.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/semantic_image_synthesis/models/networks/__pycache__/architecture.cpython-36.pyc -------------------------------------------------------------------------------- /semantic_image_synthesis/models/networks/__pycache__/architecture.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/semantic_image_synthesis/models/networks/__pycache__/architecture.cpython-37.pyc -------------------------------------------------------------------------------- /semantic_image_synthesis/models/networks/__pycache__/base_network.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/semantic_image_synthesis/models/networks/__pycache__/base_network.cpython-36.pyc -------------------------------------------------------------------------------- /semantic_image_synthesis/models/networks/__pycache__/base_network.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/semantic_image_synthesis/models/networks/__pycache__/base_network.cpython-37.pyc -------------------------------------------------------------------------------- /semantic_image_synthesis/models/networks/__pycache__/discriminator.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/semantic_image_synthesis/models/networks/__pycache__/discriminator.cpython-36.pyc -------------------------------------------------------------------------------- /semantic_image_synthesis/models/networks/__pycache__/discriminator.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/semantic_image_synthesis/models/networks/__pycache__/discriminator.cpython-37.pyc -------------------------------------------------------------------------------- /semantic_image_synthesis/models/networks/__pycache__/encoder.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/semantic_image_synthesis/models/networks/__pycache__/encoder.cpython-36.pyc -------------------------------------------------------------------------------- /semantic_image_synthesis/models/networks/__pycache__/encoder.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/semantic_image_synthesis/models/networks/__pycache__/encoder.cpython-37.pyc -------------------------------------------------------------------------------- /semantic_image_synthesis/models/networks/__pycache__/generator.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/semantic_image_synthesis/models/networks/__pycache__/generator.cpython-36.pyc -------------------------------------------------------------------------------- /semantic_image_synthesis/models/networks/__pycache__/generator.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/semantic_image_synthesis/models/networks/__pycache__/generator.cpython-37.pyc -------------------------------------------------------------------------------- /semantic_image_synthesis/models/networks/__pycache__/loss.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/semantic_image_synthesis/models/networks/__pycache__/loss.cpython-36.pyc -------------------------------------------------------------------------------- /semantic_image_synthesis/models/networks/__pycache__/loss.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/semantic_image_synthesis/models/networks/__pycache__/loss.cpython-37.pyc -------------------------------------------------------------------------------- /semantic_image_synthesis/models/networks/__pycache__/normalization.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/semantic_image_synthesis/models/networks/__pycache__/normalization.cpython-36.pyc -------------------------------------------------------------------------------- /semantic_image_synthesis/models/networks/__pycache__/normalization.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/semantic_image_synthesis/models/networks/__pycache__/normalization.cpython-37.pyc -------------------------------------------------------------------------------- /semantic_image_synthesis/models/networks/architecture.py: -------------------------------------------------------------------------------- 1 | """ 2 | Copyright (C) 2019 NVIDIA Corporation. All rights reserved. 3 | Licensed under the CC BY-NC-SA 4.0 license (https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode). 4 | """ 5 | 6 | import torch 7 | import torch.nn as nn 8 | import torch.nn.functional as F 9 | import torchvision 10 | import torch.nn.utils.spectral_norm as spectral_norm 11 | from models.networks.normalization import SPADE 12 | 13 | 14 | # ResNet block that uses SPADE. 15 | # It differs from the ResNet block of pix2pixHD in that 16 | # it takes in the segmentation map as input, learns the skip connection if necessary, 17 | # and applies normalization first and then convolution. 18 | # This architecture seemed like a standard architecture for unconditional or 19 | # class-conditional GAN architecture using residual block. 20 | # The code was inspired from https://github.com/LMescheder/GAN_stability. 21 | class SPADEResnetBlock(nn.Module): 22 | def __init__(self, fin, fout, opt): 23 | super().__init__() 24 | # Attributes 25 | self.learned_shortcut = (fin != fout) 26 | fmiddle = min(fin, fout) 27 | 28 | # create conv layers 29 | self.conv_0 = nn.Conv2d(fin, fmiddle, kernel_size=3, padding=1) 30 | self.conv_1 = nn.Conv2d(fmiddle, fout, kernel_size=3, padding=1) 31 | if self.learned_shortcut: 32 | self.conv_s = nn.Conv2d(fin, fout, kernel_size=1, bias=False) 33 | 34 | # apply spectral norm if specified 35 | if 'spectral' in opt.norm_G: 36 | self.conv_0 = spectral_norm(self.conv_0) 37 | self.conv_1 = spectral_norm(self.conv_1) 38 | if self.learned_shortcut: 39 | self.conv_s = spectral_norm(self.conv_s) 40 | 41 | # define normalization layers 42 | spade_config_str = opt.norm_G.replace('spectral', '') 43 | self.norm_0 = SPADE(spade_config_str, fin, opt.semantic_nc) 44 | self.norm_1 = SPADE(spade_config_str, fmiddle, opt.semantic_nc) 45 | if self.learned_shortcut: 46 | self.norm_s = SPADE(spade_config_str, fin, opt.semantic_nc) 47 | 48 | # note the resnet block with SPADE also takes in |seg|, 49 | # the semantic segmentation map as input 50 | def forward(self, x, seg): 51 | x_s = self.shortcut(x, seg) 52 | 53 | dx = self.conv_0(self.actvn(self.norm_0(x, seg))) 54 | dx = self.conv_1(self.actvn(self.norm_1(dx, seg))) 55 | 56 | out = x_s + dx 57 | 58 | return out 59 | 60 | def shortcut(self, x, seg): 61 | if self.learned_shortcut: 62 | x_s = self.conv_s(self.norm_s(x, seg)) 63 | else: 64 | x_s = x 65 | return x_s 66 | 67 | def actvn(self, x): 68 | return F.leaky_relu(x, 2e-1) 69 | 70 | 71 | # ResNet block used in pix2pixHD 72 | # We keep the same architecture as pix2pixHD. 73 | class ResnetBlock(nn.Module): 74 | def __init__(self, dim, norm_layer, activation=nn.ReLU(False), kernel_size=3): 75 | super().__init__() 76 | 77 | pw = (kernel_size - 1) // 2 78 | self.conv_block = nn.Sequential( 79 | nn.ReflectionPad2d(pw), 80 | norm_layer(nn.Conv2d(dim, dim, kernel_size=kernel_size)), 81 | activation, 82 | nn.ReflectionPad2d(pw), 83 | norm_layer(nn.Conv2d(dim, dim, kernel_size=kernel_size)) 84 | ) 85 | 86 | def forward(self, x): 87 | y = self.conv_block(x) 88 | out = x + y 89 | return out 90 | 91 | 92 | # VGG architecter, used for the perceptual loss using a pretrained VGG network 93 | class VGG19(torch.nn.Module): 94 | def __init__(self, requires_grad=False): 95 | super().__init__() 96 | vgg_pretrained_features = torchvision.models.vgg19(pretrained=True).features 97 | self.slice1 = torch.nn.Sequential() 98 | self.slice2 = torch.nn.Sequential() 99 | self.slice3 = torch.nn.Sequential() 100 | self.slice4 = torch.nn.Sequential() 101 | self.slice5 = torch.nn.Sequential() 102 | for x in range(2): 103 | self.slice1.add_module(str(x), vgg_pretrained_features[x]) 104 | for x in range(2, 7): 105 | self.slice2.add_module(str(x), vgg_pretrained_features[x]) 106 | for x in range(7, 12): 107 | self.slice3.add_module(str(x), vgg_pretrained_features[x]) 108 | for x in range(12, 21): 109 | self.slice4.add_module(str(x), vgg_pretrained_features[x]) 110 | for x in range(21, 30): 111 | self.slice5.add_module(str(x), vgg_pretrained_features[x]) 112 | if not requires_grad: 113 | for param in self.parameters(): 114 | param.requires_grad = False 115 | 116 | def forward(self, X): 117 | h_relu1 = self.slice1(X) 118 | h_relu2 = self.slice2(h_relu1) 119 | h_relu3 = self.slice3(h_relu2) 120 | h_relu4 = self.slice4(h_relu3) 121 | h_relu5 = self.slice5(h_relu4) 122 | out = [h_relu1, h_relu2, h_relu3, h_relu4, h_relu5] 123 | return out 124 | -------------------------------------------------------------------------------- /semantic_image_synthesis/models/networks/base_network.py: -------------------------------------------------------------------------------- 1 | """ 2 | Copyright (C) 2019 NVIDIA Corporation. All rights reserved. 3 | Licensed under the CC BY-NC-SA 4.0 license (https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode). 4 | """ 5 | 6 | import torch.nn as nn 7 | from torch.nn import init 8 | 9 | 10 | class BaseNetwork(nn.Module): 11 | def __init__(self): 12 | super(BaseNetwork, self).__init__() 13 | 14 | @staticmethod 15 | def modify_commandline_options(parser, is_train): 16 | return parser 17 | 18 | def print_network(self): 19 | if isinstance(self, list): 20 | self = self[0] 21 | num_params = 0 22 | for param in self.parameters(): 23 | num_params += param.numel() 24 | print('Network [%s] was created. Total number of parameters: %.1f million. ' 25 | 'To see the architecture, do print(network).' 26 | % (type(self).__name__, num_params / 1000000)) 27 | 28 | def init_weights(self, init_type='normal', gain=0.02): 29 | def init_func(m): 30 | classname = m.__class__.__name__ 31 | if classname.find('BatchNorm2d') != -1: 32 | if hasattr(m, 'weight') and m.weight is not None: 33 | init.normal_(m.weight.data, 1.0, gain) 34 | if hasattr(m, 'bias') and m.bias is not None: 35 | init.constant_(m.bias.data, 0.0) 36 | elif hasattr(m, 'weight') and (classname.find('Conv') != -1 or classname.find('Linear') != -1): 37 | if init_type == 'normal': 38 | init.normal_(m.weight.data, 0.0, gain) 39 | elif init_type == 'xavier': 40 | init.xavier_normal_(m.weight.data, gain=gain) 41 | elif init_type == 'xavier_uniform': 42 | init.xavier_uniform_(m.weight.data, gain=1.0) 43 | elif init_type == 'kaiming': 44 | init.kaiming_normal_(m.weight.data, a=0, mode='fan_in') 45 | elif init_type == 'orthogonal': 46 | init.orthogonal_(m.weight.data, gain=gain) 47 | elif init_type == 'none': # uses pytorch's default init method 48 | m.reset_parameters() 49 | else: 50 | raise NotImplementedError('initialization method [%s] is not implemented' % init_type) 51 | if hasattr(m, 'bias') and m.bias is not None: 52 | init.constant_(m.bias.data, 0.0) 53 | 54 | self.apply(init_func) 55 | 56 | # propagate to children 57 | for m in self.children(): 58 | if hasattr(m, 'init_weights'): 59 | m.init_weights(init_type, gain) 60 | -------------------------------------------------------------------------------- /semantic_image_synthesis/models/networks/discriminator.py: -------------------------------------------------------------------------------- 1 | """ 2 | Copyright (C) 2019 NVIDIA Corporation. All rights reserved. 3 | Licensed under the CC BY-NC-SA 4.0 license (https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode). 4 | """ 5 | 6 | import torch.nn as nn 7 | import numpy as np 8 | import torch.nn.functional as F 9 | from models.networks.base_network import BaseNetwork 10 | from models.networks.normalization import get_nonspade_norm_layer 11 | import util.util as util 12 | 13 | 14 | class MultiscaleDiscriminator(BaseNetwork): 15 | @staticmethod 16 | def modify_commandline_options(parser, is_train): 17 | parser.add_argument('--netD_subarch', type=str, default='n_layer', 18 | help='architecture of each discriminator') 19 | parser.add_argument('--num_D', type=int, default=2, 20 | help='number of discriminators to be used in multiscale') 21 | opt, _ = parser.parse_known_args() 22 | 23 | # define properties of each discriminator of the multiscale discriminator 24 | subnetD = util.find_class_in_module(opt.netD_subarch + 'discriminator', 25 | 'models.networks.discriminator') 26 | subnetD.modify_commandline_options(parser, is_train) 27 | 28 | return parser 29 | 30 | def __init__(self, opt): 31 | super().__init__() 32 | self.opt = opt 33 | 34 | for i in range(opt.num_D): 35 | subnetD = self.create_single_discriminator(opt) 36 | self.add_module('discriminator_%d' % i, subnetD) 37 | 38 | def create_single_discriminator(self, opt): 39 | subarch = opt.netD_subarch 40 | if subarch == 'n_layer': 41 | netD = NLayerDiscriminator(opt) 42 | else: 43 | raise ValueError('unrecognized discriminator subarchitecture %s' % subarch) 44 | return netD 45 | 46 | def downsample(self, input): 47 | return F.avg_pool2d(input, kernel_size=3, 48 | stride=2, padding=[1, 1], 49 | count_include_pad=False) 50 | 51 | # Returns list of lists of discriminator outputs. 52 | # The final result is of size opt.num_D x opt.n_layers_D 53 | def forward(self, input): 54 | result = [] 55 | get_intermediate_features = not self.opt.no_ganFeat_loss 56 | for name, D in self.named_children(): 57 | out = D(input) 58 | if not get_intermediate_features: 59 | out = [out] 60 | result.append(out) 61 | input = self.downsample(input) 62 | 63 | return result 64 | 65 | 66 | # Defines the PatchGAN discriminator with the specified arguments. 67 | class NLayerDiscriminator(BaseNetwork): 68 | @staticmethod 69 | def modify_commandline_options(parser, is_train): 70 | parser.add_argument('--n_layers_D', type=int, default=4, 71 | help='# layers in each discriminator') 72 | return parser 73 | 74 | def __init__(self, opt): 75 | super().__init__() 76 | self.opt = opt 77 | 78 | kw = 4 79 | padw = int(np.ceil((kw - 1.0) / 2)) 80 | nf = opt.ndf 81 | input_nc = self.compute_D_input_nc(opt) 82 | 83 | norm_layer = get_nonspade_norm_layer(opt, opt.norm_D) 84 | sequence = [[nn.Conv2d(input_nc, nf, kernel_size=kw, stride=2, padding=padw), 85 | nn.LeakyReLU(0.2, False)]] 86 | 87 | for n in range(1, opt.n_layers_D): 88 | nf_prev = nf 89 | nf = min(nf * 2, 512) 90 | stride = 1 if n == opt.n_layers_D - 1 else 2 91 | sequence += [[norm_layer(nn.Conv2d(nf_prev, nf, kernel_size=kw, 92 | stride=stride, padding=padw)), 93 | nn.LeakyReLU(0.2, False) 94 | ]] 95 | 96 | sequence += [[nn.Conv2d(nf, 1, kernel_size=kw, stride=1, padding=padw)]] 97 | 98 | # We divide the layers into groups to extract intermediate layer outputs 99 | for n in range(len(sequence)): 100 | self.add_module('model' + str(n), nn.Sequential(*sequence[n])) 101 | 102 | def compute_D_input_nc(self, opt): 103 | input_nc = opt.label_nc + opt.output_nc 104 | if opt.contain_dontcare_label: 105 | input_nc += 1 106 | if not opt.no_instance: 107 | input_nc += 1 108 | return input_nc 109 | 110 | def forward(self, input): 111 | results = [input] 112 | for submodel in self.children(): 113 | intermediate_output = submodel(results[-1]) 114 | results.append(intermediate_output) 115 | 116 | get_intermediate_features = not self.opt.no_ganFeat_loss 117 | if get_intermediate_features: 118 | return results[1:] 119 | else: 120 | return results[-1] 121 | -------------------------------------------------------------------------------- /semantic_image_synthesis/models/networks/encoder.py: -------------------------------------------------------------------------------- 1 | """ 2 | Copyright (C) 2019 NVIDIA Corporation. All rights reserved. 3 | Licensed under the CC BY-NC-SA 4.0 license (https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode). 4 | """ 5 | 6 | import torch.nn as nn 7 | import numpy as np 8 | import torch.nn.functional as F 9 | from models.networks.base_network import BaseNetwork 10 | from models.networks.normalization import get_nonspade_norm_layer 11 | 12 | 13 | class ConvEncoder(BaseNetwork): 14 | """ Same architecture as the image discriminator """ 15 | 16 | def __init__(self, opt): 17 | super().__init__() 18 | 19 | kw = 3 20 | pw = int(np.ceil((kw - 1.0) / 2)) 21 | ndf = opt.ngf 22 | norm_layer = get_nonspade_norm_layer(opt, opt.norm_E) 23 | self.layer1 = norm_layer(nn.Conv2d(3, ndf, kw, stride=2, padding=pw)) 24 | self.layer2 = norm_layer(nn.Conv2d(ndf * 1, ndf * 2, kw, stride=2, padding=pw)) 25 | self.layer3 = norm_layer(nn.Conv2d(ndf * 2, ndf * 4, kw, stride=2, padding=pw)) 26 | self.layer4 = norm_layer(nn.Conv2d(ndf * 4, ndf * 8, kw, stride=2, padding=pw)) 27 | self.layer5 = norm_layer(nn.Conv2d(ndf * 8, ndf * 8, kw, stride=2, padding=pw)) 28 | if opt.crop_size >= 256: 29 | self.layer6 = norm_layer(nn.Conv2d(ndf * 8, ndf * 8, kw, stride=2, padding=pw)) 30 | 31 | self.so = s0 = 4 32 | self.fc_mu = nn.Linear(ndf * 8 * s0 * s0, 256) 33 | self.fc_var = nn.Linear(ndf * 8 * s0 * s0, 256) 34 | 35 | self.actvn = nn.LeakyReLU(0.2, False) 36 | self.opt = opt 37 | 38 | def forward(self, x): 39 | if x.size(2) != 256 or x.size(3) != 256: 40 | x = F.interpolate(x, size=(256, 256), mode='bilinear') 41 | 42 | x = self.layer1(x) 43 | x = self.layer2(self.actvn(x)) 44 | x = self.layer3(self.actvn(x)) 45 | x = self.layer4(self.actvn(x)) 46 | x = self.layer5(self.actvn(x)) 47 | if self.opt.crop_size >= 256: 48 | x = self.layer6(self.actvn(x)) 49 | x = self.actvn(x) 50 | 51 | x = x.view(x.size(0), -1) 52 | mu = self.fc_mu(x) 53 | logvar = self.fc_var(x) 54 | 55 | return mu, logvar 56 | -------------------------------------------------------------------------------- /semantic_image_synthesis/models/networks/loss.py: -------------------------------------------------------------------------------- 1 | """ 2 | Copyright (C) 2019 NVIDIA Corporation. All rights reserved. 3 | Licensed under the CC BY-NC-SA 4.0 license (https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode). 4 | """ 5 | 6 | import torch 7 | import torch.nn as nn 8 | import torch.nn.functional as F 9 | from models.networks.architecture import VGG19 10 | 11 | 12 | # Defines the GAN loss which uses either LSGAN or the regular GAN. 13 | # When LSGAN is used, it is basically same as MSELoss, 14 | # but it abstracts away the need to create the target label tensor 15 | # that has the same size as the input 16 | class GANLoss(nn.Module): 17 | def __init__(self, gan_mode, target_real_label=1.0, target_fake_label=0.0, 18 | tensor=torch.FloatTensor, opt=None): 19 | super(GANLoss, self).__init__() 20 | self.real_label = target_real_label 21 | self.fake_label = target_fake_label 22 | self.real_label_tensor = None 23 | self.fake_label_tensor = None 24 | self.zero_tensor = None 25 | self.Tensor = tensor 26 | self.gan_mode = gan_mode 27 | self.opt = opt 28 | if gan_mode == 'ls': 29 | pass 30 | elif gan_mode == 'original': 31 | pass 32 | elif gan_mode == 'w': 33 | pass 34 | elif gan_mode == 'hinge': 35 | pass 36 | else: 37 | raise ValueError('Unexpected gan_mode {}'.format(gan_mode)) 38 | 39 | def get_target_tensor(self, input, target_is_real): 40 | if target_is_real: 41 | if self.real_label_tensor is None: 42 | self.real_label_tensor = self.Tensor(1).fill_(self.real_label) 43 | self.real_label_tensor.requires_grad_(False) 44 | return self.real_label_tensor.expand_as(input) 45 | else: 46 | if self.fake_label_tensor is None: 47 | self.fake_label_tensor = self.Tensor(1).fill_(self.fake_label) 48 | self.fake_label_tensor.requires_grad_(False) 49 | return self.fake_label_tensor.expand_as(input) 50 | 51 | def get_zero_tensor(self, input): 52 | if self.zero_tensor is None: 53 | self.zero_tensor = self.Tensor(1).fill_(0) 54 | self.zero_tensor.requires_grad_(False) 55 | return self.zero_tensor.expand_as(input) 56 | 57 | def loss(self, input, target_is_real, for_discriminator=True): 58 | if self.gan_mode == 'original': # cross entropy loss 59 | target_tensor = self.get_target_tensor(input, target_is_real) 60 | loss = F.binary_cross_entropy_with_logits(input, target_tensor) 61 | return loss 62 | elif self.gan_mode == 'ls': 63 | target_tensor = self.get_target_tensor(input, target_is_real) 64 | return F.mse_loss(input, target_tensor) 65 | elif self.gan_mode == 'hinge': 66 | if for_discriminator: 67 | if target_is_real: 68 | minval = torch.min(input - 1, self.get_zero_tensor(input)) 69 | loss = -torch.mean(minval) 70 | else: 71 | minval = torch.min(-input - 1, self.get_zero_tensor(input)) 72 | loss = -torch.mean(minval) 73 | else: 74 | assert target_is_real, "The generator's hinge loss must be aiming for real" 75 | loss = -torch.mean(input) 76 | return loss 77 | else: 78 | # wgan 79 | if target_is_real: 80 | return -input.mean() 81 | else: 82 | return input.mean() 83 | 84 | def __call__(self, input, target_is_real, for_discriminator=True): 85 | # computing loss is a bit complicated because |input| may not be 86 | # a tensor, but list of tensors in case of multiscale discriminator 87 | if isinstance(input, list): 88 | loss = 0 89 | for pred_i in input: 90 | if isinstance(pred_i, list): 91 | pred_i = pred_i[-1] 92 | loss_tensor = self.loss(pred_i, target_is_real, for_discriminator) 93 | bs = 1 if len(loss_tensor.size()) == 0 else loss_tensor.size(0) 94 | new_loss = torch.mean(loss_tensor.view(bs, -1), dim=1) 95 | loss += new_loss 96 | return loss / len(input) 97 | else: 98 | return self.loss(input, target_is_real, for_discriminator) 99 | 100 | 101 | # Perceptual loss that uses a pretrained VGG network 102 | class VGGLoss(nn.Module): 103 | def __init__(self, gpu_ids): 104 | super(VGGLoss, self).__init__() 105 | self.vgg = VGG19().cuda() 106 | self.criterion = nn.L1Loss() 107 | self.weights = [1.0 / 32, 1.0 / 16, 1.0 / 8, 1.0 / 4, 1.0] 108 | 109 | def forward(self, x, y): 110 | x_vgg, y_vgg = self.vgg(x), self.vgg(y) 111 | loss = 0 112 | for i in range(len(x_vgg)): 113 | loss += self.weights[i] * self.criterion(x_vgg[i], y_vgg[i].detach()) 114 | return loss 115 | 116 | 117 | # KL Divergence loss used in VAE with an image encoder 118 | class KLDLoss(nn.Module): 119 | def forward(self, mu, logvar): 120 | return -0.5 * torch.sum(1 + logvar - mu.pow(2) - logvar.exp()) 121 | -------------------------------------------------------------------------------- /semantic_image_synthesis/models/networks/normalization.py: -------------------------------------------------------------------------------- 1 | """ 2 | Copyright (C) 2019 NVIDIA Corporation. All rights reserved. 3 | Licensed under the CC BY-NC-SA 4.0 license (https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode). 4 | """ 5 | 6 | import re 7 | import torch 8 | import torch.nn as nn 9 | import torch.nn.functional as F 10 | from models.networks.sync_batchnorm import SynchronizedBatchNorm2d 11 | import torch.nn.utils.spectral_norm as spectral_norm 12 | 13 | 14 | # Returns a function that creates a normalization function 15 | # that does not condition on semantic map 16 | def get_nonspade_norm_layer(opt, norm_type='instance'): 17 | # helper function to get # output channels of the previous layer 18 | def get_out_channel(layer): 19 | if hasattr(layer, 'out_channels'): 20 | return getattr(layer, 'out_channels') 21 | return layer.weight.size(0) 22 | 23 | # this function will be returned 24 | def add_norm_layer(layer): 25 | nonlocal norm_type 26 | if norm_type.startswith('spectral'): 27 | layer = spectral_norm(layer) 28 | subnorm_type = norm_type[len('spectral'):] 29 | 30 | if subnorm_type == 'none' or len(subnorm_type) == 0: 31 | return layer 32 | 33 | # remove bias in the previous layer, which is meaningless 34 | # since it has no effect after normalization 35 | if getattr(layer, 'bias', None) is not None: 36 | delattr(layer, 'bias') 37 | layer.register_parameter('bias', None) 38 | 39 | if subnorm_type == 'batch': 40 | norm_layer = nn.BatchNorm2d(get_out_channel(layer), affine=True) 41 | elif subnorm_type == 'sync_batch': 42 | norm_layer = SynchronizedBatchNorm2d(get_out_channel(layer), affine=True) 43 | elif subnorm_type == 'instance': 44 | norm_layer = nn.InstanceNorm2d(get_out_channel(layer), affine=False) 45 | else: 46 | raise ValueError('normalization layer %s is not recognized' % subnorm_type) 47 | 48 | return nn.Sequential(layer, norm_layer) 49 | 50 | return add_norm_layer 51 | 52 | 53 | # Creates SPADE normalization layer based on the given configuration 54 | # SPADE consists of two steps. First, it normalizes the activations using 55 | # your favorite normalization method, such as Batch Norm or Instance Norm. 56 | # Second, it applies scale and bias to the normalized output, conditioned on 57 | # the segmentation map. 58 | # The format of |config_text| is spade(norm)(ks), where 59 | # (norm) specifies the type of parameter-free normalization. 60 | # (e.g. syncbatch, batch, instance) 61 | # (ks) specifies the size of kernel in the SPADE module (e.g. 3x3) 62 | # Example |config_text| will be spadesyncbatch3x3, or spadeinstance5x5. 63 | # Also, the other arguments are 64 | # |norm_nc|: the #channels of the normalized activations, hence the output dim of SPADE 65 | # |label_nc|: the #channels of the input semantic map, hence the input dim of SPADE 66 | class SPADE(nn.Module): 67 | def __init__(self, config_text, norm_nc, label_nc): 68 | super().__init__() 69 | 70 | assert config_text.startswith('spade') 71 | parsed = re.search('spade(\D+)(\d)x\d', config_text) 72 | param_free_norm_type = str(parsed.group(1)) 73 | ks = int(parsed.group(2)) 74 | 75 | if param_free_norm_type == 'instance': 76 | self.param_free_norm = nn.InstanceNorm2d(norm_nc, affine=False) 77 | elif param_free_norm_type == 'syncbatch': 78 | self.param_free_norm = SynchronizedBatchNorm2d(norm_nc, affine=False) 79 | elif param_free_norm_type == 'batch': 80 | self.param_free_norm = nn.BatchNorm2d(norm_nc, affine=False) 81 | else: 82 | raise ValueError('%s is not a recognized param-free norm type in SPADE' 83 | % param_free_norm_type) 84 | 85 | # The dimension of the intermediate embedding space. Yes, hardcoded. 86 | nhidden = 128 87 | 88 | pw = ks // 2 89 | self.mlp_shared = nn.Sequential( 90 | nn.Conv2d(label_nc, nhidden, kernel_size=ks, padding=pw), 91 | nn.ReLU() 92 | ) 93 | self.mlp_gamma = nn.Conv2d(nhidden, norm_nc, kernel_size=ks, padding=pw) 94 | self.mlp_beta = nn.Conv2d(nhidden, norm_nc, kernel_size=ks, padding=pw) 95 | 96 | def forward(self, x, segmap): 97 | 98 | # Part 1. generate parameter-free normalized activations 99 | normalized = self.param_free_norm(x) 100 | 101 | # Part 2. produce scaling and bias conditioned on semantic map 102 | segmap = F.interpolate(segmap, size=x.size()[2:], mode='nearest') 103 | actv = self.mlp_shared(segmap) 104 | gamma = self.mlp_gamma(actv) 105 | beta = self.mlp_beta(actv) 106 | 107 | # apply scale and bias 108 | out = normalized * (1 + gamma) + beta 109 | 110 | return out 111 | -------------------------------------------------------------------------------- /semantic_image_synthesis/options/__init__.py: -------------------------------------------------------------------------------- 1 | """ 2 | Copyright (C) 2019 NVIDIA Corporation. All rights reserved. 3 | Licensed under the CC BY-NC-SA 4.0 license (https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode). 4 | """ -------------------------------------------------------------------------------- /semantic_image_synthesis/options/__pycache__/__init__.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/semantic_image_synthesis/options/__pycache__/__init__.cpython-36.pyc -------------------------------------------------------------------------------- /semantic_image_synthesis/options/__pycache__/__init__.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/semantic_image_synthesis/options/__pycache__/__init__.cpython-37.pyc -------------------------------------------------------------------------------- /semantic_image_synthesis/options/__pycache__/base_options.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/semantic_image_synthesis/options/__pycache__/base_options.cpython-36.pyc -------------------------------------------------------------------------------- /semantic_image_synthesis/options/__pycache__/base_options.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/semantic_image_synthesis/options/__pycache__/base_options.cpython-37.pyc -------------------------------------------------------------------------------- /semantic_image_synthesis/options/__pycache__/test_options.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/semantic_image_synthesis/options/__pycache__/test_options.cpython-36.pyc -------------------------------------------------------------------------------- /semantic_image_synthesis/options/__pycache__/test_options.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/semantic_image_synthesis/options/__pycache__/test_options.cpython-37.pyc -------------------------------------------------------------------------------- /semantic_image_synthesis/options/__pycache__/train_options.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/semantic_image_synthesis/options/__pycache__/train_options.cpython-36.pyc -------------------------------------------------------------------------------- /semantic_image_synthesis/options/__pycache__/train_options.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/semantic_image_synthesis/options/__pycache__/train_options.cpython-37.pyc -------------------------------------------------------------------------------- /semantic_image_synthesis/options/test_options.py: -------------------------------------------------------------------------------- 1 | """ 2 | Copyright (C) 2019 NVIDIA Corporation. All rights reserved. 3 | Licensed under the CC BY-NC-SA 4.0 license (https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode). 4 | """ 5 | 6 | from .base_options import BaseOptions 7 | 8 | 9 | class TestOptions(BaseOptions): 10 | def initialize(self, parser): 11 | BaseOptions.initialize(self, parser) 12 | parser.add_argument('--results_dir', type=str, default='./results/', help='saves results here.') 13 | parser.add_argument('--which_epoch', type=str, default='latest', help='which epoch to load? set to latest to use latest cached model') 14 | parser.add_argument('--how_many', type=int, default=float("inf"), help='how many test images to run') 15 | 16 | parser.set_defaults(preprocess_mode='scale_width_and_crop', crop_size=256, load_size=256, display_winsize=256) 17 | parser.set_defaults(serial_batches=True) 18 | parser.set_defaults(no_flip=True) 19 | parser.set_defaults(phase='test') 20 | self.isTrain = False 21 | return parser 22 | -------------------------------------------------------------------------------- /semantic_image_synthesis/options/train_options.py: -------------------------------------------------------------------------------- 1 | """ 2 | Copyright (C) 2019 NVIDIA Corporation. All rights reserved. 3 | Licensed under the CC BY-NC-SA 4.0 license (https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode). 4 | """ 5 | 6 | from .base_options import BaseOptions 7 | 8 | 9 | class TrainOptions(BaseOptions): 10 | def initialize(self, parser): 11 | BaseOptions.initialize(self, parser) 12 | # for displays 13 | parser.add_argument('--display_freq', type=int, default=100, help='frequency of showing training results on screen') 14 | parser.add_argument('--print_freq', type=int, default=100, help='frequency of showing training results on console') 15 | parser.add_argument('--save_latest_freq', type=int, default=5000, help='frequency of saving the latest results') 16 | parser.add_argument('--save_epoch_freq', type=int, default=10, help='frequency of saving checkpoints at the end of epochs') 17 | parser.add_argument('--no_html', action='store_true', help='do not save intermediate training results to [opt.checkpoints_dir]/[opt.name]/web/') 18 | parser.add_argument('--debug', action='store_true', help='only do one epoch and displays at each iteration') 19 | parser.add_argument('--tf_log', action='store_true', help='if specified, use tensorboard logging. Requires tensorflow installed') 20 | 21 | # for training 22 | parser.add_argument('--continue_train', action='store_true', help='continue training: load the latest model') 23 | parser.add_argument('--which_epoch', type=str, default='latest', help='which epoch to load? set to latest to use latest cached model') 24 | parser.add_argument('--niter', type=int, default=50, help='# of iter at starting learning rate. This is NOT the total #epochs. Totla #epochs is niter + niter_decay') 25 | parser.add_argument('--niter_decay', type=int, default=0, help='# of iter to linearly decay learning rate to zero') 26 | parser.add_argument('--optimizer', type=str, default='adam') 27 | parser.add_argument('--beta1', type=float, default=0.5, help='momentum term of adam') 28 | parser.add_argument('--beta2', type=float, default=0.999, help='momentum term of adam') 29 | parser.add_argument('--lr', type=float, default=0.0002, help='initial learning rate for adam') 30 | parser.add_argument('--D_steps_per_G', type=int, default=1, help='number of discriminator iterations per generator iterations.') 31 | 32 | # for discriminators 33 | parser.add_argument('--ndf', type=int, default=64, help='# of discrim filters in first conv layer') 34 | parser.add_argument('--lambda_feat', type=float, default=10.0, help='weight for feature matching loss') 35 | parser.add_argument('--lambda_vgg', type=float, default=10.0, help='weight for vgg loss') 36 | parser.add_argument('--lambda_l1', type=float, default=1.0, help='weight for pixel loss') 37 | parser.add_argument('--lambda_class', type=float, default=1.0, help='weight for classification loss') 38 | parser.add_argument('--no_l1_loss', action='store_true', help='if specified, do *not* use pixel loss') 39 | parser.add_argument('--no_l1_local_loss', action='store_true', help='if specified, do *not* use local pixel loss') 40 | parser.add_argument('--no_class_loss', action='store_true', help='if specified, do *not* use classification loss') 41 | parser.add_argument('--no_ganFeat_loss', action='store_true', help='if specified, do *not* use discriminator feature matching loss') 42 | parser.add_argument('--no_vgg_loss', action='store_true', help='if specified, do *not* use VGG feature matching loss') 43 | parser.add_argument('--gan_mode', type=str, default='hinge', help='(ls|original|hinge)') 44 | parser.add_argument('--netD', type=str, default='multiscale', help='(n_layers|multiscale|image)') 45 | parser.add_argument('--no_TTUR', action='store_true', help='Use TTUR training scheme') 46 | parser.add_argument('--lambda_kld', type=float, default=0.05) 47 | self.isTrain = True 48 | return parser 49 | -------------------------------------------------------------------------------- /semantic_image_synthesis/requirements.txt: -------------------------------------------------------------------------------- 1 | torch>=1.0.0 2 | torchvision 3 | dominate>=2.3.1 4 | dill 5 | scikit-image 6 | -------------------------------------------------------------------------------- /semantic_image_synthesis/scripts/download_lggan_model.sh: -------------------------------------------------------------------------------- 1 | FILE=$1 2 | 3 | echo "Note: available models are LGGAN_cityscapes, LGGAN_ade" 4 | echo "Specified [$FILE]" 5 | 6 | URL=http://disi.unitn.it/~hao.tang/uploads/models/LGGAN/${FILE}_pretrained.tar.gz 7 | TAR_FILE=./checkpoints/${FILE}_pretrained.tar.gz 8 | TARGET_DIR=./checkpoints/${FILE}_pretrained/ 9 | 10 | wget -N $URL -O $TAR_FILE 11 | 12 | mkdir -p $TARGET_DIR 13 | tar -zxvf $TAR_FILE -C ./checkpoints/ 14 | rm $TAR_FILE -------------------------------------------------------------------------------- /semantic_image_synthesis/test_ade.py: -------------------------------------------------------------------------------- 1 | """ 2 | Copyright (C) 2019 NVIDIA Corporation. All rights reserved. 3 | Licensed under the CC BY-NC-SA 4.0 license (https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode). 4 | """ 5 | 6 | import os 7 | from collections import OrderedDict 8 | 9 | import torch 10 | import data 11 | from options.test_options import TestOptions 12 | from models.pix2pix_model import Pix2PixModel 13 | from util.visualizer import Visualizer 14 | from util import html 15 | 16 | opt = TestOptions().parse() 17 | 18 | dataloader = data.create_dataloader(opt) 19 | 20 | model = Pix2PixModel(opt) 21 | model.eval() 22 | 23 | visualizer = Visualizer(opt) 24 | 25 | # create a webpage that summarizes the all results 26 | web_dir = os.path.join(opt.results_dir, opt.name, 27 | '%s_%s' % (opt.phase, opt.which_epoch)) 28 | webpage = html.HTML(web_dir, 29 | 'Experiment = %s, Phase = %s, Epoch = %s' % 30 | (opt.name, opt.phase, opt.which_epoch)) 31 | 32 | # test 33 | for i, data_i in enumerate(dataloader): 34 | if i * opt.batchSize >= opt.how_many: 35 | break 36 | 37 | # generated, result_global, result_local, label_3_0, label_3_1, label_3_2, label_3_3, label_3_4, label_3_5, label_3_6, label_3_7, label_3_8, \ 38 | # label_3_9, label_3_10, label_3_11, label_3_12, label_3_13, label_3_14, label_3_15, label_3_16, label_3_17, label_3_18, label_3_19, label_3_20, \ 39 | # label_3_21,label_3_22, label_3_23, label_3_24, label_3_25, label_3_26, label_3_27, label_3_28, label_3_29, label_3_30, label_3_31, \ 40 | # label_3_32, label_3_33, label_3_34, result_0, result_1, result_2, result_3, result_4,result_5 ,result_6 ,result_7 , result_8 , result_9 , result_10 , \ 41 | # result_11 ,result_12 , result_13 , result_14 , result_15 , result_16 , result_17 , result_18 , result_19 , result_20, \ 42 | # result_21 , result_22 , result_23 , result_24 , result_25 , result_26 , result_27 , result_28 , result_29 , result_30, \ 43 | # result_31 , result_32 , result_33 , result_34,feature_score, target, index, attention_global, attention_local, = model(data_i, mode='inference') 44 | 45 | generated, result_global, result_local, label_3_0, label_3_1, label_3_2, label_3_3, label_3_4, label_3_5, label_3_6, label_3_7, label_3_8, \ 46 | label_3_9, label_3_10, label_3_11, label_3_12, label_3_13, label_3_14, label_3_15, label_3_16, label_3_17, label_3_18, label_3_19, label_3_20, \ 47 | label_3_21, label_3_22, label_3_23, label_3_24, label_3_25, label_3_26, label_3_27, label_3_28, label_3_29, label_3_30, label_3_31, \ 48 | label_3_32, label_3_33, label_3_34, label_3_35, label_3_36, label_3_37, label_3_38, label_3_39, label_3_40, label_3_41, label_3_42, label_3_43, \ 49 | label_3_44, label_3_45, label_3_46, label_3_47, label_3_48, label_3_49, label_3_50, label_3_51, \ 50 | result_0, result_1, result_2, result_3, result_4, result_5, result_6, result_7, result_8, result_9, result_10, \ 51 | result_11, result_12, result_13, result_14, result_15, result_16, result_17, result_18, result_19, result_20, \ 52 | result_21, result_22, result_23, result_24, result_25, result_26, result_27, result_28, result_29, result_30, \ 53 | result_31, result_32, result_33, result_34, result_35, result_36, result_37, result_38, result_39, result_40, result_41, \ 54 | result_42, result_43, result_44, result_45, result_46, result_47, result_48, result_49, result_50, result_51, feature_score, \ 55 | target, index, attention_global, attention_local = model(data_i, mode='inference') 56 | 57 | # attention_global= (attention_global - 0.5)/0.5 58 | # attention_local= (attention_local - 0.5)/0.5 59 | img_path = data_i['path'] 60 | for b in range(generated.shape[0]): 61 | print('process image... %s' % img_path[b]) 62 | visuals = OrderedDict([('input_label', data_i['label'][b]), 63 | 64 | ('global_image', result_global[b]), 65 | ('local_image', result_local[b]), 66 | ('global_attention', 1 - attention_local[b]/torch.max(attention_local[b]).item() ), 67 | ('local_attention', attention_local[b]/torch.max(attention_local[b]).item() ), 68 | # ('label_3_7', label_3_7[b]), 69 | # ('image_3_7', result_7[b]), 70 | # ('label_3_8', label_3_8[b]), 71 | # ('image_3_8', result_8[b]), 72 | # ('label_3_11', label_3_11[b]), 73 | # ('image_3_11', result_11[b]), 74 | # ('label_3_21', label_3_21[b]), 75 | # ('image_3_21', result_21[b]), 76 | # ('label_3_27', label_3_27[b]), 77 | # ('image_3_27', result_27[b]), 78 | # ('label_3_33', label_3_33[b]), 79 | # ('image_3_33', result_33[b]), 80 | ('synthesized_image', generated[b])]) 81 | visualizer.save_images(webpage, visuals, img_path[b:b + 1]) 82 | 83 | webpage.save() 84 | -------------------------------------------------------------------------------- /semantic_image_synthesis/test_ade.sh: -------------------------------------------------------------------------------- 1 | python test_ade.py --name LGGAN_ade --dataset_mode ade20k --dataroot ./datasets/ade20k --gpu_ids 0 --results_dir ./results --checkpoints_dir ./checkpoints --batchSize 1 --which_epoch 200; 2 | -------------------------------------------------------------------------------- /semantic_image_synthesis/test_city.py: -------------------------------------------------------------------------------- 1 | """ 2 | Copyright (C) 2019 NVIDIA Corporation. All rights reserved. 3 | Licensed under the CC BY-NC-SA 4.0 license (https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode). 4 | """ 5 | 6 | import os 7 | from collections import OrderedDict 8 | 9 | import torch 10 | import data 11 | from options.test_options import TestOptions 12 | from models.pix2pix_model import Pix2PixModel 13 | from util.visualizer import Visualizer 14 | from util import html 15 | 16 | opt = TestOptions().parse() 17 | 18 | dataloader = data.create_dataloader(opt) 19 | 20 | model = Pix2PixModel(opt) 21 | model.eval() 22 | 23 | visualizer = Visualizer(opt) 24 | 25 | # create a webpage that summarizes the all results 26 | web_dir = os.path.join(opt.results_dir, opt.name, 27 | '%s_%s' % (opt.phase, opt.which_epoch)) 28 | webpage = html.HTML(web_dir, 29 | 'Experiment = %s, Phase = %s, Epoch = %s' % 30 | (opt.name, opt.phase, opt.which_epoch)) 31 | 32 | # test 33 | for i, data_i in enumerate(dataloader): 34 | if i * opt.batchSize >= opt.how_many: 35 | break 36 | 37 | generated, result_global, result_local, label_3_0, label_3_1, label_3_2, label_3_3, label_3_4, label_3_5, label_3_6, label_3_7, label_3_8, \ 38 | label_3_9, label_3_10, label_3_11, label_3_12, label_3_13, label_3_14, label_3_15, label_3_16, label_3_17, label_3_18, label_3_19, label_3_20, \ 39 | label_3_21,label_3_22, label_3_23, label_3_24, label_3_25, label_3_26, label_3_27, label_3_28, label_3_29, label_3_30, label_3_31, \ 40 | label_3_32, label_3_33, label_3_34, result_0, result_1, result_2, result_3, result_4,result_5 ,result_6 ,result_7 , result_8 , result_9 , result_10 , \ 41 | result_11 ,result_12 , result_13 , result_14 , result_15 , result_16 , result_17 , result_18 , result_19 , result_20, \ 42 | result_21 , result_22 , result_23 , result_24 , result_25 , result_26 , result_27 , result_28 , result_29 , result_30, \ 43 | result_31 , result_32 , result_33 , result_34,feature_score, target, index, attention_global, attention_local, = model(data_i, mode='inference') 44 | 45 | # attention_global= (attention_global - 0.5)/0.5 46 | # attention_local= (attention_local - 0.5)/0.5 47 | img_path = data_i['path'] 48 | for b in range(generated.shape[0]): 49 | print('process image... %s' % img_path[b]) 50 | visuals = OrderedDict([('input_label', data_i['label'][b]), 51 | ('global_image', result_global[b]), 52 | ('local_image', result_local[b]), 53 | ('global_attention', 1 - attention_local[b]/torch.max(attention_local[b]).item() ), 54 | ('local_attention', attention_local[b]/torch.max(attention_local[b]).item() ), 55 | ('label_3_7', label_3_7[b]), 56 | ('image_3_7', result_7[b]), 57 | ('label_3_8', label_3_8[b]), 58 | ('image_3_8', result_8[b]), 59 | ('label_3_11', label_3_11[b]), 60 | ('image_3_11', result_11[b]), 61 | ('label_3_21', label_3_21[b]), 62 | ('image_3_21', result_21[b]), 63 | ('label_3_27', label_3_27[b]), 64 | ('image_3_27', result_27[b]), 65 | ('label_3_33', label_3_33[b]), 66 | ('image_3_33', result_33[b]), 67 | ('synthesized_image', generated[b])]) 68 | visualizer.save_images(webpage, visuals, img_path[b:b + 1]) 69 | 70 | webpage.save() 71 | -------------------------------------------------------------------------------- /semantic_image_synthesis/test_cityscapes.sh: -------------------------------------------------------------------------------- 1 | python test_city.py --name LGGAN_cityscapes --dataset_mode cityscapes --dataroot ./datasets/cityscapes/data --gpu_ids 0 --results_dir ./results --checkpoints_dir ./checkpoints --batchSize 1 --which_epoch 200; -------------------------------------------------------------------------------- /semantic_image_synthesis/train_ade.sh: -------------------------------------------------------------------------------- 1 | python train_ade.py --name LGGAN_ade --dataset_mode ade20k --dataroot ./datasets/ade20k --niter 100 --niter_decay 100 --gpu_ids 0,1,2,3,4,5,6,7 --checkpoints_dir ./checkpoints --batchSize 24 --save_epoch_freq 5 --no_l1_loss 2 | # --continue_train; -------------------------------------------------------------------------------- /semantic_image_synthesis/train_cityscapes.sh: -------------------------------------------------------------------------------- 1 | python train_city.py --name LGGAN_cityscapes --dataset_mode cityscapes --dataroot ./datasets/cityscapes/data --niter 100 --niter_decay 100 --gpu_ids 0,1,2,3,4,5,6,7 --checkpoints_dir ./checkpoints --no_l1_loss --batchSize 8 2 | # --continue_train; -------------------------------------------------------------------------------- /semantic_image_synthesis/trainers/__init__.py: -------------------------------------------------------------------------------- 1 | """ 2 | Copyright (C) 2019 NVIDIA Corporation. All rights reserved. 3 | Licensed under the CC BY-NC-SA 4.0 license (https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode). 4 | """ 5 | -------------------------------------------------------------------------------- /semantic_image_synthesis/trainers/__pycache__/__init__.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/semantic_image_synthesis/trainers/__pycache__/__init__.cpython-36.pyc -------------------------------------------------------------------------------- /semantic_image_synthesis/trainers/__pycache__/pix2pix_trainer.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/semantic_image_synthesis/trainers/__pycache__/pix2pix_trainer.cpython-36.pyc -------------------------------------------------------------------------------- /semantic_image_synthesis/util/__init__.py: -------------------------------------------------------------------------------- 1 | """ 2 | Copyright (C) 2019 NVIDIA Corporation. All rights reserved. 3 | Licensed under the CC BY-NC-SA 4.0 license (https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode). 4 | """ 5 | -------------------------------------------------------------------------------- /semantic_image_synthesis/util/__pycache__/__init__.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/semantic_image_synthesis/util/__pycache__/__init__.cpython-36.pyc -------------------------------------------------------------------------------- /semantic_image_synthesis/util/__pycache__/__init__.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/semantic_image_synthesis/util/__pycache__/__init__.cpython-37.pyc -------------------------------------------------------------------------------- /semantic_image_synthesis/util/__pycache__/coco.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/semantic_image_synthesis/util/__pycache__/coco.cpython-36.pyc -------------------------------------------------------------------------------- /semantic_image_synthesis/util/__pycache__/coco.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/semantic_image_synthesis/util/__pycache__/coco.cpython-37.pyc -------------------------------------------------------------------------------- /semantic_image_synthesis/util/__pycache__/html.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/semantic_image_synthesis/util/__pycache__/html.cpython-36.pyc -------------------------------------------------------------------------------- /semantic_image_synthesis/util/__pycache__/html.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/semantic_image_synthesis/util/__pycache__/html.cpython-37.pyc -------------------------------------------------------------------------------- /semantic_image_synthesis/util/__pycache__/iter_counter.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/semantic_image_synthesis/util/__pycache__/iter_counter.cpython-36.pyc -------------------------------------------------------------------------------- /semantic_image_synthesis/util/__pycache__/util.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/semantic_image_synthesis/util/__pycache__/util.cpython-36.pyc -------------------------------------------------------------------------------- /semantic_image_synthesis/util/__pycache__/util.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/semantic_image_synthesis/util/__pycache__/util.cpython-37.pyc -------------------------------------------------------------------------------- /semantic_image_synthesis/util/__pycache__/visualizer.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/semantic_image_synthesis/util/__pycache__/visualizer.cpython-36.pyc -------------------------------------------------------------------------------- /semantic_image_synthesis/util/__pycache__/visualizer.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ha0Tang/LGGAN/e67dfb83aee0a532ec8fa6bed8099851d8bba180/semantic_image_synthesis/util/__pycache__/visualizer.cpython-37.pyc -------------------------------------------------------------------------------- /semantic_image_synthesis/util/coco.py: -------------------------------------------------------------------------------- 1 | """ 2 | Copyright (C) 2019 NVIDIA Corporation. All rights reserved. 3 | Licensed under the CC BY-NC-SA 4.0 license (https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode). 4 | """ 5 | 6 | 7 | def id2label(id): 8 | if id == 182: 9 | id = 0 10 | else: 11 | id = id + 1 12 | labelmap = \ 13 | {0: 'unlabeled', 14 | 1: 'person', 15 | 2: 'bicycle', 16 | 3: 'car', 17 | 4: 'motorcycle', 18 | 5: 'airplane', 19 | 6: 'bus', 20 | 7: 'train', 21 | 8: 'truck', 22 | 9: 'boat', 23 | 10: 'traffic light', 24 | 11: 'fire hydrant', 25 | 12: 'street sign', 26 | 13: 'stop sign', 27 | 14: 'parking meter', 28 | 15: 'bench', 29 | 16: 'bird', 30 | 17: 'cat', 31 | 18: 'dog', 32 | 19: 'horse', 33 | 20: 'sheep', 34 | 21: 'cow', 35 | 22: 'elephant', 36 | 23: 'bear', 37 | 24: 'zebra', 38 | 25: 'giraffe', 39 | 26: 'hat', 40 | 27: 'backpack', 41 | 28: 'umbrella', 42 | 29: 'shoe', 43 | 30: 'eye glasses', 44 | 31: 'handbag', 45 | 32: 'tie', 46 | 33: 'suitcase', 47 | 34: 'frisbee', 48 | 35: 'skis', 49 | 36: 'snowboard', 50 | 37: 'sports ball', 51 | 38: 'kite', 52 | 39: 'baseball bat', 53 | 40: 'baseball glove', 54 | 41: 'skateboard', 55 | 42: 'surfboard', 56 | 43: 'tennis racket', 57 | 44: 'bottle', 58 | 45: 'plate', 59 | 46: 'wine glass', 60 | 47: 'cup', 61 | 48: 'fork', 62 | 49: 'knife', 63 | 50: 'spoon', 64 | 51: 'bowl', 65 | 52: 'banana', 66 | 53: 'apple', 67 | 54: 'sandwich', 68 | 55: 'orange', 69 | 56: 'broccoli', 70 | 57: 'carrot', 71 | 58: 'hot dog', 72 | 59: 'pizza', 73 | 60: 'donut', 74 | 61: 'cake', 75 | 62: 'chair', 76 | 63: 'couch', 77 | 64: 'potted plant', 78 | 65: 'bed', 79 | 66: 'mirror', 80 | 67: 'dining table', 81 | 68: 'window', 82 | 69: 'desk', 83 | 70: 'toilet', 84 | 71: 'door', 85 | 72: 'tv', 86 | 73: 'laptop', 87 | 74: 'mouse', 88 | 75: 'remote', 89 | 76: 'keyboard', 90 | 77: 'cell phone', 91 | 78: 'microwave', 92 | 79: 'oven', 93 | 80: 'toaster', 94 | 81: 'sink', 95 | 82: 'refrigerator', 96 | 83: 'blender', 97 | 84: 'book', 98 | 85: 'clock', 99 | 86: 'vase', 100 | 87: 'scissors', 101 | 88: 'teddy bear', 102 | 89: 'hair drier', 103 | 90: 'toothbrush', 104 | 91: 'hair brush', # Last class of Thing 105 | 92: 'banner', # Beginning of Stuff 106 | 93: 'blanket', 107 | 94: 'branch', 108 | 95: 'bridge', 109 | 96: 'building-other', 110 | 97: 'bush', 111 | 98: 'cabinet', 112 | 99: 'cage', 113 | 100: 'cardboard', 114 | 101: 'carpet', 115 | 102: 'ceiling-other', 116 | 103: 'ceiling-tile', 117 | 104: 'cloth', 118 | 105: 'clothes', 119 | 106: 'clouds', 120 | 107: 'counter', 121 | 108: 'cupboard', 122 | 109: 'curtain', 123 | 110: 'desk-stuff', 124 | 111: 'dirt', 125 | 112: 'door-stuff', 126 | 113: 'fence', 127 | 114: 'floor-marble', 128 | 115: 'floor-other', 129 | 116: 'floor-stone', 130 | 117: 'floor-tile', 131 | 118: 'floor-wood', 132 | 119: 'flower', 133 | 120: 'fog', 134 | 121: 'food-other', 135 | 122: 'fruit', 136 | 123: 'furniture-other', 137 | 124: 'grass', 138 | 125: 'gravel', 139 | 126: 'ground-other', 140 | 127: 'hill', 141 | 128: 'house', 142 | 129: 'leaves', 143 | 130: 'light', 144 | 131: 'mat', 145 | 132: 'metal', 146 | 133: 'mirror-stuff', 147 | 134: 'moss', 148 | 135: 'mountain', 149 | 136: 'mud', 150 | 137: 'napkin', 151 | 138: 'net', 152 | 139: 'paper', 153 | 140: 'pavement', 154 | 141: 'pillow', 155 | 142: 'plant-other', 156 | 143: 'plastic', 157 | 144: 'platform', 158 | 145: 'playingfield', 159 | 146: 'railing', 160 | 147: 'railroad', 161 | 148: 'river', 162 | 149: 'road', 163 | 150: 'rock', 164 | 151: 'roof', 165 | 152: 'rug', 166 | 153: 'salad', 167 | 154: 'sand', 168 | 155: 'sea', 169 | 156: 'shelf', 170 | 157: 'sky-other', 171 | 158: 'skyscraper', 172 | 159: 'snow', 173 | 160: 'solid-other', 174 | 161: 'stairs', 175 | 162: 'stone', 176 | 163: 'straw', 177 | 164: 'structural-other', 178 | 165: 'table', 179 | 166: 'tent', 180 | 167: 'textile-other', 181 | 168: 'towel', 182 | 169: 'tree', 183 | 170: 'vegetable', 184 | 171: 'wall-brick', 185 | 172: 'wall-concrete', 186 | 173: 'wall-other', 187 | 174: 'wall-panel', 188 | 175: 'wall-stone', 189 | 176: 'wall-tile', 190 | 177: 'wall-wood', 191 | 178: 'water-other', 192 | 179: 'waterdrops', 193 | 180: 'window-blind', 194 | 181: 'window-other', 195 | 182: 'wood'} 196 | if id in labelmap: 197 | return labelmap[id] 198 | else: 199 | return 'unknown' 200 | -------------------------------------------------------------------------------- /semantic_image_synthesis/util/html.py: -------------------------------------------------------------------------------- 1 | """ 2 | Copyright (C) 2019 NVIDIA Corporation. All rights reserved. 3 | Licensed under the CC BY-NC-SA 4.0 license (https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode). 4 | """ 5 | 6 | import datetime 7 | import dominate 8 | from dominate.tags import * 9 | import os 10 | 11 | 12 | class HTML: 13 | def __init__(self, web_dir, title, refresh=0): 14 | if web_dir.endswith('.html'): 15 | web_dir, html_name = os.path.split(web_dir) 16 | else: 17 | web_dir, html_name = web_dir, 'index.html' 18 | self.title = title 19 | self.web_dir = web_dir 20 | self.html_name = html_name 21 | self.img_dir = os.path.join(self.web_dir, 'images') 22 | if len(self.web_dir) > 0 and not os.path.exists(self.web_dir): 23 | os.makedirs(self.web_dir) 24 | if len(self.web_dir) > 0 and not os.path.exists(self.img_dir): 25 | os.makedirs(self.img_dir) 26 | 27 | self.doc = dominate.document(title=title) 28 | with self.doc: 29 | h1(datetime.datetime.now().strftime("%I:%M%p on %B %d, %Y")) 30 | if refresh > 0: 31 | with self.doc.head: 32 | meta(http_equiv="refresh", content=str(refresh)) 33 | 34 | def get_image_dir(self): 35 | return self.img_dir 36 | 37 | def add_header(self, str): 38 | with self.doc: 39 | h3(str) 40 | 41 | def add_table(self, border=1): 42 | self.t = table(border=border, style="table-layout: fixed;") 43 | self.doc.add(self.t) 44 | 45 | def add_images(self, ims, txts, links, width=512): 46 | self.add_table() 47 | with self.t: 48 | with tr(): 49 | for im, txt, link in zip(ims, txts, links): 50 | with td(style="word-wrap: break-word;", halign="center", valign="top"): 51 | with p(): 52 | with a(href=os.path.join('images', link)): 53 | img(style="width:%dpx" % (width), src=os.path.join('images', im)) 54 | br() 55 | p(txt.encode('utf-8')) 56 | 57 | def save(self): 58 | html_file = os.path.join(self.web_dir, self.html_name) 59 | f = open(html_file, 'wt') 60 | f.write(self.doc.render()) 61 | f.close() 62 | 63 | 64 | if __name__ == '__main__': 65 | html = HTML('web/', 'test_html') 66 | html.add_header('hello world') 67 | 68 | ims = [] 69 | txts = [] 70 | links = [] 71 | for n in range(4): 72 | ims.append('image_%d.jpg' % n) 73 | txts.append('text_%d' % n) 74 | links.append('image_%d.jpg' % n) 75 | html.add_images(ims, txts, links) 76 | html.save() 77 | -------------------------------------------------------------------------------- /semantic_image_synthesis/util/iter_counter.py: -------------------------------------------------------------------------------- 1 | """ 2 | Copyright (C) 2019 NVIDIA Corporation. All rights reserved. 3 | Licensed under the CC BY-NC-SA 4.0 license (https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode). 4 | """ 5 | 6 | import os 7 | import time 8 | import numpy as np 9 | 10 | 11 | # Helper class that keeps track of training iterations 12 | class IterationCounter(): 13 | def __init__(self, opt, dataset_size): 14 | self.opt = opt 15 | self.dataset_size = dataset_size 16 | 17 | self.first_epoch = 1 18 | self.total_epochs = opt.niter + opt.niter_decay 19 | self.epoch_iter = 0 # iter number within each epoch 20 | self.iter_record_path = os.path.join(self.opt.checkpoints_dir, self.opt.name, 'iter.txt') 21 | if opt.isTrain and opt.continue_train: 22 | try: 23 | self.first_epoch, self.epoch_iter = np.loadtxt( 24 | self.iter_record_path, delimiter=',', dtype=int) 25 | print('Resuming from epoch %d at iteration %d' % (self.first_epoch, self.epoch_iter)) 26 | except: 27 | print('Could not load iteration record at %s. Starting from beginning.' % 28 | self.iter_record_path) 29 | 30 | self.total_steps_so_far = (self.first_epoch - 1) * dataset_size + self.epoch_iter 31 | 32 | # return the iterator of epochs for the training 33 | def training_epochs(self): 34 | return range(self.first_epoch, self.total_epochs + 1) 35 | 36 | def record_epoch_start(self, epoch): 37 | self.epoch_start_time = time.time() 38 | self.epoch_iter = 0 39 | self.last_iter_time = time.time() 40 | self.current_epoch = epoch 41 | 42 | def record_one_iteration(self): 43 | current_time = time.time() 44 | 45 | # the last remaining batch is dropped (see data/__init__.py), 46 | # so we can assume batch size is always opt.batchSize 47 | self.time_per_iter = (current_time - self.last_iter_time) / self.opt.batchSize 48 | self.last_iter_time = current_time 49 | self.total_steps_so_far += self.opt.batchSize 50 | self.epoch_iter += self.opt.batchSize 51 | 52 | def record_epoch_end(self): 53 | current_time = time.time() 54 | self.time_per_epoch = current_time - self.epoch_start_time 55 | print('End of epoch %d / %d \t Time Taken: %d sec' % 56 | (self.current_epoch, self.total_epochs, self.time_per_epoch)) 57 | if self.current_epoch % self.opt.save_epoch_freq == 0: 58 | np.savetxt(self.iter_record_path, (self.current_epoch + 1, 0), 59 | delimiter=',', fmt='%d') 60 | print('Saved current iteration count at %s.' % self.iter_record_path) 61 | 62 | def record_current_iter(self): 63 | np.savetxt(self.iter_record_path, (self.current_epoch, self.epoch_iter), 64 | delimiter=',', fmt='%d') 65 | print('Saved current iteration count at %s.' % self.iter_record_path) 66 | 67 | def needs_saving(self): 68 | return (self.total_steps_so_far % self.opt.save_latest_freq) < self.opt.batchSize 69 | 70 | def needs_printing(self): 71 | return (self.total_steps_so_far % self.opt.print_freq) < self.opt.batchSize 72 | 73 | def needs_displaying(self): 74 | return (self.total_steps_so_far % self.opt.display_freq) < self.opt.batchSize 75 | --------------------------------------------------------------------------------