示例#1
0
 def transform_tr(self, sample):
     if self.table == {}:
         composed_transforms = transforms.Compose([
             tr.RandomHorizontalFlip(),
             tr.RandomScaleCrop(base_size=400, crop_size=400, fill=0),
             #tr.Remap(self.building_table, self.nonbuilding_table, self.channels)
             tr.RandomGaussianBlur(),
             #tr.ConvertFromInts(),
             #tr.PhotometricDistort(),
             tr.Normalize(mean=self.source_dist['mean'],
                          std=self.source_dist['std']),
             tr.ToTensor(),
         ])
     else:
         composed_transforms = transforms.Compose([
             tr.RandomHorizontalFlip(),
             tr.RandomScaleCrop(base_size=400, crop_size=400, fill=0),
             tr.Remap(self.table, self.channels),
             tr.RandomGaussianBlur(),
             #tr.ConvertFromInts(),
             #tr.PhotometricDistort(),
             tr.Normalize(mean=self.source_dist['mean'],
                          std=self.source_dist['std']),
             tr.ToTensor(),
         ])
     return composed_transforms(sample)
 def transform_tr(self, sample):
     train_transforms = list()
     train_transforms.append(tr.Resize(self.cfg.LOAD_SIZE))
     train_transforms.append(tr.RandomScale(self.cfg.RANDOM_SCALE_SIZE))
     train_transforms.append(
         tr.RandomCrop(self.cfg.FINE_SIZE, pad_if_needed=True, fill=0))
     train_transforms.append(tr.RandomRotate())
     train_transforms.append(tr.RandomGaussianBlur())
     train_transforms.append(tr.RandomHorizontalFlip())
     # if self.cfg.TARGET_MODAL == 'lab':
     #     train_transforms.append(tr.RGB2Lab())
     if self.cfg.MULTI_SCALE:
         for item in self.cfg.MULTI_TARGETS:
             self.ms_targets.append(item)
         train_transforms.append(
             tr.MultiScale(size=self.cfg.FINE_SIZE,
                           scale_times=self.cfg.MULTI_SCALE_NUM,
                           ms_targets=self.ms_targets))
     train_transforms.append(tr.ToTensor())
     train_transforms.append(
         tr.Normalize(mean=self.cfg.MEAN,
                      std=self.cfg.STD,
                      ms_targets=self.ms_targets))
     composed_transforms = transforms.Compose(train_transforms)
     return composed_transforms(sample)
示例#3
0
def get_transformations(p):
    """ Return transformations for training and evaluationg """
    from data import custom_transforms as tr

    db_name = p['train_db_name']

    __imagenet_pca = {
        'eigval':
        torch.Tensor([0.2175, 0.0188, 0.0045]),
        'eigvec':
        torch.Tensor([
            [-0.5675, 0.7192, 0.4009],
            [-0.5808, -0.0045, -0.8140],
            [-0.5836, -0.6948, 0.4203],
        ])
    }

    # Training transformations

    # Horizontal flips with probability of 0.5
    transforms_tr = [tr.RandomHorizontalFlip()]

    # Fixed Resize to input resolution
    transforms_tr.extend([
        tr.FixedResize(resolutions={
            x: tuple(p.TRAIN.SCALE)
            for x in p.ALL_TASKS.FLAGVALS
        },
                       flagvals={
                           x: p.ALL_TASKS.FLAGVALS[x]
                           for x in p.ALL_TASKS.FLAGVALS
                       })
    ])

    transforms_tr.extend([tr.AddIgnoreRegions(), tr.ToTensor()])

    transforms_tr.extend(
        [tr.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])

    transforms_tr = transforms.Compose(transforms_tr)

    # Testing (during training transforms)
    transforms_ts = []
    transforms_ts.extend([
        tr.FixedResize(
            resolutions={
                x: tuple(p.TRAIN.SCALE)
                for x in p.ALL_TASKS.FLAGVALS
            },
            flagvals={x: p.TASKS.FLAGVALS[x]
                      for x in p.TASKS.FLAGVALS})
    ])
    transforms_ts.extend([
        tr.AddIgnoreRegions(),
        tr.ToTensor(),
        tr.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
    ])
    transforms_ts = transforms.Compose(transforms_ts)

    return transforms_tr, transforms_ts
