-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcoarse_dropout.py
More file actions
287 lines (249 loc) · 11 KB
/
coarse_dropout.py
File metadata and controls
287 lines (249 loc) · 11 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
'''
Modified from albumentations.dropout.coarse_dropout
'''
import random
from typing import Iterable, List, Optional, Sequence, Tuple, Union, Dict, Any
import torch
# __all__ = ["CoarseDropout"]
def cutout_holes(
img: torch.Tensor, holes: Iterable[Tuple[int, int, int, int]], fill_value: Union[int, float] = 0
) -> torch.Tensor:
# Make a copy of the input image since we don't want to modify it directly
img = img.clone()
for x1, y1, x2, y2 in holes:
img[:, y1:y2, x1:x2] = fill_value
return img
def cutout_circles(
img: torch.Tensor, circles: Iterable[Tuple[int, int, int]], fill_value: Union[int, float] = 0
) -> torch.Tensor:
if len(circles) == 0:
return img
_, height, width = img.shape
# Create a meshgrid
xx, yy = torch.meshgrid(torch.arange(0, width, device=img.device), torch.arange(0, height, device=img.device))
for center_x, center_y, circle_radius in circles:
distance = (xx - center_x) ** 2 + (yy - center_y) ** 2
circle = distance <= circle_radius ** 2
img[:, circle] = fill_value
return img
class CoarseDropout():
"""CoarseDropout of the rectangular regions in the image.
Args:
max_holes (int): Maximum number of regions to zero out.
max_height (int, float): Maximum height of the hole.
If float, it is calculated as a fraction of the image height.
max_width (int, float): Maximum width of the hole.
If float, it is calculated as a fraction of the image width.
min_holes (int): Minimum number of regions to zero out. If `None`,
`min_holes` is be set to `max_holes`. Default: `None`.
min_height (int, float): Minimum height of the hole. Default: None. If `None`,
`min_height` is set to `max_height`. Default: `None`.
If float, it is calculated as a fraction of the image height.
min_width (int, float): Minimum width of the hole. If `None`, `min_height` is
set to `max_width`. Default: `None`.
If float, it is calculated as a fraction of the image width.
fill_value (int, float, list of int, list of float): value for dropped pixels.
mask_fill_value (int, float, list of int, list of float): fill value for dropped pixels
in mask. If `None` - mask is not affected. Default: `None`.
Targets:
image, mask, keypoints
Image types:
uint8, float32
Reference:
| https://arxiv.org/abs/1708.04552
| https://github.com/uoguelph-mlrg/Cutout/blob/master/util/cutout.py
| https://github.com/aleju/imgaug/blob/master/imgaug/augmenters/arithmetic.py
"""
def __init__(
self,
max_holes: int = 3,
max_height: Union[int, float] = 0.7,
max_width: Union[int, float] = 0.7,
min_holes: Optional[int] = None,
min_height: Optional[Union[int, float]] = None,
min_width: Optional[Union[int, float]] = None,
fill_value: int = -1,
mask_fill_value: Optional[int] = None,
always_apply: bool = False,
p: float = 0.7,
fully_drop_p: float = 0.1,
max_circles: int = 3,
min_circles: Optional[int] = None,
max_radius: Union[int, float] = 0.7,
min_radius: Optional[Union[int, float]] = None,
p_circle: float = 0.5,
):
self.max_holes = max_holes
self.max_height = max_height
self.max_width = max_width
self.min_holes = min_holes if min_holes is not None else max_holes
self.min_height = min_height if min_height is not None else max_height
self.min_width = min_width if min_width is not None else max_width
self.fill_value = fill_value
self.mask_fill_value = mask_fill_value
self.max_circles = max_circles
self.min_circles = min_circles if min_circles is not None else max_circles
self.max_radius = max_radius
self.min_radius = min_radius if min_radius is not None else max_radius
if not 0 < self.min_holes <= self.max_holes:
raise ValueError("Invalid combination of min_holes and max_holes. Got: {}".format([min_holes, max_holes]))
self.check_range(self.max_height)
self.check_range(self.min_height)
self.check_range(self.max_width)
self.check_range(self.min_width)
if not 0 < self.min_height <= self.max_height:
raise ValueError(
"Invalid combination of min_height and max_height. Got: {}".format([min_height, max_height])
)
if not 0 < self.min_width <= self.max_width:
raise ValueError("Invalid combination of min_width and max_width. Got: {}".format([min_width, max_width]))
self.always_apply = always_apply
self.p = p
self.fully_drop_p = fully_drop_p
self.p_circle = p_circle
if not self.p + self.fully_drop_p <= 1:
raise ValueError("Invalid combination of p and fully_drop_p, their sum should be minus 1. Got: {}".format([p, fully_drop_p]))
def check_range(self, dimension):
if isinstance(dimension, float) and not 0 <= dimension < 1.0:
raise ValueError(
"Invalid value {}. If using floats, the value should be in the range [0.0, 1.0)".format(dimension)
)
def apply(
self,
img: torch.Tensor,
fill_value: Union[int, float] = 0,
holes: Iterable[Tuple[int, int, int, int]] = (),
circles: Iterable[Tuple[int, int, int]] = (),
) -> torch.Tensor:
hole_cutout_img = cutout_holes(img, holes, fill_value)
circle_cutout_img = cutout_circles(hole_cutout_img, circles, fill_value)
return circle_cutout_img
def apply_to_mask(
self,
img: torch.Tensor,
mask_fill_value: Union[int, float] = 0,
holes: Iterable[Tuple[int, int, int, int]] = ()
) -> torch.Tensor:
if mask_fill_value is None:
return img
return cutout_holes(img, holes, mask_fill_value)
def get_params_dependent_on_targets(self, img: torch.Tensor):
# img = params["image"]
b, _, height, width = img.shape
holes_list = []
for _b in range(b):
holes = []
for _n in range(random.randint(self.min_holes, self.max_holes)):
if all(
[
isinstance(self.min_height, int),
isinstance(self.min_width, int),
isinstance(self.max_height, int),
isinstance(self.max_width, int),
]
):
hole_height = random.randint(self.min_height, self.max_height)
hole_width = random.randint(self.min_width, self.max_width)
elif all(
[
isinstance(self.min_height, float),
isinstance(self.min_width, float),
isinstance(self.max_height, float),
isinstance(self.max_width, float),
]
):
hole_height = int(height * random.uniform(self.min_height, self.max_height))
hole_width = int(width * random.uniform(self.min_width, self.max_width))
else:
raise ValueError(
"Min width, max width, \
min height and max height \
should all either be ints or floats. \
Got: {} respectively".format(
[
type(self.min_width),
type(self.max_width),
type(self.min_height),
type(self.max_height),
]
)
)
y1 = random.randint(0, height - hole_height)
x1 = random.randint(0, width - hole_width)
y2 = y1 + hole_height
x2 = x1 + hole_width
holes.append((x1, y1, x2, y2))
holes_list.append(holes)
min_edge = min(height, width)
circles_list = []
for _b in range(b):
circles = []
for _n in range(random.randint(self.min_circles, self.max_circles)):
if all(
[
isinstance(self.max_radius, int),
isinstance(self.min_radius, int),
]
):
circle_radius = random.randint(self.min_radius, self.max_radius)
elif all(
[
isinstance(self.max_radius, float),
isinstance(self.min_radius, float),
]
):
circle_radius = int(min_edge * random.uniform(self.min_radius, self.max_radius))
else:
raise ValueError(
"Min radius and max radius \
should all either be ints or floats. \
Got: {} respectively".format(
[
type(self.min_radius),
type(self.max_radius),
]
)
)
center_x, center_y = random.randint(0, width), random.randint(0, height)
circles.append((center_x, center_y, circle_radius))
circles_list.append(circles)
return {"holes_list": holes_list, "circles_list": circles_list}
@property
def targets_as_params(self):
return ["image"]
def get_transform_init_args_names(self):
return (
"max_holes",
"max_height",
"max_width",
"min_holes",
"min_height",
"min_width",
"fill_value",
"mask_fill_value",
"always_apply",
"p",
"fully_drop_p",
"max_circles",
"min_circles",
"max_radius",
"min_radius",
"p_circle",
)
def __call__(self, img):
params = self.get_params_dependent_on_targets(img)
holes_list = params["holes_list"]
circles_list = params["circles_list"]
for b in range(len(holes_list)):
r = random.random()
if self.always_apply or r < self.p + self.fully_drop_p:
if r < self.p:
r_circle = random.random()
if r_circle > self.p_circle:
circles_list[b] = []
img[b, :, :, :] = self.apply(img[b, :, :, :], fill_value=self.fill_value, holes=holes_list[b], circles=circles_list[b])
# img = self.apply_to_mask(img, mask_fill_value=self.mask_fill_value)
# img = self.apply_to_keypoints(img)
elif r > self.p and r < self.p + self.fully_drop_p:
img[b, :, :, :] = torch.ones_like(img[b, :, :, :]) * self.fill_value
return img