Exemplo n.º 1
0
    def __getitem__(self, index):
        """Return a data point and its metadata information.

        Parameters:
            index -- a random integer for data indexing

        Returns:
            a dictionary of data with their names. It usually contains the data itself and its metadata information.

        Step 1: get a random image path: e.g., path = self.image_paths[index]
        Step 2: load your data from the disk: e.g., image = Image.open(path).convert('RGB').
        Step 3: convert your data to a PyTorch tensor. You can use helpder functions such as self.transform. e.g., data = self.transform(image)
        Step 4: return a data point as a dictionary.
        """
        sketch_path, photo_path = self.image_pair_paths[
            index]  # supposed to be strings
        sketch = Image.open(sketch_path).convert(
            'RGB'
        )  # needs to be a tensor (TODO: should be 'L' not 'RGB', but need to debug why that breaks)
        photo = Image.open(photo_path).convert('RGB')  # needs to be a tensor

        transform_params = get_params(self.opt, sketch.size)
        crop_start_x = random.randint(0,
                                      self.opt.load_size - self.opt.crop_size)
        crop_start_y = random.randint(0,
                                      self.opt.load_size - self.opt.crop_size)
        transform_params['crop_pos'] = (crop_start_x, crop_start_y)
        transform_params['flip'] = random.choice([True, False])
        sketch_transform = get_transform(self.opt,
                                         transform_params,
                                         grayscale=(self.input_nc == 1))
        photo_transform = get_transform(self.opt,
                                        transform_params,
                                        grayscale=(self.output_nc == 1))

        sketch = sketch_transform(sketch)
        photo = photo_transform(photo)

        return {
            'sketch': sketch,
            'photo': photo,
            'sketch_path': sketch_path,
            'photo_path': photo_path
        }
Exemplo n.º 2
0
    def __getitem__(self, index):
        # Label Image
        label_path = self.label_paths[index]
        label = Image.open(label_path)
        params = get_params(self.opt, label.size)
        transform_label = get_transform(self.opt,
                                        params,
                                        method=Image.NEAREST,
                                        normalize=False)
        label_tensor = transform_label(label) * 255.0
        label_tensor[label_tensor ==
                     255] = self.opt.label_nc  # 'unknown' is opt.label_nc

        # input image (real images)
        image_path = self.image_paths[index]
        image = Image.open(image_path)
        image = image.convert('RGB')

        transform_image = get_transform(self.opt, params)
        image_tensor = transform_image(image)

        # if using instance maps
        if self.opt.no_instance:
            instance_tensor = 0
        else:
            instance_path = self.instance_paths[index]
            instance = Image.open(instance_path)
            if instance.mode == 'L':
                instance_tensor = transform_label(instance) * 255
                instance_tensor = instance_tensor.long()
            else:
                instance_tensor = transform_label(instance)

        input_dict = {
            'label': label_tensor,
            'instance': instance_tensor,
            'image': image_tensor,
            'path': image_path,
        }

        # Give subclasses a chance to modify the final output
        self.postprocess(input_dict)

        return input_dict
Exemplo n.º 3
0
    def __getitem__(self, index):
        ### input A (label maps)
        A_path = self.A_paths[index]
        A = Image.open(A_path)
        params = get_params(self.opt, A.size)
        '''if self.opt.label_nc == 0:
            transform_A = get_transform(self.opt, params)
            A_tensor = transform_A(A.convert('RGB'))
        #else:'''
        transform_A = get_transform(self.opt, params, method=Image.NEAREST, normalize=False)
        A_tensor = transform_A(A) * 255.0

        B_tensor = inst_tensor = feat_tensor = 0
        ### input B (real images)
        if self.opt.isTrain:
            B_path = self.B_paths[index]
            B = Image.open(B_path).convert('RGB')
            transform_B = get_transform(self.opt, params)
            B_tensor = transform_B(B)


        C_path =  "/scratch/train_numbered/train_B/"+ 'img_081663.png'
        C = Image.open(C_path).convert('RGB')
        transform_C = get_transform(self.opt, params)
        C_tensor = transform_C(C)

        D_Tensor = torch.cat((A_tensor, C_tensor), 0)

        ### if using instance maps
        if not self.opt.no_instance:
            inst_path = self.inst_paths[index]
            inst = Image.open(inst_path)
            inst_tensor = transform_A(inst)

            if self.opt.load_features:
                feat_path = self.feat_paths[index]
                feat = Image.open(feat_path).convert('RGB')
                norm = normalize()
                feat_tensor = norm(transform_A(feat))

        input_dict = {'label': D_Tensor, 'inst': inst_tensor, 'image': B_tensor,
                      'feat': feat_tensor, 'path': A_path}

        return input_dict