示例#4
0
 def transform_tr(self, sample):
     composed_transforms = transforms.Compose([
         tr.RandomHorizontalFlip(),
         tr.RandomScaleCrop(base_size=400, crop_size=400, fill=0),
         tr.Normalize(),
         tr.ToTensor(),
     ])
     return composed_transforms(sample)
 def transform_tr(self, sample):
     train_transforms = list()
     train_transforms.append(tr.RandomHorizontalFlip())
     train_transforms.append(tr.RandomScaleCrop(base_size=self.cfg.LOAD_SIZE, crop_size=self.cfg.FINE_SIZE))
     train_transforms.append(tr.RandomGaussianBlur())
     train_transforms.append(tr.ToTensor())
     train_transforms.append(tr.Normalize(mean=self.cfg.MEAN, std=self.cfg.STD, ms_targets=self.ms_targets))
     composed_transforms = transforms.Compose(train_transforms)
     return composed_transforms(sample)
示例#6
0
 def transform_tr(self, sample):
     composed_transforms = transforms.Compose([
         tr.RandomHorizontalFlip(),
         tr.RandomScaleCrop(base_size=400, crop_size=400, fill=0),
         tr.RandomGaussianBlur(),
         tr.Normalize(mean=self.source_dist['mean'], std=self.source_dist['std']),
         tr.ToTensor(),
     ])
     return composed_transforms(sample)
示例#7
0
 def transform_tr(self, sample):
     composed_transforms = transforms.Compose([
         tr.RandomHorizontalFlip(),
         tr.RandomScaleCrop(base_size=400, crop_size=400, fill=0),
         tr.RandomGaussianBlur(),
         #tr.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)),
         tr.Normalize(),
         tr.ToTensor(),
     ])
     return composed_transforms(sample)
示例#8
0
 def transform_finetune(self, sample):
     composed_transforms = transforms.Compose([
         tr.RandomHorizontalFlip(),
         # tr.RandomCrop(crop_size = 200),
         tr.RandomScaleCrop(base_size=600, crop_size=400, fill=0),
         tr.RandomGaussianBlur(),
         tr.Normalize(),
         tr.ToTensor()
     ])
     return composed_transforms(sample)
def get_transformations(p):
    """ Return transformations for training and evaluationg """
    from data import custom_transforms as tr

    # Training transformations
    if p['train_db_name'] == 'NYUD':
        # Horizontal flips with probability of 0.5
        transforms_tr = [tr.RandomHorizontalFlip()]
        
        # Rotations and scaling
        transforms_tr.extend([tr.ScaleNRotate(rots=[0], scales=[1.0, 1.2, 1.5],
                                              flagvals={x: p.ALL_TASKS.FLAGVALS[x] for x in p.ALL_TASKS.FLAGVALS})])

    elif p['train_db_name'] == 'PASCALContext':
        # Horizontal flips with probability of 0.5
        transforms_tr = [tr.RandomHorizontalFlip()]
        
        # Rotations and scaling
        transforms_tr.extend([tr.ScaleNRotate(rots=(-20, 20), scales=(.75, 1.25),
                                              flagvals={x: p.ALL_TASKS.FLAGVALS[x] for x in p.ALL_TASKS.FLAGVALS})])

    else:
        raise ValueError('Invalid train db name'.format(p['train_db_name']))


    # Fixed Resize to input resolution
    transforms_tr.extend([tr.FixedResize(resolutions={x: tuple(p.TRAIN.SCALE) for x in p.ALL_TASKS.FLAGVALS},
                                         flagvals={x: p.ALL_TASKS.FLAGVALS[x] for x in p.ALL_TASKS.FLAGVALS})])
    transforms_tr.extend([tr.AddIgnoreRegions(), tr.ToTensor(),
                          tr.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])
    transforms_tr = transforms.Compose(transforms_tr)

    
    # Testing (during training transforms)
    transforms_ts = []
    transforms_ts.extend([tr.FixedResize(resolutions={x: tuple(p.TEST.SCALE) for x in p.TASKS.FLAGVALS},
                                         flagvals={x: p.TASKS.FLAGVALS[x] for x in p.TASKS.FLAGVALS})])
    transforms_ts.extend([tr.AddIgnoreRegions(), tr.ToTensor(),
                          tr.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])
    transforms_ts = transforms.Compose(transforms_ts)

    return transforms_tr, transforms_ts
