-
Notifications
You must be signed in to change notification settings - Fork 3
/
np_transforms.py
113 lines (96 loc) · 3.61 KB
/
np_transforms.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
from __future__ import division
import random
import skimage.transform as SkT
import torchvision.transforms.functional as TF
class ToTensor(object):
r"""
Convert a ``numpy.ndarray`` to tensor.
"""
def __call__(self, sample):
"""
Args:
sample: list containing images as np.arrays.
Returns:
sample: list containing images as tensors.
"""
return [TF.to_tensor(image) for image in sample]
class Scale(object):
r"""Rescale the image in a sample to a given size.
Args:
output_size (tuple or int): desired output size; if tuple, output is
matched to output_size; if int, smaller of image edges is matched
to output_size keeping aspect ratio the same.
"""
def __init__(self, output_size):
assert isinstance(output_size, (int, tuple))
self.output_size = output_size
def __call__(self, sample):
r"""
Args:
sample: list containing images as np.arrays.
Returns:
sample: list containing scaled images as np.arrays.
"""
return [SkT.resize(image, self.output_size) for image in sample]
class RandomHorizontalFlip(object):
r"""Horizontally flip the given numpy array randomly with a probability of 0.5 by default."""
def __init__(self, prob=0.5, keep_state=False):
r"""
Args:
prob: probability of flipping the image (default: 0.5).
keep_state: whether or not to keep using the same transformation until rand_state is reset (default: `False`).
"""
self.prob = prob
self.keep_state = keep_state
self.rand_state = None
def __call__(self, sample):
r"""
Args:
sample: list containing images as np.arrays
Returns:
output: list containing flipped images as np.arrays
"""
if self.rand_state or ((self.rand_state is None) and (random.random() < self.prob)):
if self.keep_state:
self.rand_state = True
output = []
for image in sample:
output.append(image[:, ::-1, :].copy())
return output
else:
if self.keep_state:
self.rand_state = False
return sample
def reset_rand_state(self):
self.rand_state = None
class RandomVerticalFlip(object):
r"""Vertically flip the given numpy array randomly with a probability of 0.5 by default."""
def __init__(self, prob=0.5, keep_state=False):
r"""
Args:
prob: probability of flipping the image (default: 0.5)
keep_state: whether or not to keep using the same transformation until rand_state is reset (default: `False`)
"""
self.prob = prob
self.keep_state = keep_state
self.rand_state = None
def __call__(self, sample):
r"""
Args:
sample: list containing images as np.arrays
Returns:
output: list containing flipped images as np.arrays
"""
if self.rand_state or ((self.rand_state is None) and (random.random() < self.prob)):
if self.keep_state:
self.rand_state = True
output = []
for image in sample:
output.append(image[::-1, :, :].copy())
return output
else:
if self.keep_state:
self.rand_state = False
return sample
def reset_rand_state(self):
self.rand_state = None