Exemplo n.º 4
0
    def __getitem__(self, index):
        # Label Image
        label_path = self.label_paths[index]
        label = Image.open(label_path)
        # print(label_path)
        params = get_params(self.opt, label.size)
        transform_label = get_transform(self.opt,
                                        params,
                                        method=Image.NEAREST,
                                        normalize=False)
        label_tensor = transform_label(label) * 255.0
        label_tensor[label_tensor ==
                     255] = self.opt.label_nc  # 'unknown' is opt.label_nc

        image_path = self.image_paths[index]
        assert self.paths_match(label_path, image_path), \
            "The label_path %s and image_path %s don't match." % \
            (label_path, image_path)
        image = Image.open(image_path)
        image = image.convert('RGB')

        transform_image = get_transform(self.opt, params)
        image_tensor = transform_image(image)

        # if using instance maps
        if self.opt.no_instance:
            instance_tensor = 0
        else:
            instance_path = self.instance_paths[index]
            instance = Image.open(instance_path)
            if instance.mode == 'L':
                instance_tensor = transform_label(instance) * 255
                instance_tensor = instance_tensor.long()
            else:
                instance_tensor = transform_label(instance)
        input_dict = {
            'label': label_tensor,
            'instance': instance_tensor,
            'image': image_tensor,
            'path': image_path,
        }
        self.postprocess(input_dict)

        return input_dict
    def __getitem__(self, index):        
        ### input A (label maps)
        A_path = self.A_paths[index]
        AR_path = self.AR_paths[index]
        A = Image.open(A_path)
        AR = Image.open(AR_path)

        if self.opt.isTrain:
            A_path_inter_1 = self.A_paths_inter_1[index]
            A_path_inter_2 = self.A_paths_inter_2[index]
            A_inter_1 = Image.open(A_path_inter_1)
            A_inter_2 = Image.open(A_path_inter_2)
  
        params = get_params(self.opt, A.size)
        if self.opt.label_nc == 0:
            transform_A = get_transform(self.opt, params)
            A_tensor = transform_A(A.convert('RGB'))
            if self.opt.isTrain:
                A_inter_1_tensor = transform_A(A_inter_1.convert('RGB'))
                A_inter_2_tensor = transform_A(A_inter_2.convert('RGB'))
            AR_tensor = transform_A(AR.convert('RGB'))
        else:
            transform_A = get_transform(self.opt, params, method=Image.NEAREST, normalize=False)
            A_tensor = transform_A(A) * 255.0
            if self.opt.isTrain:
                A_inter_1_tensor = transform_A(A_inter_1) * 255.0
                A_inter_2_tensor = transform_A(A_inter_2) * 255.0
            AR_tensor = transform_A(AR) * 255.0
        B_tensor = inst_tensor = feat_tensor = 0
        ### input B (real images)
        B_path = self.B_paths[index]   
        BR_path = self.BR_paths[index]   
        B = Image.open(B_path).convert('RGB')
        BR = Image.open(BR_path).convert('RGB')
        transform_B = get_transform(self.opt, params)      
        B_tensor = transform_B(B)
        BR_tensor = transform_B(BR)

        if self.opt.isTrain:
            input_dict = {'inter_label_1': A_inter_1_tensor, 'label': A_tensor, 'inter_label_2': A_inter_2_tensor, 'label_ref': AR_tensor, 'image': B_tensor, 'image_ref': BR_tensor, 'path': A_path, 'path_ref': AR_path}
        else:
            input_dict = {'label': A_tensor, 'label_ref': AR_tensor, 'image': B_tensor, 'image_ref': BR_tensor, 'path': A_path, 'path_ref': AR_path}

        return input_dict
    def __getitem__old(self, index):
        """Return a data point and its metadata information.

        Parameters:
            index - - 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 (same as A_paths)
        """
        # read a image given a random integer index
        AB_path = self.AB_paths[index]
        cv2_img = cv2.imread(AB_path, cv2.IMREAD_COLOR | cv2.IMREAD_ANYDEPTH)
        #float_img = cv2_img.astype(float)
        #AB = Image.fromarray(float_img)
        #AB = Image.open(AB_path).convert('RGB')

        AB = Image.open(AB_path).convert('RGB')
        # print(img.getextrema());
        nparr = np.array(AB)
        max_val = np.amax(nparr)
        #
        # split AB image into A and B

        w, h = AB.size
        w2 = int(w / 2)
        A = AB.crop((0, 0, w2, h))
        B = AB.crop((w2, 0, w, h))

        # apply the same transform to both A and B
        transform_params = get_params(self.opt, A.size)
        A_transform = get_transform(self.opt,
                                    transform_params,
                                    grayscale=(self.input_nc == 1))
        B_transform = get_transform(self.opt,
                                    transform_params,
                                    grayscale=(self.output_nc == 1))

        A = A_transform(A)
        B = B_transform(B)

        return {'A': A, 'B': B, 'A_paths': AB_path, 'B_paths': AB_path}
