-
Notifications
You must be signed in to change notification settings - Fork 1
/
prepost.py
264 lines (199 loc) · 8.76 KB
/
prepost.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
import torch
from torch import nn as nn
from torch.nn import functional as F
import random
#PRE
def fix_random_seed(seed: int = 6247423):
import torch
import numpy as np
torch.manual_seed(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
random.seed(seed)
np.random.seed(seed)
class Tile(nn.Module):
def __init__(self, rep: int = 384 // 16):
super().__init__()
self.rep = rep
def forward(self, x: torch.tensor) -> torch.tensor:
dim = x.dim()
if dim < 3:
raise NotImplementedError
elif dim == 3:
x.unsqueeze(0)
final_shape = x.shape[:2] + (x.shape[2] * self.rep, x.shape[3] * self.rep)
return x.unsqueeze(2).unsqueeze(4).repeat(1, 1, self.rep, 1, self.rep, 1).view(final_shape)
class Jitter(nn.Module):
def __init__(self, lim: int = 32):
super().__init__()
self.lim = lim
def forward(self, x: torch.tensor) -> torch.tensor:
off1 = random.randint(-self.lim, self.lim)
off2 = random.randint(-self.lim, self.lim)
return torch.roll(x, shifts=(off1, off2), dims=(2, 3))
class ColorJitter(nn.Module):
def __init__(self, batch_size: int, shuffle_every: bool = False, mean: float = 1., std: float = 1., use_fixed_random_seed: bool = False):
super(ColorJitter, self).__init__()
if use_fixed_random_seed:
fix_random_seed(seed=6247423)
self.batch_size, self.mean_p, self.std_p = batch_size, mean, std
self.mean = self.std = None
self.shuffle_every = shuffle_every
self.shuffle()
def shuffle(self):
self.mean = (torch.rand((self.batch_size, 3, 1, 1,)).cuda() - 0.5) * 2 * self.mean_p
self.std = ((torch.rand((self.batch_size, 3, 1, 1,)).cuda() - 0.5) * 2 * self.std_p).exp()
def forward(self, img: torch.tensor) -> torch.tensor:
if self.shuffle_every:
self.shuffle()
return (img - self.mean) / self.std
class GaussianNoise(nn.Module):
def __init__(self, batch_size: int, shuffle_every: bool = False, std: float = 1., max_iter: int = 400, use_fixed_random_seed: bool = False):
super(GaussianNoise, self).__init__()
if use_fixed_random_seed:
fix_random_seed(seed=6247423)
self.batch_size, self.std_p, self.max_iter = batch_size, std, max_iter
self.shuffle_every = shuffle_every
self.std = None
self.rem = max_iter - 1
self.shuffle()
def shuffle(self):
self.std = torch.randn(self.batch_size, 3, 1, 1).cuda() * self.rem * self.std_p / self.max_iter
self.rem = (self.rem - 1 + self.max_iter) % self.max_iter
def forward(self, img: torch.tensor) -> torch.tensor:
if self.shuffle_every:
self.shuffle()
return img + self.std
# POST
class ClipSTD(nn.Module):
@torch.no_grad()
def forward(self, x: torch.tensor, inflate: float = 1., per_sample: bool = True) -> torch.tensor:
std = x.std() if not per_sample else x.view(x.shape[0], -1).std(dim=-1).view(-1, 1, 1, 1)
mean = x.mean() if not per_sample else x.view(x.shape[0], -1).mean(dim=-1).view(-1, 1, 1, 1)
x = inflate * (x - mean) / (std * 2)
return x.clamp(min=-0.5, max=0.5) + 0.5
class Clip(nn.Module):
@torch.no_grad()
def forward(self, x: torch.tensor) -> torch.tensor:
return x.clamp(min=0, max=1)
class LInfClip(nn.Module):
def __init__(self, original: torch.tensor, eps: float = 16 / 255):
super().__init__()
self.base = original.detach().clone().cuda()
self.eps = eps
@torch.no_grad()
def forward(self, x: torch.tensor) -> torch.tensor:
return x + torch.clip(self.base - x, min=-self.eps, max=self.eps)
class L2Clip(nn.Module):
def __init__(self, original: torch.tensor, eps: float = 16 / 255):
super().__init__()
self.base = original.detach().clone().cuda()
self.eps = eps
@torch.no_grad()
def forward(self, x: torch.tensor) -> torch.tensor:
delta = self.base - x
norm = delta.norm(p=2)
delta = self.eps * delta / norm if norm > self.eps else delta
return x + delta
class Gray4D(nn.Module):
def __init__(self, n_channels: int = 3):
super().__init__()
self.n = n_channels
def forward(self, x: torch.tensor) -> torch.tensor:
shape = tuple([1] * (4 - x.dim())) + x.shape
return x.view(shape).repeat(1)
class Layered(nn.Module):
def __init__(self, x: torch.tensor):
super().__init__()
self.x = x
def forward(self, x: torch.tensor) -> torch.tensor:
return x + self.x
class Jitter(nn.Module):
def __init__(self, lim: int = 32):
super().__init__()
self.lim = lim
def forward(self, x: torch.tensor) -> torch.tensor:
off1 = random.randint(-self.lim, self.lim)
off2 = random.randint(-self.lim, self.lim)
return torch.roll(x, shifts=(off1, off2), dims=(2, 3))
class ColorJitter(nn.Module):
def __init__(self, batch_size: int, shuffle_every: bool = False, mean: float = 1., std: float = 1.):
super().__init__()
self.batch_size, self.mean_p, self.std_p = batch_size, mean, std
self.mean = self.std = None
self.shuffle()
self.shuffle_every = shuffle_every
def shuffle(self):
self.mean = (torch.rand((self.batch_size, 3, 1, 1,)).cuda() - 0.5) * 2 * self.mean_p
self.std = ((torch.rand((self.batch_size, 3, 1, 1,)).cuda() - 0.5) * 2 * self.std_p).exp()
def forward(self, img: torch.tensor) -> torch.tensor:
if self.shuffle_every:
self.shuffle()
return (img - self.mean) / self.std
class GaussianNoise(nn.Module):
def __init__(self, batch_size: int, shuffle_every: bool = False, std: float = 1., max_iter: int = 400):
super().__init__()
self.batch_size, self.std_p, self.max_iter = batch_size, std, max_iter
self.std = None
self.rem = max_iter - 1
self.shuffle()
self.shuffle_every = shuffle_every
def shuffle(self):
self.std = torch.randn(self.batch_size, 3, 1, 1).cuda() * self.rem * self.std_p / self.max_iter
self.rem = (self.rem - 1 + self.max_iter) % self.max_iter
def forward(self, img: torch.tensor) -> torch.tensor:
if self.shuffle_every:
self.shuffle()
return img + self.std
class ColorJitterR(ColorJitter):
def forward(self, img: torch.tensor) -> torch.tensor:
if self.shuffle_every:
self.shuffle()
return (img * self.std) + self.mean
class Centering(nn.Module):
def __init__(self, size: int, std: float):
super().__init__()
self.size = size
self.std = std
def forward(self, img: torch.tensor) -> torch.tensor:
pert = (torch.rand(2) * 2 - 1) * self.std
w, h = img.shape[-2:]
x = (pert[0] + w // 2 - self.size // 2).long().clamp(min=0, max=w - self.size)
y = (pert[1] + h // 2 - self.size // 2).long().clamp(min=0, max=h - self.size)
return img[:, :, x:x + self.size, y:y + self.size]
class Zoom(nn.Module):
def __init__(self, out_size: int = 384):
super().__init__()
self.up = torch.nn.Upsample(size=(out_size, out_size), mode='bilinear', align_corners=False).cuda()
def forward(self, img: torch.tensor) -> torch.tensor:
return self.up(img)
class Tile(nn.Module):
def __init__(self, rep: int = 384 // 16):
super().__init__()
self.rep = rep
def forward(self, x: torch.tensor) -> torch.tensor:
dim = x.dim()
if dim < 3:
raise NotImplementedError
elif dim == 3:
x.unsqueeze(0)
final_shape = x.shape[:2] + (x.shape[2] * self.rep, x.shape[3] * self.rep)
return x.unsqueeze(2).unsqueeze(4).repeat(1, 1, self.rep, 1, self.rep, 1).view(final_shape)
class RepeatBatch(nn.Module):
def __init__(self, repeat: int = 32):
super().__init__()
self.size = repeat
def forward(self, img: torch.tensor):
return img.repeat(self.size, 1, 1, 1)
class MaskBatch(nn.Module):
def forward(self, x: torch.tensor) -> torch.tensor:
return self.other(x[:self.count] if self.count > 0 else x)
def __init__(self, count: int = -1):
super().__init__()
self.count = count
class Flip(nn.Module):
def __init__(self, p: float = 0.5):
super().__init__()
self.p = p
def forward(self, x: torch.tensor) -> torch.tensor:
return torch.flip(x, dims=(3,)) if random.random() < self.p else x