Пример #1
0
    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_canonical = os.path.join(opt.dataroot, opt.phase + 'canonical')  # create a path '/path/to/data/trainA'
        self.dir_random = os.path.join(opt.dataroot, opt.phase + 'random')  # create a path '/path/to/data/trainB'
        self.dir_seg = os.path.join(opt.dataroot, opt.phase + 'seg')  # create a path '/path/to/data/trainB'

        self.canonical_paths = sorted(make_dataset(self.dir_canonical, opt.max_dataset_size))   # load images from '/path/to/data/trainA'
        self.random_paths = sorted(make_dataset(self.dir_random, opt.max_dataset_size))    # load images from '/path/to/data/trainB'
        self.seg_paths = sorted(make_dataset(self.dir_seg, opt.max_dataset_size))    # load images from '/path/to/data/trainB'

        self.canonical_size = len(self.canonical_paths)  # get the size of dataset A
        self.random_size = len(self.random_paths)  # get the size of dataset B
        self.seg_size = len(self.seg_paths)  # get the size of dataset B

        assert self.canonical_size == self.random_size == self.seg_size, 'Dataset sizes are not the same'

        input_nc = self.opt.input_nc       # get the number of channels of input image
        output_nc = self.opt.output_nc      # get the number of channels of output image
        self.transform_A = get_transform(self.opt, grayscale=False)
        self.transform_seg = get_transform(self.opt, grayscale=True)
        self.transform_B = get_transform(self.opt, grayscale=False)
Пример #2
0
	def __init__(self, opt, type_of_data, mydir = None):
		"""Initialize this dataset class.

		Parameters:
			opt (Option class) -- stores all the experiment flags; needs to be a subclass of BaseOptions
		"""
		BaseDataset.__init__(self, opt)
		if mydir == None:
			self.dir = os.path.join(opt.dataroot, 'fashion-{}.npz'.format(type_of_data))
		else:
			self.dir = os.path.join(mydir, 'fashion-{}.npz'.format(type_of_data))
		self.type_of_data = type_of_data
		self.num_classes = 10

		loaded = np.load(self.dir)
		self.inputs = loaded['inputs'].astype(np.float32)
		if np.max(self.inputs) > 1:
			self.inputs = self.inputs / 255.0
		self.targets = loaded['targets'].astype(np.float32)

		# print(np.min(self.inputs), np.max(self.inputs))

		self.num_data_points = int(self.inputs.shape[0] / self.batch_size) * self.batch_size
		if opt.num_batches != -1:# and trim_data:
			self.num_data_points = min(opt.num_batches * self.batch_size,self.num_data_points)
    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.A_dir = os.path.join(self.opt.dataroot, 'ct', self.opt.phase)
        self.B_dir = os.path.join(self.opt.dataroot, 'mri', self.opt.phase)
        self.A_paths = sorted(make_dataset(self.A_dir, opt.max_dataset_size))
        self.B_paths = sorted(make_dataset(self.B_dir, opt.max_dataset_size))
        self.A_size = len(self.A_paths)
        self.B_size = len(self.B_paths)

        transformations = [
            transforms.ToTensor(),
            transforms.Lambda(
                lambda x: self.center(x, opt.mean_norm, opt.std_norm)),
            # transforms.RandomCrop((148,100)),
            RandomCropIfNecessary(1200, opt.n_downsampling)
            if opt.uniform_size is None else PadTo(*opt.uniform_size),
            # transforms.Pad((1,0,0,0), padding_mode='reflect')
        ]
        if (opt.isTrain):
            transformations += [
                transforms.RandomHorizontalFlip(),
                transforms.RandomVerticalFlip()
            ]
        self.transformations = transforms.Compose(transformations)
    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_AB = os.path.join(opt.dataroot,
                                   opt.phase)  # get the image directory
        mask_dir = os.path.join(opt.dataroot, "mask")
        self.AB_paths = sorted(make_dataset(
            self.dir_AB, opt.max_dataset_size))  # get image paths
        self.dataset_len = len(self.AB_paths)
        assert (self.opt.load_size >= self.opt.crop_size
                )  # crop_size should be smaller than the size of loaded image
        self.input_nc = self.opt.output_nc if self.opt.direction == 'BtoA' else self.opt.input_nc
        self.output_nc = self.opt.input_nc if self.opt.direction == 'BtoA' else self.opt.output_nc
        mask_paths = []
        for ab_path in self.AB_paths:
            ab_name = os.path.basename(ab_path)
            mask_name = "{}_{}.json".format(opt.phase,
                                            ab_name.rsplit('.', 1)[0])
            mask_path = os.path.join(mask_dir, mask_name)
            mask_paths.append(mask_path)
            assert os.path.isfile(mask_path)
        self.mask_paths = mask_paths
        random.seed(30)