Exemplo n.º 7
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.data_type + 'A')  # create a path '/path/to/data/trainA' : RGB
     self.dir_B = os.path.join(
         opt.dataroot, opt.data_type +
         'B')  # create a path '/path/to/data/trainB': Thermal
     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))
     if 'M' in opt.loss_type:
         if opt.data_type == 'train':
             gps_name = 'AM09_gps.txt'
         else:
             gps_name = 'AM05_gps.txt'
         with open(os.path.join('./datasets', gps_name), 'r') as fp:
             gps = [[
                 float(x.rstrip().split()[0]),
                 float(x.rstrip().split()[1])
             ] for x in fp.readlines()]
         gps = np.array(gps)
         gps = np.float32(gps)
         import sklearn.metrics as skm
         Distance = skm.pairwise_distances(gps, gps)
         D = (Distance > opt.mt_neg)
         self.DM = D
         self.DV = Distance
     else:
         self.DM = None
Exemplo n.º 8
0
    def __getitem__(self, index):
        ### input A (label maps)
        A_path = self.A_paths[index]
        A = Image.open(A_path)
        params = get_params(self.opt, A.size)
        if self.opt.label_nc == 0:
            transform_A = get_transform(self.opt, params)
            A_tensor = transform_A(A.convert('RGB'))
        else:
            transform_A = get_transform(self.opt,
                                        params,
                                        method=Image.NEAREST,
                                        normalize=False)
            A_tensor = transform_A(A) * 255.0

        B_tensor = inst_tensor = feat_tensor = 0
        ### input B (real images)
        #if self.opt.isTrain:# or self.opt.use_encoded_image:
        B_path = self.B_paths[index]
        B = Image.open(B_path).convert('RGB')
        transform_B = get_transform(self.opt, params)
        B_tensor = transform_B(B)

        ### if using instance maps
        if not self.opt.no_instance:
            inst_path = self.inst_paths[index]
            inst = Image.open(inst_path)
            inst_tensor = transform_A(inst)

            if self.opt.load_features:
                feat_path = self.feat_paths[index]
                feat = Image.open(feat_path).convert('RGB')
                norm = normalize()
                feat_tensor = norm(transform_A(feat))

        input_dict = {
            'label': A_tensor,
            'inst': inst_tensor,
            'image': B_tensor,
            'feat': feat_tensor,
            'path': A_path
        }

        return input_dict
Exemplo n.º 9
0
 def load_label(self, path, params):
     label = Image.open(path)
     transform_label = get_transform(self.opt,
                                     params,
                                     method=Image.NEAREST,
                                     normalize=False)
     label_tensor = transform_label(label) * 255.0
     label_tensor[label_tensor ==
                  255] = self.opt.label_nc  # 'unknown' is opt.label_nc
     return label_tensor.unsqueeze(0)
