コード例 #1
0
    def gettraintransforms(self, mean, std, p=1):
        # Train Phase transformations

        albumentations_transform = Compose([
            # RandomRotate90(),
            PadIfNeeded(72, 72, border_mode=cv2.BORDER_REFLECT, always_apply=True),
            RandomCrop(64, 64, True),
            Flip(),
            GaussNoise(p=0.8, mean=mean),
            OneOf([
                MotionBlur(p=0.4),
                MedianBlur(blur_limit=3, p=0.2),
                Blur(blur_limit=3, p=0.2),
            ], p=0.4),
            ShiftScaleRotate(shift_limit=0.0625, scale_limit=0.2, rotate_limit=45, p=0.6),
            OneOf([
                OpticalDistortion(p=0.8),
                GridDistortion(p=0.4),
            ], p=0.6),
            HueSaturationValue(hue_shift_limit=20, sat_shift_limit=0.1, val_shift_limit=0.1, p=0.6),
            CoarseDropout(always_apply=True, max_holes=1, min_holes=1, max_height=16, max_width=16,
                          fill_value=(255 * .6), min_height=16, min_width=16),
            Normalize(mean=mean, std=std, always_apply=True),
            pytorch.ToTensorV2(always_apply=True),

        ], p=p)

        return albumentations_transform;
コード例 #2
0
    def __init__(self, root="data/interim", transform=None, train=True, size=512):
        df = pd.read_csv(os.path.join(root, "train_val.csv"))
        df = df[df["is_train"].astype(np.bool) == train]
        self.filenames = [
            os.path.join(root, f"train_data_{size}", path) for path in df["file_path"].values.tolist()]

        # Сheck that all images exist
        assert map(lambda x: pathlib.Path(x).exists(), self.filenames), "Found missing images!"

        self.targets = df["label"].values.tolist()
        self.ar = df["aspect_ratio"].values

        # For each aspect ration in `ar` find closest value from the
        # `_aspect_ratios` and return it's index (group)
        self.group_ids = np.argmin(
            np.abs(self._aspect_ratios.reshape(1, -1) - self.ar.reshape(-1, 1)),
            axis=1
        )

        self.sizes = []
        for group in self.group_ids:
            ar = self._aspect_ratios[group]
            # Resize image to have `size` shape on smaller side and be devidable by 16 on another
            if ar <= 1:
                H = size
                W = int(size / ar) // 8 * 8
            else:
                W = size
                H = int(size * ar) // 8 * 8
            self.sizes.append((H, W))

        logger.info(f"Using sizes {set(self.sizes)} for {'train'if train else 'validation'}")
        self.transform = albu.Compose([albu_pt.ToTensorV2()]) if transform is None else transform
コード例 #3
0
    def __init__(self, root="data/interim", transform=None, size=512, test_type="B"):
        df = pd.read_csv(os.path.join(root, "test_B.csv"))

        self.filenames = [
            os.path.join(root, f"test_data_B_{size}", path) for path in df["file_path"].values.tolist()]

        # Сheck that all images exist
        assert map(lambda x: pathlib.Path(x).exists(), self.filenames), "Found missing images!"

        self.ar = df["aspect_ratio"].values
        self.size = size

        # For each aspect ration in `ar` find closest value from the
        # `_aspect_ratios` and return it's index (group)
        self.group_ids = np.argmin(
            np.abs(self._aspect_ratios.reshape(1, -1) - self.ar.reshape(-1, 1)),
            axis=1
        )

        self.sizes = []
        for group in self.group_ids:
            ar = self._aspect_ratios[group]
            # Resize image to have `size` shape on smaller side and be devidable by 8 on another
            if ar <= 1:
                H = size
                W = int(size / ar) // 8 * 8
            else:
                W = size
                H = int(size / ar) // 8 * 8
            self.sizes.append((H, W))

        self.transform = albu.Compose([albu_pt.ToTensorV2()]) if transform is None else transform
