-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata_utils.py
175 lines (140 loc) · 5.92 KB
/
data_utils.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
import numpy as np
import os
import cv2
import scipy
import random
SMILE_FOLDER = './data/smile_data/'
EMOTION_FOLDER = './data/emotion_data2/'
GENDER_FOLDER = './data/wiki_data_gender/'
IMDB_FOLDER = './data/imdb_data/'
AGE_FOLDER = './data/age_data/'
NUM_SMILE_IMAGE = 4000
SMILE_SIZE = 48
EMOTION_SIZE = 48
AGE_SIZE = 48
def getAgeImage():
print('Load age image..................')
X1 = np.load(AGE_FOLDER + 'train.npy', allow_pickle=True)
X2 = np.load(AGE_FOLDER + 'test.npy', allow_pickle=True)
train_data = []
test_data = []
for i in range(X1.shape[0]):
train_data.append(X1[i])
for i in range(X2.shape[0]):
test_data.append(X2[i])
print('Done !')
print('Number of age train data: ', str(len(train_data)))
print('---------------------------------------------------------------')
return train_data, test_data
def getSmileImage():
print('Load smile image...................')
X1 = np.load(SMILE_FOLDER + 'train.npy', allow_pickle=True)
X2 = np.load(SMILE_FOLDER + 'test.npy', allow_pickle=True)
train_data = []
test_data = []
for i in range(X1.shape[0]):
train_data.append(X1[i])
for i in range(X2.shape[0]):
test_data.append(X2[i])
print('Done !')
print('Number of smile train data: ', str(len(train_data)))
print('---------------------------------------------------------------')
return train_data, test_data
def getGenderImage():
print('Load gender image...................')
X1 = np.load(GENDER_FOLDER + 'train.npy', allow_pickle=True)
X2 = np.load(GENDER_FOLDER + 'test.npy', allow_pickle=True)
train_data = []
test_data = []
for i in range(X1.shape[0]):
train_data.append(X1[i])
for i in range(X2.shape[0]):
test_data.append(X2[i])
print('Done !')
print('Number of gender train data: ', str(len(train_data)))
print('---------------------------------------------------------------')
return train_data, test_data
def getEmotionImage():
print('Load emotion image..................')
train_images, train_labels, validation_images, validation_labels = [], [], [], []
if os.path.isfile(EMOTION_FOLDER + 'temp_train_and_validation/train/images.npy'):
train_images = np.load(EMOTION_FOLDER + 'temp_train_and_validation/train/images.npy', allow_pickle=True)
train_labels = np.load(EMOTION_FOLDER + 'temp_train_and_validation/train/labels.npy', allow_pickle=True)
validation_images = np.load(EMOTION_FOLDER + 'temp_train_and_validation/validation/images.npy', allow_pickle=True)
validation_labels = np.load(EMOTION_FOLDER + 'temp_train_and_validation/validation/labels.npy', allow_pickle=True)
train_data = []
for i in range(len(train_images)):
T = train_images[i]
label = train_labels[i]
train_data.append((T, label))
for i in range(len(validation_images)):
T = validation_images[i]
label = validation_labels[i]
train_data.append((T, label))
np.random.shuffle(train_data)
public_test_images = np.load(EMOTION_FOLDER + 'public test/images.npy', allow_pickle=True)
public_test_labels = np.load(EMOTION_FOLDER + 'public test/labels.npy', allow_pickle=True)
private_test_images = np.load(EMOTION_FOLDER + 'private test/images.npy', allow_pickle=True)
private_test_labels = np.load(EMOTION_FOLDER + 'private test/labels.npy', allow_pickle=True)
public_test_data = []
private_test_data = []
for i in range(len(public_test_images)):
T = public_test_images[i]
label = public_test_labels[i]
public_test_data.append((T, label))
for i in range(len(private_test_images)):
T = private_test_images[i]
label = private_test_labels[i]
private_test_data.append((T, label))
print('Done !')
print('Number of emotion train data: ', str(len(train_data)))
print('---------------------------------------------------------------')
return train_data, public_test_data, private_test_data
''' Data augmentation method '''
def random_crop(batch, crop_shape, padding=None):
oshape = np.shape(batch[0])
if padding:
oshape = (oshape[0] + 2 * padding, oshape[1] + 2 * padding)
new_batch = []
npad = ((padding, padding), (padding, padding), (0, 0))
for i in range(len(batch)):
new_batch.append(batch[i])
if padding:
new_batch[i] = np.lib.pad(batch[i], pad_width=npad, mode='constant', constant_values=0)
nh = random.randint(0, oshape[0] - crop_shape[0])
nw = random.randint(0, oshape[1] - crop_shape[1])
new_batch[i] = new_batch[i][nh:nh + crop_shape[0], nw:nw + crop_shape[1]]
return new_batch
def random_flip_leftright(batch):
for i in range(len(batch)):
if bool(random.getrandbits(1)):
batch[i] = np.fliplr(batch[i])
return batch
def random_flip_updown(batch):
for i in range(len(batch)):
if bool(random.getrandbits(1)):
batch[i] = np.flipud(batch[i])
return batch
def random_90degrees_rotation(batch, rotations=[0, 1, 2, 3]):
for i in range(len(batch)):
num_rotations = random.choice(rotations)
batch[i] = np.rot90(batch[i], num_rotations)
return batch
def random_rotation(batch, max_angle):
for i in range(len(batch)):
if bool(random.getrandbits(1)):
angle = random.uniform(-max_angle, max_angle)
batch[i] = scipy.ndimage.interpolation.rotate(batch[i], angle, reshape=False)
return batch
def random_blur(batch, sigma_max=5.0):
for i in range(len(batch)):
if bool(random.getrandbits(1)):
sigma = random.uniform(0., sigma_max)
batch[i] = scipy.ndimage.filters.gaussian_filter(batch[i], sigma)
return batch
def augmentation(batch, img_size):
batch = random_crop(batch, (img_size, img_size), 10)
#batch = random_blur(batch)
batch = random_flip_leftright(batch)
batch = random_rotation(batch, 10)
return batch