Exemplo n.º 10
0
    def __getitem__(self, index):   # The specific operations in getitem are the key operations of this class
 ### input A (label maps) # Read label map      
        ### input A (label maps)
        A_path = self.A_paths[index]   # Get image path
         # A = Image.open(self.dir_A +'/' + A_path) # First read an image

        A = Image.open(A_path)        
        params = get_params(self.opt, A.size) # According to the input opt ​​and size, return random parameters
        if self.opt.label_nc == 0:
            transform_A = get_transform(self.opt, params)
            tmp = A.convert('RGB')
            A_tensor = transform_A(tmp)
        else:
            transform_A = get_transform(self.opt, params, method=Image.NEAREST, normalize=False)
            A_tensor = transform_A(A) * 255.0 # To preprocess the data, after to_tensor operation, multiply by 255


        B_tensor = inst_tensor = feat_tensor = 0
        ### input B (real images) ### input B (real images) # Then read in the real image B
        if self.opt.isTrain or self.opt.use_encoded_image:
            B_path = self.B_paths[index]  # B = Image.open(self.dir_B + '/' + B_path).convert('RGB') 
            B = Image.open(B_path).convert('RGB')
            transform_B = get_transform(self.opt, params)      
            B_tensor = transform_B(B)

        ### if using instance maps    ### if using instance maps # Then read in the instance, and then it will be processed into an edge map, which is consistent with the description in the paper.
      
        if not self.opt.no_instance: # no_instance default value is true
            inst_path = self.inst_paths[index] # inst = Image.open(self.dir_inst + '/' + inst_path)
            inst = Image.open(inst_path)
            inst_tensor = transform_A(inst) # Same as semantic processing 0-1

            if self.opt.load_features: # Note that the role of self.opt.load_features is to read the pre-calculated features of each category. There are 10 categories in the paper, which are formed by clustering. But the default is not implemented. I personally read the thesis and knew nothing about this part. I will study it later if there is demand.

                feat_path = self.feat_paths[index]            
                feat = Image.open(feat_path).convert('RGB')
                norm = normalize()
                feat_tensor = norm(transform_A(feat))                            

        input_dict = {'label': A_tensor, 'inst': inst_tensor, 'image': B_tensor, 
                      'feat': feat_tensor, 'path': A_path}

        return input_dict # Return a dictionary recording the above read and processed data set.
Exemplo n.º 11
0
 def initialize(self, opt):
     self.opt = opt
     self.root = opt.dataroot
     self.fineSize = opt.fineSize
     self.dir_A = os.path.join(opt.dataroot, opt.phase + 'A')
     self.A_paths = make_dataset(self.dir_A)
     self.A_paths = sorted(self.A_paths)
     self.A_size = len(self.A_paths)
     self.transform = get_transform(opt)
     self.nintrm = opt.nintrm
Exemplo n.º 12
0
    def initialize(self, opt):
        self.opt = opt
        self.root = opt.dataroot
        # self.dir_A = os.path.join(opt.dataroot, 'cyclegan')
        self.dir_A = '/home/jwang/cycada_feature/cyclegan/data/cifar10/images'
        # self.dir_B = os.path.join(opt.dataroot, 'cityscapes/leftImg8bit/test')
        self.dir_B = '/mrtstorage/users/jwang/cyclegta5'

        self.A_paths = make_dataset(self.dir_A)
        self.B_paths = make_dataset(self.dir_B)

        self.A_paths = sorted(self.A_paths)
        self.B_paths = sorted(self.B_paths)
        self.A_size = len(self.A_paths)
        self.B_size = len(self.B_paths)
        # self.transform = get_transform(opt)
        self.A_transform = get_transform(opt)
        opt.resize_or_crop = 'resize_and_crop'
        self.B_transform = get_transform(opt)