Пример #5
0
    def __init__(self, opt):
        """Initialize this dataset class.

        Parameters:
            opt (Option class) -- stores all the experiment flags; needs to be a subclass of BaseOptions

        A few things can be done here.
        - save the options (have been done in BaseDataset)
        - get image paths and meta information of the dataset.
        - define the image transformation.
        """
        # save the option and dataset root
        BaseDataset.__init__(self, opt)

        self.patch_size = opt.patch_size

        # set whether to perform downsampling and to include
        if opt.model in ['srcnn', 'srgan']:
            self.downsample_factor = opt.downsample_factor
            if hasattr(opt, 'd_condition'):
                self.include_original_res = opt.d_condition
            else:
                self.include_original_res = False
        else:
            self.downsample_factor = None

        # Check that translation is not being performed during training
        assert not opt.isTrain, '3D translation not defined for training mode'

        set_inc = opt.save_name if opt.save_name is not None else opt.phase  # optionally allow for sampling from training set-connected TXM images
        self.save_txm_dir = os.path.join(
            opt.results_dir, opt.name, 'volume_txm_{}'.format(
                set_inc))  # save directory for TXM image patches
        self.save_pred_dir = os.path.join(
            opt.results_dir, opt.name, 'volume_pred_{}'.format(
                set_inc))  # save directory for translated images
        mkdirs([self.save_pred_dir, self.save_txm_dir])

        # Get image paths and discard all but the first N for N=opt.patch_size
        img_dir = './images/{}/txm_full_stack/'.format(
            opt.phase)  # original TXM image directory
        self.image_paths = sorted(glob.glob(img_dir + '*.tif'))
        if len(self.image_paths) > self.patch_size:
            self.image_paths = self.image_paths[:self.patch_size]

        # Define the default transform function from base transform funtion.
        opt.no_flip = True
        self.transform = get_transform(opt)

        self.length = opt.patch_size
        self.patch_size = opt.patch_size

        # Get x-y indices for sampling
        I_dummy = Image.open(self.image_paths[0])
        self.x_ind = np.random.randint(
            0, I_dummy.size[0] -
            opt.patch_size) if opt.x_ind is None else opt.x_ind
        self.y_ind = np.random.randint(
            0, I_dummy.size[0] -
            opt.patch_size) if opt.y_ind is None else opt.y_ind
    def __init__(self, opt):
        BaseDataset.__init__(self, opt)

        # 3d feature
        self.shapes_info = np.load(os.path.join(opt.dataroot, 'data_info',
                                                opt.dataset_name),
                                   allow_pickle=True)

        self.phase = opt.phase
        self.data_size = len(self.shapes_info)

        self.input_2d_path = os.path.join(opt.dataroot, 'input_data')
        self.input_3d_path = os.path.join(opt.dataroot, 'notexture_pool_data')

        self.query_transform = transforms.Compose([
            transforms.Resize(256),
            transforms.ToTensor(),
            transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                 std=[0.229, 0.224, 0.225])
        ])

        opt.fine_size = 256
        self.fine_size = opt.fine_size
        self.phase = opt.phase
        self.query_random_erase = transforms.Compose(
            [transforms.RandomErasing()])
    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.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'

        # Shuffle dataset for good measure
        random.shuffle(self.A_paths)
        random.shuffle(self.B_paths)

        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
        btoA = self.opt.direction == 'BtoA'
        input_nc = self.opt.output_nc if btoA else self.opt.input_nc  # get the number of channels of input image
        output_nc = self.opt.input_nc if btoA else self.opt.output_nc  # get the number of channels of output image
        self.transform_A = get_transform(self.opt, grayscale=(input_nc == 1))
        self.transform_B = get_transform(self.opt, grayscale=(output_nc == 1))

        if self.opt.backgrounds_path:
            self.backgrounds = sorted(make_dataset(self.opt.backgrounds_path))
    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)

        btoA = self.opt.direction == 'BtoA'
        self.input_nc = self.opt.output_nc if btoA else self.opt.input_nc       # get the number of channels of input image
        output_nc = self.opt.input_nc if btoA else self.opt.output_nc      # get the number of channels of output image

        
        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'
        if os.path.exists(self.dir_A):
            self.A_img_paths, self.A_label_paths = make_labeled_path_dataset(self.dir_A,'/paths.txt', opt.max_dataset_size)   # load images from '/path/to/data/trainA/paths.txt' as well as labels
        else:
            self.A_img_paths, self.A_label_paths = make_labeled_path_dataset(opt.dataroot,'/paths.txt', opt.max_dataset_size)   # load images from '/path/to/data/trainA/paths.txt' as well as labels
        self.A_size = len(self.A_img_paths)  # get the size of dataset A

        if os.path.exists(self.dir_B):
            self.B_img_paths, self.B_label_paths = make_labeled_path_dataset(self.dir_B,'/paths.txt', opt.max_dataset_size)    # load images from '/path/to/data/trainB'
            self.B_size = len(self.B_img_paths)  # get the size of dataset B

        self.transform=get_transform_seg(self.opt, grayscale=(self.input_nc == 1))
        self.transform_noseg=get_transform(self.opt, grayscale=(self.input_nc == 1))
