示例#1
0
def get_test_augmentations(image_size: int = 224,
                           mean: tuple = (0, 0, 0),
                           std: tuple = (1, 1, 1)):
    return A.Compose([
        A.Resize(image_size, image_size),
        A.LongestMaxSize(image_size),
        A.Normalize(mean=mean, std=std),
        A.PadIfNeeded(image_size, image_size, 0),
        ToTensor(),
    ])
示例#2
0
def get_transforms(size: int, scope: str = 'geometric', crop='random'):
    augs = {
        'strong':
        albu.Compose([
            albu.HorizontalFlip(),
            albu.ShiftScaleRotate(shift_limit=0.0,
                                  scale_limit=0.2,
                                  rotate_limit=20,
                                  p=.4),
            albu.ElasticTransform(),
            albu.OpticalDistortion(),
            albu.OneOf([
                albu.CLAHE(clip_limit=2),
                albu.IAASharpen(),
                albu.IAAEmboss(),
                albu.RandomBrightnessContrast(),
                albu.RandomGamma()
            ],
                       p=0.5),
            albu.OneOf([
                albu.RGBShift(),
                albu.HueSaturationValue(),
            ], p=0.5),
        ]),
        'weak':
        albu.Compose([
            albu.HorizontalFlip(),
        ]),
        'geometric':
        albu.OneOf([
            albu.HorizontalFlip(always_apply=True),
            albu.ShiftScaleRotate(always_apply=True),
            #albu.Transpose(always_apply=True),
            albu.OpticalDistortion(always_apply=True),
            albu.ElasticTransform(always_apply=True),
        ])
    }

    aug_fn = augs[scope]
    crop_fn = {
        'random': albu.RandomCrop(size, size, always_apply=True),
        'center': albu.CenterCrop(size, size, always_apply=True)
    }[crop]
    pad = albu.PadIfNeeded(size, size)

    pipeline = albu.Compose([aug_fn, crop_fn, pad],
                            additional_targets={'target': 'image'})

    # pipeline = albu.Compose([aug_fn], additional_targets={'target': 'image'})

    def process(a, b):
        r = pipeline(image=a, target=b)
        return r['image'], r['target']

    return process
示例#3
0
    def __init__(self, segmentation_model, classification_model, face_detector,
                 class_names: List[str] = None,
                 segmentation_threshold: float = 0.3,
                 class_count: int = 5,
                 rules: Dict = None,
                 device: torch.device = None):

        self.segmentation_model = segmentation_model
        self.classification_model = classification_model
        self.face_detector = face_detector

        self.segmentation_preprocess = A.Compose([
            A.LongestMaxSize(max_size=512, p=1.0),
            A.PadIfNeeded(min_height=512, min_width=512, border_mode=cv2.BORDER_CONSTANT, value=0, p=1.0),
        ], p=1.0)
        self.segmentation_norm = A.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225), p=1.0)

        self.denorm = A.Normalize(mean=(-0.485 * 0.229, -0.456 * 0.224, -0.406 * 0.255),
                                  std=(1 / 0.229, 1 / 0.224, 1 / 0.255), p=1.0)

        self.classification_preprocess = A.Compose([
            A.LongestMaxSize(max_size=512, p=1.0),
            A.PadIfNeeded(min_height=512, min_width=512, border_mode=cv2.BORDER_CONSTANT, value=0, p=1.0),
            A.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225), p=1.0)
        ], p=1.0)

        self.threshold = segmentation_threshold

        self.class_count = class_count

        if class_names is None:
            self.class_names = [str(i) for i in range(self.class_count)]
        else:
            self.class_names = class_names

        self.device = device
        if device is None:
            self.device = torch.device('cpu')

        self.rules = rules

        self.iface = None
示例#4
0
 def _valid_enhance(self, sample):
     compose = A.Compose([
         A.PadIfNeeded(self.base_size[0], self.base_size[1], p=1),
         A.CenterCrop(self.crop_size[0], self.crop_size[1], p=1),
         A.Normalize(mean=self.mean, std=self.std, p=1)
     ],
                         additional_targets={
                             'image': 'image',
                             'label': 'mask'
                         })
     return compose(**sample)