Exemplo n.º 13
0
    def __getitem__(self, index):
        self.A, self.B, self.I, seq_idx = self.update_frame_idx(
            self.A_paths, index)
        tG = self.opt.n_frames_G

        A_img = Image.open(self.A_paths[seq_idx][0]).convert('RGB')
        params = get_img_params(self.opt, A_img.size)
        transform_scaleB = get_transform(self.opt, params)
        transform_scaleA = get_transform(
            self.opt, params, method=Image.NEAREST,
            normalize=False) if self.A_is_label else transform_scaleB
        frame_range = list(range(tG)) if self.A is None else [tG - 1]

        for i in frame_range:
            A_path = self.A_paths[seq_idx][self.frame_idx + i]
            Ai = self.get_image(A_path,
                                transform_scaleA,
                                is_label=self.A_is_label)
            self.A = concat_frame(self.A, Ai, tG)

            if self.use_real:
                B_path = self.B_paths[seq_idx][self.frame_idx + i]
                Bi = self.get_image(B_path, transform_scaleB)
                self.B = concat_frame(self.B, Bi, tG)
            else:
                self.B = 0

            if self.opt.use_instance:
                I_path = self.I_paths[seq_idx][self.frame_idx + i]
                Ii = self.get_image(I_path, transform_scaleA) * 255.0
                self.I = concat_frame(self.I, Ii, tG)
            else:
                self.I = 0

        self.frame_idx += 1
        return_list = {
            'A': self.A,
            'B': self.B,
            'inst': self.I,
            'A_path': A_path,
            'change_seq': self.change_seq
        }
        return return_list
    def __getitem__(self, index):
        # Label Image
        label_path = self.label_paths[index]
        label = Image.open(label_path)
        params = get_params(self.opt, label.size)
        transform_label = get_transform(self.opt, params, method=Image.NEAREST, normalize=False)
        label_tensor = transform_label(label) * 255.0
        label_tensor[label_tensor == 255] = self.opt.label_nc  # 'unknown' is opt.label_nc

        # input image (real images)
        image_path = self.image_paths[index]
        assert self.paths_match(
            label_path, image_path
        ), "The label_path %s and image_path %s don't match." % (label_path, image_path)
        image = Image.open(image_path)
        image = image.convert("RGB")

        transform_image = get_transform(self.opt, params)
        image_tensor = transform_image(image)

        # if using instance maps
        if self.opt.no_instance:
            instance_tensor = 0
        else:
            instance_path = self.instance_paths[index]
            instance = Image.open(instance_path)
            if instance.mode == "L":
                instance_tensor = transform_label(instance) * 255
                instance_tensor = instance_tensor.long()
            else:
                instance_tensor = transform_label(instance)

        input_dict = {
            "label": label_tensor,
            "instance": instance_tensor,
            "image": image_tensor,
            "path": image_path,
        }

        # Give subclasses a chance to modify the final output
        self.postprocess(input_dict)

        return input_dict
Exemplo n.º 15
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(
            "/home/robot/andrewk/RobotTeleop/pilot_scripts/canonical_no_shift_priv_info",
            opt.phase + 'canonical')
        self.dir_random = os.path.join(opt.dataroot, opt.phase + 'random')
        #self.dir_real = os.path.join(opt.dataroot, opt.phase + 'real')
        self.dir_seg = os.path.join(opt.dataroot, opt.phase + 'segmentation')
        self.dir_depth = os.path.join(opt.dataroot, opt.phase + 'depth')
        #self.real_states = np.load(os.path.join(opt.dataroot, opt.phase + 'real_states.npy'))

        #self.dir_canonical_pi = '/home/robot/andrewk/RobotTeleop/pilot_scripts/priv_info' # np.load(os.path.join(opt.dataroot, opt.phase + '_canonical_pi.npy'))
        self.dir_canonical_pi = '/home/robot/andrewk/RobotTeleop/pilot_scripts/canonical_no_shift_priv_info'
        self.canonical_pi_paths = [
            pth for pth in os.listdir(self.dir_canonical_pi) if '.npy' in pth
        ]

        self.canonical_paths = sorted(
            make_dataset(self.dir_canonical, opt.max_dataset_size))[::-1]
        self.random_paths = sorted(
            make_dataset(self.dir_random, opt.max_dataset_size))[::-1]
        #self.real_paths = sorted(make_dataset(self.dir_#real, opt.max_dataset_size))
        self.seg_paths = sorted(
            make_dataset(self.dir_seg, opt.max_dataset_size))[::-1]
        self.depth_paths = sorted(
            make_dataset(self.dir_depth, opt.max_dataset_size))[::-1]

        self.canonical_size = len(self.canonical_paths)
        self.random_size = len(self.random_paths)
        #self.real_size = len(self.real_paths)
        self.seg_size = len(self.seg_paths)
        self.depth_size = len(self.depth_paths)
        self.index = 0

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

        self.transform_rgb = get_transform(self.opt, grayscale=False)
        self.transform_grayscale = get_transform(self.opt, grayscale=True)
