-
Notifications
You must be signed in to change notification settings - Fork 8
/
custom_augmentations.py
48 lines (39 loc) · 1.3 KB
/
custom_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
import numpy as np
import random
class Mirror(object):
"""Mirror image."""
def __init__(self, p=0.5, always_apply=False):
self.p = p
self.always_apply = always_apply
def __call__(self, imgdata, fptdata):
if self.always_apply:
self.p = 0
if self.p < random.random():
imgdata = np.flip(imgdata, 2)
fptdata = np.flip(fptdata, 1)
return imgdata, fptdata
class Flip(object):
"""Flip image."""
def __init__(self, p=0.5, always_apply=False):
self.p = p
self.always_apply = always_apply
def __call__(self, imgdata, fptdata):
if self.always_apply:
self.p = 0
if self.p < random.random():
imgdata = np.flip(imgdata, 1)
fptdata = np.flip(fptdata, 0)
return imgdata, fptdata
class Rotate(object):
"""Rotate image."""
def __init__(self, p=0.5, always_apply=False):
self.p = p
self.always_apply = always_apply
def __call__(self, imgdata, fptdata):
if self.always_apply:
self.p = 0
if self.p < random.random():
rot = np.random.randint(0, 4)
imgdata = np.rot90(imgdata, rot, axes=(1, 2))
fptdata = np.rot90(fptdata, rot, axes=(0, 1))
return imgdata, fptdata