示例#10
0
 def transform_pair_train(self, sample):
     composed_transforms = transforms.Compose([
         tr.RandomHorizontalFlip(),
         tr.RandomScaleCrop(base_size=400, crop_size=400, fill=0),
         #tr.RandomGaussianBlur(),
         tr.HorizontalFlip(),
         tr.GaussianBlur(),
         tr.Normalize(if_pair=True),
         tr.ToTensor(if_pair=True),
     ])
     return composed_transforms(sample)
示例#11
0
 def transform_pair_train(self, sample):
     composed_transforms = transforms.Compose([
         tr.RandomHorizontalFlip(),
         tr.RandomScaleCrop(base_size=400, crop_size=400, fill=0),
         tr.RandomGaussianBlur(),
         tr.ColorDistort(if_pair=True),
         #tr.CropAndResize(if_pair=True),
         tr.Normalize(mean=self.source_dist['mean'],
                      std=self.source_dist['std'],
                      if_pair=True),
         tr.ToTensor(if_pair=True),
     ])
     return composed_transforms(sample)
示例#12
0
    def transform_tr(self, sample):
        composed_transforms = transforms.Compose([
            #tr.FixScaleCrop(400),
            tr.RandomHorizontalFlip(),
            tr.RandomVerticalFlip(),
            tr.RandomRotate(180),
            tr.RandomScaleCrop(base_size=400, crop_size=400, fill=0),
            tr.RandomGaussianBlur(),
            tr.Normalize(mean=self.mean_std[0], std=self.mean_std[1]),
            tr.ToTensor(),
        ])

        #print(self.mean_std)
        return composed_transforms(sample)
示例#13
0
    def transform_tr(self, sample):
        composed_transforms = transforms.Compose([
            tr.RandomHorizontalFlip(),
            tr.RandomVerticalFlip(),
            tr.RandomRotate(180),
            tr.RandomScaleCrop(base_size=400, crop_size=400, fill=0),
            tr.RandomGaussianBlur(),
            # tr.Normalize(),
            tr.Normalize(mean=(0.1420, 0.2116, 0.2823),
                         std=(0.0899, 0.1083, 0.1310)),
            tr.ToTensor(),
        ])

        return composed_transforms(sample)
示例#14
0
    def transform_tr(self, sample):
        composed_transforms = transforms.Compose([
            tr.RandomHorizontalFlip(),
            tr.RandomVerticalFlip(),
            tr.RandomRotate(180),
            tr.RandomScaleCrop(base_size=400, crop_size=400, fill=0),
            tr.RandomGaussianBlur(),
            # tr.Normalize(),
            tr.Normalize(mean=(0.2382, 0.2741, 0.3068),
                         std=(0.1586, 0.1593, 0.1618)),
            tr.ToTensor(),
        ])

        return composed_transforms(sample)
示例#15
0
    def transform_tr(self, sample):
        composed_transforms = transforms.Compose([
            tr.RandomHorizontalFlip(),
            tr.RandomVerticalFlip(),
            tr.RandomRotate(180),
            tr.RandomScaleCrop(base_size=400, crop_size=400, fill=0),
            tr.RandomGaussianBlur(),
            # tr.Normalize(),
            tr.Normalize(mean=(0.3441, 0.3809, 0.4014),
                         std=(0.1883, 0.2039, 0.2119)),
            tr.ToTensor(),
        ])

        return composed_transforms(sample)
示例#16
0
 def transform_tr(self, sample):
     train_transforms = list()
     # train_transforms.append(tr.RandomScale(base_size=self.cfg.LOAD_SIZE, crop_size=self.cfg.FINE_SIZE))
     train_transforms.append(tr.RandomScale(self.cfg.RANDOM_SCALE_SIZE))
     train_transforms.append(tr.Resize(self.cfg.LOAD_SIZE))
     train_transforms.append(
         tr.RandomCrop(self.cfg.FINE_SIZE, pad_if_needed=True, fill=0))
     train_transforms.append(tr.RandomGaussianBlur())
     train_transforms.append(tr.RandomHorizontalFlip())
     train_transforms.append(tr.ToTensor())
     train_transforms.append(
         tr.Normalize(mean=self.cfg.MEAN,
                      std=self.cfg.STD,
                      ms_targets=self.ms_targets))
     composed_transforms = transforms.Compose(train_transforms)
     return composed_transforms(sample)