Пример #9
0
    def __init__(self,opt):
        BaseDataset.__init__(self,opt)
        if self.opt.isTrain:
            self.dir_no_label_A = os.path.join(opt.dataroot, opt.phase + 'A')  # create a path '/path/to/unlabeldata/trainA'
            self.dir_no_label_B = os.path.join(opt.dataroot, opt.phase + 'B') # create a path '/path/to/unlabeldata/trainB'
            self.no_label_A_paths = sorted(make_dataset(self.dir_no_label_A, opt.max_dataset_size))   # load images from '/path/to/data/trainA'
            self.no_label_B_paths = sorted(make_dataset(self.dir_no_label_B, opt.max_dataset_size))
            self.A_size = len(self.no_label_A_paths)  # get the size of dataset A
            self.B_size = len(self.no_label_B_paths)
            self.dir_label = os.path.join(opt.dataroot, opt.phase) # get the image directory
            self.label_paths = sorted(make_dataset(self.dir_label, opt.max_dataset_size))  # get image paths
            self.label_size = len(self.label_paths)
            self.no_label_size = len(self.no_label_A_paths)
            assert (self.opt.load_size >= self.opt.crop_size)  # crop_size should be smaller than the size of loaded image

        else:
            self.test=os.path.join(opt.dataroot,opt.phase)
            self.test_paths = sorted(make_dataset(self.test,opt.max_dataset_size))
            self.test_size = len(self.test_paths)

        self.input_nc = self.opt.input_nc
        self.output_nc = self.opt.output_nc
        # self.transform = get_transform(self.opt, grayscale=(self.input_nc == 1))
        self.transform_A = get_transform(self.opt, grayscale=(self.input_nc == 1))
        self.transform_B = get_transform(self.opt, grayscale=(self.output_nc == 1))
Пример #10
0
    def __init__(self, opt):
        BaseDataset.__init__(self, opt)

        # 2d features
        self.shapes_info = os.listdir(os.path.join(opt.dataroot, 'input_data'))
        if '.DS_Store' in self.shapes_info:
            self.shapes_info.remove('.DS_Store')
        self.shapes_info.sort()

        self.phase = opt.phase
        self.data_size = len(self.shapes_info)

        self.input_2d_path = os.path.join(opt.dataroot, 'input_data')
        self.input_3d_path = os.path.join(opt.dataroot, 'render5_black')
        #self.input_3d_path = '/home/dh/zdd/data/test/render5_black/' #os.path.join(opt.dataroot, 'notexture_pool_data')

        self.query_transform = transforms.Compose([
            transforms.Resize(256),
            transforms.ToTensor(),
            transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                 std=[0.229, 0.224, 0.225])
        ])

        opt.fine_size = 256
        self.fine_size = opt.fine_size
        self.phase = opt.phase
