Spaces:
Sleeping
Sleeping
File size: 6,202 Bytes
add142e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | import cv2
import albumentations as albu
from albumentations.pytorch import ToTensorV2
def blur_transforms(p=0.5, blur_limit=5):
"""
Applies MotionBlur or GaussianBlur random with a probability p.
Args:
p (float, optional): probability. Defaults to 0.5.
blur_limit (int, optional): Blur intensity limit. Defaults to 5.
Returns:
albumentation transforms: transforms.
"""
return albu.OneOf(
[
albu.MotionBlur(always_apply=True),
albu.GaussianBlur(always_apply=True),
],
p=p,
)
def color_transforms(p=0.5):
"""
Applies RandomGamma or RandomBrightnessContrast random with a probability p.
Args:
p (float, optional): probability. Defaults to 0.5.
Returns:
albumentation transforms: transforms.
"""
return albu.OneOf(
[
albu.RandomGamma(gamma_limit=(50, 150), always_apply=True),
albu.RandomBrightnessContrast(
brightness_limit=0.1, contrast_limit=0.2, always_apply=True
),
],
p=p,
)
def distortion_transforms(p=0.5):
"""
Applies ElasticTransform with a probability p.
Args:
p (float, optional): probability. Defaults to 0.5.
Returns:
albumentation transforms: transforms.
"""
return albu.OneOf(
[
albu.ElasticTransform(
alpha=1,
sigma=5,
alpha_affine=10,
border_mode=cv2.BORDER_CONSTANT,
always_apply=True,
)
],
p=p,
)
def get_transfos(
augment=True,
resize=None,
crop=False,
mean=0,
std=1,
strength=1,
use_keypoints=False,
):
"""
Returns transformations.
Args:
augment (bool, optional): Whether to apply augmentations. Defaults to True.
resize (tuple, optional): Target image size as a tuple (height, width). Defaults to None.
crop (bool, optional): Whether to perform center cropping. Defaults to False.
mean (np array, optional): Mean for normalization. Defaults to 0.
std (np array, optional): Standard deviation for normalization. Defaults to 1.
strength (int, optional): Augmentation strength level. Defaults to 1.
use_keypoints (bool, optional): Whether to augment keypoints. Default to False.
Returns:
albumentation transforms: Transforms.
"""
if resize is None:
resize_aug = []
elif not crop:
resize_aug = [albu.Resize(resize[0], resize[1])]
else:
resize_aug = [
albu.Compose(
[
albu.LongestMaxSize(int(resize[0] / 0.4)),
albu.PadIfNeeded(resize[0], resize[1], border_mode=0),
albu.CenterCrop(resize[0], resize[1]),
]
)
]
normalizer = albu.Compose(
resize_aug
+ [
ToTensorV2(),
],
p=1,
)
if augment:
if strength == 0:
augs = [
color_transforms(p=0.25),
]
elif strength == 1:
augs = [
# albu.HorizontalFlip(p=0.5),
albu.ShiftScaleRotate(
scale_limit=0.1,
shift_limit=0.0,
rotate_limit=10,
p=0.5,
),
color_transforms(p=0.5),
]
elif strength == 2:
augs = [
# albu.HorizontalFlip(p=0.5),
albu.ShiftScaleRotate(
scale_limit=0.1,
shift_limit=0.0,
rotate_limit=20,
p=0.5,
),
color_transforms(p=0.5),
]
elif strength == 3:
augs = [
# albu.HorizontalFlip(p=0.5),
albu.ShiftScaleRotate(
scale_limit=0.2,
shift_limit=0.0,
rotate_limit=30,
p=0.75,
),
color_transforms(p=0.5),
blur_transforms(p=0.25),
distortion_transforms(p=0.25),
]
elif strength == 4:
augs = [
# albu.HorizontalFlip(p=0.5),
albu.ShiftScaleRotate(
scale_limit=0.0,
shift_limit=0.0,
rotate_limit=45,
p=0.75,
),
color_transforms(p=0.25),
blur_transforms(p=0.25),
distortion_transforms(p=0.5),
]
elif strength == 5:
augs = [
# albu.HorizontalFlip(p=0.5),
albu.ShiftScaleRotate(
scale_limit=0.2,
shift_limit=0.1,
rotate_limit=45,
p=0.75,
),
color_transforms(p=0.25),
blur_transforms(p=0.25),
distortion_transforms(p=0.5),
]
elif strength == 6:
augs = [
albu.ShiftScaleRotate(
scale_limit=0.2,
shift_limit=0.2,
rotate_limit=10,
p=0.75,
),
color_transforms(p=0.25),
blur_transforms(p=0.25),
]
elif strength == 7:
augs = [
# albu.HorizontalFlip(p=0.5),
albu.VerticalFlip(p=0.5),
albu.ShiftScaleRotate(
scale_limit=0.2,
shift_limit=0.1,
rotate_limit=45,
p=0.75,
),
color_transforms(p=0.25),
blur_transforms(p=0.25),
distortion_transforms(p=0.5),
]
else:
augs = []
keypoint_params = (
albu.KeypointParams(format="xy", remove_invisible=False)
if use_keypoints
else None
)
return albu.Compose(augs + [normalizer], keypoint_params=keypoint_params)
|