-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtransform.py
More file actions
348 lines (283 loc) · 11.7 KB
/
Copy pathtransform.py
File metadata and controls
348 lines (283 loc) · 11.7 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
"""Data augmentation transforms for semantic segmentation.
This module provides various transformation classes for augmenting image and label pairs
during training. All transforms operate on dictionaries with 'im' (image) and 'lb' (label) keys.
Transforms include:
- Geometric: RandomScale, RandomHorizontalFlip, RandomCrop, RandomRotate
- Photometric: RandomColorJitter, RandomGamma, RandomNoise, RandomGrayscale
- Regularization: RandomCutout, RandomGaussianBlur
"""
import random # nosec B311 — used for data augmentation, not security/cryptographic purposes
from typing import Any
from PIL import Image, ImageEnhance
import numpy as np
class Compose(object):
def __init__(self, transforms):
self.transforms = transforms
def __call__(self, im_lb):
for t in self.transforms:
im_lb = t(im_lb)
return im_lb
class RandomScale(object):
"""Random resize, either from a discrete list of scale factors
(default) or a continuous range — pass ``continuous=True`` with
``scales=(low, high)`` to sample uniformly from ``[low, high]``,
matching Ultralytics' ``scale`` augmentation (``scale=X`` means the
continuous range ``[1-X, 1+X]``)."""
def __init__(
self,
scales=(1,),
continuous=False,
interp_image=Image.BILINEAR,
interp_label=Image.NEAREST,
):
self.continuous = continuous
if continuous:
lo, hi = scales
self.scale_range = (float(lo), float(hi))
else:
self.scales = [float(s) for s in scales]
self.interp_image = interp_image
self.interp_label = interp_label
def __call__(self, im_lb):
im = im_lb["im"]
lb = im_lb["lb"]
if not (isinstance(im, Image.Image) and isinstance(lb, Image.Image)):
raise TypeError(f"Expected PIL images, got {type(im)}, {type(lb)}")
if self.continuous:
scale = random.uniform(*self.scale_range) # nosec B311
else:
scale = random.choice(self.scales) # nosec B311
W, H = im.size
w = int(round(W * scale))
h = int(round(H * scale))
return {
"im": im.resize((w, h), self.interp_image),
"lb": lb.resize((w, h), self.interp_label),
}
class RandomHorizontalFlip(object):
def __init__(self, p=0.5):
self.p = p
def __call__(self, im_lb):
if random.random() > self.p:
return im_lb
im = im_lb["im"].transpose(Image.FLIP_LEFT_RIGHT)
lb = im_lb["lb"].transpose(Image.FLIP_LEFT_RIGHT)
return {"im": im, "lb": lb}
class RandomVerticalFlip(object):
"""Vertical flip — matches Ultralytics' ``flipud`` augmentation (valid
for top-down aerial imagery, unlike ground-level datasets)."""
def __init__(self, p=0.5):
self.p = p
def __call__(self, im_lb):
if random.random() > self.p:
return im_lb
im = im_lb["im"].transpose(Image.FLIP_TOP_BOTTOM)
lb = im_lb["lb"].transpose(Image.FLIP_TOP_BOTTOM)
return {"im": im, "lb": lb}
class RandomTranslate(object):
"""Random translation by up to ``translate`` fraction of image size in
each axis, matching Ultralytics' ``translate`` augmentation."""
def __init__(self, translate=0.05, ignore_label=255):
self.translate = translate
self.ignore_label = ignore_label
def __call__(self, im_lb):
im = im_lb["im"]
lb = im_lb["lb"]
w, h = im.size
dx = random.uniform(-self.translate, self.translate) * w # nosec B311
dy = random.uniform(-self.translate, self.translate) * h # nosec B311
im = im.transform(
im.size, Image.AFFINE, (1, 0, dx, 0, 1, dy), resample=Image.BILINEAR
)
lb = lb.transform(
lb.size,
Image.AFFINE,
(1, 0, dx, 0, 1, dy),
resample=Image.NEAREST,
fillcolor=self.ignore_label,
)
return {"im": im, "lb": lb}
class RandomCrop(object):
def __init__(self, size, pad_if_needed=True, ignore_label=255):
self.size = tuple(size) if hasattr(size, "__iter__") else (size, size)
self.pad_if_needed = pad_if_needed
self.ignore_label = ignore_label
def __call__(self, im_lb):
im = im_lb["im"]
lb = im_lb["lb"]
if not (isinstance(im, Image.Image) and isinstance(lb, Image.Image)):
raise TypeError(f"Expected PIL images, got {type(im)}, {type(lb)}")
target_w, target_h = self.size
w, h = im.size
if self.pad_if_needed:
pad_w = max(target_w - w, 0)
pad_h = max(target_h - h, 0)
if pad_w > 0 or pad_h > 0:
# Pad image — pad_width type varies by ndim, annotated as Any
im_np = np.array(im)
pad_width: Any
if len(im_np.shape) == 3:
pad_width = ((0, pad_h), (0, pad_w), (0, 0))
else:
pad_width = ((0, pad_h), (0, pad_w))
im_np = np.pad(im_np, pad_width, mode="reflect")
im = Image.fromarray(im_np)
# Pad label
lb_np = np.array(lb)
lb_np = np.pad(
lb_np, ((0, pad_h), (0, pad_w)), constant_values=self.ignore_label
).astype(np.uint8)
lb = Image.fromarray(lb_np)
w, h = im.size
if w < target_w or h < target_h:
scale = max(target_w / w, target_h / h)
new_w, new_h = int(w * scale + 1), int(h * scale + 1)
im = im.resize((new_w, new_h), Image.BILINEAR)
lb = lb.resize((new_w, new_h), Image.NEAREST)
sw = random.randint(0, w - target_w) if w > target_w else 0 # nosec B311
sh = random.randint(0, h - target_h) if h > target_h else 0 # nosec B311
crop_box = (sw, sh, sw + target_w, sh + target_h)
im_lb["im"] = im.crop(crop_box)
im_lb["lb"] = lb.crop(crop_box)
return im_lb
class RandomHSV(object):
"""Multiplicative saturation/value + additive hue jitter in HSV colour
space, matching Ultralytics' ``RandomHSV`` augmentation formula exactly:
``hue = (hue + gain_h * full_circle) % full_circle`` (additive, wraps),
``sat = clip(sat * (1 + gain_s), 0, max)``, ``val = clip(val * (1 +
gain_v), 0, max)``, with each gain drawn as ``uniform(-1, 1) * hgain``
(etc). Reimplemented via PIL's HSV conversion rather than OpenCV — PIL's
H channel spans 0-255 for the full hue circle (vs OpenCV's 0-179), so
the hue shift is scaled to 255 instead of 180 to preserve the same
*fraction of the circle* shifted.
"""
def __init__(self, hgain=0.015, sgain=0.4, vgain=0.3):
self.hgain = hgain
self.sgain = sgain
self.vgain = vgain
def __call__(self, im_lb):
if self.hgain or self.sgain or self.vgain:
im = im_lb["im"]
hsv = np.array(im.convert("HSV"), dtype=np.int16)
r_h = random.uniform(-1, 1) * self.hgain # nosec B311
r_s = random.uniform(-1, 1) * self.sgain # nosec B311
r_v = random.uniform(-1, 1) * self.vgain # nosec B311
hsv[..., 0] = (hsv[..., 0] + round(r_h * 255)) % 255
hsv[..., 1] = np.clip(hsv[..., 1] * (r_s + 1), 0, 255)
hsv[..., 2] = np.clip(hsv[..., 2] * (r_v + 1), 0, 255)
hsv = hsv.astype(np.uint8)
# Image.fromarray(..., mode="HSV") is deprecated (removed in
# Pillow 13) — Image.merge is the non-deprecated equivalent.
im_hsv = Image.merge(
"HSV",
[Image.fromarray(hsv[..., c]) for c in range(3)],
)
im_lb["im"] = im_hsv.convert("RGB")
return im_lb
class RandomColorJitter(object):
def __init__(self, brightness=None, contrast=None, saturation=None):
self.brightness = self._check(brightness)
self.contrast = self._check(contrast)
self.saturation = self._check(saturation)
@staticmethod
def _check(v):
return None if v is None else [max(1 - v, 0), 1 + v]
def __call__(self, im_lb):
im = im_lb["im"]
if self.brightness:
r = random.uniform(*self.brightness)
im = ImageEnhance.Brightness(im).enhance(r)
if self.contrast:
r = random.uniform(*self.contrast)
im = ImageEnhance.Contrast(im).enhance(r)
if self.saturation:
r = random.uniform(*self.saturation)
im = ImageEnhance.Color(im).enhance(r)
im_lb["im"] = im
return im_lb
class RandomCutout:
def __init__(self, p=0.5, size=64):
self.p = p
self.size = size
def __call__(self, im_lb):
if random.random() < self.p:
im = np.array(im_lb["im"])
h, w, _ = im.shape
y = random.randint(0, h - self.size)
x = random.randint(0, w - self.size)
im[y : y + self.size, x : x + self.size, :] = 0
im_lb["im"] = Image.fromarray(im)
return im_lb
class RandomGaussianBlur:
def __init__(self, p=0.5, radius=(0.1, 2.0)):
self.p = p
self.radius = radius
def __call__(self, im_lb):
if random.random() < self.p:
from PIL import ImageFilter
r = random.uniform(*self.radius)
im_lb["im"] = im_lb["im"].filter(ImageFilter.GaussianBlur(radius=r))
return im_lb
class RandomGrayscale:
def __init__(self, p=0.5):
self.p = p
def __call__(self, im_lb):
if random.random() < self.p:
im = im_lb["im"].convert("L") # convert to grayscale
im = im.convert("RGB") # back to 3 channels
im_lb["im"] = im
return im_lb
class RandomGamma:
def __init__(self, gamma_range=(0.7, 1.5), p=0.5):
self.gamma_range = gamma_range
self.p = p
def __call__(self, im_lb):
if random.random() < self.p:
gamma = random.uniform(*self.gamma_range)
im = np.array(im_lb["im"]).astype(np.float32) / 255.0
im = np.clip(im**gamma, 0, 1) # gamma correction
im = (im * 255).astype(np.uint8)
im_lb["im"] = Image.fromarray(im)
return im_lb
class RandomNoise:
def __init__(self, mode="gaussian", sigma=0.05, p=0.5):
"""
mode: 'gaussian' or 'poisson'
sigma: std for Gaussian (fraction of 255)
p: probability of applying
"""
self.mode = mode
self.sigma = sigma
self.p = p
def __call__(self, im_lb):
if random.random() < self.p:
arr = np.array(im_lb["im"]).astype(np.float32)
if self.mode == "gaussian":
noise = np.random.normal(0, self.sigma * 255, arr.shape)
arr = arr + noise
elif self.mode == "poisson":
vals = 2 ** np.ceil(np.log2(len(np.unique(arr))))
arr = np.random.poisson(arr * vals) / float(vals)
arr = np.clip(arr, 0, 255).astype(np.uint8)
im_lb["im"] = Image.fromarray(arr)
return im_lb
class RandomRotate(object):
"""Small random rotation to simulate UAV yaw changes."""
def __init__(
self,
degrees=(-15, 15),
interp_image=Image.BILINEAR,
interp_label=Image.NEAREST,
ignore_label=255,
):
self.degrees = degrees
self.interp_image = interp_image
self.interp_label = interp_label
self.ignore_label = ignore_label
def __call__(self, im_lb):
angle = random.uniform(*self.degrees)
im = im_lb["im"].rotate(angle, resample=self.interp_image, expand=True)
lb = im_lb["lb"].rotate(
angle, resample=self.interp_label, expand=True, fillcolor=self.ignore_label
)
return {"im": im, "lb": lb}