コード例 #4
0
def get_transform(conf_augmentation):
    # For multi-channel mask
    additional_targets = {
        "mask0": "mask",
        "mask1": "mask",
        "mask2": "mask",
        "mask3": "mask",
        "mask4": "mask",
    }

    def get_object(trans):
        if trans.name in {"Compose", "OneOf"}:
            augs_tmp = [get_object(aug) for aug in trans.member]
            return getattr(alb, trans.name)(augs_tmp, **trans.params)

        if hasattr(alb, trans.name):
            return getattr(alb, trans.name)(**trans.params)
        else:
            return eval(trans.name)(**trans.params)

    if conf_augmentation is None:
        augs = list()
    else:
        augs = [get_object(aug) for aug in conf_augmentation]
    augs.append(albp.ToTensorV2())
    return alb.Compose(augs, additional_targets=additional_targets)
コード例 #5
0
    def gettraintransforms(self, mean, std, p=1):
        # Train Phase transformations

        albumentations_transform = Compose([
            RandomRotate90(),
            Flip(),
            GaussNoise(p=0.6, mean=mean),
            OneOf([
                MotionBlur(p=0.2),
                MedianBlur(blur_limit=3, p=0.1),
                Blur(blur_limit=3, p=0.1),
            ],
                  p=0.2),
            ShiftScaleRotate(
                shift_limit=0.0625, scale_limit=0.2, rotate_limit=45, p=0.3),
            OneOf([
                OpticalDistortion(p=0.4),
                GridDistortion(p=0.2),
            ], p=0.3),
            HueSaturationValue(hue_shift_limit=20,
                               sat_shift_limit=0.1,
                               val_shift_limit=0.1,
                               p=0.3),
            Cutout(always_apply=True,
                   num_holes=2,
                   max_h_size=10,
                   max_w_size=10,
                   fill_value=(255 * .6)),
            Normalize(mean=mean, std=std, always_apply=True),
            pytorch.ToTensorV2(always_apply=True),
        ],
                                           p=p)

        return albumentations_transform
コード例 #6
0
    def __init__(self, 
                 dataset_path,
                 phase
                 ):
        
        self.imgs, self.labels = load_dataset(dataset_path)

        if phase == "train":
            self.transform = A.Compose([A.RandomResizedCrop(224, 224, always_apply=True, p=1),
                                        A.HorizontalFlip(p=0.5),
                                        A.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
                                        Atorch.ToTensorV2()])
        elif phase == "validation":
            self.transform = A.Compose([A.Resize(256, 256), 
                            A.CenterCrop(224, 224),
                            A.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
                            Atorch.ToTensorV2()])
コード例 #7
0
def get_aug(aug_type="val", size=512):
    """Return augmentations by type
    Args:
        aug_type (str): one of `val`, `test`, `light`, `medium`
        size (int): final size of the crop
    """

    NORM_TO_TENSOR = albu.Compose(
        [albu.Normalize(mean=MEAN, std=STD),
         albu_pt.ToTensorV2()])

    # CROP_AUG = albu.Compose([albu.RandomCrop(size, size)])

    VAL_AUG = albu.Compose([
        # albu.CenterCrop(size, size),
        NORM_TO_TENSOR
    ])

    LIGHT_AUG = albu.Compose(
        [
            albu.Flip(),
            albu.Cutout(num_holes=12,
                        max_h_size=size // 16,
                        max_w_size=size // 16,
                        fill_value=0,
                        p=0.5), NORM_TO_TENSOR
        ],
        p=1.0,
    )

    # aug from github.com/lyakaap/Landmark2019-1st-and-3rd-Place-Solution/
    HARD_AUG = albu.Compose(
        [
            albu.Flip(p=0.5),
            albu.IAAAffine(rotate=0.2, shear=0.2, mode='constant', p=0.5),
            albu.RandomBrightnessContrast(
                brightness_limit=0.3, contrast_limit=0.3, p=0.5),
            albu.MotionBlur(p=0.5),
            albu.CLAHE(p=0.5),
            albu.Cutout(num_holes=16,
                        max_h_size=size // 16,
                        max_w_size=size // 16,
                        fill_value=0,
                        p=0.5),
            RandomCropThenScaleToOriginalSize(limit=0.3, p=1.0),
            NORM_TO_TENSOR,
        ],
        p=1.0,
    )

    types = {
        "val": VAL_AUG,
        "test": VAL_AUG,
        "light": LIGHT_AUG,
        "hard": HARD_AUG,
    }

    return types[aug_type]
