forked from raquelduenass/Air-writing-initial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_utils_mod.py
267 lines (223 loc) · 9.89 KB
/
data_utils_mod.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
import os
import numpy as np
import cv2
from scipy.ndimage import zoom
from common_flags import FLAGS
import keras
from keras import backend as K
from keras.preprocessing.image import Iterator
from keras.preprocessing.image import ImageDataGenerator
from sklearn.model_selection import train_test_split
class DataGenerator(ImageDataGenerator):
"""
Generate minibatches of images and labels with real-time augmentation.
The only function that changes w.r.t. parent class is the flow that
generates data. This function needed in fact adaptation for different
directory structure and labels. All the remaining functions remain
unchanged.
"""
def flow_from_directory(self, phase, num_classes, target_size=(120,224,224),
img_mode='grayscale', batch_size=32, shuffle=True,
seed=None, follow_links=False):
return DirectoryIterator(
phase, num_classes, self, target_size=target_size, img_mode=img_mode,
batch_size=batch_size, shuffle=shuffle, seed=seed,
follow_links=follow_links)
class DirectoryIterator(Iterator):
"""
Class for managing data loading of images and labels
We assume that the folder structure is:
root_folder/
class_0/
repetition_0/
frame_00000000.png
frame_00000001.png
.
.
frame_00999999.png
trajectories.csv
repetition_1/
.
.
repetition_m/
class_1/
.
.
class_n/
# Arguments
directory: Path to the root directory to read data from.
num_classes: Output dimension (number of classes).
image_data_generator: Image Generator.
target_size: tuple of integers, dimensions to resize input images to.
img_mode: One of `"rgb"`, `"grayscale"`. Color mode to read images.
batch_size: The desired batch size
shuffle: Whether to shuffle data or not
seed : numpy seed to shuffle data
follow_links: Bool, whether to follow symbolic links or not
# TODO: Add functionality to save images to have a look at the augmentation
"""
def __init__(self, phase, num_classes, image_data_generator,
target_size=(120,224,224), img_mode = 'grayscale',
batch_size=32, shuffle=True, seed=None, follow_links=False):
self.image_data_generator = image_data_generator
self.target_size = target_size
self.follow_links = follow_links
# Initialize image mode
if img_mode not in {'rgb', 'grayscale'}:
raise ValueError('Invalid color mode:', img_mode,
'; expected "rgb" or "grayscale".')
self.img_mode = img_mode
# File of database for the phase
if phase == 'train':
dirs_file = os.path.join(FLAGS.experiment_rootdir, 'train_files.txt')
labels_file = os.path.join(FLAGS.experiment_rootdir, 'train_labels.txt')
elif phase == 'val':
dirs_file = os.path.join(FLAGS.experiment_rootdir, 'val_files.txt')
labels_file = os.path.join(FLAGS.experiment_rootdir, 'val_labels.txt')
elif phase == 'test':
dirs_file = os.path.join(FLAGS.experiment_rootdir, 'test_files.txt')
labels_file = os.path.join(FLAGS.experiment_rootdir, 'test_labels.txt')
else:
raise ValueError('Invalid phase mode:', phase,
'; expected "train", "val" or "test".')
# Initialize number of classes
self.num_classes = num_classes
# Filenames of all samples/repetitions in dataset and Labels
# (ground truth) of all samples/repetitions in dataset
self.filenames, self.ground_truth = cross_val_load(dirs_file, labels_file)
# Number of samples in dataset
self.samples = len(self.filenames)
# Check if dataset is empty
if self.samples == 0:
raise IOError("Did not find any data")
# Conversion of list into array
self.ground_truth = np.array(self.ground_truth, dtype= K.floatx())
print('Found {} samples belonging to {} classes.'.format(
self.samples, self.num_classes))
super(DirectoryIterator, self).__init__(self.samples,
batch_size, shuffle, seed)
def _recursive_list(self, subpath):
return sorted(os.walk(subpath, followlinks=self.follow_links),
key=lambda tpl: tpl[0])
def next(self):
"""
Public function to fetch next batch
# Returns
The next batch of images and commands.
"""
with self.lock:
index_array = next(self.index_generator)
return self._get_batches_of_transformed_samples(index_array)
def _get_batches_of_transformed_samples(self, index_array):
"""
Public function to fetch next batch.
Image transformation is not under thread lock, so it can be done in
parallel
# Returns
The next batch of images and categorical labels.
"""
current_batch_size = index_array.shape[0]
# Initialize batch of images
batch_x = np.zeros((current_batch_size,) + self.target_size,
dtype=K.floatx())
# Initialize batch of ground truth
batch_y = np.zeros((current_batch_size, self.num_classes,),
dtype=K.floatx())
grayscale = self.img_mode == 'grayscale'
# Build batch of block-image data
for i, j in enumerate(index_array):
fname = self.filenames[j]
x = load_imgs(fname,
grayscale=grayscale,
target_size=self.target_size)
# Data augmentation
x = self.image_data_generator.random_transform(x)
x = self.image_data_generator.standardize(x)
batch_x[i] = x
# Build batch of labels
batch_y = np.array(self.ground_truth[index_array], dtype=K.floatx())
batch_y = keras.utils.to_categorical(batch_y, num_classes=self.num_classes)
return batch_x, batch_y
def adjust(data, size):
factors = (size[0]/data.shape[0], size[1]/data.shape[1], size[2]/data.shape[2])
new_array = zoom(data, factors)
return new_array
def cross_val_create(data_path):
train_list, val_list, test_list = [], [], []
train_labels, val_labels, test_labels = [], [], []
for gesture_id, gest_dir in enumerate(sorted(os.listdir(data_path))):
gesture_path = os.path.join(data_path, gest_dir)
rep_paths = [os.path.join(gesture_path, rep_dir) for rep_dir in os.listdir(gesture_path)]
# Separate sets of data
gest_train, gest_test = train_test_split(rep_paths, test_size=0.2)
gest_train, gest_val = train_test_split(gest_train, test_size=0.25)
# Create lists of gestures and ground truth labels
train_labels.extend(gesture_id*np.ones(len(gest_train), dtype=np.int))
val_labels.extend(gesture_id*np.ones(len(gest_val), dtype=np.int))
test_labels.extend(gesture_id*np.ones(len(gest_test), dtype=np.int))
train_list.extend(gest_train)
val_list.extend(gest_val)
test_list.extend(gest_test)
# Name of files
train_file = os.path.join(FLAGS.experiment_rootdir, 'train_files.txt')
val_file = os.path.join(FLAGS.experiment_rootdir, 'val_files.txt')
test_file = os.path.join(FLAGS.experiment_rootdir, 'test_files.txt')
train_labels_file = os.path.join(FLAGS.experiment_rootdir, 'train_labels.txt')
val_labels_file = os.path.join(FLAGS.experiment_rootdir, 'val_labels.txt')
test_labels_file = os.path.join(FLAGS.experiment_rootdir, 'test_labels.txt')
# Create file of training directories
with open(train_file, 'w') as f:
for item in train_list:
f.write("%s\n" % item)
# Create file of validation directories
with open(val_file, 'w') as f:
for item in val_list:
f.write("%s\n" % item)
# Create file of test directories
with open(test_file, 'w') as f:
for item in test_list:
f.write("%s\n" % item)
# Create file of training labels
with open(train_labels_file, 'w') as f:
for item in train_labels:
f.write("%s\n" % item)
# Create file of validation labels
with open(val_labels_file, 'w') as f:
for item in val_labels:
f.write("%s\n" % item)
# Create file of test labels
with open(test_labels_file, 'w') as f:
for item in test_labels:
f.write("%s\n" % item)
return
def cross_val_load(dirs_file, labels_file):
with open(dirs_file) as f:
dirs_list = f.read().splitlines()
with open(labels_file) as f:
labels_list = f.read().splitlines()
return dirs_list, labels_list
def load_imgs(path, grayscale=False, target_size=None):
"""
Load a block of images.
# Arguments
path: Path to image files.
grayscale: Boolean, wether to load the image as grayscale.
target_size: Either `None` (default to original size)
or ints `(num_imgs, img_height, img_width)`.
# Returns
Block of images as numpy array.
"""
# Read input images
paths = [os.path.join(path, x) for x in os.listdir(path) if x.endswith('.png')]
imgs = []
for img_path in paths:
img = cv2.imread(img_path)
if grayscale:
if len(img.shape) != 2:
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
if grayscale:
img = img.reshape((img.shape[0], img.shape[1]))
imgs.append(img)
if target_size:
imgs = adjust(np.asarray(imgs, dtype=np.float32), target_size)
return imgs