示例#17
0
def test_mt():
    import torch
    import data.custom_transforms as tr
    import matplotlib.pyplot as plt
    from torchvision import transforms
    transform = transforms.Compose([
        tr.RandomHorizontalFlip(),
        tr.ScaleNRotate(rots=(-2, 2),
                        scales=(.75, 1.25),
                        flagvals={
                            'image': cv2.INTER_CUBIC,
                            'edge': cv2.INTER_NEAREST,
                            'semseg': cv2.INTER_NEAREST,
                            'normals': cv2.INTER_LINEAR,
                            'depth': cv2.INTER_LINEAR
                        }),
        tr.FixedResize(resolutions={
            'image': (512, 512),
            'edge': (512, 512),
            'semseg': (512, 512),
            'normals': (512, 512),
            'depth': (512, 512)
        },
                       flagvals={
                           'image': cv2.INTER_CUBIC,
                           'edge': cv2.INTER_NEAREST,
                           'semseg': cv2.INTER_NEAREST,
                           'normals': cv2.INTER_LINEAR,
                           'depth': cv2.INTER_LINEAR
                       }),
        tr.AddIgnoreRegions(),
        tr.ToTensor()
    ])
    dataset = NYUD_MT(split='train',
                      transform=transform,
                      retname=True,
                      do_edge=True,
                      do_semseg=True,
                      do_normals=True,
                      do_depth=True)

    dataloader = torch.utils.data.DataLoader(dataset,
                                             batch_size=5,
                                             shuffle=False,
                                             num_workers=5)

    for i, sample in enumerate(dataloader):
        print(i)
        for j in range(sample['image'].shape[0]):
            f, ax_arr = plt.subplots(5)
            for k in range(len(ax_arr)):
                ax_arr[k].cla()
            ax_arr[0].imshow(np.transpose(sample['image'][j], (1, 2, 0)))
            ax_arr[1].imshow(sample['edge'][j, 0])
            ax_arr[2].imshow(sample['semseg'][j, 0] / 40)
            ax_arr[3].imshow(np.transpose(sample['normals'][j], (1, 2, 0)))
            max_depth = torch.max(
                sample['depth'][j, 0][sample['depth'][j, 0] != 255]).item()
            ax_arr[4].imshow(
                sample['depth'][j, 0] /
                max_depth)  # Not ideal. Better is to show inverse depth.

            plt.show()
        break
