-
Notifications
You must be signed in to change notification settings - Fork 28
/
dataset_hw.py
427 lines (334 loc) · 17.5 KB
/
dataset_hw.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
import numpy as np
import scipy.signal
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import OneHotEncoder
import utils_hw as utils
from dataset import BaseDataset
class HandWritingDataset(BaseDataset):
"""
Customized for handwriting dataset.
Stroke data is assumed to be consisting of 3 dimensions x, y and pen, respectively. If the stroke data is required
to be concatenated with other modalities, then stroke data relies in the first 3 dimensions.
Args:
data_path (str): path to numpy dataset file. See data_scripts/preprocessing.py for details.
var_len_seq (bool): whether the dataset consists of variable-length sequences or not. If set to False, then
it is determined from the dataset samples.
"""
def __init__(self, data_path, var_len_seq=False):
super(HandWritingDataset, self).__init__(data_path)
# TODO new_dataset
#self.samples = self.data_dict['strokes']
self.samples = self.data_dict['samples'] if 'samples' in self.data_dict.keys() else self.data_dict['strokes']
self.char_labels = self.data_dict['char_labels']
self.subject_labels = self.data_dict['subject_labels']
self.texts= self.data_dict['texts']
self.feature_size = self.samples[0].shape[-1] # x,y,pen
# Models require input and target dimensionality. They are useful if the inputs and targets are concatenation
# of different modalities. They are used to split the input/target into components.
self.input_dims = [self.feature_size]
self.target_dims = [2, 1] # Stroke, pen
# The dimensions with None will be padded if seq_len isn't passed.
self.sequence_length = None if var_len_seq else self.extract_seq_len()
self.is_dynamic = self.sequence_length == None
# sequence length, strokes, targets (i.e., strokes).
self.sample_shape = [[], [self.sequence_length, self.feature_size], [self.sequence_length, self.feature_size]]
self.sample_np_type = [np.int32, np.float32, np.float32]
self.num_samples = len(self.samples)
# Preprocessing
self.normalization = 'normalization' in self.data_dict['preprocessing']
if not self.normalization:
print("Warning: data is not normalized.")
elif not ('mean' in self.data_dict):
raise Exception("Normalization statistics (mean and std) are missing.")
else:
self.norm_mean = self.data_dict['mean']
self.norm_std = self.data_dict['std']
self.relative_representation = 'relative_representation' in self.data_dict['preprocessing']
self.offset_removal = 'origin_translation' in self.data_dict['preprocessing']
self.scale = 'scale' in self.data_dict['preprocessing']
if self.scale and not('min' in self.data_dict):
pass
#raise Exception("Scaling statistics (min and max) are missing.")
else:
self.scale_min = self.data_dict['min']
self.scale_max = self.data_dict['max']
def preprocess_raw_sample(self, sample):
"""
Gets a raw (!) sample and applies preprocessing steps that the dataset has been applied.
Args:
sample: [seq_len, 3]
Returns:
"""
sample_copy = np.copy(sample[:, :3])
statistics = {}
if self.scale:
sample_copy[:, [0, 1]] = ((sample-self.scale_min)/(self.scale_max-self.scale_min))[:, [0, 1]]
if self.offset_removal:
statistics['x_offset'] = sample_copy[0, 0]
statistics['y_offset'] = sample_copy[0, 1]
sample_copy[:, 0] -= statistics['x_offset']
sample_copy[:, 1] -= statistics['y_offset']
if self.relative_representation:
source = np.vstack((sample_copy[0], sample_copy))
sample_copy = np.diff(source, axis=0)
sample_copy[:, 2] = sample[:, 2] # Keep original pen information since it is already relative.
if self.normalization:
sample_copy[:, [0, 1]] = ((sample_copy-self.norm_mean)/self.norm_std)[:, [0, 1]]
return sample_copy, statistics
def undo_preprocess(self, sample, statistics=None):
"""
Applies preprocessing in reverse order by using statistics parameters.
Args:
sample (numpy.ndarray): [seq_len, 3]
statistics (dict): Contains dataset ("min", "max", "mean", "std") and sample ("x_offset", "y_offset")
statistics. If a (dataset statistics) key is not found in the dictionary or has None value, then class
statistics will be used.
Returns:
(numpy.ndarray): [seq_len, 3]
"""
if statistics is None:
statistics = {}
sample_copy = np.copy(sample[:, :3])
if self.normalization:
mean_ = self.norm_mean
std_ = self.norm_std
if ('mean' in statistics) and (statistics['mean'] is not None):
mean_ = statistics['mean']
std_ = statistics['std']
sample_copy[:, :2] = (sample_copy*std_ + mean_)[:, :2]
if self.relative_representation:
sample_copy = np.cumsum(sample_copy, 0) # Assuming that the sequence always starts with 0.
if self.offset_removal and 'x_offset' in statistics:
sample_copy[:, 0] += statistics['x_offset']
sample_copy[:, 1] += statistics['y_offset']
if self.scale:
min_ = self.scale_min
max_ = self.scale_max
if ('min' in statistics) and (statistics['min'] is not None):
min_ = statistics['min']
max_ = statistics['max']
sample_copy[:, :2] = (sample_copy[:,:3]*(max_-min_) + min_)[:, :2]
sample_copy[:, 2] = sample[:, 2]
return sample_copy
def prepare_for_visualization(self, sample, detrend_sample=False):
"""
TODO: Move this method into a more proper class.
Args:
sample:
Returns:
"""
sample_copy = np.copy(sample[:,:3])
if self.normalization:
sample_copy = sample_copy*self.norm_std+self.norm_mean
if detrend_sample:
sample_copy[:,1] = scipy.signal.detrend(sample_copy[:,1])
if self.relative_representation:
sample_copy = np.cumsum(sample_copy, 0) # Assuming that the sequence always starts with 0.
sample_copy[:,2] = sample[:,2]
return sample_copy
def undo_normalization(self, sample, detrend_sample=False):
"""
TODO: Move this method into a more proper class.
Args:
sample:
Returns:
"""
sample_copy = np.copy(sample[:,:3])
if self.normalization:
sample_copy = sample_copy*self.norm_std+self.norm_mean
if detrend_sample:
sample_copy[:,1] = scipy.signal.detrend(sample_copy[:,1])
sample_copy[:,2] = sample[:,2]
return sample_copy
def sample_generator(self):
"""
Creates a generator object which returns one data sample at a time. It is used by DataFeeder objects.
Returns:
(generator): each sample is a list of data elements.
"""
for stroke in self.samples:
yield [stroke.shape[0], stroke, stroke]
def fetch_sample(self, sample_idx):
"""
Prepares one data sample (i.e. return of sample_generator) given index.
Args:
sample_idx:
Returns:
"""
stroke = self.samples[sample_idx]
return [stroke.shape[0], stroke, stroke]
# TODO Auxiliary methods can be in utils.
def get_seq_len_histogram(self, num_bins=10, collapse_first_and_last_bins=[1, -1]):
"""
Creates a histogram of sequence-length.
Args:
num_bins:
collapse_first_and_last_bins: selects bin edges between the provided indices by discarding from the
first
and last bins.
Returns:
(list): bin edges.
"""
seq_lens = [s.shape[0] for s in self.samples]
h, bins = np.histogram(seq_lens, bins=num_bins)
if collapse_first_and_last_bins is not None:
return [int(b) for b in bins[collapse_first_and_last_bins[0]:collapse_first_and_last_bins[1]]]
else:
return [int(b) for b in bins]
def extract_seq_len(self):
seq_lens = [s.shape[0] for s in self.samples]
if max(seq_lens) == min(seq_lens):
return min(seq_lens)
else:
return None
class HandWritingDatasetConditional(HandWritingDataset):
"""
Uses character labels.
In contrast to HandWritingDataset dataset (i.e., non-conditional), concatenates one-hot-vector char labels with
strokes.
Args:
data_path (str): path to numpy dataset file. See data_scripts/preprocessing.py for details.
var_len_seq (bool): whether the dataset consists of variable-length sequences or not. If set to False, then
it is determined from the dataset samples.
use_bow_labels (bool): whether beginning-of-word labels (bow_labels) are yielded as model inputs or not.
"""
def __init__(self, data_path, var_len_seq=None, use_bow_labels=True):
super(HandWritingDatasetConditional, self).__init__(data_path, var_len_seq)
self.use_bow_labels = use_bow_labels
if not('alphabet' in self.data_dict):
raise Exception("Alphabet is missing.")
self.alphabet = self.data_dict['alphabet']
self.alphabet_size = len(self.alphabet)
self.feature_size = self.samples[0].shape[-1] # x,y,pen
# Models require input and target dimensionality. They are useful if the inputs and targets are concatenation
# of different modalities. They are used to split the input/target into components.
self.input_dims = [self.feature_size, len(self.alphabet)]
self.target_dims = [2, 1, len(self.alphabet), 1] # Stroke, pen, character labels, eoc
if use_bow_labels:
self.input_dims = [self.feature_size, len(self.alphabet), 1]
self.target_dims = [2, 1, len(self.alphabet), 1, 1] # Stroke, pen, character labels, eoc, bow
int_alphabet = np.expand_dims(np.array(range(self.alphabet_size)), axis=1)
self.char_encoder = LabelEncoder()
self.char_encoder.fit(self.alphabet)
self.one_hot_encoder = OneHotEncoder(sparse=False)
self.one_hot_encoder.fit(int_alphabet)
self.__encode_labels()
self.eoc_labels = self.data_dict['eoc_labels']
self.boc_labels = self.data_dict['boc_labels'] if 'boc_labels' in self.data_dict.keys() else self.data_dict['soc_labels']
self.eow_labels = self.data_dict['eow_labels']
self.bow_labels = self.data_dict['bow_labels'] if 'bow_labels' in self.data_dict.keys() else self.data_dict['sow_labels']
# sequence length, strokes, targets (i.e., strokes+end-of-character).
# The dimensions with None will be padded if seq_len isn't passed.
self.sample_shape = [[], [self.sequence_length, sum(self.input_dims)], [self.sequence_length, sum(self.target_dims)]]
def text_to_one_hot(self, text):
integer_labels = self.char_encoder.transform(list(text))
return self.one_hot_encoder.transform(np.expand_dims(integer_labels, axis=1))
def int_labels_to_one_hot(self, int_labels):
return self.one_hot_encoder.transform(np.expand_dims(int_labels, axis=1))
def logit_to_one_hot(self, one_hot):
integer_labels = np.argmax(one_hot, -1)
return self.int_labels_to_one_hot(integer_labels)
def one_hot_to_int_labels(self, one_hot):
return np.argmax(one_hot, -1)
def int_labels_to_text(self, int_labels):
text_labels = utils.simplify_int_labels(int_labels)
text = self.char_encoder.inverse_transform(text_labels)
return text
def __encode_labels(self):
"""
Encodes integer character labels as one-hot vectors.
Returns:
"""
self.one_hot_char_labels = []
for idx, label in enumerate(self.data_dict['char_labels']):
self.one_hot_char_labels .append(self.one_hot_encoder.transform(np.expand_dims(label, axis=1)))
def sample_generator(self):
"""
Creates a generator object which returns one data sample at a time. It is used by DataFeeder objects.
Returns:
(generator): each sample is a list of data elements.
"""
for stroke, char_label, eoc_label, bow_label in zip(self.samples, self.one_hot_char_labels, self.eoc_labels, self.bow_labels):
bow_label_ = np.expand_dims(bow_label, axis=1)
eoc_label_ = np.expand_dims(eoc_label, axis=1)
if self.use_bow_labels:
yield [stroke.shape[0], np.float32(np.hstack([stroke, char_label, bow_label_])), np.float32(np.hstack([stroke, char_label, eoc_label_, bow_label_]))]
else:
yield [stroke.shape[0], np.float32(np.hstack([stroke, char_label])), np.float32(np.hstack([stroke, char_label, eoc_label_]))]
def fetch_sample(self, sample_idx):
"""
Prepares one data sample (i.e. return of sample_generator) given index.
Args:
sample_idx:
Returns:
"""
stroke = self.samples[sample_idx]
char_label = self.one_hot_char_labels[sample_idx]
eoc_label = np.expand_dims(self.eoc_labels[sample_idx], axis=1)
if self.use_bow_labels:
bow_label = np.expand_dims(self.bow_labels[sample_idx], axis=1)
return [stroke.shape[0], np.expand_dims(np.float32(np.hstack([stroke, char_label, bow_label])), axis=0), np.expand_dims(np.float32(np.hstack([stroke, char_label, eoc_label, bow_label])), axis=0)]
else:
return [stroke.shape[0], np.expand_dims(np.float32(np.hstack([stroke, char_label])), axis=0), np.expand_dims(np.float32(np.hstack([stroke, char_label, eoc_label])), axis=0)]
class HandWritingClassificationDataset(HandWritingDatasetConditional):
"""
Handwriting dataset for character classification/segmentation models. In contrast to parent class
HandWritingDatasetConditional, its sample_generator method yields only strokes as model input and
[char_label, eoc_label, (bow_label)] as model target.
Args:
data_path (str): path to numpy dataset file. See data_scripts/preprocessing.py for details.
var_len_seq (bool): whether the dataset consists of variable-length sequences or not. If set to False, then
it is determined from the dataset samples.
use_bow_labels (bool): whether beginning-of-word labels (bow_labels) are yielded as model targets or not.
data_augmentation (bool): whether to apply data augmentation or not. If set True, strokes are scaled randomly.
"""
def __init__(self, data_path, var_len_seq=None, use_bow_labels=False, data_augmentation=False):
super(HandWritingClassificationDataset, self).__init__(data_path, var_len_seq)
self.bow_target = use_bow_labels
self.data_augmentation = data_augmentation
self.input_dims = [self.samples[0].shape[-1]]
self.feature_size = sum(self.input_dims)
if self.bow_target:
self.target_dims = [self.alphabet_size, 1, 1] # char_labels, end-of-character, sow
else:
self.target_dims = [self.alphabet_size, 1] # char_labels, end-of-character
# sequence length, strokes, targets
# The dimensions with None will be padded if sequence_length isn't passed.
self.sample_shape = [[], [self.sequence_length, sum(self.input_dims)], [self.sequence_length, sum(self.target_dims)]]
def sample_generator(self):
"""
Creates a generator object which returns one data sample at a time. It is used by DataFeeder objects.
Returns:
(generator): each sample is a list of data elements.
"""
if self.bow_target:
for stroke, char_label, eoc_label, bow_label in zip(self.samples, self.one_hot_char_labels, self.eoc_labels, self.bow_labels):
if self.data_augmentation:
stroke_augmented = stroke.copy()
stroke_augmented *= np.random.uniform(0.7,1.3, (1))
else:
stroke_augmented = stroke
yield [stroke.shape[0], stroke_augmented, np.float32(np.hstack([char_label, np.expand_dims(eoc_label,-1), np.expand_dims(bow_label,-1)]))]
else:
for stroke, char_label, eoc_label in zip(self.samples, self.one_hot_char_labels, self.eoc_labels):
if self.data_augmentation:
stroke_augmented = stroke.copy()
stroke_augmented *= np.random.uniform(0.7,1.3, (1))
else:
stroke_augmented = stroke
yield [stroke.shape[0], stroke_augmented, np.float32(np.hstack([char_label, np.expand_dims(eoc_label,-1)]))]
def fetch_sample(self, sample_idx):
"""
Prepares one data sample (i.e. return of sample_generator) given index.
Args:
sample_idx:
Returns:
"""
stroke = np.expand_dims(self.samples[sample_idx], axis=0)
char_label = self.one_hot_char_labels[sample_idx]
eoc_label = np.expand_dims(self.eoc_labels[sample_idx], -1)
bow_label = np.expand_dims(self.bow_labels[sample_idx], -1)
if self.bow_target:
return [stroke.shape[0], stroke, np.expand_dims(np.float32(np.hstack([char_label, eoc_label, bow_label])), axis=1)]
else:
return [stroke.shape[0], stroke, np.expand_dims(np.float32(np.hstack([char_label, eoc_label])), axis=1)]