Пример #11
0
    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_AB = os.path.join(opt.dataroot,
                                   opt.phase)  # get the image directory
        self.AB_paths = sorted(make_dataset(
            self.dir_AB, opt.max_dataset_size))  # get image paths

        # real_A paths
        self.dir_A = os.path.join(opt.dataroot, opt.phase + '/A')
        # real_B paths
        self.dir_B = os.path.join(opt.dataroot, opt.phase + '/B')

        # real_A
        self.A_paths = sorted(make_dataset(self.dir_A, opt.max_dataset_size))
        # real_B
        self.B_paths = sorted(make_dataset(self.dir_B, opt.max_dataset_size))

        assert (self.opt.load_size >= self.opt.crop_size
                )  # crop_size should be smaller than the size of loaded image
        self.input_nc = self.opt.output_nc if self.opt.direction == 'BtoA' else self.opt.input_nc
        self.output_nc = self.opt.input_nc if self.opt.direction == 'BtoA' else self.opt.output_nc
Пример #12
0
    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'
        #TODO: BB Path
        if 'BBCrop' in self.opt.preprocess:
            self.dir_A_BB = os.path.join(
                opt.dataroot,
                opt.phase + 'A_BB')  #Create a path '/path/to/data/trainA_BB'
            self.A_BB = make_dataset_bb(self.dir_A_BB, 'bbox_A.txt',
                                        opt.max_dataset_size)

        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_size = len(self.A_paths)  # get the size of dataset A
        self.B_size = len(self.B_paths)  # get the size of dataset B
        btoA = self.opt.direction == 'BtoA'
        input_nc = self.opt.output_nc if btoA else self.opt.input_nc  # get the number of channels of input image
        output_nc = self.opt.input_nc if btoA else self.opt.output_nc  # get the number of channels of output image
        self.transform_A = get_transform(self.opt, grayscale=(input_nc == 1))
        self.transform_B = get_transform(self.opt,
                                         grayscale=(output_nc == 1),
                                         B=True)
    def __init__(self, opt):
        """Initialize this dataset class.

        Parameters:
            opt (Option class) -- stores all the experiment flags; needs to be a subclass of BaseOptions

        A few things can be done here.
        - save the options (have been done in BaseDataset)
        - get image paths and meta information of the dataset.
        - define the image transformation.
        """
        # save the option and dataset root
        BaseDataset.__init__(self, opt)
        # define the default transform function. You can use <base_dataset.get_transform>; You can also define your custom transform function
        
        # import torchvision dataset
        if opt.dataset_name == 'CIFAR10':
            from torchvision.datasets import CIFAR10 as torchvisionlib
            self.transform = get_transform(opt, channels=3)
        elif opt.dataset_name == 'CIFAR100':
            from torchvision.datasets import CIFAR100 as torchvisionlib
            self.transform = get_transform(opt, channels=3)
        elif opt.dataset_name == 'FashionMNIST':
            from torchvision.datasets import FashionMNIST as torchvisionlib
            self.transform = get_transform(opt, channels=1)
        else:
            raise ValueError('torchvision_dataset import fault.')

        self.dataload = torchvisionlib(root = opt.download_root,
                                       transform = self.transform,
                                       download = True)
    def __init__(self, opt):
        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_C = os.path.join(
            opt.dataroot, opt.phase + 'C'
        )  # create a path '/path/to/data/trainC' for lowdose image concat

        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.C_paths = sorted(make_dataset(
            self.dir_C,
            opt.max_dataset_size))  # load images from '/path/to/data/trainC'
        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
        btoA = self.opt.direction == 'BtoA'
        self.opt = opt

        n_patches = opt.batch_size * 1000
        n_images = len(self.A_paths)
        if n_images == 0:
            self.repeat = 0
        else:
            self.repeat = max(n_patches // n_images, 1)
Пример #15
0
    def __init__(self, opt):
        """Initialize this dataset class."""
        BaseDataset.__init__(self, opt)
        self.im_suffix = '.png'
        self.image_name = opt.image_name
        if self.opt.phase == 'train':
            self.data_dir = os.path.join(self.opt.dataroot, self.opt.data_name)
            self.AB_paths = make_gesture_dataset(
                os.path.join(self.data_dir, self.opt.image_name),
                os.path.join(self.data_dir, self.opt.train_name),
                self.opt.max_dataset_size, self.opt.phase)
            random.seed(1234)
            random.shuffle(self.AB_paths)
            assert (
                self.opt.load_size >= self.opt.crop_size
            )  # crop_size should be smaller than the size of loaded image
        else:
            self.data_dir = os.path.join(self.opt.dataroot, self.opt.data_name)
            self.AB_paths = make_gesture_dataset(
                os.path.join(self.data_dir, self.opt.image_name),
                os.path.join(self.data_dir, self.opt.test_name),
                self.opt.max_dataset_size, self.opt.phase)

        self.transform = transforms.Compose([
            transforms.ToTensor(),
            transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
        ])
Пример #16
0
    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.A_paths = sorted(make_dataset(
            opt.dataroot, opt.max_dataset_size))  # get image paths
        assert (self.opt.load_size >= self.opt.crop_size
                )  # crop_size should be smaller than the size of loaded image
        self.input_nc = self.opt.output_nc if self.opt.direction == 'BtoA' else self.opt.input_nc
        self.Nw = opt.Nw

        self.A_Images = []

        for i in range(len(self.A_paths)):
            self.A_Images.append(Image.open(self.A_paths[i]).convert('RGB'))
            transform_params = get_params(self.opt, self.A_Images[i].size)
            self.A_transform = get_transform(self.opt,
                                             transform_params,
                                             grayscale=(self.input_nc == 1))
            self.A_Images[i] = self.A_transform(self.A_Images[i])

        self.image_shape = self.A_Images[0].shape
Пример #17
0
    def __init__(self, opt):
        BaseDataset.__init__(self, opt)

        imglistA = 'datasets/list/%s/%s.txt' % (opt.phase + 'A', opt.dataroot)
        imglistB = 'datasets/list/%s/%s.txt' % (opt.phase + 'B', opt.dataroot)

        if not os.path.exists(imglistA) or not os.path.exists(imglistB):
            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.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'
        else:
            self.A_paths = sorted(open(imglistA, 'r').read().splitlines())
            self.B_paths = sorted(open(imglistB, 'r').read().splitlines())

        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
        print("A size:", self.A_size)
        print("B size:", self.B_size)
        btoA = self.opt.direction == 'BtoA'
        self.input_nc = self.opt.output_nc if btoA else self.opt.input_nc  # get the number of channels of input image
        self.output_nc = self.opt.input_nc if btoA else self.opt.output_nc  # get the number of channels of output image

        self.softmaxloc = os.path.join('style_features/styles2_sn_equal/',
                                       '1vgg19_softmax')
Пример #18
0
    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_source = os.path.join(opt.dataroot,
                                       'source')  # get the image directory
        self.dir_target = os.path.join(opt.dataroot,
                                       'target')  # get the image directory
        self.dir_source_mask = os.path.join(
            opt.dataroot, 'source_mask')  # get the image directory
        self.dir_target_mask = os.path.join(opt.dataroot, 'target_mask')  #

        self.source_paths = sorted(
            make_dataset(self.dir_source,
                         opt.max_dataset_size))  # get image paths
        self.target_paths = sorted(
            make_dataset(self.dir_target,
                         opt.max_dataset_size))  # get image paths
        self.source_mask_paths = sorted(
            make_dataset(self.dir_source_mask,
                         opt.max_dataset_size))  # get image paths
        self.target_mask_paths = sorted(
            make_dataset(self.dir_target_mask,
                         opt.max_dataset_size))  # get image paths

        self.target_size = len(self.target_paths)
        assert (self.opt.load_size >= self.opt.crop_size
                )  # crop_size should be smaller than the size of loaded image

        self.input_nc = self.opt.output_nc if self.opt.direction == 'BtoA' else self.opt.input_nc
        self.output_nc = self.opt.input_nc if self.opt.direction == 'BtoA' else self.opt.output_nc
        self.isTrain = opt.isTrain
Пример #19
0
    def __init__(self, opt):
        """Initialize this dataset class.

        Parameters:
            opt (Option class) -- stores all the experiment flags; needs to be a subclass of BaseOptions

        A few things can be done here.
        - save the options (have been done in BaseDataset)
        - get image paths and meta information of the dataset.
        - define the image transformation.
        """
        # save the option and dataset root
        BaseDataset.__init__(self, opt)
        self.dir_images = os.path.join(
            opt.dataroot,
            opt.phase)  # get the image directory (eg ./datasets/CUHK/train)
        self.image_pair_paths = sorted(
            CuhkDataset.make_pairs_dataset(self.dir_images,
                                           opt.max_dataset_size))
        # assert(self.opt.load_size >= self.opt.crop_size)

        # define the default transform function. We can use <base_dataset.get_transform>, or we can define our own custom transform function
        # self.transform = get_transform(opt)

        self.input_nc = 3  # if self.opt.direction == 'photo2sketch' else 1 # TODO: put this back in and get it working
        self.output_nc = 3  #1 if self.opt.direction == 'photo2sketch' else 3
Пример #20
0
    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 = opt.dataroot  # create a path '/path/to/data/trainA'
        # self.dir_B = opt.dataroot  # create a path '/path/to/data/trainB'
        self.dir_A = os.path.join(
            opt.dataroot,
            'ExtremeWeather')  # create a path '/path/to/data/trainA'
        self.dir_B = os.path.join(
            opt.dataroot,
            'slp_resized')  # create a path '/path/to/data/trainB'
        print(opt.dataroot)
        print(opt.phase)
        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_size = len(self.A_paths)  # get the size of dataset A
        self.B_size = len(self.B_paths)  # get the size of dataset B
        btoA = self.opt.direction == 'BtoA'
        input_nc = self.opt.output_nc if btoA else self.opt.input_nc  # get the number of channels of input image
        output_nc = self.opt.input_nc if btoA else self.opt.output_nc  # get the number of channels of output image
        self.transform_A = None
        self.transform_B = None
Пример #21
0
    def __init__(self, opt, bw_threshold=220):
        """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.bw_threshold = bw_threshold
        self.img_path = os.path.join(opt.dataroot, opt.phase, opt.wsi_name)
        self.img = np.load(self.img_path)
        print("WSI image shape", self.img.shape)
        if opt.bgr2rgb:
            self.img = cv2.cvtColor(self.img, cv2.COLOR_BGR2RGB)
        self.img_gray = cv2.cvtColor(self.img, cv2.COLOR_RGB2GRAY)
        self.img_bw = (self.img_gray >= self.bw_threshold).astype(np.uint8)
        self.mask = np.zeros_like(self.img_bw, dtype=np.uint8)
        self.img_new = np.zeros_like(self.img, dtype=np.uint32)
        if opt.dps == 0:
            opt.dps = opt.load_size

        input_nc = self.opt.output_nc if self.opt.direction == 'BtoA' else self.opt.input_nc
        # self.transform = get_transform(opt, grayscale=(input_nc == 1))
        self.transform = get_transform(opt,
                                       grayscale=(input_nc == 1),
                                       to_pil=True)

        self.init_patch_info(opt)
    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_AB = os.path.join(opt.dataroot,
                                   opt.phase)  # get the image directory
        self.AB_paths = [
            e[1] for e in sorted(make_numbering_dataset(
                self.dir_AB, opt.max_dataset_size),
                                 key=lambda idx: idx[0])
        ]
        assert (self.opt.load_size >= self.opt.crop_size
                )  # crop_size should be smaller than the size of loaded image
        self.input_nc = self.opt.output_nc if self.opt.direction == 'BtoA' else self.opt.input_nc
        self.output_nc = self.opt.input_nc if self.opt.direction == 'BtoA' else self.opt.output_nc

        with open(opt.captions, 'rb') as f:
            x = pickle.load(f)
            train_captions, test_captions = x[0], x[1]
            self.captions = train_captions if opt.phase == "train" else test_captions
            self.ixtoword, self.wordtoix = x[2], x[3]
            del x, train_captions, test_captions
            self.n_words = len(self.ixtoword)
            print('Load from: ', opt.captions)
        self.captions_per_image = opt.captions_per_image
        self.text_words_num = opt.text_words_num
    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_image = os.path.join(
            opt.dataroot, "full_images")  # create a path '/path/to/data/train'
        self.dir_class = os.path.join(opt.dataroot, opt.class_csv)

        self.paths = sorted(make_dataset(self.dir_image, opt.max_dataset_size))
        self.size = len(self.paths)  # get the size of dataset A
        input_nc = self.opt.input_nc
        self.transform = get_transform(self.opt, grayscale=(input_nc == 1))

        self.current_classes = 0
        class_to_int = {}

        self.class_dict = {}

        with open(self.dir_class) as file:
            input_file = csv.DictReader(file)
            for row in input_file:
                filename_var = row['filename'].lower()
                class_var = row['class'].lower()
                label = class_to_int.get(class_var)
                if label is None:
                    class_to_int[class_var] = self.current_classes
                    self.current_classes += 1
                    label = class_to_int.get(class_var)
                self.class_dict[filename_var] = label
Пример #24
0
    def __init__(self, opt):
        """Initialize this dataset class.

        Parameters:
            opt (Option class) -- stores all the experiment flags; needs to be a subclass of BaseOptions
        """
        self.label = False
        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.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_size = len(self.A_paths)  # get the size of dataset A
        self.B_size = len(self.B_paths)  # get the size of dataset B

        btoA = self.opt.direction == 'BtoA'
        input_nc = self.opt.output_nc if btoA else self.opt.input_nc  # get the number of channels of input image
        output_nc = self.opt.input_nc if btoA else self.opt.output_nc  # get the number of channels of output image
        self.original_crop = opt.crop_size
        if (not self.label):
            self.transform_B = get_transform(self.opt,
                                             grayscale=(output_nc == 1),
                                             rotate=True)
            self.transform_A = get_transform(self.opt,
                                             grayscale=(input_nc == 1),
                                             rotate=False)
    def __init__(self, opt):
        """Initialize this dataset class.

        Parameters:
            opt -- stores all the experiment flags; needs to be a subclass of BaseOptions
        """
        BaseDataset.__init__(self, opt)
        self.dir_AB = os.path.join(opt.dataroot,
                                   opt.phase)  # get the image directory
        self.AB_paths = sorted(make_dataset(
            self.dir_AB, opt.max_dataset_size))  # get image paths
        assert (self.opt.load_size >= self.opt.crop_size
                )  # crop_size should be smaller than the size of loaded image
        input_nc = self.opt.output_nc if self.opt.direction == 'BtoA' else self.opt.input_nc
        output_nc = self.opt.input_nc if self.opt.direction == 'BtoA' else self.opt.output_nc
        # we manually crop and flip in __getitem__ to make sure we apply the same crop and flip for image A and B
        # we disable the cropping and flipping in the function get_transform
        self.transform_A = get_transform(opt,
                                         grayscale=(input_nc == 1),
                                         crop=False,
                                         flip=False)
        self.transform_B = get_transform(opt,
                                         grayscale=(output_nc == 1),
                                         crop=False,
                                         flip=False)
Пример #26
0
    def __init__(self, opt):
        BaseDataset.__init__(self, opt)

        self.sketch_file = []
        self.sketch_label = []

        self.sketch_path = os.path.join(opt.dataroot, 'sketch')
        self.render_path = os.path.join(opt.dataroot, 'render5')
        self.label_model = json.load(
            open(os.path.join(opt.dataroot, 'data_info', 'label_model.json'),
                 'r'))
        self.level_model = json.load(
            open(os.path.join(opt.dataroot, 'data_info', 'level_model.json'),
                 'r'))
        self.model_file = sorted(self.label_model.keys())
        self.data_size = len(self.label_model)

        self.sketch_transform = transforms.Compose([
            transforms.Resize(256),
            transforms.ToTensor(),
            transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                 std=[0.229, 0.224, 0.225])
        ])
        # render
        self.render_transform = transforms.Compose([
            transforms.Resize(256),
            transforms.ToTensor(),
            transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                 std=[0.229, 0.224, 0.225])
        ])
Пример #27
0
    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)

        first_char = opt.dataset_class[0]
        self.dir = os.path.join(opt.dataroot, 'images', opt.phase, first_char,
                                opt.dataset_class)
        self.paths = sorted(make_dataset(self.dir, float("inf")))

        self.A_paths = [p for p in self.paths if p[-4:] == '.jpg']
        self.B_paths = [p for p in self.paths if p[-7:] == 'seg.jpg']
        #self.B_paths = [p for p in self.paths if os.path.split(p)[-1][4:10] == 'merged']

        assert len(self.A_paths) == len(self.B_paths)
        if opt.max_dataset_size != float("inf"):
            self.A_paths = self.A_paths[:opt.max_dataset_size]
            self.B_paths = self.B_paths[:opt.max_dataset_size]
        self.size = len(self.A_paths)

        btoA = self.opt.direction == 'BtoA'
        self.input_nc = self.opt.output_nc if btoA else self.opt.input_nc  # get the number of channels of input image
        self.output_nc = self.opt.input_nc if btoA else self.opt.output_nc  # get the number of channels of output image
    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.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_size = len(self.A_paths)  # get the size of dataset A
        # self.B_size = len(self.B_paths)  # get the size of dataset B

        # btoA = self.opt.direction == 'BtoA'
        # input_nc = self.opt.output_nc if btoA else self.opt.input_nc       # get the number of channels of input image
        # output_nc = self.opt.input_nc if btoA else self.opt.output_nc      # get the number of channels of output image
        self.transform = get_transform(self.opt,
                                       grayscale=(self.opt.input_nc == 1))
        self.toTensor = transforms.ToTensor()
Пример #29
0
    def __init__(self, opt):
        BaseDataset.__init__(self, opt)
        self.transform = get_transform(opt)
        self.celeba_id = {}
        with open(opt.celeba_id_list) as f:
            for line in f:
                info = line.rstrip('\n').split()
                self.celeba_id[info[0]] = int(info[1])

        self.celeba_hq_id = {}
        with open(opt.celeba_hq_list) as f:
            f.readline() # drop the first line
            for line in f:
                line = line.rstrip('\n').split()
                self.celeba_hq_id[int(line[0])] = self.celeba_id[line[2]]

        celeba_hq_id_unique = np.sort(np.unique(list(self.celeba_hq_id.values())))
        print('There is totally {} people in celeba_hq dataset'.format(len(celeba_hq_id_unique)))
        mapping_old2new = {}
        for index, old_id in enumerate(celeba_hq_id_unique):
            mapping_old2new[old_id] = index

        self.indexlist = []
        for img, id in self.celeba_hq_id.items():
            self.indexlist.append((img, mapping_old2new[id]))
        if opt.isTrain:
            self.indexlist = self.indexlist[:25000]
            random.shuffle(self.indexlist)
        else:
            self.indexlist = self.indexlist[25000:]
Пример #30
0
    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.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_size = len(self.A_paths)  # get the size of dataset A
        self.B_size = len(self.B_paths)  # get the size of dataset B
        btoA = self.opt.direction == "BtoA"
        input_nc = (self.opt.output_nc if btoA else self.opt.input_nc
                    )  # get the number of channels of input image
        output_nc = (self.opt.input_nc if btoA else self.opt.output_nc
                     )  # get the number of channels of output image
        self.transform_A = TransformWithMask(self.opt,
                                             grayscale=(input_nc == 1))
        self.transform_B = TransformWithMask(self.opt,
                                             grayscale=(output_nc == 1))