-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpreprocessing_util.py
54 lines (41 loc) · 1.79 KB
/
preprocessing_util.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
# -*- coding: utf-8 -*-
import numpy as np
def autoencoder_vae_add_noise(batch_xs,batch_ys):
#add noising step
batch_xs_target = batch_xs
batch_xs = batch_xs * np.random.randint(2, size=batch_xs.shape)
batch_xs += np.random.randint(2, size=batch_xs.shape)
batch_xs = batch_xs
batch_ys = batch_xs_target
return batch_xs,batch_ys
# Augment training data
def expend_training_data(images, labels):
expanded_images = []
expanded_labels = []
j = 0 # counter
for x, y in zip(images, labels):
j = j+1
if j%100==0:
print ('expanding data : %03d / %03d' % (j,numpy.size(images,0)))
# register original data
expanded_images.append(x)
expanded_labels.append(y)
# get a value for the background
# zero is the expected value, but median() is used to estimate background's value
bg_value = np.median(x) # this is regarded as background's value
image = np.reshape(x, (-1, 28))
for i in range(4):
# rotate the image with random degree
angle = np.random.randint(-15,15,1)
new_img = ndimage.rotate(image,angle,reshape=False, cval=bg_value)
# shift the image with random distance
shift = np.random.randint(-2, 2, 2)
new_img_ = ndimage.shift(new_img,shift, cval=bg_value)
# register new training data
expanded_images.append(np.reshape(new_img_, 784))
expanded_labels.append(y)
# images and labels are concatenated for random-shuffle at each epoch
# notice that pair of image and label should not be broken
expanded_train_total_data = np.concatenate((expanded_images, expanded_labels), axis=1)
np.random.shuffle(expanded_train_total_data)
return expanded_train_total_data