Exemplo n.º 16
0
def get_parsing_label_tensor(parsing_path, opt):
    label = Image.open(parsing_path)
    params = get_params(opt, label.size)
    transform_label = get_transform(opt,
                                    params,
                                    method=Image.NEAREST,
                                    normalize=False)
    label_tensor = transform_label(label) * 255.0

    return label_tensor
Exemplo n.º 17
0
    def initialize(self, opt):
        self.opt = opt
        self.root = opt.dataroot
        self.dir_P = os.path.join(opt.dataroot, opt.phase)  #person images
        self.dir_K = os.path.join(opt.dataroot, opt.phase + 'K')  #keypoints

        print(self.dir_P, self.dir_K)

        self.init_categories(opt.pairLst)
        self.transform = get_transform(opt)
Exemplo n.º 18
0
    def __init__(self, opt):
        super(UnalignedDataset, self).__init__()
        self.opt = opt
        self.transform = get_transform(opt)

        datapath = os.path.join(opt.dataroot, opt.phase + '*')
        self.dirs = sorted(glob.glob(datapath))

        self.paths = [sorted(make_dataset(d)) for d in self.dirs]
        self.sizes = [len(p) for p in self.paths]
    def __getitem__(self, index):
        """Return a data point and its metadata information.

        Parameters:
            index - - 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 (same as A_paths)
            true_label (str) - - true label of B image
        """
        # read a image given a random integer index
        AB_path = self.AB_paths[index]
        AB = Image.open(AB_path).convert('RGB')
        # split AB image into A and B
        w, h = AB.size
        w2 = int(w / 2)
        A = AB.crop((0, 0, w2, h))
        B = AB.crop((w2, 0, w, h))

        # apply the same transform to both A and B
        transform_params = get_params(self.opt, A.size)
        A_transform = get_transform(self.opt,
                                    transform_params,
                                    grayscale=(self.input_nc == 1))
        B_transform = get_transform(self.opt,
                                    transform_params,
                                    grayscale=(self.output_nc == 1))

        A = A_transform(A)
        B = B_transform(B)

        # print(self.true_labels[AB_path])
        return {
            'A': A,
            'B': B,
            'A_paths': AB_path,
            'B_paths': AB_path,
            'true_label': self.true_labels[AB_path],
            'labels_dict': self.true_labels
        }
Exemplo n.º 20
0
    def __init__(self, opt):
        super(UnalignedDataset, self).__init__()
        self.opt = opt
        self.transform = get_transform(opt)

        datapath = os.path.join(opt.dataroot, opt.phase + '*')
        self.dirs = sorted(glob.glob(datapath))

        self.paths = [sorted(make_dataset(d)) for d in self.dirs]
        self.sizes = [len(p) for p in self.paths]
Exemplo n.º 21
0
    def initialize(self, opt):
        self.opt = opt
        self.root = opt.dataroot
        self.dir_A = os.path.join(opt.dataroot)

        self.A_paths = make_dataset(self.dir_A)

        self.A_paths = sorted(self.A_paths)

        self.transform = get_transform(opt)
