Exemplo n.º 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)
Exemplo n.º 2
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
Exemplo n.º 3
0
def get_transformations(p):
    """ Return transformations for training and evaluationg """
    from data import custom_transforms as tr

    # Training transformations

    # 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
                        })
    ])
    # 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
 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)
Exemplo n.º 5
0
    def transform_val(self, sample):

        composed_transforms = transforms.Compose([
            tr.FixScaleCrop(400),
            tr.Normalize(mean=self.source_dist['mean'], std=self.source_dist['std']),
            tr.ToTensor(),
        ])
        return composed_transforms(sample)
Exemplo n.º 6
0
    def transform_ts(self, sample):

        composed_transforms = transforms.Compose([
            tr.FixedResize(size=400),
            tr.Normalize(mean=self.source_dist['mean'], std=self.source_dist['std']),
            tr.ToTensor(),
        ])
        return composed_transforms(sample)
Exemplo n.º 7
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)
Exemplo n.º 8
0
 def transform_tr(self, sample):
     composed_transforms = transforms.Compose([
         # tr.RandomHorizontalFlip(),
         tr.RandomScaleCrop(base_size=self.args.base_size, crop_size=self.args.crop_size, fill=255),
         # tr.RandomGaussianBlur(),
         tr.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)),
         tr.ToTensor()])
     return composed_transforms(sample)
Exemplo n.º 9
0
    def transform_ts(self, sample):

        composed_transforms = transforms.Compose([
            tr.FixedResize(size=self.args.crop_size),
            tr.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)),
            tr.ToTensor()])

        return composed_transforms(sample)
 def transform_val(self, sample):
     val_transforms = list()
     val_transforms.append(tr.Resize(self.cfg.LOAD_SIZE))
     if self.cfg.MULTI_SCALE:
         val_transforms.append(tr.MultiScale(size=self.cfg.FINE_SIZE,scale_times=self.cfg.MULTI_SCALE_NUM, ms_targets=self.ms_targets))
     val_transforms.append(tr.ToTensor())
     val_transforms.append(tr.Normalize(mean=self.cfg.MEAN, std=self.cfg.STD, ms_targets=self.ms_targets))
     composed_transforms = transforms.Compose(val_transforms)
Exemplo n.º 11
0
    def transform_val(self, sample):

        composed_transforms = transforms.Compose([
            tr.FixScaleCrop(400),
            tr.Normalize(),
            tr.ToTensor(),
        ])
        return composed_transforms(sample)
Exemplo n.º 12
0
    def transform_ts(self, sample):

        composed_transforms = transforms.Compose([
            tr.FixedResize(size=400),
            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)
Exemplo n.º 14
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)
Exemplo n.º 15
0
    def transform_val(self, sample):

        composed_transforms = transforms.Compose([
            tr.FixScaleCrop(400),
            tr.Normalize(mean=self.mean_std[0], std=self.mean_std[1]),
            tr.ToTensor()
        ])

        return composed_transforms(sample)
Exemplo n.º 16
0
    def transform_val(self, sample):

        composed_transforms = transforms.Compose([
            tr.FixScaleCrop(400),
            #tr.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)),
            tr.Normalize(),
            tr.ToTensor(),
        ])
        return composed_transforms(sample)
Exemplo n.º 17
0
    def transform_ts(self, sample):

        composed_transforms = transforms.Compose([
            tr.FixedResize(size=400),
            tr.Normalize(mean=self.mean_std[0],
                         std=self.mean_std[1]),  # tr.Normalize(),
            tr.ToTensor()
        ])

        return composed_transforms(sample)
Exemplo n.º 18
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)
Exemplo n.º 19
0
    def transform_pair_val(self, sample):

        composed_transforms = transforms.Compose([
            tr.FixScaleCrop(400),
            tr.HorizontalFlip(),
            tr.GaussianBlur(),
            tr.Normalize(mean=self.source_dist['mean'], std=self.source_dist['std'], if_pair=True),
            tr.ToTensor(if_pair=True),
        ])
        return composed_transforms(sample)