コード例 #8
0
def to_tensor_normalize() -> Transform:
    """
    :return: Albumentations transform [imagenet normalization, to tensor]
    """
    base_transform = albu.Compose([
        albu.Normalize([0.4802, 0.4481, 0.3975], [0.2302, 0.2265, 0.2262],
                       max_pixel_value=255),
        albu_pytorch.ToTensorV2(),
    ])
    return base_transform
コード例 #9
0
 def transform_test(self, sample):
     """No random operation so that test set always keeps the same."""
     composed_transform = albu.Compose([
         # albu.resize(512, 1024, p=1),
         albu.Normalize(mean=(0.485, 0.456, 0.406),
                        std=(0.229, 0.224, 0.225)),
         albupt.ToTensorV2()
     ])
     transformed = composed_transform(image=sample['image'],
                                      mask=sample['mask'])
     return transformed['image'], transformed['mask']
コード例 #10
0
 def transform_train(self, sample):
     composed_transform = albu.Compose([
         albu.VerticalFlip(),
         albu.HorizontalFlip(),
         albu.Normalize(
         ),  # different form torchvision.transforms, dtype shoud be uint8, /255 -mean /std
         albupt.ToTensorV2()
     ])
     augmented = composed_transform(image=sample['image'],
                                    mask=sample['mask'])
     return augmented['image'], augmented['mask']
コード例 #11
0
ファイル: datasets.py プロジェクト: zakajd/metrics-comparison
    def __init__(self, root="data/raw", transform=None):
        self.images_root = os.path.join(root, self._folder, "images")

        # Read file mith DMOS and names
        self.df = pd.read_csv(os.path.join(root, self._folder, self._filename))
        self.df.rename(columns={"dmos": "score"}, inplace=True)
        self.scores = self.df["score"].to_numpy()
        self.df['dist_type'] = self.df['dist_img'].apply(lambda x: x[4:-4])

        if transform is None:
            self.transform = albu_pt.ToTensorV2()
        else:
            self.transform = transform
コード例 #12
0
ファイル: utils.py プロジェクト: ivanpanshin/SupCon-Framework
def build_transforms(second_stage):
    if second_stage:
        train_transforms = A.Compose([
            #A.Flip(),
            #A.Rotate(),
            A.Resize(224, 224),
            A.Normalize(),
            AT.ToTensorV2()
        ])
        valid_transforms = A.Compose(
            [A.Resize(224, 224),
             A.Normalize(), AT.ToTensorV2()])

        transforms_dict = {
            "train_transforms": train_transforms,
            "valid_transforms": valid_transforms,
        }
    else:
        train_transforms = A.Compose([
            A.RandomResizedCrop(height=224, width=224, scale=(0.15, 1.)),
            A.Rotate(),
            A.ColorJitter(0.4, 0.4, 0.4, 0.1, p=0.9),
            A.ToGray(p=0.2),
            A.Normalize(),
            AT.ToTensorV2(),
        ])

        valid_transforms = A.Compose(
            [A.Resize(224, 224),
             A.Normalize(), AT.ToTensorV2()])

        transforms_dict = {
            "train_transforms": train_transforms,
            'valid_transforms': valid_transforms,
        }

    return transforms_dict
コード例 #13
0
ファイル: datasets.py プロジェクト: zakajd/metrics-comparison
    def __init__(self, root="data/raw/tid2013", transform=None):
        self.df = pd.read_csv(os.path.join(root, self._filename),
                              sep=' ',
                              names=['score', 'dist_img'],
                              header=None)

        self.df["ref_img"] = self.df["dist_img"].apply(
            lambda x: (x[:3] + x[-4:]).upper())
        self.df['dist_type'] = self.df['dist_img'].apply(lambda x: x[4:-4])
        self.scores = self.df['score'].to_numpy()
        self.root = root

        if transform is None:
            self.transform = albu_pt.ToTensorV2()
        else:
            self.transform = transform
