-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdata_transforms.py
272 lines (214 loc) · 9.08 KB
/
data_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
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
from collections import namedtuple
import numpy as np
import scipy.ndimage
import math
import skimage.io
import skimage.transform
from collections import defaultdict
import utils_plots
import app
rng = np.random.RandomState(37145)
def pixelnorm(x, MIN=0, MAX=61440.0):
x = (x - MIN) / (MAX - MIN)
return x
default_augmentation_params = {
'zoom_range': (1 / 1.1, 1.1),
'rotation_range': (0, 360),
'shear_range': (0, 0),
'translation_range': (-4, 4),
'do_flip': True,
'allow_stretch': False,
}
no_augmentation_params = {
'zoom_range': (1.0, 1.0),
'rotation_range': (0, 0),
'shear_range': (0, 0),
'translation_range': (0, 0),
'do_flip': False,
'allow_stretch': False,
}
def random_lossless(x, p_aug, rng):
rot90_value = rng.choice(p_aug['rot90_values'])
flip = rng.choice(p_aug['flip'])
x = apply_lossless(x, rot90_value, flip)
return x
def apply_lossless(x, rot90_value, flip):
if rot90_value:
x = np.rot90(x, k=rot90_value, axes=(1, 2))
if flip:
x = np.flip(x, 1)
return x
def rescale(x, factor):
return skimage.transform.rescale(x, factor)
def generate_all_lossless(x, p_aug):
augmentations = []
for rot90_value in p_aug['rot90_values']:
for flip in p_aug['flip']:
augmentations.append(apply_lossless(x, rot90_value, flip))
return np.stack(augmentations)
def generate_all_lossless_plus_translation(x, p_aug, p_transform=None):
augmentations = []
pert_aug = dict((k, p_aug[k]) for k in (
'zoom_range', 'rotation_range', 'shear_range', 'translation_range', 'do_flip', 'allow_stretch') if
k in p_aug)
for rot90_value in p_aug['rot90_values']:
for flip in p_aug['flip']:
lossless = apply_lossless(x, rot90_value, flip)
for i in [-1, 1, 0]:
lost = perturb(lossless, pert_aug, p_transform['patch_size'], rng, n_channels=p_transform["channels"])
augmentations.append(lost)
return np.stack(augmentations)
# def tiling(img, tile_shape):
# tiles = []
# for x in range(0,img.shape[0], tile_shape[0]):
# for y in range(0,img.shape[1], tile_shape[1]):
# for z in range(0,img.shape[2], tile_shape[2]):
# print x,y,z
# tiles.append(img[x:x+tile_shape[0],y:y+tile_shape[1],z:z+tile_shape[2]])
# return tiles
#
# def _test_tiling():
# tif = app.read_image('train', 0)
# print tif.shape
# tiles = tiling(tif,(4,128,128))
# for tile in tiles:
# print tile.shape
def random_crop_x(x, w, h, rng):
o_w = x.shape[1]
o_h = x.shape[2]
w_range = o_w - w
h_range = o_h - h
w_offset = rng.choice(np.arange(w_range))
h_offset = rng.choice(np.arange(h_range))
return x[:, w_offset:w_offset + w, h_offset:h_offset + h]
def random_crop_x_y(x, y, w, h, rng):
o_w = x.shape[1]
o_h = x.shape[2]
w_range = o_w - w
h_range = o_h - h
w_offset = rng.choice(np.arange(w_range))
h_offset = rng.choice(np.arange(h_range))
return x[:, w_offset:w_offset + w, h_offset:h_offset + h], y[:, w_offset:w_offset + w, h_offset:h_offset + h]
def build_center_uncenter_transforms(image_shape):
"""
These are used to ensure that zooming and rotation happens around the center of the image.
Use these transforms to center and uncenter the image around such a transform.
"""
center_shift = np.array(
[image_shape[1], image_shape[0]]) / 2.0 - 0.5 # need to swap rows and cols here apparently! confusing!
tform_uncenter = skimage.transform.SimilarityTransform(translation=-center_shift)
tform_center = skimage.transform.SimilarityTransform(translation=center_shift)
return tform_center, tform_uncenter
def build_centering_transform(image_shape, target_shape=(50, 50)):
rows, cols = image_shape
trows, tcols = target_shape
shift_x = (cols - tcols) / 2.0
shift_y = (rows - trows) / 2.0
return skimage.transform.SimilarityTransform(translation=(shift_x, shift_y))
def fast_warp(img, tf, output_shape=(50, 50), mode='constant', order=1):
"""
This wrapper function is faster than skimage.transform.warp
"""
m = tf.params # tf._matrix is
return skimage.transform._warps_cy._warp_fast(img, m, output_shape=output_shape, mode=mode, order=order)
def random_perturbation_transform(zoom_range, rotation_range, shear_range, translation_range, do_flip=True,
allow_stretch=False, rng=np.random):
shift_x = rng.uniform(*translation_range)
shift_y = rng.uniform(*translation_range)
translation = (shift_x, shift_y)
rotation = rng.uniform(*rotation_range)
shear = rng.uniform(*shear_range)
if do_flip:
flip = (rng.randint(2) > 0) # flip half of the time
else:
flip = False
# random zoom
log_zoom_range = [np.log(z) for z in zoom_range]
if isinstance(allow_stretch, float):
log_stretch_range = [-np.log(allow_stretch), np.log(allow_stretch)]
zoom = np.exp(rng.uniform(*log_zoom_range))
stretch = np.exp(rng.uniform(*log_stretch_range))
zoom_x = zoom * stretch
zoom_y = zoom / stretch
elif allow_stretch is True: # avoid bugs, f.e. when it is an integer
zoom_x = np.exp(rng.uniform(*log_zoom_range))
zoom_y = np.exp(rng.uniform(*log_zoom_range))
else:
zoom_x = zoom_y = np.exp(rng.uniform(*log_zoom_range))
# the range should be multiplicatively symmetric, so [1/1.1, 1.1] instead of [0.9, 1.1] makes more sense.
return build_augmentation_transform((zoom_x, zoom_y), rotation, shear, translation, flip)
def build_augmentation_transform(zoom=(1.0, 1.0), rotation=0, shear=0, translation=(0, 0), flip=False):
if flip:
shear += 180
rotation += 180
# shear by 180 degrees is equivalent to rotation by 180 degrees + flip.
# So after that we rotate it another 180 degrees to get just the flip.
tform_augment = skimage.transform.AffineTransform(scale=(1 / zoom[0], 1 / zoom[1]), rotation=np.deg2rad(rotation),
shear=np.deg2rad(shear), translation=translation)
return tform_augment
def PCA_augmentation(batch, l, V):
for i in xrange(batch.shape[0]):
alpha = np.random.randn(3) * 0.1
noise = np.dot(V, alpha * l)
batch[i, :, :, :] = batch[i, :, :, :] + noise[:, np.newaxis, np.newaxis]
return batch
def perturb(img, augmentation_params, target_shape, rng=rng, n_channels=4):
# # DEBUG: draw a border to see where the image ends up
# img[0, :] = 0.5
# img[-1, :] = 0.5
# img[:, 0] = 0.5
# img[:, -1] = 0.5
img_spat_shape = (img.shape[1], img.shape[2])
tform_centering = build_centering_transform(img_spat_shape, target_shape)
tform_center, tform_uncenter = build_center_uncenter_transforms(img_spat_shape)
tform_augment = random_perturbation_transform(rng=rng, **augmentation_params)
tform_augment = tform_uncenter + tform_augment + tform_center # shift to center, augment, shift back (for the rotation/shearing)
chs = []
for ch in range(n_channels):
ch_warped = fast_warp(img[ch], tform_centering + tform_augment, output_shape=target_shape,
mode='constant').astype('float32')
chs.append(ch_warped)
out_img = np.stack(chs, axis=0)
return out_img
def _print_stats_channels(img, channel_stats, channel_data):
n_channels = img.shape[-1]
for ch in range(n_channels):
print('ch', ch, )
ch_data = img[:, :, ch]
channel_data[ch].append(img[:, :, ch])
print('max', np.amax(ch_data), )
channel_stats[str(ch) + 'max'].append(np.amax(ch_data))
print('min', np.amin(ch_data), )
channel_stats[str(ch) + 'min'].append(np.amin(ch_data))
print('avg', np.average(ch_data), )
channel_stats[str(ch) + 'avg'].append(np.average(ch_data))
print('std', np.std(ch_data), )
channel_stats[str(ch) + 'std'].append(np.std(ch_data))
print('var', np.var(ch_data))
channel_stats[str(ch) + 'var'].append(np.var(ch_data))
if __name__ == "__main__":
_test_tiling()
# channel_stats = defaultdict(list)
# channel_data = defaultdict(list)
# for i in range(2000):
# #read in image
# print '***** ',i
# tif = app.read_image('train', i)
# print tif.shape
# utils_plots.plot_img(tif,'plots/test'+str(i)+'.jpg')
# p_tif = perturb(tif, default_augmentation_params, (256,256), rng)
# utils_plots.plot_img(p_tif,'plots/test'+str(i)+'_random_augm.jpg')
# tif = tif.astype('float32')
# print tif.shape
# print tif.dtype
# _print_stats_channels(tif,channel_stats,channel_data)
# print 'overall stats'
# for ch in range(4):
# print 'ch', ch,
# ch_data = np.concatenate(channel_data[ch])
# for p in [0.1,0.5,1,5,10,50,90,95,99,99.5,99.9]:
# print p, '-', np.percentile(ch_data,p), '|',
# print
# print 'avg', np.average(ch_data)
# print 'std', np.std(ch_data)
# utils_plots.show_img(calibrate_image(tif[:,:,:3]))