-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_augmentation.py
341 lines (267 loc) · 10.9 KB
/
data_augmentation.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
"""
This script allows for data augmentation using different methods.
"""
import numpy as np
from scipy import ndimage
# SECTION 1. Geometrical transformations
def identity():
"Returns a 2D-identity matrix"
T = np.eye(2)
return T
def scale(sx, sy):
"""
Returns the transformation matrix that scales an image.
Inputs are sx and sy (scale factors in x and y directions).
"""
T = np.array([[sx,0],[0,sy]])
return T
def rotate(phi):
"""
Returns the transformation matrix that performs a rotation on an image.
Input is phi; the angle (in rad) that the transformation matrix should produce.
"""
T = np.array([[np.cos(phi),-np.sin(phi)],[np.sin(phi),np.cos(phi)]])
return T
def shear(cx, cy):
"""
Returns the transformation matrix that performs a shearing on an image.
Inputs are cx and cy; the amount of shearing in x and y direction.
"""
T = np.array([[1,cx],[cy,1]])
return T
def reflect(rx, ry):
"""
Returns the transformation matrix that performs a reflection on an image.
Inputs are rx and ry (both should be either -1 or 1); this determines whether
the image should be reflected in x and/or y direction.
"""
allowed = [-1, 1]
if rx not in allowed or ry not in allowed:
T = 'Invalid input parameter'
return T
T = np.array([[rx,0],[0,ry]])
return T
def t2h(T, t=np.array([0,0])):
"""
Converts a 2D transformation matrix to homogeneous form. The default translation
vector is [0,0], movement of the image is not included,
"""
# Creates a row vector with zeroes with the size of T
n = np.zeros([1,T.shape[1]])
# Adds the row vector under the T matrix
T1 = np.concatenate((T,n))
# Adds a one under to the translation vector
tn = np.append(t,1)
# Creates the final homogeneous transformation matrix by adding the
# translation vector as the final row to the T1 matrix
Th = np.c_[T1,tn]
return Th
def c2h(X):
"""
Converts cartesian to homogeneous coordinates.
"""
# Creates a row vector with ones with in the size of X.
n = np.ones([1,X.shape[1]])
# Creates the homogeneous coordinates by adding the vector of ones
# under the matrix.
Xh = np.concatenate((X,n))
return Xh
def make_rotation(angle, img_shape):
T_1 = t2h(identity(), (img_shape[0]/2)*np.ones(2))
T_2 = t2h(rotate(angle), np.zeros(2))
T_3 = t2h(identity(), (img_shape[0]/2)*np.ones(2))
T_rot = T_1.dot(T_2).dot(T_3)
return T_rot
def image_transform(I, Th):
"""
This function transforms an image using the homogenous transformation matrix.
"""
output_shape = I.shape
# Spatial coordinates of the transformed image
x = np.arange(0, output_shape[1])
y = np.arange(0, output_shape[0])
xx, yy = np.meshgrid(x, y)
# Convert to a 2-by-p matrix (p is the number of pixels)
X = np.concatenate((xx.reshape((1, xx.size)), yy.reshape((1, yy.size))))
# Convert to homogeneous coordinates
Xh = c2h(X)
# Calculate the inverse of the homogenous transformation matrix.
Th_inv = np.linalg.inv(Th)
# Calculate the dot product from the inverted Th with the homogenous
# X-matrix.
Xt = Th_inv.dot(Xh)
# Calculate the transformed image.
It = ndimage.interpolation.map_coordinates(I, [Xt[1,:], Xt[0,:]], order=1, mode='constant').reshape(I.shape)
return It
def augment_data(data,labels,augm_nb_samples,transforms):
"""
DESCRIPTION:
-----------
This function takes all the data and increases the number of samples by
randomly selecting a transformation to be done.
Returns:
-------
data: TYPE np.ndarray
Array with images
labels: TYPE nd.ndarray
Array with the labels
"""
# Record the number of samples before augmentation.
nb_samples_start = data.shape[0]
# Make two lists for augmented samples and labels
augmented_data = []
augmented_labels = []
# Determine the possible values for the different transformations
reflect_options = [-1, 1]
scale_options = np.arange(1,2,0.05)
rotate_options = np.arange(-np.pi, np.pi, 0.05*np.pi)
shear_options = np.arange(-1,1,0.05)
gaussian_options = np.arange(0.5, 5.5, 0.5)
while len(augmented_data) <= augm_nb_samples:
# Select image to be transformed:
index = np.random.randint(nb_samples_start)
im = data[index,:,:,0]
im_label = labels[index]
# Select random transformation:
transformation = np.random.choice(transforms)
if transformation == 'reflect':
# Reflection:
rx, ry = 1, 1;
while rx == 1 and ry == 1:
rx = np.random.choice(reflect_options);
ry = np.random.choice(reflect_options);
T = reflect(rx,ry);
if transformation == 'scale':
# Scaling:
sx = np.random.choice(scale_options);
sy = np.random.choice(scale_options);
T = scale(sx,sy);
if transformation == 'rotate':
# Rotation:
angle = np.random.choice(rotate_options);
Th = make_rotation(angle, im.shape);
im_T = image_transform(im,Th);
if transformation == 'shear':
# Shearing:
cx = np.random.choice(shear_options);
cy = np.random.choice(shear_options);
T = shear(cx,cy);
if transformation == 'gaussblur':
# Gaussian blur:
sigma = np.random.choice(gaussian_options);
im_T = ndimage.gaussian_filter(im, sigma=sigma);
if transformation != 'gaussblur' and transformation != 'rotate':
# Do some steps which are not required in Gaussian blurring
# Check for singularity. When the matrix is singular, it cannot be
# inverted or applied to the image and this step is reset.
det = T[0,0]*T[1,1] - T[0,1]*T[1,0];
if det == 0: continue;
# Converts the 2D-transformation matrix to the homogenous form:
Th = t2h(T);
# Transform the image using the homogenous transformation matrix.
im_T = image_transform(im,Th);
# Add a new axis to be able to append to the data array
im_T = im_T[..., np.newaxis];
# Append data and labels to the augmented data lists
augmented_data.append(im_T);
augmented_labels.append(im_label);
# Convert the lists to arrays
print('{} samples augmented'.format(len(augmented_data)))
augmented_data = np.array(augmented_data);
augmented_labels = np.array(augmented_labels);
# Remove duplicate augmented samples
augmented_data_unique, i_unique = np.unique(augmented_data, return_index=True, axis=0);
augmented_labels_unique = augmented_labels[i_unique];
print('{} augmented samples left after removing duplicates'.format(augmented_data_unique.shape[0]))
# Return augmented samples
return augmented_data_unique, augmented_labels_unique
def augment_data_with_masks(data,masks,labels,augm_nb_samples,transforms):
"""
DESCRIPTION:
-----------
This function takes all the data and increases the number of samples by
randomly selecting a transformation to be done.
Returns:
-------
data: TYPE np.ndarray
Array with images
labels: TYPE nd.ndarray
Array with the labels
"""
# Record the number of samples before augmentation.
nb_samples_start = data.shape[0]
# Make two lists for augmented samples and labels
augmented_data = []
augmented_masks = []
augmented_labels = []
# Determine the possible values for the different transformations
reflect_options = [-1, 1]
scale_options = np.arange(1,2,0.05)
rotate_options = np.arange(-np.pi, np.pi, 0.05*np.pi)
shear_options = np.arange(-1,1,0.05)
gaussian_options = np.arange(0.5, 5.5, 0.5)
while len(augmented_data) <= augm_nb_samples:
# Select image to be transformed:
index = np.random.randint(nb_samples_start)
im = data[index,:,:,0]
msk = data[index,:,:,0]
im_label = labels[index]
# Select random transformation:
transformation = np.random.choice(transforms)
if transformation == 'reflect':
# Reflection:
rx, ry = 1, 1;
while rx == 1 and ry == 1:
rx = np.random.choice(reflect_options);
ry = np.random.choice(reflect_options);
T = reflect(rx,ry);
if transformation == 'scale':
# Scaling:
sx = np.random.choice(scale_options);
sy = np.random.choice(scale_options);
T = scale(sx,sy);
if transformation == 'rotate':
# Rotation:
angle = np.random.choice(rotate_options);
Th = make_rotation(angle, im.shape);
im_T = image_transform(im,Th);
msk_T = image_transform(msk, Th);
if transformation == 'shear':
# Shearing:
cx = np.random.choice(shear_options);
cy = np.random.choice(shear_options);
T = shear(cx,cy);
if transformation == 'gaussblur':
# Gaussian blur:
sigma = np.random.choice(gaussian_options);
im_T = ndimage.gaussian_filter(im, sigma=sigma);
if transformation != 'gaussblur' and transformation != 'rotate':
# Do some steps which are not required in Gaussian blurring
# Check for singularity. When the matrix is singular, it cannot be
# inverted or applied to the image and this step is reset.
det = T[0,0]*T[1,1] - T[0,1]*T[1,0];
if det == 0: continue;
# Converts the 2D-transformation matrix to the homogenous form:
Th = t2h(T);
# Transform the image using the homogenous transformation matrix.
im_T = image_transform(im,Th);
msk_T = image_transform(msk, Th);
# Add a new axis to be able to append to the data array
im_T = im_T[..., np.newaxis];
msk_T = msk_T[..., np.newaxis];
# Append data and labels to the augmented data lists
augmented_data.append(im_T);
augmented_masks.append(msk_T);
augmented_labels.append(im_label);
# Convert the lists to arrays
print('{} samples augmented'.format(len(augmented_data)))
augmented_data = np.array(augmented_data);
augmented_masks = np.array(augmented_data)
augmented_labels = np.array(augmented_labels);
# Remove duplicate augmented samples
augmented_data_unique, i_unique = np.unique(augmented_data, return_index=True, axis=0);
augmented_masks_unique = augmented_masks[i_unique];
augmented_labels_unique = augmented_labels[i_unique];
print('{} augmented samples left after removing duplicates'.format(augmented_data_unique.shape[0]))
# Return augmented samples
return augmented_data_unique, augmented_masks_unique, augmented_labels_unique