Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def light_augmentations():
return A.Compose([
A.HorizontalFlip(),
A.RandomBrightnessContrast(),
A.OneOf([
A.ShiftScaleRotate(scale_limit=0.05, rotate_limit=15, border_mode=cv2.BORDER_CONSTANT),
A.IAAAffine(),
A.IAAPerspective(),
A.NoOp()
]),
A.HueSaturationValue(),
A.Normalize()
])
def get_transforms(size: int, scope: str = 'weak', 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(),
]),
}
aug_fn = augs[scope]
crop_fn = {'random': albu.RandomCrop(size, size),
'center': albu.CenterCrop(size, size)}[crop]
normalize = albu.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
pipeline = albu.Compose([aug_fn, crop_fn, normalize], additional_targets={'target': 'image'})
def process(a, b):
r = pipeline(image=a, target=b)
return r['image'], r['target']
A.Transpose(),
A.RandomGridShuffle(),
A.ShiftScaleRotate(
scale_limit=0.1, rotate_limit=45, border_mode=cv2.BORDER_CONSTANT, mask_value=0, value=0
),
A.ElasticTransform(border_mode=cv2.BORDER_CONSTANT, alpha_affine=5, mask_value=0, value=0),
# Add occasion blur
A.OneOf([A.GaussianBlur(), A.GaussNoise(), A.IAAAdditiveGaussianNoise(), A.NoOp()]),
# D4 Augmentations
A.OneOf([A.CoarseDropout(), A.MaskDropout(max_objects=10), A.NoOp()]),
# Spatial-preserving augmentations:
A.OneOf(
[
A.RandomBrightnessContrast(brightness_by_max=True),
A.CLAHE(),
A.HueSaturationValue(),
A.RGBShift(),
A.RandomGamma(),
A.NoOp(),
]
),
# Weather effects
A.OneOf([A.RandomFog(fog_coef_lower=0.01, fog_coef_upper=0.3, p=0.1), A.NoOp()]),
A.Normalize(),
]
A.ISONoise(p=0.5),
# Brightness/contrast augmentations
A.OneOf([
A.RandomBrightnessContrast(brightness_limit=0.5,
contrast_limit=0.4),
IndependentRandomBrightnessContrast(brightness_limit=0.25,
contrast_limit=0.24),
A.RandomGamma(gamma_limit=(50, 150)),
A.NoOp()
]),
A.OneOf([
A.RGBShift(r_shift_limit=40, b_shift_limit=30, g_shift_limit=30),
A.HueSaturationValue(hue_shift_limit=10,
sat_shift_limit=10),
A.ToGray(p=0.2),
A.NoOp()
]),
A.ChannelDropout(),
A.RandomGridShuffle(p=0.3),
# D4
A.Compose([
A.RandomRotate90(),
A.Transpose()
])
def get_transform(train: bool) -> Callable:
train_initial_size = 2048
crop_min_max_height = (400, 533)
crop_width = 512
crop_height = 384
if train:
transforms = [
A.LongestMaxSize(max_size=train_initial_size),
A.RandomSizedCrop(
min_max_height=crop_min_max_height,
width=crop_width,
height=crop_height,
w2h_ratio=crop_width / crop_height,
),
A.HueSaturationValue(
hue_shift_limit=7,
sat_shift_limit=10,
val_shift_limit=10,
),
A.RandomBrightnessContrast(),
A.RandomGamma(),
]
else:
test_size = int(train_initial_size *
crop_height / np.mean(crop_min_max_height))
print(f'Test image max size {test_size} px')
transforms = [
A.LongestMaxSize(max_size=test_size),
]
transforms.extend([
ToTensor(),
) -> Callable:
train_initial_size = 3072 # this value should not matter any more?
crop_ratio = crop_height / test_height
crop_min_max_height = tuple(
int(train_initial_size * crop_ratio * (1 + sign * scale_aug))
for sign in [-1, 1])
if train:
transforms = [
LongestMaxSizeRandomSizedCrop(
max_size=train_initial_size,
min_max_height=crop_min_max_height,
width=crop_width,
height=crop_height,
w2h_ratio=crop_width / crop_height,
),
A.HueSaturationValue(
hue_shift_limit=color_hue_aug,
sat_shift_limit=color_sat_aug,
val_shift_limit=color_val_aug,
),
A.RandomBrightnessContrast(),
A.RandomGamma(),
]
else:
transforms = [
A.LongestMaxSize(max_size=test_height),
]
if normalize:
transforms.append(A.Normalize())
transforms.extend([
ToTensor(),
])