Exemplo n.º 20
0
    def __init__(self, args):

        root = os.path.join('data', args.src.lower())
        
        ## default transforms
        tforms_dft = [
            ctforms.Grayscale(3),
            ctforms.ToTensor(),
        ]
        

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


        ## load data using pytorch datasets
        train_ds = datasets.MNIST(root=root, train=True, download=True, transform=None)
        test_ds = datasets.MNIST(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), args.seed)
        index_val = index_rnd[:len(index_rnd)//2] # split by half
        index_test = index_rnd[len(index_rnd)//2:]

        x_train, y_train = np.array(train_ds.data), np.array(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(MNISTDataset(x_train, y_train, classes, class_to_idx, transform=tforms.Compose(tforms_train)),
                    data.get_random_index(len(y_train), len(y_train), args.seed))
        self.train = DataLoader(ds, batch_size=args.batch_size, shuffle=True, num_workers=args.n_workers)

        ## create a data loader for validation
        ds = Subset(MNISTDataset(x_val, y_val, classes, class_to_idx, transform=tforms.Compose(tforms_val)),
                    data.get_random_index(len(y_val), len(y_val), args.seed))
        self.val = DataLoader(ds, batch_size=args.batch_size, shuffle=True, num_workers=args.n_workers)

        ## create a data loader for test
        ds = Subset(MNISTDataset(x_test, y_test, classes, class_to_idx, transform=tforms.Compose(tforms_test)),
                    data.get_random_index(len(y_test), len(y_test), args.seed))
        self.test = DataLoader(ds, batch_size=args.batch_size, shuffle=True, num_workers=args.n_workers)
        
        ## print data statistics
        print(f'#train = {len(self.train.dataset)}, #val = {len(self.val.dataset)}, #test = {len(self.test.dataset)}')
Exemplo n.º 21
0
    def transform_ts(self, sample):

        composed_transforms = transforms.Compose([
            tr.FixedResize(size=400),
            tr.Normalize(mean=(0.3441, 0.3809, 0.4014),
                         std=(0.1883, 0.2039, 0.2119)),
            # tr.Normalize(),
            tr.ToTensor()
        ])

        return composed_transforms(sample)
Exemplo n.º 22
0
    def transform_ts(self, sample):

        composed_transforms = transforms.Compose([
            tr.FixedResize(size=400),
            tr.Normalize(mean=(0.1420, 0.2116, 0.2823),
                         std=(0.0899, 0.1083, 0.1310)),
            # tr.Normalize(),
            tr.ToTensor()
        ])

        return composed_transforms(sample)
Exemplo n.º 23
0
    def transform_ts(self, sample):
        sample = tr.Normalize()(sample)
        sample = tr.ToTensor()(sample)
        # composed_transforms = transforms.Compose([
        #     tr.FixedResize(size=400),
        #     #tr.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)),
        #     tr.Normalize(),
        #     tr.ToTensor()])

        # return composed_transforms(sample)
        return sample
Exemplo n.º 24
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)
Exemplo n.º 25
0
    def transform_ts(self, sample):

        composed_transforms = transforms.Compose([
            tr.FixedResize(size=400),
            tr.Normalize(mean=(0.2382, 0.2741, 0.3068),
                         std=(0.1586, 0.1593, 0.1618)),
            # tr.Normalize(),
            tr.ToTensor()
        ])

        return composed_transforms(sample)
Exemplo n.º 26
0
    def transform_ts(self, sample):

        composed_transforms = transforms.Compose([
            tr.FixedResize(size=400),
            tr.Normalize(mean=(0.2709, 0.3400, 0.3707),
                         std=(0.1403, 0.1570, 0.1658)),
            # tr.Normalize(),
            tr.ToTensor()
        ])

        return composed_transforms(sample)
    def transform_val(self, sample):
        val_transforms = list()
        val_transforms.append(tr.FixScaleCrop(crop_size=self.cfg.FINE_SIZE))
        val_transforms.append(tr.ToTensor())
        val_transforms.append(
            tr.Normalize(mean=self.cfg.MEAN,
                         std=self.cfg.STD,
                         ms_targets=self.ms_targets))
        composed_transforms = transforms.Compose(val_transforms)

        return composed_transforms(sample)
Exemplo n.º 28
0
    def transform_pair_val(self, sample):

        composed_transforms = transforms.Compose([
            tr.FixScaleCrop(400),
            #tr.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)),
            tr.HorizontalFlip(),
            tr.GaussianBlur(),
            tr.Normalize(if_pair=True),
            tr.ToTensor(if_pair=True),
        ])
        return composed_transforms(sample)
Exemplo n.º 29
0
    def transform_tr(self, sample):
        sample = tr.Normalize()(sample)
        sample = tr.ToTensor()(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)
        return sample
Exemplo n.º 30
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)