Beispiel #1
0
    def __init__(self,
                 root,
                 end,
                 transform=None,
                 target_transform=None,
                 is_test=False):
        self.root = root
        self.end = end

        self.transform1 = transforms.Compose([
            transforms.RandomAffine(180, shear=30),
            transforms.RandomPerspective(0.7),
        ])
        self.transform2 = transforms.Compose([
            transforms.ColorJitter(brightness=0.2,
                                   contrast=0.2,
                                   saturation=0.5),
            transforms.RandomHorizontalFlip(),
            transforms.RandomVerticalFlip(),
        ])
        self.transform3 = transforms.Compose([
            transforms.RandomPosterize(3),
        ])
        self.transform = transform
        self.target_transform = target_transform

        if is_test:
            image_sets_file = os.path.join(self.root, "test")
        else:
            image_sets_file = os.path.join(self.root, "train")
        self.ids_hand, self.ids_background = self._read_image_ids(
            image_sets_file)

        self.class_names = ("BACKGROUND", "GUU", "PAA")

        self.class_dict = {
            class_name: i
            for i, class_name in enumerate(self.class_names)
        }
# ~~~~~~~~~~~~
# The :class:`~torchvision.transforms.RandomInvert` transform
# (see also :func:`~torchvision.transforms.functional.invert`)
# randomly inverts the colors of the given image.
inverter = T.RandomInvert()
invertered_imgs = [inverter(orig_img) for _ in range(4)]
plot(invertered_imgs)

####################################
# RandomPosterize
# ~~~~~~~~~~~~~~~
# The :class:`~torchvision.transforms.RandomPosterize` transform
# (see also :func:`~torchvision.transforms.functional.posterize`)
# randomly posterizes the image by reducing the number of bits
# of each color channel.
posterizer = T.RandomPosterize(bits=2)
posterized_imgs = [posterizer(orig_img) for _ in range(4)]
plot(posterized_imgs)

####################################
# RandomSolarize
# ~~~~~~~~~~~~~~
# The :class:`~torchvision.transforms.RandomSolarize` transform
# (see also :func:`~torchvision.transforms.functional.solarize`)
# randomly solarizes the image by inverting all pixel values above
# the threshold.
solarizer = T.RandomSolarize(threshold=192.0)
solarized_imgs = [solarizer(orig_img) for _ in range(4)]
plot(solarized_imgs)

####################################
Beispiel #3
0
all_augs_dict = {
    'gaussian':
    transforms.GaussianBlur(5, sigma=(0.1, 2.0)),
    'jitter':
    transforms.ColorJitter(brightness=0.2,
                           contrast=0.2,
                           saturation=0.2,
                           hue=0.2),
    'perspective':
    transforms.RandomPerspective(distortion_scale=0.3, p=1.0),
    'affine':
    transforms.RandomAffine(degrees=(-10, 10),
                            translate=(0.1, 0.2),
                            scale=(0.9, 1)),
    'posterize':
    transforms.RandomPosterize(bits=2)
}


def createTransform(augmentations):

    augs_to_compose = []

    if 'auto' in augmentations:
        for data_aug in all_augs_dict.keys():
            if np.random.rand() > 0.5:
                action = all_augs_dict[data_aug]
                augs_to_compose.append(action)
    elif 'all' in augmentations:
        for data_aug in all_augs_dict.keys():
            action = all_augs_dict[data_aug]