-
Notifications
You must be signed in to change notification settings - Fork 15
/
augmentations.py
81 lines (67 loc) · 3.32 KB
/
augmentations.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
import torch
from torch import nn
from torch.nn import functional as F
def rotation():
def _transform(model, images, labels=None):
size = images.shape[1:]
return torch.stack([torch.rot90(images, k, (2, 3)) for k in range(4)], 1).view(-1, *size)
return _transform, 4
def rotation2():
def _transform(model, images, labels=None):
size = images.shape[1:]
return torch.stack([torch.rot90(images, k, (2, 3)) for k in [0, 2]], 1).view(-1, *size)
return _transform, 2
def color_perm():
def _transform(model, images, labels=None):
size = images.shape[1:]
images = torch.stack([images,
torch.stack([images[:, 0, :, :], images[:, 2, :, :], images[:, 1, :, :]], 1),
torch.stack([images[:, 1, :, :], images[:, 0, :, :], images[:, 2, :, :]], 1),
torch.stack([images[:, 1, :, :], images[:, 2, :, :], images[:, 0, :, :]], 1),
torch.stack([images[:, 2, :, :], images[:, 0, :, :], images[:, 1, :, :]], 1),
torch.stack([images[:, 2, :, :], images[:, 1, :, :], images[:, 0, :, :]], 1)], 1).view(-1, *size)
return images.contiguous()
return _transform, 6
def color_perm3():
def _transform(model, images, labels=None):
size = images.shape[1:]
images = torch.stack([images,
torch.stack([images[:, 1, :, :], images[:, 2, :, :], images[:, 0, :, :]], 1),
torch.stack([images[:, 2, :, :], images[:, 0, :, :], images[:, 1, :, :]], 1)], 1).view(-1, *size)
return images.contiguous()
return _transform, 3
def rot_color_perm6():
def _transform(model, images, labels=None):
size = images.shape[1:]
out = []
for x in [images, torch.rot90(images, 2, (2, 3))]:
out.append(x)
out.append(torch.stack([x[:, 1, :, :], x[:, 2, :, :], x[:, 0, :, :]], 1))
out.append(torch.stack([x[:, 2, :, :], x[:, 0, :, :], x[:, 1, :, :]], 1))
return torch.stack(out, 1).view(-1, *size).contiguous()
return _transform, 6
def rot_color_perm12():
def _transform(model, images, labels=None):
size = images.shape[1:]
out = []
for k in range(4):
x = torch.rot90(images, k, (2, 3))
out.append(x)
out.append(torch.stack([x[:, 1, :, :], x[:, 2, :, :], x[:, 0, :, :]], 1))
out.append(torch.stack([x[:, 2, :, :], x[:, 0, :, :], x[:, 1, :, :]], 1))
return torch.stack(out, 1).view(-1, *size).contiguous()
return _transform, 12
def rot_color_perm24():
def _transform(model, images, labels=None):
size = images.shape[1:]
out = []
for k in range(4):
x = torch.rot90(images, k, (2, 3))
out.append(x)
out.append(torch.stack([x[:, 0, :, :], x[:, 2, :, :], x[:, 1, :, :]], 1))
out.append(torch.stack([x[:, 1, :, :], x[:, 0, :, :], x[:, 2, :, :]], 1))
out.append(torch.stack([x[:, 1, :, :], x[:, 2, :, :], x[:, 0, :, :]], 1))
out.append(torch.stack([x[:, 2, :, :], x[:, 0, :, :], x[:, 1, :, :]], 1))
out.append(torch.stack([x[:, 2, :, :], x[:, 1, :, :], x[:, 0, :, :]], 1))
return torch.stack(out, 1).view(-1, *size).contiguous()
return _transform, 24