/
transforms.py
173 lines (145 loc) · 5.85 KB
/
transforms.py
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
"""
Copyright (c) Meta Platforms, Inc. and affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
"""
# Implements image augmentation
import albumentations as alb
from albumentations.pytorch import ToTensorV2
import cv2
import numpy as np
from timm.data.constants import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD
def alb_wrapper(transform):
def f(im):
return transform(image=np.asarray(im))["image"]
return f
class Erosion(alb.ImageOnlyTransform):
"""
Apply erosion operation to an image.
Erosion is a morphological operation that shrinks the white regions in a binary image.
Args:
scale (int or tuple/list of int): The scale or range for the size of the erosion kernel.
If an integer is provided, a square kernel of that size will be used.
If a tuple or list is provided, it should contain two integers representing the minimum
and maximum sizes for the erosion kernel.
always_apply (bool, optional): Whether to always apply this transformation. Default is False.
p (float, optional): The probability of applying this transformation. Default is 0.5.
Returns:
numpy.ndarray: The transformed image.
"""
def __init__(self, scale, always_apply=False, p=0.5):
super().__init__(always_apply=always_apply, p=p)
if type(scale) is tuple or type(scale) is list:
assert len(scale) == 2
self.scale = scale
else:
self.scale = (scale, scale)
def apply(self, img, **params):
kernel = cv2.getStructuringElement(
cv2.MORPH_ELLIPSE, tuple(np.random.randint(self.scale[0], self.scale[1], 2))
)
img = cv2.erode(img, kernel, iterations=1)
return img
class Dilation(alb.ImageOnlyTransform):
"""
Apply dilation operation to an image.
Dilation is a morphological operation that expands the white regions in a binary image.
Args:
scale (int or tuple/list of int): The scale or range for the size of the dilation kernel.
If an integer is provided, a square kernel of that size will be used.
If a tuple or list is provided, it should contain two integers representing the minimum
and maximum sizes for the dilation kernel.
always_apply (bool, optional): Whether to always apply this transformation. Default is False.
p (float, optional): The probability of applying this transformation. Default is 0.5.
Returns:
numpy.ndarray: The transformed image.
"""
def __init__(self, scale, always_apply=False, p=0.5):
super().__init__(always_apply=always_apply, p=p)
if type(scale) is tuple or type(scale) is list:
assert len(scale) == 2
self.scale = scale
else:
self.scale = (scale, scale)
def apply(self, img, **params):
kernel = cv2.getStructuringElement(
cv2.MORPH_ELLIPSE, tuple(np.random.randint(self.scale[0], self.scale[1], 2))
)
img = cv2.dilate(img, kernel, iterations=1)
return img
class Bitmap(alb.ImageOnlyTransform):
"""
Apply a bitmap-style transformation to an image.
This transformation replaces all pixel values below a certain threshold with a specified value.
Args:
value (int, optional): The value to replace pixels below the threshold with. Default is 0.
lower (int, optional): The threshold value below which pixels will be replaced. Default is 200.
always_apply (bool, optional): Whether to always apply this transformation. Default is False.
p (float, optional): The probability of applying this transformation. Default is 0.5.
Returns:
numpy.ndarray: The transformed image.
"""
def __init__(self, value=0, lower=200, always_apply=False, p=0.5):
super().__init__(always_apply=always_apply, p=p)
self.lower = lower
self.value = value
def apply(self, img, **params):
img = img.copy()
img[img < self.lower] = self.value
return img
train_transform = alb_wrapper(
alb.Compose(
[
Bitmap(p=0.05),
alb.OneOf([Erosion((2, 3)), Dilation((2, 3))], p=0.02),
alb.Affine(shear={"x": (0, 3), "y": (-3, 0)}, cval=(255, 255, 255), p=0.03),
alb.ShiftScaleRotate(
shift_limit_x=(0, 0.04),
shift_limit_y=(0, 0.03),
scale_limit=(-0.15, 0.03),
rotate_limit=2,
border_mode=0,
interpolation=2,
value=(255, 255, 255),
p=0.03,
),
alb.GridDistortion(
distort_limit=0.05,
border_mode=0,
interpolation=2,
value=(255, 255, 255),
p=0.04,
),
alb.Compose(
[
alb.Affine(
translate_px=(0, 5), always_apply=True, cval=(255, 255, 255)
),
alb.ElasticTransform(
p=1,
alpha=50,
sigma=120 * 0.1,
alpha_affine=120 * 0.01,
border_mode=0,
value=(255, 255, 255),
),
],
p=0.04,
),
alb.RandomBrightnessContrast(0.1, 0.1, True, p=0.03),
alb.ImageCompression(95, p=0.07),
alb.GaussNoise(20, p=0.08),
alb.GaussianBlur((3, 3), p=0.03),
alb.Normalize(IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD),
ToTensorV2(),
]
)
)
test_transform = alb_wrapper(
alb.Compose(
[
alb.Normalize(IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD),
ToTensorV2(),
]
)
)