-
Notifications
You must be signed in to change notification settings - Fork 2
/
md_aug.py
241 lines (181 loc) · 8.34 KB
/
md_aug.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
import os
import random
import copy
import imageio
import string
import numpy as np
try: # SciPy >= 0.19
from scipy.special import comb
except ImportError:
from scipy.misc import comb
def bernstein_poly(i, n, t):
"""
The Bernstein polynomial of n, i as a function of t
"""
return comb(n, i) * (t ** (n - i)) * (1 - t) ** i
def bezier_curve(points, nTimes=1000):
"""
Given a set of control points, return the
bezier curve defined by the control points.
Control points should be a list of lists, or list of tuples
such as [ [1,1],
[2,3],
[4,5], ..[Xn, Yn] ]
nTimes is the number of time steps, defaults to 1000
See http://processingjs.nihongoresources.com/bezierinfo/
"""
nPoints = len(points)
xPoints = np.array([p[0] for p in points])
yPoints = np.array([p[1] for p in points])
t = np.linspace(0.0, 1.0, nTimes)
polynomial_array = np.array([bernstein_poly(i, nPoints - 1, t) for i in range(0, nPoints)])
xvals = np.dot(xPoints, polynomial_array)
yvals = np.dot(yPoints, polynomial_array)
return xvals, yvals
def data_augmentation(x, y, prob=0.5):
# augmentation by flipping
cnt = 3
while random.random() < prob and cnt > 0:
degree = random.choice([0, 1, 2])
x = np.flip(x, axis=degree)
y = np.flip(y, axis=degree)
cnt = cnt - 1
return x, y
def nonlinear_transformation(x, prob=0.9):
if random.random() >= prob:
return x
points = [[0, 0], [random.random(), random.random()], [random.random(), random.random()], [1, 1]]
xpoints = [p[0] for p in points]
ypoints = [p[1] for p in points]
xvals, yvals = bezier_curve(points, nTimes=500)
if random.random() < 0.5:
# Half change to get flip
xvals = np.sort(xvals)
else:
xvals, yvals = np.sort(xvals), np.sort(yvals)
nonlinear_x = np.interp(x, xvals, yvals)
return nonlinear_x
def local_pixel_shuffling(x, prob=0.5):
# if random.random() >= prob:
# return x
image_temp = copy.deepcopy(x)
orig_image = copy.deepcopy(x)
img_deps, img_rows, img_cols = x.shape
num_block = 40
for _ in range(num_block):
block_noise_size_x = random.randint(1, img_rows // 10)
block_noise_size_y = random.randint(1, img_cols // 10)
noise_x = random.randint(0, img_rows - block_noise_size_x)
noise_y = random.randint(0, img_cols - block_noise_size_y)
window = orig_image[:, noise_x:noise_x + block_noise_size_x, noise_y:noise_y + block_noise_size_y]
window = window.flatten()
np.random.shuffle(window)
window = window.reshape((img_deps, block_noise_size_x,
block_noise_size_y))
image_temp[:, noise_x:noise_x + block_noise_size_x,
noise_y:noise_y + block_noise_size_y] = window
local_shuffling_x = image_temp
return local_shuffling_x
def local_pixel_shuffling_500(x, prob=0.5):
if random.random() >= prob:
return x
image_temp = copy.deepcopy(x)
orig_image = copy.deepcopy(x)
img_deps, img_rows, img_cols = x.shape
num_block = 500
for _ in range(num_block):
block_noise_size_x = random.randint(1, img_rows // 10)
block_noise_size_y = random.randint(1, img_cols // 10)
noise_x = random.randint(0, img_rows - block_noise_size_x)
noise_y = random.randint(0, img_cols - block_noise_size_y)
window = orig_image[:, noise_x:noise_x + block_noise_size_x, noise_y:noise_y + block_noise_size_y]
window = window.flatten()
np.random.shuffle(window)
window = window.reshape((img_deps, block_noise_size_x,
block_noise_size_y))
image_temp[:, noise_x:noise_x + block_noise_size_x,
noise_y:noise_y + block_noise_size_y] = window
local_shuffling_x = image_temp
return local_shuffling_x
def image_in_painting(x):
img_deps, img_rows, img_cols = x.shape
cnt = 5
while cnt > 0 and random.random() < 0.95:
block_noise_size_x = random.randint(img_rows // 6, img_rows // 3)
block_noise_size_y = random.randint(img_cols // 6, img_cols // 3)
noise_x = random.randint(3, img_rows - block_noise_size_x - 3)
noise_y = random.randint(3, img_cols - block_noise_size_y - 3)
noise = np.random.rand(block_noise_size_x,block_noise_size_y) * 1.0
for i in range(0,img_deps):
x[i, noise_x:noise_x + block_noise_size_x,noise_y:noise_y + block_noise_size_y] = noise
cnt -= 1
return x
def image_out_painting(x):
img_deps, img_rows, img_cols = x.shape
image_temp = copy.deepcopy(x)
x = np.random.rand(x.shape[0], x.shape[1], x.shape[2] ) * 1.0
block_noise_size_x = img_rows - random.randint(3 * img_rows // 7, 4 * img_rows // 7)
block_noise_size_y = img_cols - random.randint(3 * img_cols // 7, 4 * img_cols // 7)
noise_x = random.randint(3, img_rows - block_noise_size_x - 3)
noise_y = random.randint(3, img_cols - block_noise_size_y - 3)
x[:, noise_x:noise_x + block_noise_size_x,
noise_y:noise_y + block_noise_size_y] = image_temp[:, noise_x:noise_x + block_noise_size_x,
noise_y:noise_y + block_noise_size_y]
cnt = 4
while cnt > 0 and random.random() < 0.95:
block_noise_size_x = img_rows - random.randint(3 * img_rows // 7, 4 * img_rows // 7)
block_noise_size_y = img_cols - random.randint(3 * img_cols // 7, 4 * img_cols // 7)
noise_x = random.randint(3, img_rows - block_noise_size_x - 3)
noise_y = random.randint(3, img_cols - block_noise_size_y - 3)
x[:, noise_x:noise_x + block_noise_size_x,
noise_y:noise_y + block_noise_size_y] = image_temp[:, noise_x:noise_x + block_noise_size_x,
noise_y:noise_y + block_noise_size_y]
cnt -= 1
return x
def paint(img,outpaint_rate = 0.8):
if random.random() < outpaint_rate:
return image_out_painting(img)
else:
return image_in_painting(img)
def generate_pair(img, batch_size, config, status="test"):
img_rows, img_cols, img_deps = img.shape[2], img.shape[3], img.shape[4]
while True:
index = [i for i in range(img.shape[0])]
random.shuffle(index)
y = img[index[:batch_size]]
x = copy.deepcopy(y)
for n in range(batch_size):
# Autoencoder
x[n] = copy.deepcopy(y[n])
# Flip
x[n], y[n] = data_augmentation(x[n], y[n], config.flip_rate)
# Local Shuffle Pixel
x[n] = local_pixel_shuffling(x[n], prob=config.local_rate)
# Apply non-Linear transformation with an assigned probability
x[n] = nonlinear_transformation(x[n], config.nonlinear_rate)
# Inpainting & Outpainting
if random.random() < config.paint_rate:
if random.random() < config.inpaint_rate:
# Inpainting
x[n] = image_in_painting(x[n])
else:
# Outpainting
x[n] = image_out_painting(x[n])
# Save sample images module
if config.save_samples is not None and status == "train" and random.random() < 0.01:
n_sample = random.choice([i for i in range(config.batch_size)])
sample_1 = np.concatenate(
(x[n_sample, 0, :, :, 2 * img_deps // 6], y[n_sample, 0, :, :, 2 * img_deps // 6]), axis=1)
sample_2 = np.concatenate(
(x[n_sample, 0, :, :, 3 * img_deps // 6], y[n_sample, 0, :, :, 3 * img_deps // 6]), axis=1)
sample_3 = np.concatenate(
(x[n_sample, 0, :, :, 4 * img_deps // 6], y[n_sample, 0, :, :, 4 * img_deps // 6]), axis=1)
sample_4 = np.concatenate(
(x[n_sample, 0, :, :, 5 * img_deps // 6], y[n_sample, 0, :, :, 5 * img_deps // 6]), axis=1)
final_sample = np.concatenate((sample_1, sample_2, sample_3, sample_4), axis=0)
final_sample = final_sample * 255.0
final_sample = final_sample.astype(np.uint8)
file_name = ''.join(
[random.choice(string.ascii_letters + string.digits) for n in range(10)]) + '.' + config.save_samples
imageio.imwrite(os.path.join(config.sample_path, config.exp_name, file_name), final_sample)
yield (x, y)