コード例 #14
0
def get_transform(conf_augmentation, img_height, img_width):
    def get_object(trans):
        if trans.name in {'Compose', 'OneOf'}:
            augs_tmp = [get_object(aug) for aug in trans.member]
            return getattr(alb, trans.name)(augs_tmp, **trans.params)

        if hasattr(alb, trans.name):
            return getattr(alb, trans.name)(**trans.params)
        else:
            return eval(trans.name)(**trans.params)

    if conf_augmentation is None:
        augs = list()
    else:
        augs = [get_object(aug) for aug in conf_augmentation]
    augs.extend([
        alb.Resize(height=img_height, width=img_width),
        albp.ToTensorV2()
    ])
    return alb.Compose(augs)
コード例 #15
0
ファイル: common_aug.py プロジェクト: seefun/TorchUtils
IMAGE_SIZE = 512

train_transform_randaug = albumentations.Compose([
    albumentations.Resize(IMAGE_SIZE, IMAGE_SIZE),
    albumentations.RandomRotate90(
        p=0.5),  # albumentations.SafeRotate(border_mode=1, p=0.5),
    albumentations.Transpose(p=0.5),
    albumentations.Flip(p=0.5),
    randAugment(),
    albumentations.Normalize(),
    albumentations.Cutout(num_holes=8,
                          max_h_size=IMAGE_SIZE // 8,
                          max_w_size=IMAGE_SIZE // 8,
                          fill_value=0,
                          p=0.25),
    AT.ToTensorV2(),
])

