-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransformations.py
198 lines (142 loc) · 5.23 KB
/
transformations.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
#!/usr/bin/env python3
# Detailed description about image augmentation and how to use the code:
# https://medium.com/@stefan.herdy/how-to-augment-images-for-semantic-segmentation-2d7df97544de
import numpy as np
from sklearn.externals._pilutil import bytescale
import random
import matplotlib.pyplot as plt
import cv2
import random
import torch
import torchvision.transforms as transforms
def create_dense_target(tar: np.ndarray):
dense_tar = tar[:,:]
return dense_tar
def normalize_01(inp: np.ndarray):
inp_out = (inp - np.min(inp)) / np.ptp(inp)
return inp_out
def normalize(inp: np.ndarray, mean: float, std: float):
inp_out = (inp - mean) / std
return inp_out
def re_normalize(inp: np.ndarray,
low: int = 0,
high: int = 255
):
"""Normalize the data to a certain range. Default: [0-255]"""
inp_out = bytescale(inp, low=low, high=high)
return inp_out
class Compose:
"""
Composes several transformations together.
"""
def __init__(self, transforms: list):
self.transforms = transforms
def __call__(self, inp, target):
for t in self.transforms:
inp, target = t(inp, target)
return inp, target
def __repr__(self): return str([transform for transform in self.transforms])
class MoveAxis:
"""From [H, W, C] to [C, H, W]"""
def __init__(self, transform_input: bool = True, transform_target: bool = False):
self.transform_input = transform_input
self.transform_target = transform_target
def __call__(self, inp: np.ndarray, tar: np.ndarray):
inp = np.moveaxis(inp, -1, 0)
return inp, tar
def __repr__(self):
return str({self.__class__.__name__: self.__dict__})
class ColorTransformations:
def __init__(self):
pass
def __call__(self, inp: np.ndarray, tar: np.ndarray):
inp_tensor = torch.from_numpy(inp)
tar_tensor = torch.from_numpy(tar)
color_transform = transforms.Compose([
transforms.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2, hue=0.2),
])
inp_tensor = color_transform(inp_tensor)
inp = inp_tensor.numpy()
tar = tar_tensor.numpy()
return inp, tar
class ColorNoise:
def __init__(self, noise_std=0.05):
self.noise_std = noise_std
def __call__(self, inp: np.ndarray, tar: np.ndarray):
inp_tensor = torch.from_numpy(inp)
tar_tensor = torch.from_numpy(tar)
noise = torch.randn_like(inp_tensor) * self.noise_std
inp_tensor += noise
inp_tensor = torch.clamp(inp_tensor, 0, 1)
inp = inp_tensor.numpy()
tar = tar_tensor.numpy()
return inp, tar
class RandomFlip:
def __init__(self):
pass
def __call__(self, inp: np.ndarray, tar: np.ndarray):
rand = random.choice([0, 1])
if rand == 1:
inp = np.moveaxis(inp, 0, -1)
inp = cv2.flip(inp, 1)
inp = np.moveaxis(inp, -1, 0)
tar = np.ndarray.copy(np.fliplr(tar))
rand = random.choice([0, 1])
if rand == 1:
inp = np.moveaxis(inp, 0, -1)
inp = cv2.flip(inp, 0)
inp = np.moveaxis(inp, -1, 0)
tar = np.ndarray.copy(np.flipud(tar))
rand = random.choice([0, 1])
if rand == 1:
inp = np.ndarray.copy(np.rot90(inp, k=1, axes=(1, 2)))
tar = np.ndarray.copy(np.rot90(tar, k=1, axes=(0, 1)))
return inp, tar
def __repr__(self):
return str({self.__class__.__name__: self.__dict__})
class RandomCrop:
def __init__(self, crop_size):
self.crop_size = crop_size
pass
def __call__(self, inp: np.ndarray, tar: np.ndarray):
max_x = inp.shape[1] - self.crop_size
max_y = inp.shape[2] - self.crop_size
x = random.randint(0, max_x)
y = random.randint(0, max_y)
inp = np.moveaxis(inp, 0, -1)
inp = inp[x: x + self.crop_size, y: y + self.crop_size,:]
inp = np.moveaxis(inp, -1, 0)
tar = tar[x: x + self.crop_size, y: y + self.crop_size]
return inp, tar
class Resize:
def __init__(self, img_size):
self.img_size = img_size
pass
def __call__(self, inp: np.ndarray, tar: np.ndarray):
inp = np.moveaxis(inp, 0, -1)
inp = cv2.resize(inp, (self.img_size,self.img_size), interpolation = cv2.INTER_NEAREST)
inp = np.moveaxis(inp, -1, 0)
tar = cv2.resize(tar, (self.img_size,self.img_size), interpolation = cv2.INTER_NEAREST)
return inp, tar
class Normalize01:
"""Squash image input to the value range [0, 1] (no clipping)"""
def __init__(self):
pass
def __call__(self, inp, tar):
inp = normalize_01(inp)
return inp, tar
class Normalize:
"""Normalize based on mean and standard deviation."""
def __init__(self,
mean: float,
std: float,
transform_input=True,
transform_target=False
):
self.transform_input = transform_input
self.transform_target = transform_target
self.mean = mean
self.std = std
def __call__(self, inp, tar):
inp = normalize(inp)
return inp, tar