import os from data.pix2pix_dataset import Pix2pixDataset, BaseDataset from data.image_folder import make_dataset from torchvision import transforms import os from PIL import Image class Summer2WinterYosemiteDataset(BaseDataset): @staticmethod def modify_commandline_options(parser, is_train): parser = Pix2pixDataset.modify_commandline_options(parser, is_train) parser.set_defaults(preprocess_mode='resize_and_crop') parser.set_defaults(load_size=512) parser.set_defaults(crop_size=256) return parser def initialize(self, opt): self.opt = opt label_paths, image_paths, instance_paths = self.get_paths(opt) self.label_paths = label_paths[:opt.max_dataset_size] self.image_paths = image_paths[:opt.max_dataset_size] self.dataset_size = len(self.label_paths) print(f"Number of labels: {len(self.label_paths)}, Number of images: {len(self.image_paths)}") if len(self.label_paths) != len(self.image_paths): raise ValueError("The number of labels and images do not match.") def get_paths(self, opt): croot = opt.croot sroot = opt.sroot c_image_dir = os.path.join(croot, f'{opt.phase}A') s_image_dir = os.path.join(sroot, f'{opt.phase}B') c_image_paths = sorted(make_dataset(c_image_dir, recursive=True)) s_image_paths = sorted(make_dataset(s_image_dir, recursive=True)) return c_image_paths, s_image_paths, [] def __getitem__(self, index): label_path = self.label_paths[index] image_path = self.image_paths[index] label = Image.open(label_path).convert('RGB') image = Image.open(image_path).convert('RGB') transform = transforms.Compose([ transforms.Resize((self.opt.load_size, self.opt.load_size)), transforms.RandomCrop(self.opt.crop_size), transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) ]) return {'image': transform(label), 'label': transform(image),"cpath":image_path} def __len__(self): return self.dataset_size