-
Notifications
You must be signed in to change notification settings - Fork 10
/
keras_transforms.py
214 lines (177 loc) · 7.01 KB
/
keras_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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import numpy as np
import scipy.ndimage as ndi
from six.moves import range
import time
import cv2
import random
import matplotlib.pyplot as plt
def channel_shift(xs, intensity, channel_axis):
ys = []
for x in xs:
if x.ndim == 3: # image
x = np.rollaxis(x, channel_axis, 0)
min_x, max_x = np.min(x), np.max(x)
channel_images = [np.clip(x_channel + intensity, min_x, max_x)
for x_channel in x]
x = np.stack(channel_images, axis=0)
x = np.rollaxis(x, 0, channel_axis + 1)
ys.append(x)
else:
ys.append(x)
return ys
def apply_transforms(xs, transform_matrix, output_shape=None):
"""Apply the image transformation specified by a matrix.
"""
final_affine_matrix = transform_matrix[:2, :2]
final_offset = transform_matrix[:2, 2]
print('ke:', transform_matrix)
ys = []
for x in xs:
if x.ndim == 3: # image
x = np.rollaxis(x, 2, 0)
channel_images = [ndi.interpolation.affine_transform(x_channel,
final_affine_matrix,
final_offset,
order=1,
output_shape=output_shape,
mode='constant',
cval=0) for x_channel in x]
x = np.stack(channel_images, axis=0)
x = np.rollaxis(x, 0, 2 + 1)
ys.append(x)
else: # mask
x = ndi.interpolation.affine_transform(x,
final_affine_matrix,
final_offset,
order=0,
output_shape=output_shape,
mode='constant',
cval=0)
ys.append(x)
return ys
def apply_transforms_cv(xs, M):
"""Apply the image transformation specified by a matrix.
"""
dsize = (np.int(xs[0].shape[1]), np.int(xs[0].shape[0]))
aff = M[:2, :2]
off = M[:2, 2]
cvM = np.zeros_like(M[:2, :])
# cvM[:2,:2] = aff
cvM[:2,:2] = np.flipud(np.fliplr(aff))
# cvM[:2,:2] = np.transpose(aff)
cvM[:2, 2] = np.flip(off, axis=0)
ys = []
for x in xs:
if x.ndim == 3: # image
x = cv2.warpAffine(x, cvM, dsize, flags=cv2.INTER_LINEAR)
ys.append(x)
# M = cv2.getRotationMatrix2D((dsize[0] // 2, dsize[1] // 2), angle, 1)
# _img = cv2.warpAffine(_img, M, dsize, flags=cv2.INTER_LINEAR)#, borderMode=cv2.BORDER_REPLICATE)
# _mask = cv2.warpAffine(_mask, M, dsize, flags=cv2.INTER_NEAREST)#, borderMode=cv2.BORDER_REPLICATE)
# _oomk = cv2.warpAffine(_oomk, M, dsize, flags=cv2.INTER_NEAREST)
else: # mask
x = cv2.warpAffine(x, cvM, dsize, flags=cv2.INTER_NEAREST)
ys.append(x)
return ys
def transform_matrix_offset_center(matrix, x, y):
o_x = float(x) / 2 + 0.5
o_y = float(y) / 2 + 0.5
offset_matrix = np.array([[1, 0, o_x], [0, 1, o_y], [0, 0, 1]])
reset_matrix = np.array([[1, 0, -o_x], [0, 1, -o_y], [0, 0, 1]])
transform_matrix = np.dot(np.dot(offset_matrix, matrix), reset_matrix)
return transform_matrix
def flip_axis(xs, axis):
ys = []
for x in xs:
x = np.asarray(x).swapaxes(axis, 0)
x = x[::-1, ...]
x = x.swapaxes(0, axis)
ys.append(x)
return ys
def random_transform(xs, rnd,
rt=False, # rotation
hs=False, # height_shift
ws=False, # width_shift
sh=False, # shear
zm=[1,1], # zoom
sc=[1,1],
cs=False, # channel shift
hf=False): # horizontal flip
"""Randomly augment a single image tensor.
"""
# x is a single image, so it doesn't have image number at index 0
img_row_axis = 0
img_col_axis = 1
img_channel_axis = 2
h, w = xs[0].shape[img_row_axis], xs[0].shape[img_col_axis]
# use composition of homographies
# to generate final transform that needs to be applied
if rt:
theta = np.pi / 180 * rnd.uniform(-rt, rt)
else:
theta = 0
if hs:
tx = rnd.uniform(-hs, hs) * h
else:
tx = 0
if ws:
ty = rnd.uniform(-ws, ws) * w
else:
ty = 0
if sh:
shear = np.pi / 180 * rnd.uniform(-sh, sh)
else:
shear = 0
if zm[0] == 1 and zm[1] == 1:
zx, zy = 1, 1
else:
zx = rnd.uniform(zm[0], zm[1])
zy = rnd.uniform(zm[0], zm[1])
if sc[0] == 1 and sc[1] == 1:
zx, zy = zx, zy
else:
s = rnd.uniform(sc[0], sc[1])
zx = zx * s
zy = zy * s
transform_matrix = None
if theta != 0:
rotation_matrix = np.array([[np.cos(theta), -np.sin(theta), 0],
[np.sin(theta), np.cos(theta), 0],
[0, 0, 1]])
transform_matrix = rotation_matrix
if tx != 0 or ty != 0:
shift_matrix = np.array([[1, 0, tx],
[0, 1, ty],
[0, 0, 1]])
transform_matrix = shift_matrix if transform_matrix is None else np.dot(transform_matrix, shift_matrix)
if shear != 0:
if rnd.random() < 0.5:
shear_matrix = np.array([[1, -np.sin(shear), 0],
[0, np.cos(shear), 0],
[0, 0, 1]])
else:
shear_matrix = np.array([[np.cos(shear), 0, 0],
[np.sin(shear), 1, 0],
[0, 0, 1]])
transform_matrix = shear_matrix if transform_matrix is None else np.dot(transform_matrix, shear_matrix)
if zx != 1 or zy != 1:
zoom_matrix = np.array([[zx, 0, 0],
[0, zy, 0],
[0, 0, 1]])
transform_matrix = zoom_matrix if transform_matrix is None else np.dot(transform_matrix, zoom_matrix)
if transform_matrix is not None:
transform_matrix = transform_matrix_offset_center(transform_matrix, h, w)
xs = apply_transforms_cv(xs, transform_matrix)
# plt.figure(1)
# plt.subplot(2,1,1); plt.imshow(xs[0])
# plt.subplot(2,1,2); plt.imshow(xs[1])
# plt.show()
if cs != 0:
intensity = rnd.uniform(-cs, cs)
xs = channel_shift(xs,
intensity,
img_channel_axis)
if hf:
if rnd.random() < 0.5:
xs = flip_axis(xs, img_col_axis)
return xs