-
Notifications
You must be signed in to change notification settings - Fork 2
/
presets.py
118 lines (105 loc) · 3.99 KB
/
presets.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
import torch
import transforms as T
# ------------------------------------------------------------
# Code copied from
# https://github.com/pytorch/vision/blob/main/references/detection/presets.py
# ------------------------------------------------------------
class DetectionPresetTrain:
def __init__(self, *, data_augmentation, hflip_prob=0.5, mean=(123.0, 117.0, 104.0)):
if data_augmentation == "hflip":
self.transforms = T.Compose(
[
T.RandomHorizontalFlip(p=hflip_prob),
T.PILToTensor(),
T.ConvertImageDtype(torch.float),
]
)
elif data_augmentation == "lsj":
self.transforms = T.Compose(
[
T.ScaleJitter(target_size=(1024, 1024)),
T.FixedSizeCrop(size=(1024, 1024), fill=mean),
T.RandomHorizontalFlip(p=hflip_prob),
T.PILToTensor(),
T.ConvertImageDtype(torch.float),
]
)
elif data_augmentation == "multiscale":
self.transforms = T.Compose(
[
T.RandomShortestSize(
min_size=(480, 512, 544, 576, 608, 640, 672, 704, 736, 768, 800), max_size=1333
),
T.RandomHorizontalFlip(p=hflip_prob),
T.PILToTensor(),
T.ConvertImageDtype(torch.float),
]
)
elif data_augmentation == "ssd":
self.transforms = T.Compose(
[
T.RandomPhotometricDistort(),
T.RandomZoomOut(fill=list(mean)),
T.RandomIoUCrop(),
T.RandomHorizontalFlip(p=hflip_prob),
T.PILToTensor(),
T.ConvertImageDtype(torch.float),
]
)
elif data_augmentation == "ssdlite":
self.transforms = T.Compose(
[
T.RandomIoUCrop(),
T.RandomHorizontalFlip(p=hflip_prob),
T.PILToTensor(),
T.ConvertImageDtype(torch.float),
]
)
else:
raise ValueError(f'Unknown data augmentation policy "{data_augmentation}"')
def __call__(self, img, target):
return self.transforms(img, target)
class DetectionPresetEval:
def __init__(self, data_augmentation: str, noise_intensity=0.05):
if data_augmentation == 'gaussian':
self.transforms = T.Compose(
[
T.PILToTensor(),
T.ConvertImageDtype(torch.float),
T.RandomNoise(noise_type=data_augmentation, noise_intensity=noise_intensity),
T.ConvertImageDtype(torch.float),
]
)
elif data_augmentation == 'rain':
self.transforms = T.Compose(
[
T.PILToTensor(),
T.Rain(rain_type=data_augmentation, number_of_drops=noise_intensity),
T.ConvertImageDtype(torch.float),
]
)
elif data_augmentation == 'light-rain':
self.transforms = T.Compose(
[
T.PILToTensor(),
T.Rain(rain_type='light'),
T.ConvertImageDtype(torch.float),
]
)
elif data_augmentation == 'heavy-rain':
self.transforms = T.Compose(
[
T.PILToTensor(),
T.Rain(rain_type='heavy'),
T.ConvertImageDtype(torch.float),
]
)
else:
self.transforms = T.Compose(
[
T.PILToTensor(),
T.ConvertImageDtype(torch.float),
]
)
def __call__(self, img, target):
return self.transforms(img, target)