-
Notifications
You must be signed in to change notification settings - Fork 18
/
transforms.py
104 lines (89 loc) · 3.13 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
import torch
from torchvision import transforms
from packaging import version
def data_transforms(cfg):
data_aug = cfg.data.data_augmentation
aug_args = cfg.data_augmentation_args
input_size = format_input_size(cfg.data.input_size)
operations = {
'random_crop': random_apply(
transforms.RandomResizedCrop(
size=input_size,
scale=aug_args.random_crop.scale,
ratio=aug_args.random_crop.ratio
),
p=aug_args.random_crop.prob
),
'horizontal_flip': transforms.RandomHorizontalFlip(
p=aug_args.horizontal_flip.prob
),
'vertical_flip': transforms.RandomVerticalFlip(
p=aug_args.vertical_flip.prob
),
'color_distortion': random_apply(
transforms.ColorJitter(
brightness=aug_args.color_distortion.brightness,
contrast=aug_args.color_distortion.contrast,
saturation=aug_args.color_distortion.saturation,
hue=aug_args.color_distortion.hue
),
p=aug_args.color_distortion.prob
),
'rotation': random_apply(
transforms.RandomRotation(
degrees=aug_args.rotation.degrees,
fill=aug_args.value_fill
),
p=aug_args.rotation.prob
),
'translation': random_apply(
transforms.RandomAffine(
degrees=0,
translate=aug_args.translation.range,
fill=aug_args.value_fill
),
p=aug_args.translation.prob
),
'grayscale': transforms.RandomGrayscale(
p=aug_args.grayscale.prob
)
}
if version.parse(torch.__version__) >= version.parse('1.7.1'):
operations['gaussian_blur'] = random_apply(
transforms.GaussianBlur(
kernel_size=aug_args.gaussian_blur.kernel_size,
sigma=aug_args.gaussian_blur.sigma
),
p=aug_args.gaussian_blur.prob
)
augmentations = []
for op in data_aug:
if op not in operations:
raise NotImplementedError('Not implemented data augmentation operations: {}'.format(op))
augmentations.append(operations[op])
normalization = [
transforms.Resize(input_size),
transforms.ToTensor(),
transforms.Normalize(cfg.data.mean, cfg.data.std)
]
train_preprocess = transforms.Compose([
*augmentations,
*normalization
])
test_preprocess = transforms.Compose(normalization)
return train_preprocess, test_preprocess
def random_apply(op, p):
return transforms.RandomApply([op], p=p)
def simple_transform(input_size):
input_size = format_input_size(input_size)
return transforms.Compose([
transforms.Resize(input_size),
transforms.ToTensor()
])
def format_input_size(input_size):
if isinstance(input_size, int):
input_size = (input_size, input_size)
else:
assert isinstance(input_size, tuple) or isinstance(input_size, list)
assert len(input_size) == 2
return input_size