diff --git a/data/image_folder.py b/data/image_folder.py index 2a137d3..ec52367 100644 --- a/data/image_folder.py +++ b/data/image_folder.py @@ -13,7 +13,7 @@ import os.path IMG_EXTENSIONS = [ '.jpg', '.JPG', '.jpeg', '.JPEG', '.png', '.PNG', '.ppm', '.PPM', '.bmp', '.BMP', - '.tif', '.TIF', '.tiff', '.TIFF', + '.tif', '.TIF', '.tiff', '.TIFF', '.pth', ] diff --git a/data/with_logist_dataset.py b/data/with_logist_dataset.py new file mode 100644 index 0000000..0cc67bc --- /dev/null +++ b/data/with_logist_dataset.py @@ -0,0 +1,86 @@ +import os.path +from data.base_dataset import BaseDataset, get_transform +from data.image_folder import make_dataset +from PIL import Image +import random +import util.util as util +from glob import glob +import torch + +class UnalignedDataset(BaseDataset): + """ + This dataset class can load unaligned/unpaired datasets. + + It requires two directories to host training images from domain A '/path/to/data/trainA' + and from domain B '/path/to/data/trainB' respectively. + You can train the model with the dataset flag '--dataroot /path/to/data'. + Similarly, you need to prepare two directories: + '/path/to/data/testA' and '/path/to/data/testB' during test time. + """ + + def __init__(self, opt): + """Initialize this dataset class. + + Parameters: + opt (Option class) -- stores all the experiment flags; needs to be a subclass of BaseOptions + """ + BaseDataset.__init__(self, opt) + self.dir_A = os.path.join(opt.dataroot, opt.phase + 'A') # create a path '/path/to/data/trainA' + self.dir_B = os.path.join(opt.dataroot, opt.phase + 'B') # create a path '/path/to/data/trainB' + self.dir_A_logi = '/home/openxs/kunyu/datasets/InfraredCity-Lite/Single/Monitor/trainA_dino' + + if opt.phase == "test" and not os.path.exists(self.dir_A) \ + and os.path.exists(os.path.join(opt.dataroot, "valA")): + self.dir_A = os.path.join(opt.dataroot, "valA") + self.dir_B = os.path.join(opt.dataroot, "valB") + + self.A_paths = sorted(make_dataset(self.dir_A, opt.max_dataset_size)) # load images from '/path/to/data/trainA' + self.B_paths = sorted(make_dataset(self.dir_B, opt.max_dataset_size)) # load images from '/path/to/data/trainB' + self.A_logi_paths = sorted(make_dataset(self.dir_A_logi, opt.max_dataset_size)) + self.A_size = len(self.A_paths) # get the size of dataset A + self.B_size = len(self.B_paths) # get the size of dataset B + + def __getitem__(self, index): + """Return a data point and its metadata information. + + Parameters: + index (int) -- a random integer for data indexing + + Returns a dictionary that contains A, B, A_paths and B_paths + A (tensor) -- an image in the input domain + B (tensor) -- its corresponding image in the target domain + A_paths (str) -- image paths + B_paths (str) -- image paths + """ + A_path = self.A_paths[index % self.A_size] # make sure index is within then range + A_logi_path = self.A_logi_paths[index % self.A_size] + if self.opt.serial_batches: # make sure index is within then range + index_B = index % self.B_size + else: # randomize the index for domain B to avoid fixed pairs. + index_B = random.randint(0, self.B_size - 1) + B_path = self.B_paths[index_B] + A_img = Image.open(A_path).convert('RGB') + B_img = Image.open(B_path).convert('RGB') + + # shape: [1, 150, 256, 256] + A_logi = torch.load(A_logi_path, map_location=f'cuda:{self.opt.gpu_id}') + + # Apply image transformation + # For FastCUT mode, if in finetuning phase (learning rate is decaying), + # do not perform resize-crop data augmentation of CycleGAN. + # print('current_epoch', self.current_epoch) + is_finetuning = self.opt.isTrain and self.current_epoch > self.opt.n_epochs + modified_opt = util.copyconf(self.opt, load_size=self.opt.crop_size if is_finetuning else self.opt.load_size) + transform = get_transform(modified_opt) + A = transform(A_img) + B = transform(B_img) + + return {'A': A, 'B': B, 'A_paths': A_path, 'B_paths': B_path, 'A_logi': A_logi, 'A_logi_paths': A_logi_path} + + def __len__(self): + """Return the total number of images in the dataset. + + As we have two datasets with potentially different number of images, + we take a maximum of + """ + return max(self.A_size, self.B_size)