train_transform = albumentations.Compose([
    albumentations.Resize(IMAGE_SIZE, IMAGE_SIZE),
    albumentations.RandomRotate90(p=0.5),
    albumentations.Transpose(p=0.5),
    albumentations.Flip(p=0.5),
    albumentations.OneOf([
        albumentations.RandomBrightness(0.2, p=1),
        albumentations.RandomContrast(0.2, p=1),
        albumentations.HueSaturationValue(
            hue_shift_limit=15, sat_shift_limit=20, val_shift_limit=15, p=1),
    ],
                         p=0.5),
    albumentations.OneOf([
コード例 #16
0
 configs = read_parameters()
 device = get_device()
 transform = A.Compose([
                     A.Resize(256, 256, p = 1),
                     A.OneOf([
                                 A.Blur(p = 1),
                                 A.RandomGamma(p = 1),
                                 A.RandomBrightness(p = 1),
                                 A.RandomContrast(p = 1),
                                 ]),
                         A.OneOf([
                                 A.VerticalFlip(p = 1),
                         ]),
                     A.CoarseDropout(p = 0.5),
                     A.Normalize(p = 1),
                     pytorch.ToTensorV2()
 ])
 transform_val_test = A.Compose([
     A.Resize(height = 256, width = 256, p = 1.0),
     A.Normalize(p = 1.0),
     pytorch.ToTensorV2(),
 ])
 KF = StratifiedKFold()
 for train, val in KF.split(df["labels"].values, df["classes"].values):
     x_train, x_val = np.array(df["labels"].values)[train], np.array(df["labels"].values)[val]
     y_train, y_val = np.array(df["classes"].values)[train], np.array(df["classes"].values)[val],
     train_loader, val_loader = generate_train_validation_dataloader(x_train, 
                                                                     y_train,
                                                                     transform,
                                                                     x_val,
                                                                     y_val,
コード例 #17
0
ファイル: dataset.py プロジェクト: vadik6666/hair_seg
    def transform(self, image, mask):
        if self.color_aug:
            transforms = A.Compose([
                A.Resize(self.img_size, self.img_size),
                A.OneOf(
                    [
                        A.IAAAdditiveGaussianNoise(),
                        A.GaussNoise(var_limit=(100, 200)),
                        A.JpegCompression(quality_lower=75, p=0.2),
                    ],
                    p=0.3,
                ),
                A.OneOf(
                    [
                        A.MotionBlur(blur_limit=10, p=0.2),
                        A.MedianBlur(blur_limit=11, p=0.1),
                        A.Blur(blur_limit=11, p=0.1),
                    ],
                    p=0.3,
                ),
                A.ShiftScaleRotate(shift_limit=0,
                                   scale_limit=(-0.1, 0.4),
                                   rotate_limit=5,
                                   p=0.6),
                A.Cutout(num_holes=8, max_h_size=48, max_w_size=48, p=0.3),
                A.OneOf(
                    [
                        A.CLAHE(clip_limit=4),
                        A.IAASharpen(),
                        A.IAAEmboss(),
                        A.RandomBrightnessContrast(),
                    ],
                    p=0.5,
                ),
                A.HueSaturationValue(hue_shift_limit=20,
                                     sat_shift_limit=50,
                                     val_shift_limit=50,
                                     p=0.5),
            ])
        else:
            transforms = A.Compose([
                A.Resize(self.img_size, self.img_size),
            ])

        image_norm_and_to_tensor = A.Compose([
            A.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]),
            AT.ToTensorV2(transpose_mask=True),
        ])
        mask_norm_and_to_tensor = A.Compose([
            A.Normalize(mean=[0.0, 0.0, 0.0], std=[1, 1, 1]),
            AT.ToTensorV2(transpose_mask=True),
        ])

        transform_pair = transforms(image=image, mask=mask)
        transform_image = transform_pair["image"]
        transform_mask = transform_pair["mask"]

        transform_image = image_norm_and_to_tensor(
            image=transform_image)["image"]
        transform_mask = mask_norm_and_to_tensor(image=transform_mask)["image"]

        return transform_image, transform_mask
コード例 #18
0
 def gettesttransforms(self, mean, std):
     # Test Phase transformations
     return Compose([
         Normalize(mean=mean, std=std, always_apply=True),
         pytorch.ToTensorV2(always_apply=True),
     ])
コード例 #19
0
ファイル: mnist.py プロジェクト: ugurgudelek/berries
    def __init__(self):

        self.params = {
            "entity":
            "ugurgudelek",
            "project":
            "sample-project",
            "experiment":
            "mnist",
            "seed":
            42,
            "device":
            torch.device("cpu") if not torch.cuda.is_available() else
            torch.device(type="cuda", index=cuda_ops.get_free_gpu()),
            "resume":
            False,
            "pretrained":
            False,
            "checkpoint": {
                "metric": metrics.Accuracy.__name__.lower(),
                "trigger": lambda new, old: new > old
            },
            "log": {
                "on_epoch": 1,
            },
            "stdout": {
                "verbose": True,
                "on_epoch": 1,
                "on_batch": 10
            },
            "validate_epoch_0":
            True,
            "root":
            Path("./"),
        }

        self.hyperparams = {
            "lr": 0.001,
            "batch_size": 1000,
            "validation_batch_size": 1000,
            "epoch": 50,
        }

        self.dataset = MNIST(
            root="./input/",
            transform=A.Compose([
                #  A.ShiftScaleRotate(shift_limit=0.05,
                #                     scale_limit=0.05,
                #                     rotate_limit=15,
                #                     p=0.5),
                #  A.HorizontalFlip(p=0.5),
                #  A.RandomBrightnessContrast(p=0.2),
                A.Normalize(mean=(0.1307, ), std=(0.3081, )),
                Ap.ToTensorV2(),
            ]),
        )

        # self.dataset.trainset = self.dataset.trainset.get_first_n_sample(200)
        # self.dataset.testset = self.dataset.testset.get_first_n_sample(100)

        self.model = CNN(in_channels=1, out_channels=10, input_dim=(1, 28, 28))

        self.logger = logger(
            project=self.params["project"],
            entity=self.params["entity"],
            config=self.params | self.hyperparams,
        )

        self.logger.watch(self.model)

        self.optimizer = Adam(
            params=self.model.parameters(),
            lr=self.hyperparams.get("lr", 0.001),
            weight_decay=self.hyperparams.get("weight_decay", 0),
        )

        # Decay LR by a factor of 0.1 every 7 epochs
        self.exp_lr_scheduler = lr_scheduler.StepLR(self.optimizer,
                                                    step_size=7,
                                                    gamma=0.1,
                                                    verbose=True)

        self.trainer = CNNTrainer(
            model=self.model,
            criterion=nn.CrossEntropyLoss(reduction="none"),
            optimizer=self.optimizer,
            scheduler=self.exp_lr_scheduler,
            metrics=[torchmetrics.Accuracy()],
            hyperparams=self.hyperparams,
            params=self.params,
            logger=self.logger,
        )
コード例 #20
0
def get_aug(aug_type: str = "val",
            task: str = "denoise",
            dataset: str = "cifar100",
            size: int = 64):
    """
    Args:
        aug_type: {`val`, `test`, `light`, `medium`}
        task: {"denoise", "deblur", "sr"}
        dataset: Name of dataset to get MEAN and STD
        size: final size of the crop
    """

    assert aug_type in ["val", "test", "light", "medium"]

    # Add the same noise for all channels for single-channel images
    mean, std, max_value = MEAN_STD_BY_NAME[dataset]
    if dataset == "medicaldecathlon":
        singlechannel = True
        normalization = albu.NoOp()
        noise = GaussNoiseNoClipping(
            singlechannel,
            var_limit=0.1,
        )
    else:
        singlechannel = False
        normalization = albu.Normalize(mean=mean,
                                       std=std,
                                       max_pixel_value=max_value)
        noise = albu.MultiplicativeNoise(multiplier=(0.75, 1.25),
                                         per_channel=True,
                                         elementwise=True)

    NORM_TO_TENSOR = albu.Compose(
        [normalization, albu_pt.ToTensorV2()],
        additional_targets={"mask": "image"})

    CROP_AUG = albu.Compose([
        albu.PadIfNeeded(size, size),
        albu.RandomResizedCrop(size, size, scale=(0.5, 1.)),
    ])

    if task == "deblur":
        TASK_AUG = albu.OneOf(
            [
                albu.Blur(blur_limit=(3, 5)),
                albu.GaussianBlur(blur_limit=(3, 5)),
                # albu.MotionBlur(),
                # albu.MedianBlur(),
                # albu.GlassBlur(),
            ],
            p=1.0)
    elif task == "denoise":
        # TASK_AUG = noise
        TASK_AUG = albu.OneOf(
            [
                noise,
                # albu.GaussNoise(),
                # GaussNoiseNoClipping(singlechannel, var_limit=0.1 if singlechannel else (20., 50.)),
                # albu.GlassBlur(),
                # albu.ISONoise(),
                # albu.MultiplicativeNoise(),
            ],
            p=1.0)
    elif task == "sr":
        TASK_AUG = albu.Downscale(scale_min=0.5,
                                  scale_max=0.5,
                                  interpolation=cv2.INTER_CUBIC,
                                  always_apply=True)
    else:
        raise ValueError("Name of task must be in {'deblur', 'denosie', 'sr'}")

    VAL_AUG = albu.Compose([
        albu.PadIfNeeded(size, size),
        albu.CenterCrop(size, size),
        TASK_AUG,
        NORM_TO_TENSOR,
    ])

    LIGHT_AUG = albu.Compose([
        CROP_AUG,
        TASK_AUG,
        NORM_TO_TENSOR,
    ])

    MEDIUM_AUG = albu.Compose([
        albu.Flip(),
        albu.RandomRotate90(), CROP_AUG, TASK_AUG, NORM_TO_TENSOR
    ])

    types = {
        "val": VAL_AUG,
        "light": LIGHT_AUG,
        "medium": MEDIUM_AUG,
    }

    return types[aug_type]