示例#18
0
    def __init__(
            self, root, batch_size,
            aug_types=[],
            sample_size={'train': None, 'val': None, 'test': None},
            seed=None,
            num_workers=4,
            #ext='JPEG',
    ):
        
        ## data augmentation
        tforms_aug = data.get_aug_tforms(aug_types)

        ## default transforms
        tforms_dft_rnd = [
            ctforms.RandomCrop(32, padding=4),
            ctforms.RandomHorizontalFlip(),
            *tforms_aug, # add before ToTensor()
            ctforms.ToTensor(),
            ctforms.Normalize(mean=[0.4914, 0.4822, 0.4465], std=[0.2023, 0.1994, 0.2010]),
        ]
        tforms_dft = [
            #ctforms.RandomCrop(32, padding=4),
            #ctforms.RandomHorizontalFlip(),
            *tforms_aug, # add before ToTensor()
            ctforms.ToTensor(),
            ctforms.Normalize(mean=[0.4914, 0.4822, 0.4465], std=[0.2023, 0.1994, 0.2010]),
        ]
        


        ## transformations for each data split
        tforms_train = tforms_dft_rnd 
        tforms_val = tforms_dft #tforms_dft_rnd 
        tforms_test = tforms_dft #tforms_dft_rnd
        print("[tforms_train] ", tforms_train)
        print("[tforms_val] ", tforms_val)
        print("[tforms_test] ", tforms_test)


        ## load data using pytorch datasets
        train_ds = datasets.CIFAR10(root=root, train=True, download=True, transform=None)
        test_ds = datasets.CIFAR10(root=root, train=False, download=True, transform=None)

        ## get splits
        x_test, y_test = np.array(test_ds.data), np.array(test_ds.targets)
        
        index_rnd = data.get_random_index(len(x_test), len(x_test), seed)
        index_val = index_rnd[:len(index_rnd)//2]
        index_test = index_rnd[len(index_rnd)//2:]

        x_train, y_train = train_ds.data, train_ds.targets
        x_val, y_val = x_test[index_val], y_test[index_val]
        x_test, y_test = x_test[index_test], y_test[index_test]
               
        ## get class name
        classes, class_to_idx = train_ds.classes, train_ds.class_to_idx

        ## create a data loader for training
        ds = Subset(CIFAR10Dataset(x_train, y_train, classes, class_to_idx, transform=tforms.Compose(tforms_train)),
                    data.get_random_index(len(y_train),
                                          len(y_train) if sample_size['train'] is None else sample_size['train'], seed))
        self.train = DataLoader(ds, batch_size=batch_size, shuffle=True, num_workers=num_workers)

        ## create a data loader for validation
        ds = Subset(CIFAR10Dataset(x_val, y_val, classes, class_to_idx, transform=tforms.Compose(tforms_val)),
                    data.get_random_index(len(y_val),
                                          len(y_val) if sample_size['val'] is None else sample_size['val'], seed))
        self.val = DataLoader(ds, batch_size=batch_size, shuffle=True, num_workers=num_workers)

        ## create a data loader for test
        ds = Subset(CIFAR10Dataset(x_test, y_test, classes, class_to_idx, transform=tforms.Compose(tforms_test)),
                    data.get_random_index(len(y_test),
                                          len(y_test) if sample_size['test'] is None else sample_size['test'], seed))
        self.test = DataLoader(ds, batch_size=batch_size, shuffle=True, num_workers=num_workers)
        
        # ## add id-name map
        # self.names = label_to_name(root, self.test.dataset.dataset.classes)

        ## print data statistics
        print(f'#train = {len(self.train.dataset)}, #val = {len(self.val.dataset)}, #test = {len(self.test.dataset)}')
示例#19
0
def test_all():
    import matplotlib.pyplot as plt
    import torch
    import data.custom_transforms as tr
    from torchvision import transforms
    from utils.custom_collate import collate_mil

    transform = transforms.Compose([
        tr.RandomHorizontalFlip(),
        tr.ScaleNRotate(rots=(-90, 90),
                        scales=(1., 1.),
                        flagvals={
                            'image': cv2.INTER_CUBIC,
                            'edge': cv2.INTER_NEAREST,
                            'semseg': cv2.INTER_NEAREST,
                            'human_parts': cv2.INTER_NEAREST,
                            'normals': cv2.INTER_CUBIC,
                            'sal': cv2.INTER_NEAREST
                        }),
        tr.FixedResize(resolutions={
            'image': (512, 512),
            'edge': (512, 512),
            'semseg': (512, 512),
            'human_parts': (512, 512),
            'normals': (512, 512),
            'sal': (512, 512)
        },
                       flagvals={
                           'image': cv2.INTER_CUBIC,
                           'edge': cv2.INTER_NEAREST,
                           'semseg': cv2.INTER_NEAREST,
                           'human_parts': cv2.INTER_NEAREST,
                           'normals': cv2.INTER_CUBIC,
                           'sal': cv2.INTER_NEAREST
                       }),
        tr.AddIgnoreRegions(),
        tr.ToTensor()
    ])
    dataset = PASCALContext(split='train',
                            transform=transform,
                            retname=True,
                            do_edge=True,
                            do_semseg=True,
                            do_human_parts=True,
                            do_normals=True,
                            do_sal=True)

    dataloader = torch.utils.data.DataLoader(dataset,
                                             batch_size=2,
                                             shuffle=False,
                                             num_workers=0)

    for i, sample in enumerate(dataloader):
        print(i)
        for j in range(sample['image'].shape[0]):
            f, ax_arr = plt.subplots(2, 3)

            for k in range(len(ax_arr)):
                for l in range(len(ax_arr[k])):
                    ax_arr[k][l].cla()

            ax_arr[0][0].imshow(np.transpose(sample['image'][j], (1, 2, 0)))
            ax_arr[0][1].imshow(
                np.transpose(sample['edge'][j], (1, 2, 0))[:, :, 0])
            ax_arr[0][2].imshow(
                np.transpose(sample['semseg'][j], (1, 2, 0))[:, :, 0] / 20.)
            ax_arr[1][0].imshow(
                np.transpose(sample['human_parts'][j],
                             (1, 2, 0))[:, :, 0] / 6.)
            ax_arr[1][1].imshow(np.transpose(sample['normals'][j], (1, 2, 0)))
            ax_arr[1][2].imshow(
                np.transpose(sample['sal'][j], (1, 2, 0))[:, :, 0])

            plt.show()
        break