-
Notifications
You must be signed in to change notification settings - Fork 63
/
generator.py
174 lines (145 loc) · 6.46 KB
/
generator.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
import os
import cv2
import random
import numpy as np
from visualize import visualize
from preprocess.datagen import label_generator
from preprocess.augmentation import rotation, translate, crop, noise, salt
def train_generator(sample_per_batch, batch_number):
""" Generating training data """
train_image_file = []
directory = '../EgoGesture Dataset/'
folder_name = ['SingleOne', 'SingleTwo', 'SingleThree', 'SingleFour', 'SingleFive',
'SingleSix', 'SingleSeven', 'SingleEight']
for folder in folder_name:
train_image_file = train_image_file + os.listdir(directory + folder + '/')
for i in range(0, 10):
random.shuffle(train_image_file)
print('Training Dataset Size: {0}'.format(len(train_image_file)))
while True:
for i in range(0, batch_number - 1):
start = i * sample_per_batch
end = (i + 1) * sample_per_batch
x_batch = []
y_batch_prob = []
y_batch_pos = []
for n in range(start, end):
image_name = train_image_file[n]
try:
image, probability, position = label_generator(directory=directory,
image_name=image_name,
type='')
except cv2.error:
print(image_name)
continue
# 1.0 Original image
x_batch.append(image)
y_batch_prob.append(probability)
pos = np.array([position, ] * 10)
y_batch_pos.append(pos)
# visualize(image, probability, pos[0])
""" Augmentation """
# 2.0 Original + translate
im, pos = translate(image, probability, position)
x_batch.append(im)
y_batch_prob.append(probability)
pos = np.array([pos, ] * 10)
y_batch_pos.append(pos)
# visualize(im, probability, pos[0])
# 3.0 Original + rotation
im, pos = rotation(image, probability, position)
x_batch.append(im)
y_batch_prob.append(probability)
pos = np.array([pos, ] * 10)
y_batch_pos.append(pos)
# visualize(im, probability, pos[0])
# 4.0 Original + salt
im, pos = salt(image, probability, position)
x_batch.append(im)
y_batch_prob.append(probability)
pos = np.array([pos, ] * 10)
y_batch_pos.append(pos)
# visualize(im, probability, pos[0])
# 5.0 Original + crop
im, pos = crop(image, probability, position)
x_batch.append(im)
y_batch_prob.append(probability)
pos = np.array([pos, ] * 10)
y_batch_pos.append(pos)
# visualize(im, probability, pos)
# 6.0 Original + noise
im, pos = noise(image, probability, position)
x_batch.append(im)
y_batch_prob.append(probability)
pos = np.array([pos, ] * 10)
y_batch_pos.append(pos)
# visualize(im, probability, pos[0])
# 7.0 Original + rotate + translate
im, pos = rotation(image, probability, position)
im, pos = translate(im, probability, pos)
x_batch.append(im)
y_batch_prob.append(probability)
pos = np.array([pos, ] * 10)
y_batch_pos.append(pos)
# visualize(im, probability, pos)
# 8.0 Original + rotate + crop
im, pos = rotation(image, probability, position)
im, pos = crop(im, probability, pos)
x_batch.append(im)
y_batch_prob.append(probability)
pos = np.array([pos, ] * 10)
y_batch_pos.append(pos)
# visualize(im, probability, pos[0])
x_batch = np.asarray(x_batch)
x_batch = x_batch.astype('float32')
x_batch = x_batch / 255.
y_batch_prob = np.asarray(y_batch_prob)
y_batch_pos = np.asarray(y_batch_pos)
y_batch_pos = y_batch_pos.astype('float32')
y_batch_pos = y_batch_pos / 128.
y_batch = [y_batch_prob, y_batch_pos]
yield (x_batch, y_batch)
def valid_generator(sample_per_batch, batch_number):
""" Generating validation data """
valid_image_file = []
directory = '../EgoGesture Dataset/'
folder_name = ['SingleOneValid', 'SingleTwoValid', 'SingleThreeValid', 'SingleFourValid', 'SingleFiveValid',
'SingleSixValid', 'SingleSevenValid', 'SingleEightValid']
for folder in folder_name:
valid_image_file = valid_image_file + os.listdir(directory + folder + '/')
# print(len(valid_image_file))
while True:
for i in range(0, batch_number - 1):
start = i * sample_per_batch
end = (i + 1) * sample_per_batch
x_batch = []
y_batch_prob = []
y_batch_pos = []
for n in range(start, end):
image_name = valid_image_file[n]
try:
image, probability, position = label_generator(directory=directory,
image_name=image_name,
type='Valid')
except cv2.error:
print(image_name)
continue
# 1.0 Original image
x_batch.append(image)
y_batch_prob.append(probability)
pos = np.array([position, ] * 10)
y_batch_pos.append(pos)
x_batch = np.asarray(x_batch)
x_batch = x_batch.astype('float32')
x_batch = x_batch / 255.
y_batch_prob = np.asarray(y_batch_prob)
y_batch_pos = np.asarray(y_batch_pos)
y_batch_pos = y_batch_pos.astype('float32')
y_batch_pos = y_batch_pos / 128.
y_batch = [y_batch_prob, y_batch_pos]
yield (x_batch, y_batch)
if __name__ == '__main__':
gen = train_generator(sample_per_batch=100, batch_number=220)
batch_x, batch_y = next(gen)
print(batch_x)
print(batch_y)