示例#5
0
    def __init__(self, corpus_path, target_path, model_path, **kwargs):
        self.char2idx = read_json(kwargs['char2idx_path'])
        self.corpus = read_json(corpus_path)
        self.target = read_json(target_path)
        self.model = Chargrid2D(len(self.corpus) + 1, len(self.target))
        if kwargs['device'] == 'cpu':
            self.model.load_state_dict(
                torch.load(model_path, map_location='cpu'))
        else:
            self.model.load_state_dict(torch.load(model_path))
        self.device = kwargs['device']
        self.model.to(self.device)

        self.size = 512
        self.aug = alb.Compose([
            alb.LongestMaxSize(self.size + 24, interpolation=0),
            alb.PadIfNeeded(self.size + 24,
                            self.size + 24,
                            border_mode=cv2.BORDER_CONSTANT),
            alb.RandomCrop(self.size, self.size, p=0.3),
            alb.Resize(self.size, self.size, 0)
        ])
        self.enc = OneHotEncoder(self.corpus)

        self.all_color = [
            (0, 0, 0),
            (0, 255, 0),
            (0, 0, 255),
            (0, 255, 255),
            (255, 0, 255),
            (255, 255, 0),
            (127, 255, 212),
            (69, 139, 116),
            (131, 139, 139),
            (227, 207, 87),
            (139, 125, 107),
            (138, 43, 226),
            (156, 102, 31),
            (165, 42, 42),
            (255, 64, 64),
            (255, 97, 3),
            (127, 255, 0),
            (238, 18, 137),
            (128, 128, 128),
            (34, 139, 34),
            (139, 105, 20),
            (255, 105, 180),
            (60, 179, 113),
            (139, 0, 0),
            (0, 139, 0),
            (0, 0, 139),
        ]
        self.all_color = self.all_color * (
            len(self.target) // len(self.all_color) + 1)
def get_middle_man_data_aug(mean, standard_deviation, height, width):
    return [
        A.Normalize(mean=mean, std=standard_deviation, always_apply=True, p=1.0),
        A.PadIfNeeded(min_height=height+8, min_width=width+8),
        A.RandomCrop(height, width, always_apply=True, p=1.0),
        A.HorizontalFlip(p=0.5),
        A.GridDistortion (num_steps=5, distort_limit=0.3, interpolation=1, border_mode=4, value=None, mask_value=None, always_apply=False, p=0.5),
        A.ChannelDropout(channel_drop_range=(1, 1), fill_value=0, always_apply=False, p=0.5),
        A.Cutout(num_holes=1, max_h_size=8, max_w_size=8, fill_value=mean, always_apply=False, p=0.5),
        ToTensor()
    ]
示例#7
0
def get_speech_transform(is_train):
    ## border mode 0 : constant padding, border mode 4 : repeats
    if is_train:
        transform = albumentations.Compose([
            albumentations.PadIfNeeded(min_height=128,
                                       min_width=400,
                                       border_mode=0),
            albumentations.RandomCrop(height=128, width=400),
            albumentations.RandomBrightnessContrast(brightness_limit=0.3,
                                                    contrast_limit=0.3,
                                                    p=0.7),
        ])
    else:
        transform = albumentations.Compose([
            albumentations.PadIfNeeded(min_height=128,
                                       min_width=400,
                                       border_mode=0),
            albumentations.CenterCrop(height=128, width=400),
        ])
    return transform
示例#8
0
    def _val_sync_transform(self, sample):
        compose = A.Compose([
            A.PadIfNeeded(self.base_size, self.base_size, p=1),
            A.CenterCrop(self.crop_size, self.crop_size, p=1),
            A.Normalize(),
        ], additional_targets={'image': 'image', 'label': 'mask'})
        sample = compose(**sample)

        sample["image"] = self._transform(sample["image"])
        sample["label"] = self._mask_transform(sample["label"])
        return sample
def get_transform(image_size: int = 512):
    transform = albu.Compose([
        albu.LongestMaxSize(max_size=image_size),
        albu.PadIfNeeded(min_height=image_size,
                         min_width=image_size,
                         value=0,
                         border_mode=cv2.BORDER_CONSTANT),
        albu.Normalize(mean=0, std=1),
        ToTensorV2()
    ])
    return transform
示例#10
0
 def test_transforms(self):
     return A.Compose([
         self.normalizer(),
         A.PadIfNeeded(
             self.hparams.aug_pad_size, 
             self.hparams.aug_pad_size, 
             border_mode=self.hparams.aug_border_mode, 
             p=1.
         ),
         ToTensorV2(),
     ])
示例#11
0
def get_training_augmentation_padding(dataset):
    test_transform = [
        albu.PadIfNeeded(256, 256, cv2.BORDER_CONSTANT, (0, 0, 0)),
    ]

    if dataset == 'brats':
        pass
    else:
        test_transform.extend([albu.Resize(256, 256), albu.Normalize()])

    return albu.Compose(test_transform)
示例#12
0
    def build_train(self):
        train_transforms = A.Compose([
            A.PadIfNeeded(min_height=36, min_width=36),
            A.RandomCrop(height=32, width=32),
            A.HorizontalFlip(),
            A.Normalize(mean=self.mean, std=self.std),
            A.Cutout(num_holes=4),
            AT.ToTensor()
        ])

        return AlbumentationTransforms(train_transforms)
def get_validation_augmentation():
    """Add paddings to make image shape divisible by 32"""
    test_transform = [
        albu.PadIfNeeded(min_height=padheight,
                         min_width=padwidth,
                         border_mode=cv2.BORDER_CONSTANT),
        albu.RandomCrop(height=inputheight,
                        width=inputwidth,
                        always_apply=True),
    ]
    return albu.Compose(test_transform)
示例#14
0
 def __init__(self):
     self.transform = A.Compose([
         A.HorizontalFlip(p=0.5),
         A.Normalize(mean=mean, std=std, always_apply=True),
         A.PadIfNeeded(min_height=32,
                       min_width=32,
                       border_mode=cv2.BORDER_REFLECT_101,
                       p=0.5),
         A.Cutout(num_holes=1, max_h_size=16, max_w_size=16, p=0.5),
         ToTensor(),
     ])
示例#15
0
    def __init__(self, directory: str, use_augmentation: bool, image_height: int = 480, image_width: int = 480):
        self.directory = pathlib.Path(directory)
        self.use_augmentation = use_augmentation
        assert self.directory.exists()
        assert self.directory.is_dir()

        self.image_paths = []
        self.categories = collections.defaultdict(list)

        for image_path in self.directory.rglob('*_img.png'):
            with open(image_path, 'r') as labelme_file:
                self.image_paths += [image_path]
                self.categories['skin'] += [image_path]

        for category, paths in self.categories.items():
            for path in paths:
                logging.debug(f'{category} - {path}')
        self.categories = sorted(list(self.categories.keys()))

        logging.info(f'loaded {len(self)} annotations from {self.directory}')
        logging.info(f'use augmentation: {self.use_augmentation}')
        logging.info(f'categories: {self.categories}')

        aug_transforms = [ToTensor()]
        if self.use_augmentation:
            aug_transforms = [
                alb.HueSaturationValue(always_apply=True),
                alb.RandomBrightnessContrast(always_apply=True),
                alb.HorizontalFlip(),
                alb.RandomGamma(always_apply=True),
                alb.ShiftScaleRotate(shift_limit=0.2, scale_limit=0.2, always_apply=True),
                alb.PadIfNeeded(min_height=image_height, min_width=image_width, always_apply=True),
                alb.RandomCrop(image_height, image_width, always_apply=True),
            ] + aug_transforms
        else:
            aug_transforms = [
                alb.PadIfNeeded(min_height=image_height, min_width=image_width, always_apply=True),
                alb.CenterCrop(image_height, image_width, always_apply=True),
            ] + aug_transforms

        self.transforms = alb.Compose(transforms=aug_transforms)
示例#16
0
def get_canvas_inference_transforms(min_height: int = 1080,
                                    min_width: int = 1920,
                                    divider: int = 32) -> albu.Compose:

    min_height = _check_and_get_new_side(min_height, divider)
    min_width = _check_and_get_new_side(min_width, divider)

    return albu.Compose([
        albu.PadIfNeeded(min_height=min_height, min_width=min_width, p=1.0),
        albu.Normalize(p=1.0)
    ],
                        p=1)
def valid_transform(crop_size: int, pad_height: int,
                    pad_width: int) -> A.Compose:
    return A.Compose([
        A.PadIfNeeded(
            min_height=pad_height,
            min_width=pad_width,
            always_apply=True,
            p=1.,
            border_mode=cv2.BORDER_REPLICATE,
        ),
        post_transform(),
    ])
示例#18
0
def visualize_reid(query, galleries, query_pid, gallery_pids):
    transforms = albu.Compose([
        # albu.SmallestMaxSize(256),
        albu.LongestMaxSize(256),
        # albu.CenterCrop(256, 256),
        albu.PadIfNeeded(min_height=256,
                         min_width=256,
                         border_mode=cv2.BORDER_CONSTANT,
                         value=(150, 150, 150))
        # albu.PadIfNeeded(min_height=256, min_width=256, border_mode=cv2.BORDER_REPLICATE)
    ])
    n = len(galleries)
    plt.figure(figsize=(4 * (n + 1), 5))
    plt.subplot(1, n + 1, 1)
    plt.subplots_adjust(left=0.0,
                        right=1.0,
                        top=1.0,
                        bottom=0.0,
                        wspace=0,
                        hspace=0)
    plt.xticks([])
    plt.yticks([])
    plt.title(query_pid)
    # query = cv2.resize(query, (256, 256))
    query = transforms(image=query)['image']
    plt.imshow(query)
    # plt.gca().add_patch(Rectangle((0, 0), query.shape[1], query.shape[0], edgecolor='w', linewidth=10, fill=False))
    for i in range(len(galleries)):
        g_img = galleries[i]
        # g_img = cv2.resize(g_img, (256,256))
        g_img = transforms(image=g_img)['image']
        g_pid = gallery_pids[i]
        plt.subplot(1, n + 1, i + 2)
        plt.xticks([])
        plt.yticks([])
        plt.title(g_pid)
        plt.imshow(g_img)
        if g_pid == query_pid:
            plt.gca().add_patch(
                Rectangle((0, 0),
                          g_img.shape[1],
                          g_img.shape[0],
                          edgecolor='g',
                          linewidth=10,
                          fill=False))
        else:
            plt.gca().add_patch(
                Rectangle((0, 0),
                          g_img.shape[1],
                          g_img.shape[0],
                          edgecolor='r',
                          linewidth=10,
                          fill=False))
示例#19
0
 def __build_augmentator(self):
     return albu.Compose([
         albu.CropNonEmptyMaskIfExists(height=self.height, width=self.width, p=1.0),
         albu.ShiftScaleRotate(shift_limit=0.15, scale_limit=0.15, p=0.6),
         albu.PadIfNeeded(256, 256),
         albu.OneOf([
             albu.VerticalFlip(p=0.5),
             albu.HorizontalFlip(p=0.5),
         ], p=0.5),
         albu.RandomBrightnessContrast(0.1, 0.1),
         # albu.RandomGamma()
     ], p=self.p)
示例#20
0
    def compose_transform_val(self):
        composed_transforms = A.Compose([
            A.LongestMaxSize(self.base_size, always_apply=True),
            A.PadIfNeeded(self.base_size,
                          self.base_size,
                          always_apply=True,
                          border_mode=cv2.BORDER_CONSTANT),
            A.Normalize(),
            ToTensorV2()
        ])

        return composed_transforms
示例#21
0
def get_valid_transforms(img_size):
    return A.Compose([
        A.PadIfNeeded(img_size, img_size),
        A.CenterCrop(img_size, img_size, p=0.5),
        A.Resize(img_size, img_size, p=1),
        A.Normalize(
            mean=[0.485, 0.456, 0.406],
            std=[0.229, 0.224, 0.225],
            max_pixel_value=255, always_apply=True
        ),
        ToTensorV2()
    ])
示例#22
0
 def paddingIfNeed(self, img):
     img = np.array(img)
     height, width, _ = img.shape
     padded_height, padded_width, _ = img.shape
     if padded_height % 32 != 0:
         padded_height = (padded_height // 32 + 1) * 32
     if padded_width % 32 != 0:
         padded_width = (padded_width // 32 + 1) * 32
     pad = albu.PadIfNeeded(padded_height, padded_width)
     crop = albu.CenterCrop(height, width)
     img = pad(image=img)["image"]
     return img, crop
示例#23
0
 def get_validation_augmentation(self, conf, is_stub=False):
     # Since batch-size in validation is 1, validation could be performed by whole crop-size.
     # To provide pos
     test_transform = [
         alb.HorizontalFlip(p=0.5),
         alb.VerticalFlip(p=0.5),
         alb.RandomRotate90(always_apply=False, p=0.5),
         alb.PadIfNeeded(conf.img_wh_crop,
                         conf.img_wh_crop,
                         always_apply=True,
                         border_mode=0),
         # alb.RandomCrop(height=conf.img_wh_crop, width=conf.img_wh_crop, always_apply=True),
     ]
     if is_stub:
         return alb.Compose([
             alb.PadIfNeeded(conf.img_wh_crop,
                             conf.img_wh_crop,
                             always_apply=True,
                             border_mode=0)
         ])
     return alb.Compose(test_transform)
示例#24
0
    def get_video_load_transform(self):
        """Load videos at double the resoluton as HRNet downsamples.
        If we aren't generating labels, however, we then need to downsample x2."""
        video_height, video_width = self.label_generation_dim

        return A.Compose([
            A.PadIfNeeded(min_height=video_height,
                          min_width=video_width,
                          border_mode=cv2.BORDER_CONSTANT,
                          mask_value=0),
            A.CenterCrop(height=video_height, width=video_width)],
        )
def get_test_transform_mxnet(image_size):
    longest_size = max(image_size[0], image_size[1])
    return A.Compose([
        #Resize(int(config.img_height*1.5),int(config.img_weight*1.5)),
        # CenterCrop(config.img_height,config.img_weight),
        A.LongestMaxSize(longest_size, interpolation=cv2.INTER_CUBIC),
        A.PadIfNeeded(image_size[0],
                      image_size[1],
                      border_mode=cv2.BORDER_CONSTANT,
                      value=0),
        A.Normalize()
    ])
def best_cifar10_train_transforms(stats):
    cutout_fill = sum(stats[0])/3.0
    return alb.Compose([
        alb.Rotate(limit=10, p=0.5), 
        alb.HorizontalFlip(p=0.2),
        #alb.RandomCrop(height=32, width=32, p=1.0),
        alb.Normalize(mean=list(stats[0]), std=list(stats[1])),
        alb.PadIfNeeded(min_height=48, min_width=48, border_mode=cv2.BORDER_REPLICATE, p=1.0),
        alb.Cutout(num_holes=1, max_h_size=16, max_w_size=16, fill_value=cutout_fill),
        alb.CenterCrop (height=32, width=32, always_apply=False, p=1.0),
        alb_torch.transforms.ToTensor()
        ], p=1.0)
示例#27
0
 def __init__(self, base_size, crop_size, fill=0):
     self.base_size = base_size
     self.crop_size = crop_size
     self.base_height = base_size[0]
     self.fill = fill
     self.aug = alb.Compose([
         alb.RandomScale(),
         alb.PadIfNeeded(min_height=base_size[0],
                         min_width=base_size[1],
                         border_mode=cv2.BORDER_REFLECT101),
         alb.RandomCrop(height=base_size[0], width=base_size[1])
     ])
def get_cc_training_augmentation():
    train_transform = [
        A.CropNonEmptyMaskIfExists(height=640, width=640, always_apply=True),
        A.Resize(height=256, width=256),
        A.HorizontalFlip(p=0.5),
        A.VerticalFlip(p=0.5),
        A.PadIfNeeded(min_height=256,
                      min_width=256,
                      always_apply=True,
                      border_mode=0),
    ]
    return A.Compose(train_transform)
示例#29
0
def get_augumentation(phase,
                      width=512,
                      height=512,
                      min_area=0.,
                      min_visibility=0.):
    list_transforms = []
    if phase == 'train':
        list_transforms.extend([
            albu.augmentations.transforms.LongestMaxSize(max_size=width,
                                                         always_apply=True),
            albu.PadIfNeeded(min_height=height,
                             min_width=width,
                             always_apply=True,
                             border_mode=0,
                             value=[0, 0, 0]),
            albu.augmentations.transforms.RandomResizedCrop(height=height,
                                                            width=width,
                                                            p=0.3),
            albu.augmentations.transforms.Flip(),
            albu.augmentations.transforms.Transpose(),
            albu.OneOf([
                albu.RandomBrightnessContrast(brightness_limit=0.5,
                                              contrast_limit=0.4),
                albu.RandomGamma(gamma_limit=(50, 150)),
                albu.NoOp()
            ]),
            albu.OneOf([
                albu.RGBShift(r_shift_limit=20,
                              b_shift_limit=15,
                              g_shift_limit=15),
                albu.HueSaturationValue(hue_shift_limit=5, sat_shift_limit=5),
                albu.NoOp()
            ]),
            albu.CLAHE(p=0.8),
            albu.HorizontalFlip(p=0.5),
            albu.VerticalFlip(p=0.5),
        ])
    if (phase == 'test' or phase == 'valid'):
        list_transforms.extend([albu.Resize(height=height, width=width)])
    list_transforms.extend([
        albu.Normalize(mean=(0.485, 0.456, 0.406),
                       std=(0.229, 0.224, 0.225),
                       p=1),
        ToTensor()
    ])
    if (phase == 'test'):
        return albu.Compose(list_transforms)
    return albu.Compose(list_transforms,
                        bbox_params=albu.BboxParams(
                            format='pascal_voc',
                            min_area=min_area,
                            min_visibility=min_visibility,
                            label_fields=['category_id']))
示例#30
0
    def __init__(self):
        self.trans=A.Compose(
                [
                A.PadIfNeeded(min_height=40, min_width=40),
                A.RandomCrop(32,32),
                A.HorizontalFlip(p=0.5),
                A.CoarseDropout(max_holes=1, max_height=8, max_width=8, min_height=8,
						min_width=8, fill_value=(np.array([0.4914, 0.4822, 0.4465]))*255.0, p=0.75),
                A.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
                ToTensor()
                
                ])