-
Notifications
You must be signed in to change notification settings - Fork 53
/
CNN2Head_input.py
126 lines (100 loc) · 3.68 KB
/
CNN2Head_input.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
import numpy as np
import os
import cv2
import scipy
import random
SMILE_FOLDER = './data/smile_data/'
GENDER_FOLDER = './data/gender_data/'
AGE_FOLDER = './data/age_data/'
NUM_SMILE_IMAGE = 4000
SMILE_SIZE = 48
EMOTION_SIZE = 48
def getSmileImage():
print('Load smile image...................')
X1 = np.load(SMILE_FOLDER + 'train.npy')
X2 = np.load(SMILE_FOLDER + 'test.npy')
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')
X2 = np.load(GENDER_FOLDER + 'test.npy')
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 getAgeImage():
print('Load age image...................')
X1 = np.load(AGE_FOLDER + 'train.npy')
X2 = np.load(AGE_FOLDER + 'test.npy')
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 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