-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathhelper.py
executable file
·393 lines (272 loc) · 9.19 KB
/
helper.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
"""Create images with simple shapes for use in ShapeShop. (28x28 pixels)
The first batch is for VGG16.
The second batch is for MNIST.
Most function contain the following inputs and outputs:
Args:
mnist_X_train_sample: a 28x28 image, can have MNIST digit information or be blank.
Returns:
image: the created image.
"""
from scipy.misc import imresize
import numpy as np
from scipy.ndimage import imread
from keras import backend as K
from scipy import ndimage
# Misc functions
def preprocess_image(image_path):
img_width = 28
img_height = 28
img = imresize(imread(image_path), (img_width, img_height)).astype('float32')
img = img.transpose((2, 0, 1))
img = img.astype('float32')
# img = img[None,]
return img
def deprocess_image(x):
# util function to convert a tensor into a valid image
# normalize tensor: center on 0., ensure std is 0.1
x -= x.mean()
x /= (x.std() + 1e-6)
x *= 0.1
# clip to [0, 1]
x += 0.5
x = np.clip(x, 0, 1)
# convert to RGB array
x *= 255
x = x.transpose((1, 2, 0))
x = np.clip(x, 0, 255).astype('uint8')
return x
def normalize(x):
# utility function to normalize a tensor by its L2 norm
return x / (K.sqrt(K.mean(K.square(x))) + 1e-5)
def make_box_img(img_width, img_height):
img = ndimage.gaussian_filter(np.random.random((1, 3, img_width, img_height))*1.0 + 1, 1)
img[0, 0, 24:40, 24:40] = 0.0
img[0, 1, 24:40, 24:40] = 0.0
img[0, 2, 24:40, 24:40] = 0.0
return img[0]
def make_noise_img(img_width, img_height):
img = ndimage.gaussian_filter(np.random.random((1, 3, img_width, img_height))*1.0 + 1, 1)
return img[0]
def make_box_img_no_blur(img_width, img_height):
img = np.random.random((1, 3, img_width, img_height))*1.0 + 1
img[0, 0, 24:40, 24:40] = 0.0
img[0, 1, 24:40, 24:40] = 0.0
img[0, 2, 24:40, 24:40] = 0.0
return img[0]
def make_noise_img_no_blur(img_width, img_height):
img = np.random.random((1, 3, img_width, img_height))*1.0 + 1
return img[0]
# Shape functions
def boxify_top_left(mnist_X_train_sample):
image_with_box = mnist_X_train_sample
image_with_box[0, 0:6, 0:6] = 1.0
return image_with_box
def boxify_top_right(mnist_X_train_sample):
image_with_box = mnist_X_train_sample
image_with_box[0, 0:6, 22:28] = 1.0
return image_with_box
def boxify_bottom_left(mnist_X_train_sample):
image_with_box = mnist_X_train_sample
image_with_box[0, 22:28, 0:6] = 1.0
return image_with_box
def boxify_bottom_right(mnist_X_train_sample):
image_with_box = mnist_X_train_sample
image_with_box[0, 22:28, 22:28] = 1.0
return image_with_box
def lineify_top_left(mnist_X_train_sample):
image_with_line = mnist_X_train_sample
image_with_line[0, 0:6, 2] = 1.0
image_with_line[0, 0:6, 3] = 1.0
return image_with_line
def lineify_bottom_right(mnist_X_train_sample):
image_with_line = mnist_X_train_sample
image_with_line[0, 22:28, 28-2] = 1.0
image_with_line[0, 22:28, 28-3] = 1.0
return image_with_line
def circleify_top_left(mnist_X_train_sample):
image_with_circle = mnist_X_train_sample
a, b = 3, 3
n = 7
r = 2
y, x = np.ogrid[-a:n-a, -b:n-b]
mask = x*x + y*y <= r*r
circle = np.ones((n, n))
circle = circle*mask
image_with_circle[0, 0:7, 0:7] = circle
return image_with_circle
def circleify_bottom_right(mnist_X_train_sample):
image_with_circle = mnist_X_train_sample
a, b = 3, 3
n = 7
r = 2
y, x = np.ogrid[-a:n-a, -b:n-b]
mask = x*x + y*y <= r*r
circle = np.ones((n, n))
circle = circle*mask
image_with_circle[0, 28-7:28, 28-7:28] = circle
return image_with_circle
def triangulify_top_left(mnist_X_train_sample):
image_with_tri = mnist_X_train_sample
n = 7
tri = np.zeros((n, n))
tri[3, 0:7] = 1.0
tri[2, 1:6] = 1.0
tri[1, 2:5] = 1.0
tri[0, 3] = 1.0
image_with_tri[0, 0:7, 0:7] = tri
return image_with_tri
def triangulify_bottom_right(mnist_X_train_sample):
image_with_tri = mnist_X_train_sample
n = 7
tri = np.zeros((n, n))
tri[3, 0:7] = 1.0
tri[2, 1:6] = 1.0
tri[1, 2:5] = 1.0
tri[0, 3] = 1.0
image_with_tri[0, 28-7:28, 28-7:28] = tri
return image_with_tri
def boxify_center(mnist_X_train_sample):
image_with_box = mnist_X_train_sample
image_with_box[0, 11-4:17+4, 11-4:17+4] = 1.0
return image_with_box
def lineify_center(mnist_X_train_sample):
image_with_line = mnist_X_train_sample
image_with_line[0, 11-4:17+4, 2+11] = 1.0
image_with_line[0, 11-4:17+4, 3+11] = 1.0
return image_with_line
def circleify_center(mnist_X_train_sample):
image_with_circle = mnist_X_train_sample
a, b = 7, 7
n = 15
r = 8
y, x = np.ogrid[-a:n-a, -b:n-b]
mask = x*x + y*y <= r*r
circle = np.ones((n, n))
circle = circle*mask
image_with_circle[0, 7:22, 7:22] = circle
return image_with_circle
def triangulify_center(mnist_X_train_sample):
image_with_tri = mnist_X_train_sample
a = 12
b = a/2
n = 7+a
tri = np.zeros((n, n))
tri[n-1, 0:n] = 1.0
tri[n-2, 1:n-1] = 1.0
tri[n-3, 2:n-2] = 1.0
tri[n-4, 3:n-3] = 1.0
tri[n-5, 4:n-4] = 1.0
tri[n-6, 5:n-5] = 1.0
tri[n-7, 6:n-6] = 1.0
tri[n-8, 7:n-7] = 1.0
tri[n-9, 8:n-8] = 1.0
tri[n-10, 9:n-9] = 1.0
image_with_tri[0, 11-b-5:18+b-5, 11-b:18+b] = tri
return image_with_tri
def noiseify():
image_with_noise = np.random.random([1, 28, 28])
return image_with_noise
def boxify_center_hollow(mnist_X_train_sample):
image_with_box = mnist_X_train_sample
border = 2
image_with_box[0, 11-4:17+4, 11-4:17+4] = 1.0
image_with_box[0, 11-4+border:17+4-border, 11-4+border:17+4-border] = 0.0
return image_with_box
def lineify_center_horizontal(mnist_X_train_sample):
image_with_line = mnist_X_train_sample
image_with_line[0, 2+11, 11-4:17+4] = 1.0
image_with_line[0, 3+11, 11-4:17+4] = 1.0
return image_with_line
def circleify_center_hollow(mnist_X_train_sample):
image_with_circle = mnist_X_train_sample
image_with_circle_big = np.copy(mnist_X_train_sample)
circleify_center(image_with_circle_big)
a, b = 7, 7
n = 15
r = 5
y, x = np.ogrid[-a:n-a, -b:n-b]
mask = x*x + y*y <= r*r
circle = np.ones((n, n))
circle = circle*mask
image_with_circle[0, 7:22, 7:22] = circle
final_image = image_with_circle_big[0]-image_with_circle[0]
return final_image[None, ]
def triangulify_center_hollow(mnist_X_train_sample):
image_with_tri = mnist_X_train_sample
image_with_tri_big = np.copy(mnist_X_train_sample)
triangulify_center(image_with_tri_big)
a = 6
b = a/2
n = 7+a
tri = np.zeros((n, n))
tri[n-1, 0:n] = 1.0
tri[n-2, 1:n-1] = 1.0
tri[n-3, 2:n-2] = 1.0
tri[n-4, 3:n-3] = 1.0
tri[n-5, 4:n-4] = 1.0
tri[n-6, 5:n-5] = 1.0
tri[n-7, 6:n-6] = 1.0
tri[n-8, 7:n-7] = 1.0
tri[n-9, 8:n-8] = 1.0
tri[n-10, 9:n-9] = 1.0
image_with_tri[0, 11-b-3:18+b-3, 11-b:18+b] = tri
final_image = image_with_tri_big[0] - image_with_tri[0]
return final_image[None, ]
def noiseify_blur():
image_with_noise = ndimage.gaussian_filter(np.random.random((1, 28, 28))*1.0, 1)
return image_with_noise
# Random shapes
def boxify(mnist_X_train_sample):
box_diameter = np.random.randint(28)
i = np.random.randint(28-box_diameter)
j = np.random.randint(28-box_diameter)
image_with_box = mnist_X_train_sample
image_with_box[0, i:i+box_diameter, j:j+box_diameter] = 1.0
return image_with_box
def lineify(mnist_X_train_sample):
line_diameter = np.random.randint(28)
i = np.random.randint(28-line_diameter)
j = np.random.randint(28-line_diameter)
image_with_line = mnist_X_train_sample
image_with_line[0, i:i+line_diameter, j] = 1.0
image_with_line[0, i:i+line_diameter, j+1] = 1.0
return image_with_line
def circleify(mnist_X_train_sample):
image_with_circle = mnist_X_train_sample
n = 28
r = np.random.randint(1, high=n/2)
a = np.random.randint(r, high=n-r)
b = np.random.randint(r, high=n-r)
y, x = np.ogrid[-a:n-a, -b:n-b]
mask = x*x + y*y <= r*r
circle = np.ones((n, n))
circle = circle*mask
image_with_circle[0, 0:n, 0:n] = circle
return image_with_circle
# Half shapes
def boxify_top_left_half(mnist_X_train_sample):
image_with_box = mnist_X_train_sample
image_with_box[0, 3:6, 3:6] = 1.0
return image_with_box
# Testing
def house(mnist_X_train_sample):
image_with_house = mnist_X_train_sample
box = boxify_center(np.zeros([1, 28, 28]))
shifted_box = np.roll(box[0], 5, axis=0)
tri = triangulify_center(np.zeros([1, 28, 28]))
shifted_tri = np.roll(tri[0], -7, axis=0)
image_with_house = shifted_box + shifted_tri
return image_with_house[None, ]
# Saving
# def save_image(data, cm, fn, dpi):
# sizes = np.shape(data)
# height = float(sizes[0])
# width = float(sizes[1])
# fig = plt.figure()
# fig.set_size_inches(width/height, 1, forward=False)
# ax = plt.Axes(fig, [0., 0., 1., 1.])
# ax.set_axis_off()
# fig.add_axes(ax)
# ax.matshow(data, cmap=cm)
# plt.savefig(fn, dpi=dpi)
# plt.close()