Exemplo n.º 22
0
    def __getitem__(self, index):
        """Return a data point and its metadata information.

        Parameters:
            index - - 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 (same as A_paths)
        """

        # Read one image from each set given a random integer index.
        A_path = self.A_paths[index % self.A_size]
        if self.opt.serial_batches:
            index_BC = index % self.BC_size
        else:  # Randomize the index for the second set to avoid fixed pairs (A, (B,C)).
            index_BC = random.randint(0, self.BC_size - 1)
        BC_path = self.BC_paths[index_BC]
        A_img = Image.open(A_path).convert('RGB')
        # Split {B,C} image into B and C.
        BC_img = Image.open(BC_path).convert('RGB')
        w, h = BC_img.size
        w2 = int(w / 2)
        B_img = BC_img.crop((0, 0, w2, h))
        C_img = BC_img.crop((w2, 0, w, h))

        # Apply image transformations, using the same transform both for B and C.
        A_transform = get_transform(self.opt, grayscale=(self.input_nc == 1))
        transform_params_BC = get_params(self.opt, B_img.size)
        B_transform = get_transform(self.opt,
                                    transform_params_BC,
                                    grayscale=(self.output_nc == 1))
        C_transform = get_transform(self.opt,
                                    transform_params_BC,
                                    grayscale=(self.output_nc == 1))

        A = A_transform(A_img)
        B = B_transform(B_img)
        C = C_transform(C_img)

        return {'A': A, 'B': B, 'C': C, 'A_paths': A_path, 'BC_paths': BC_path}
Exemplo n.º 23
0
def classify(model, inputs):
    opt = model['opt']
    model = model['model']
    img = inputs['image']
    # Preprocess
    transform = get_transform(opt, img.size)
    label = transform(img).unsqueeze(0)
    generated = model.inference(label, torch.tensor([0]))
    output = util.tensor2im(generated.data[0])
    return {'image': output}
