-
Notifications
You must be signed in to change notification settings - Fork 0
/
augmentations.py
35 lines (26 loc) · 939 Bytes
/
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
import funcy
import torch
import torchvision.transforms.functional as F
from torchvision import transforms
class MultiCompose(transforms.Compose):
def __call__(self, *imgs):
for t in self.transforms:
imgs = t(*imgs)
return imgs
class MultiRandomHorizontalFlip(transforms.RandomHorizontalFlip):
def forward(self, *imgs):
if torch.rand(1) < self.p:
return funcy.lmap(F.hflip, imgs)
return imgs
class MultiRandomVerticalFlip(transforms.RandomVerticalFlip):
def forward(self, *imgs):
if torch.rand(1) < self.p:
return funcy.lmap(F.vflip, imgs)
return imgs
class MultiRandomRotation(transforms.RandomRotation):
def forward(self, *imgs):
angle = self.get_params(self.degrees)
return funcy.lmap(
lambda img: F.rotate(img, angle, self.resample, self.expand, self.center, self.fill),
imgs
)