-
Notifications
You must be signed in to change notification settings - Fork 516
/
image_torch.py
462 lines (392 loc) · 16.9 KB
/
image_torch.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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#
# For licensing see accompanying LICENSE file.
# Copyright (C) 2024 Apple Inc. All Rights Reserved.
#
import argparse
import math
import random
from typing import Dict, Tuple
import torch
from torch import Tensor
from torch.nn import functional as F_torch
from torchvision.transforms import functional as F
from corenet.data.transforms import TRANSFORMATIONS_REGISTRY, BaseTransformation
from corenet.utils import logger
# Copied from PyTorch Torchvision
@TRANSFORMATIONS_REGISTRY.register(name="random_mixup", type="image_torch")
class RandomMixup(BaseTransformation):
"""
Given a batch of input images and labels, this class randomly applies the
`MixUp transformation <https://arxiv.org/abs/1710.09412>`_
Args:
opts (argparse.Namespace): Arguments
num_classes (int): Number of classes in the dataset
"""
def __init__(
self, opts: argparse.Namespace, num_classes: int, *args, **kwargs
) -> None:
super().__init__(opts=opts, *args, **kwargs)
alpha = getattr(opts, "image_augmentation.mixup.alpha")
assert (
num_classes > 0
), "Please provide a valid positive value for the num_classes."
assert alpha > 0, "Alpha param can't be zero."
self.num_classes = num_classes
self.p = getattr(opts, "image_augmentation.mixup.p")
assert (
0.0 < self.p <= 1.0
), "MixUp probability should be between 0 and 1, where 1 is inclusive"
self.alpha = alpha
self.inplace = getattr(opts, "image_augmentation.mixup.inplace")
self.sample_key = getattr(opts, "image_augmentation.mixup.sample_key")
self.target_key = getattr(opts, "image_augmentation.mixup.target_key")
@classmethod
def add_arguments(cls, parser: argparse.ArgumentParser) -> argparse.ArgumentParser:
group = parser.add_argument_group(title=cls.__name__)
group.add_argument(
"--image-augmentation.mixup.enable",
action="store_true",
help="use {}. This flag is useful when you want to study the effect of different "
"transforms.".format(cls.__name__),
)
group.add_argument(
"--image-augmentation.mixup.alpha",
type=float,
default=0.2,
help="Alpha for MixUp augmentation. Defaults to 0.2",
)
group.add_argument(
"--image-augmentation.mixup.p",
type=float,
default=1.0,
help="Probability for applying MixUp augmentation. Defaults to 1.0 ."
" If both MixUp and CutMix are enabled,"
" one is used with probability 0.5 per batch.",
)
group.add_argument(
"--image-augmentation.mixup.inplace",
action="store_true",
default=False,
help="Apply MixUp augmentation inplace. Defaults to False.",
)
group.add_argument(
"--image-augmentation.mixup.sample-key",
type=str,
default=None,
help="Name of the key if input is a dictionart. Defaults to None.",
)
group.add_argument(
"--image-augmentation.mixup.target-key",
type=str,
default=None,
help="Name of the key if target is a dictionary. Defaults to None.",
)
return parser
def _apply_mixup_transform(
self, image_tensor: Tensor, target_tensor: Tensor
) -> Tuple[Tensor, Tensor]:
if image_tensor.ndim != 4:
logger.error(f"Batch ndim should be 4. Got {image_tensor.ndim}")
if target_tensor.ndim != 1:
logger.error(f"Target ndim should be 1. Got {target_tensor.ndim}")
if not image_tensor.is_floating_point():
logger.error(
f"Batch datatype should be a float tensor. Got {image_tensor.dtype}."
)
if target_tensor.dtype != torch.int64:
logger.error(
f"Target datatype should be torch.int64. Got {target_tensor.dtype}"
)
if not self.inplace:
image_tensor = image_tensor.clone()
target_tensor = target_tensor.clone()
if target_tensor.ndim == 1:
target_tensor = F_torch.one_hot(
target_tensor, num_classes=self.num_classes
).to(dtype=image_tensor.dtype)
# It's faster to roll the batch by one instead of shuffling it to create image pairs
batch_rolled = image_tensor.roll(1, 0)
target_rolled = target_tensor.roll(1, 0)
# Implemented as on mixup paper, page 3.
lambda_param = float(
torch._sample_dirichlet(torch.tensor([self.alpha, self.alpha]))[0]
)
batch_rolled.mul_(1.0 - lambda_param)
image_tensor.mul_(lambda_param).add_(batch_rolled)
target_rolled.mul_(1.0 - lambda_param)
target_tensor.mul_(lambda_param).add_(target_rolled)
return image_tensor, target_tensor
def __call__(self, data: Dict) -> Dict:
"""
Input data format:
data: mapping of: {
"samples": {"sample_key": Tensor of shape: [Batch, Channels, Height, Width]},
"targets": {"target_key": IntTensor of shape: [Batch]}
}
OR
data: mapping of: {
"samples": {"sample_key": Tensor of shape: [Batch, Channels, Height, Width]},
"targets": IntTensor of shape: [Batch]
}
OR
data: mapping of: {
"samples": Tensor of shape: [Batch, Channels, Height, Width],
"targets": {"target_key": IntTensor of shape: [Batch]}
}
OR
data: mapping of: {
"samples": Tensor of shape: [Batch, Channels, Height, Width],
"targets": IntTensor of shape: [Batch]
}
Output data format: Same as the input
"""
if torch.rand(1).item() >= self.p:
return data
samples, targets = data.pop("samples"), data.pop("targets")
if self.sample_key is not None:
samples = samples[self.sample_key]
if not isinstance(samples, Tensor):
logger.error(
f"Samples need to be of type Tensor. Got: {type(samples)}. "
f"Maybe you want to check the value of --image-augmentation.mixup.sample-key"
)
if self.target_key is not None:
targets = targets[self.target_key]
if not isinstance(targets, Tensor):
logger.error(
f"Targets need to be of type Tensor. Got: {type(targets)}. "
f"Maybe you want to check the value of --image-augmentation.mixup.target-key"
)
samples, targets = self._apply_mixup_transform(
image_tensor=samples, target_tensor=targets
)
if self.sample_key is not None:
if isinstance(samples, Tensor):
samples = {self.sample_key: samples}
else:
samples[self.sample_key] = samples
if self.target_key is not None:
if isinstance(targets, Tensor):
targets = {self.target_key: targets}
else:
targets[self.target_key] = targets
data.update({"samples": samples, "targets": targets})
return data
def __repr__(self) -> str:
return "{}(num_classes={}, p={}, alpha={}, inplace={})".format(
self.__class__.__name__, self.num_classes, self.p, self.alpha, self.inplace
)
@TRANSFORMATIONS_REGISTRY.register(name="random_cutmix", type="image_torch")
class RandomCutmix(BaseTransformation):
"""
Given a batch of input images and labels, this class randomly applies the
`CutMix transformation <https://arxiv.org/abs/1905.04899>`_
Args:
opts (argparse.Namespace): Arguments
num_classes (int): Number of classes in the dataset
"""
def __init__(
self, opts: argparse.Namespace, num_classes: int, *args, **kwargs
) -> None:
super().__init__(opts=opts, *args, **kwargs)
alpha = getattr(opts, "image_augmentation.cutmix.alpha")
assert (
num_classes > 0
), "Please provide a valid positive value for the num_classes."
assert alpha > 0, "Alpha param can't be zero."
self.num_classes = num_classes
self.p = getattr(opts, "image_augmentation.cutmix.p")
assert (
0.0 < self.p <= 1.0
), "CutMix probability should be between 0 and 1, where 1 is inclusive"
self.alpha = alpha
self.inplace = getattr(opts, "image_augmentation.cutmix.inplace")
self.sample_key = getattr(opts, "image_augmentation.cutmix.sample_key")
self.target_key = getattr(opts, "image_augmentation.cutmix.target_key")
@classmethod
def add_arguments(cls, parser: argparse.ArgumentParser) -> argparse.ArgumentParser:
group = parser.add_argument_group(title=cls.__name__)
group.add_argument(
"--image-augmentation.cutmix.enable",
action="store_true",
help="use {}. This flag is useful when you want to study the effect of different "
"transforms.".format(cls.__name__),
)
group.add_argument(
"--image-augmentation.cutmix.alpha",
type=float,
default=1.0,
help="Alpha for cutmix augmentation. Defaults to 1.0",
)
group.add_argument(
"--image-augmentation.cutmix.p",
type=float,
default=1.0,
help="Probability for applying cutmix augmentation. Defaults to 1.0"
" If both MixUp and CutMix are enabled,"
" one is used with probability 0.5 per batch.",
)
group.add_argument(
"--image-augmentation.cutmix.inplace",
action="store_true",
default=False,
help="Apply cutmix operation inplace. Defaults to False",
)
group.add_argument(
"--image-augmentation.cutmix.sample-key",
type=str,
default=None,
help="Name of the key if input is a dictionary. Defaults to None.",
)
group.add_argument(
"--image-augmentation.cutmix.target-key",
type=str,
default=None,
help="Name of the key if target is a dictionary. Defaults to None.",
)
return parser
def _apply_cutmix_transform(
self, image_tensor: Tensor, target_tensor: Tensor
) -> Tuple[Tensor, Tensor]:
if image_tensor.ndim != 4:
logger.error(f"Batch ndim should be 4. Got {image_tensor.ndim}")
if target_tensor.ndim != 1:
logger.error(f"Target ndim should be 1. Got {target_tensor.ndim}")
if not image_tensor.is_floating_point():
logger.error(
f"Batch dtype should be a float tensor. Got {image_tensor.dtype}."
)
if target_tensor.dtype != torch.int64:
logger.error(
f"Target dtype should be torch.int64. Got {target_tensor.dtype}"
)
if not self.inplace:
image_tensor = image_tensor.clone()
target_tensor = target_tensor.clone()
if target_tensor.ndim == 1:
target_tensor = F_torch.one_hot(
target_tensor, num_classes=self.num_classes
).to(dtype=image_tensor.dtype)
# It's faster to roll the batch by one instead of shuffling it to create image pairs
batch_rolled = image_tensor.roll(1, 0)
target_rolled = target_tensor.roll(1, 0)
# Implemented as on cutmix paper, page 12 (with minor corrections on typos).
lambda_param = float(
torch._sample_dirichlet(torch.tensor([self.alpha, self.alpha]))[0]
)
W, H = F.get_image_size(image_tensor)
r_x = torch.randint(W, (1,))
r_y = torch.randint(H, (1,))
r = 0.5 * math.sqrt(1.0 - lambda_param)
r_w_half = int(r * W)
r_h_half = int(r * H)
x1 = int(torch.clamp(r_x - r_w_half, min=0))
y1 = int(torch.clamp(r_y - r_h_half, min=0))
x2 = int(torch.clamp(r_x + r_w_half, max=W))
y2 = int(torch.clamp(r_y + r_h_half, max=H))
image_tensor[:, :, y1:y2, x1:x2] = batch_rolled[:, :, y1:y2, x1:x2]
lambda_param = float(1.0 - (x2 - x1) * (y2 - y1) / (W * H))
target_rolled.mul_(1.0 - lambda_param)
target_tensor.mul_(lambda_param).add_(target_rolled)
return image_tensor, target_tensor
def __call__(self, data: Dict) -> Dict:
"""
Input data format:
data: mapping of: {
"samples": {"sample_key": Tensor of shape: [Batch, Channels, Height, Width]},
"targets": {"target_key": IntTensor of shape: [Batch]}
}
OR
data: mapping of: {
"samples": {"sample_key": Tensor of shape: [Batch, Channels, Height, Width]},
"targets": IntTensor of shape: [Batch]
}
OR
data: mapping of: {
"samples": Tensor of shape: [Batch, Channels, Height, Width],
"targets": {"target_key": IntTensor of shape: [Batch]}
}
OR
data: mapping of: {
"samples": Tensor of shape: [Batch, Channels, Height, Width],
"targets": IntTensor of shape: [Batch]
}
Output data format: Same as the input
"""
if torch.rand(1).item() >= self.p:
return data
samples, targets = data.pop("samples"), data.pop("targets")
if self.sample_key is not None:
samples = samples[self.sample_key]
if not isinstance(samples, Tensor):
logger.error(
f"Samples need to be of type Tensor. Got: {type(samples)}. "
f"Maybe you want to check the value of --image-augmentation.cutmix.sample-key"
)
if self.target_key is not None:
targets = targets[self.target_key]
if not isinstance(targets, Tensor):
logger.error(
f"Targets need to be of type Tensor. Got: {type(targets)}. "
f"Maybe you want to check the value of --image-augmentation.cutmix.target-key"
)
samples, targets = self._apply_cutmix_transform(
image_tensor=samples, target_tensor=targets
)
if self.sample_key is not None:
if isinstance(samples, Tensor):
samples = {self.sample_key: samples}
else:
samples[self.sample_key] = samples
if self.target_key is not None:
if isinstance(targets, Tensor):
targets = {self.target_key: targets}
else:
targets[self.target_key] = targets
data.update({"samples": samples, "targets": targets})
return data
def __repr__(self) -> str:
return "{}(num_classes={}, p={}, alpha={}, inplace={})".format(
self.__class__.__name__, self.num_classes, self.p, self.alpha, self.inplace
)
def apply_mixing_transforms(opts: argparse.Namespace, data: Dict) -> Dict:
"""
Helper function to apply MixUp/CutMix transforms. If both MixUp and CutMix transforms
are selected with 0.0 < p <= 1.0, then one of them is chosen randomly and applied.
Input data format:
data: mapping of: {
"samples": {"sample_key": Tensor of shape: [Batch, Channels, Height, Width]},
"targets": {"target_key": IntTensor of shape: [Batch]}
}
OR
data: mapping of: {
"samples": {"sample_key": Tensor of shape: [Batch, Channels, Height, Width},
"targets": IntTensor of shape: [Batch]
}
OR
data: mapping of: {
"samples": Tensor of shape: [Batch, Channels, Height, Width],
"targets": {"target_key": IntTensor of shape: [Batch]}
}
OR
data: mapping of: {
"samples": Tensor of shape: [Batch, Channels, Height, Width],
"targets": IntTensor of shape: [Batch]
}
Output data format: Same as the input
"""
mixup_transforms = []
if getattr(opts, "image_augmentation.mixup.enable"):
n_classes = getattr(opts, "model.classification.n_classes")
if n_classes is None:
logger.error("Please specify number of classes. Got None.")
mixup_transforms.append(RandomMixup(opts=opts, num_classes=n_classes))
if getattr(opts, "image_augmentation.cutmix.enable"):
n_classes = getattr(opts, "model.classification.n_classes")
if n_classes is None:
logger.error("Please specify number of classes. Got None.")
mixup_transforms.append(RandomCutmix(opts=opts, num_classes=n_classes))
if len(mixup_transforms) > 0:
_mixup_transform = random.choice(mixup_transforms)
data = _mixup_transform(data)
return data