Exemplo n.º 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
        """
        BaseDataset.__init__(self, opt)
        self.A_paths = sorted(make_dataset(opt.dataroot, opt.max_dataset_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))
    def initialize(self, opt):
        self.opt = opt
        self.root = opt.dataroot
        self.dir_A = os.path.join(opt.dataroot, opt.phase)
        self.dir_B = os.path.join(opt.dataroot, opt.phase + 'annot')

        self.A_paths = sorted(make_dataset(self.dir_A))
        self.B_paths = sorted(make_dataset(self.dir_B))

        self.transform = get_transform(opt)
Exemplo n.º 26
0
    def initialize(self, opt):
        self.opt = opt
        self.root = opt.dataroot
        self.dir_A = os.path.join(opt.dataroot)

        self.A_paths = make_dataset(self.dir_A)

        self.A_paths = sorted(self.A_paths)

        self.transform = get_transform(opt)
    def __init__(self, opt):
        BaseDataset.__init__(self, opt)
        with open(opt.dataroot+'.csv', newline='') as f:
            lines = f.readlines()
        self.A_paths = []
        for line in lines:
            line = line.rstrip('\r\n')
            self.A_paths.append(line)

        self.transform_rgb = get_transform(opt, grayscale=False)
Exemplo n.º 28
0
 def initialize(self, opt):
     self.opt = opt
     self.root = opt.dataroot
     self.dir_P = os.path.join(opt.dataroot,
                               opt.phase + '_in')  #person images
     self.dir_K_in = os.path.join(opt.dataroot,
                                  opt.phase + '_inK')  #keypoints
     self.dir_K_out = os.path.join(opt.dataroot, opt.phase + '_outK')
     self.init_categories(opt.pairLst)
     self.transform = get_transform(opt)
    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_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 = get_transform(opt, grayscale=(input_nc == 1))   # if nc == 1, we convert RGB to grayscale image
        self.transform_B = get_transform(opt, grayscale=(output_nc == 1))  # if nc == 1, we convert RGB to grayscale image
Exemplo n.º 30
0
    def __getitem__(self, index):
        ### input A (label maps)
        edges_path, edges_im = None, None
        A_path = self.A_paths[index]
        A = Image.open(A_path)
        params = get_params(self.opt, A.size, A)
        if self.opt.label_nc == 0:
            transform_A = get_transform(self.opt, params)
            A_img = A.convert('RGB')
            A_tensor = transform_A(A_img)
            if self.opt.apply_binary_threshold == 1:
                ones = torch.ones_like(A_tensor)
                minus_ones = torch.ones_like(A_tensor)*-1
                A_tensor = torch.where(A_tensor>=self.opt.edge_threshold, ones,minus_ones)
            
        else:
            transform_A = get_transform(self.opt, params, method=Image.NEAREST, normalize=False)
            A_tensor = transform_A(A) * 255.0

        B_tensor = inst_tensor = feat_tensor = 0
        ### input B (real images)
        if self.opt.isTrain or self.opt.use_encoded_image:
            B_path = self.B_paths[index]   
            B = Image.open(B_path).convert('RGB')
            transform_B = get_transform(self.opt, params)
            B_tensor = transform_B(B)

        ### if using instance maps        
        if not self.opt.no_instance:
            inst_path = self.inst_paths[index]
            inst = Image.open(inst_path)
            inst_tensor = transform_A(inst)

            if self.opt.load_features:
                feat_path = self.feat_paths[index]            
                feat = Image.open(feat_path).convert('RGB')
                norm = normalize()
                feat_tensor = norm(transform_A(feat))                            

        input_dict = {'label': A_tensor, 'inst': inst_tensor, 'image': B_tensor, 
                      'feat': feat_tensor, 'path': A_path}

        return input_dict
Exemplo n.º 31
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_styled = os.path.join(opt.dataroot)  # create a path '/path/to/data/trainA'
		self.styled_paths = sorted(make_dataset(self.dir_styled, opt.max_dataset_size))   # load images from '/path/to/data/trainA'
		self.styled_size = len(self.styled_paths)  # get the size of dataset A

		self.input_nc = self.opt.input_nc       # get the number of channels of input image
		self.output_nc = self.opt.output_nc      # get the number of channels of output image
		if self.input_nc == 1:
			self.transform = get_transform(self.opt, grayscale=True)
		else:
			self.transform = get_transform(self.opt)

		self.imsize = opt.crop_size
Exemplo n.º 32
0
    def initialize(self, opt):
        self.opt = opt
        self.root = opt.dataroot
        self.dir_P = os.path.join(opt.dataroot, opt.phase)  #person images
        self.dir_K = os.path.join(opt.dataroot, opt.phase + 'K')  #keypoints
        self.dir_SP = opt.dirSem  #semantic
        self.SP_input_nc = opt.SP_input_nc

        self.init_categories(opt.pairLst)
        self.transform = get_transform(opt)
Exemplo n.º 33
0
    def initialize(self, opt):
        self.opt = opt
        self.root = opt.dataroot
        self.dir_A = os.path.join(opt.dataroot, opt.phase + 'A')
        self.dir_B = os.path.join(opt.dataroot, opt.phase + 'B')

        self.A_paths = make_dataset(self.dir_A)
        self.B_paths = make_dataset(self.dir_B)

        self.A_paths = sorted(self.A_paths)
        self.B_paths = sorted(self.B_paths)
        self.A_size = len(self.A_paths)
        self.B_size = len(self.B_paths)
        self.transform = get_transform(opt)
Exemplo n.º 34
0
    def __getitem__(self, index):        
        ### input A (label maps)
        A_path = self.A_paths[index]              
        A = Image.open(A_path)        
        params = get_params(self.opt, A.size)
        if self.opt.label_nc == 0:
            transform_A = get_transform(self.opt, params)
            A_tensor = transform_A(A.convert('RGB'))
        else:
            transform_A = get_transform(self.opt, params, method=Image.NEAREST, normalize=False)
            A_tensor = transform_A(A) * 255.0

        B_tensor = inst_tensor = feat_tensor = 0
        ### input B (real images)
        if self.opt.isTrain:
            B_path = self.B_paths[index]   
            B = Image.open(B_path).convert('RGB')
            transform_B = get_transform(self.opt, params)      
            B_tensor = transform_B(B)

        ### if using instance maps        
        if not self.opt.no_instance:
            inst_path = self.inst_paths[index]
            inst = Image.open(inst_path)
            inst_tensor = transform_A(inst)

            if self.opt.load_features:
                feat_path = self.feat_paths[index]            
                feat = Image.open(feat_path).convert('RGB')
                norm = normalize()
                feat_tensor = norm(transform_A(feat))                            

        input_dict = {'label': A_tensor, 'inst': inst_tensor, 'image': B_tensor, 
                      'feat': feat_tensor, 'path': A_path}

        return input_dict