forked from rwth-i6/returnn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dataset.py
1440 lines (1333 loc) · 52.7 KB
/
Dataset.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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
This defines the base dataset class :class:`Dataset`.
"""
from __future__ import print_function
__author__ = "Patrick Doetsch"
__copyright__ = "Copyright 2015"
__credits__ = ["Patrick Doetsch", "Paul Voigtlaender"]
__license__ = "RWTHASR"
__version__ = "0.9"
__maintainer__ = "Patrick Doetsch"
__email__ = "doetsch@i6.informatik.rwth-aachen.de"
from threading import RLock
from random import Random, random
import sys
import os
import numpy
import functools
import typing
from Log import log
from EngineBatch import Batch, BatchSetGenerator
from Util import PY3, try_run, NumbersDict, unicode, OptionalNotImplementedError
class Dataset(object):
"""
Base class for any dataset. This defines the dataset API.
"""
@staticmethod
def kwargs_update_from_config(config, kwargs):
"""
:type config: Config.Config
:type kwargs: dict[str]
"""
def set_or_remove(key, value):
"""
:param str key:
:param value:
"""
if key in kwargs and kwargs[key] is None:
del kwargs[key]
if value is not None and key not in kwargs:
kwargs[key] = value
set_or_remove("window", config.int('window', 0) or None)
set_or_remove("context_window", config.typed_value("context_window"))
set_or_remove("chunking", config.opt_typed_value("chunking", None))
set_or_remove("seq_ordering", config.value("batching", None))
set_or_remove("shuffle_frames_of_nseqs", config.int('shuffle_frames_of_nseqs', 0) or None)
set_or_remove("min_chunk_size", config.int('min_chunk_size', 0) or None)
set_or_remove("chunking_variance", config.float("chunking_variance", 0))
@staticmethod
def get_default_kwargs_eval(config):
"""
:param Config.Config config:
:rtype: dict[str]
"""
# For dev/eval, by default, we should not do chunking (i.e. chunking = "0").
chunking = "0"
if config.value("on_size_limit", "ignore") == "chunk":
chunking = config.value("batch_size", "0")
elif config.value('chunking', "0") == "1": # MLP mode
chunking = "1"
elif config.bool('chunk_eval', False):
chunking = config.value('chunking', "0")
return dict(chunking=chunking, seq_ordering="sorted", shuffle_frames_of_nseqs=0)
@classmethod
def from_config(cls, config, **kwargs):
"""
:type config: Config.Config
:param dict[str] kwargs: passed on to __init__
:rtype: Dataset
"""
cls.kwargs_update_from_config(config, kwargs)
return cls(**kwargs)
def __init__(self, name=None,
window=1, context_window=None, chunking=None,
seq_ordering='default', random_seed_offset=None,
partition_epoch=None, repeat_epoch=None,
seq_list_filter_file=None, unique_seq_tags=False,
seq_order_seq_lens_file=None,
shuffle_frames_of_nseqs=0, min_chunk_size=0, chunking_variance=0,
estimated_num_seqs=None):
"""
:param str name: e.g. "train" or "eval"
:param int window: features will be of dimension window * feature_dim, as we add a context-window around.
not all datasets support this option.
:param None|int|dict|NumbersDict|(dict,dict) context_window: will add this context for each chunk
:param None|str|int|(int,int)|dict|(dict,dict) chunking: "chunk_size:chunk_step"
:param str seq_ordering: "batching"-option in config. e.g. "default", "sorted" or "random".
See self.get_seq_order_for_epoch() for more details.
:param int|None random_seed_offset:
:param int|None partition_epoch:
:param int|None repeat_epoch: Repeat the sequences in an epoch this many times. Useful to scale the dataset
relative to other datasets, e.g. when used in CombinedDataset. Not allowed to be used in combination with
partition_epoch.
:param str|None seq_list_filter_file: defines a subset of sequences (by tag) to use
:param bool unique_seq_tags: uniquify seqs with same seq tags in seq order
:param str|None seq_order_seq_lens_file: for seq order, use the seq length given by this file
:param int shuffle_frames_of_nseqs: shuffles the frames. not always supported
:param None|int estimated_num_seqs: for progress reporting in case the real num_seqs is unknown
"""
self.name = name or ("dataset_id%s" % id(self))
self.lock = RLock() # Used when manipulating our data potentially from multiple threads.
self.rnd_seq_drop = None # type: typing.Optional[Random]
self.num_inputs = 0 # usually not used, but num_outputs instead, which is more generic
self.num_outputs = None # type: typing.Optional[typing.Dict[str,typing.Tuple[int,int]]] # tuple is num-classes, len(shape). # nopep8
self.window = window
self.seq_ordering = seq_ordering # "default", "sorted" or "random". See self.get_seq_order_for_epoch().
if random_seed_offset is None:
random_seed_offset = self._get_default_random_seed_offset()
self.random_seed_offset = random_seed_offset
self.partition_epoch = partition_epoch or 1
self.repeat_epoch = repeat_epoch or 1
self.seq_tags_filter = set(self._load_seq_list_file(seq_list_filter_file)) if seq_list_filter_file else None
self.unique_seq_tags = unique_seq_tags
self._seq_order_seq_lens_file = seq_order_seq_lens_file
self._seq_order_seq_lens_by_idx = None
# There is probably no use case for combining the two, so avoid potential misconfiguration.
assert self.partition_epoch == 1 or self.repeat_epoch == 1, (
"Combining partition_epoch and repeat_epoch is prohibited.")
self.timestamps = None
self.labels = {} # type: typing.Dict[str,typing.List[str]]
self.weights = {}
self.nbytes = 0
self.num_running_chars = 0 # CTC running chars.
self._num_timesteps = 0
self._num_codesteps = None # type: typing.Optional[int] # Num output frames, could be different from input, seq2seq, ctc. # nopep8
self._num_seqs = 0
self._estimated_num_seqs = estimated_num_seqs
self.min_chunk_size = min_chunk_size
self.chunking_variance = chunking_variance
self.chunk_size, self.chunk_step = self._parse_chunking(chunking)
if isinstance(context_window, (tuple, list)):
assert len(context_window) == 2
for elem in context_window:
assert isinstance(elem, dict)
# assume that first element of tuple|list is for left context and second element for right context
self.ctx_left = NumbersDict(numbers_dict=context_window[0])
self.ctx_right = NumbersDict(numbers_dict=context_window[1])
else:
if context_window is None:
context_window = NumbersDict()
elif isinstance(context_window, int):
context_window = NumbersDict(numbers_dict={"data": context_window})
elif isinstance(context_window, dict):
context_window = NumbersDict(numbers_dict=context_window)
assert isinstance(context_window, NumbersDict)
# ctx_total is how much frames we add additionally.
# One less because the original frame also counts, and context_window=1 means that we just have that single frame.
ctx_total = NumbersDict.max([context_window, 1]) - 1
# In case ctx_total is odd / context_window is even, we have to decide where to put one more frame.
# To keep it consistent with e.g. 1D convolution with a kernel of even size, we add one more to the right.
# See test_tfconv1d_evensize().
self.ctx_left = ctx_total // 2
self.ctx_right = ctx_total - self.ctx_left
assert isinstance(self.ctx_left, NumbersDict)
assert isinstance(self.ctx_right, NumbersDict)
self.shuffle_frames_of_nseqs = shuffle_frames_of_nseqs
self.epoch = None
def __repr__(self):
return "<%s %r epoch=%s>" % (
self.__class__.__name__,
getattr(self, "name", "<unknown>"),
getattr(self, "epoch", "<unknown>"))
@staticmethod
def _get_default_random_seed_offset():
"""
:return: 0 usually
:rtype: int
"""
from Config import get_global_config
config = get_global_config(raise_exception=False)
if not config:
return 0
if config.is_true("use_horovod") and config.value("horovod_dataset_distribution", "") == "random_seed_offset":
# noinspection PyPackageRequirements,PyUnresolvedReferences
import horovod.tensorflow as hvd
return hvd.rank() * 13
return 0
@staticmethod
def _parse_chunking(chunking):
"""
:param None|str|int|(int,int)|dict|(dict,dict)|(NumbersDict,NumbersDict) chunking:
as it comes from the config / from the user
:return: chunk_size, chunk_step
:rtype: (NumbersDict,NumbersDict)
"""
if isinstance(chunking, str):
if ":" in chunking:
chunking = tuple(map(int, chunking.split(":")))
else:
chunking = int(chunking)
if not isinstance(chunking, (tuple, list)):
chunking = (chunking, None)
chunk_size, chunk_step = chunking
if chunk_size is None:
chunk_size = 0
assert isinstance(chunk_size, (int, dict, NumbersDict))
chunk_size = NumbersDict(chunk_size)
assert chunk_size == 0 or chunk_size.min_value() > 0, "chunk size must not be negative"
if chunk_step in (None, 0):
chunk_step = chunk_size
assert isinstance(chunk_step, (int, dict, NumbersDict))
chunk_step = NumbersDict(chunk_step)
if chunk_size != 0:
assert sorted(chunk_step.keys()) == sorted(chunk_size.keys())
assert chunk_step.max_value() > 0, "chunking step must be positive (for some key)"
return chunk_size, chunk_step
@staticmethod
def _load_seq_list_file(filename, use_cache_manager=False, expect_list=True):
"""
:param str filename:
:param bool use_cache_manager:
:param bool expect_list:
:rtype: list[str]|dict[str,list[str]]
"""
if use_cache_manager:
import Util
filename = Util.cf(filename)
if filename.endswith(".pkl"):
import pickle
seq_list = pickle.load(open(filename, 'rb'))
if expect_list:
assert isinstance(seq_list, list)
elif filename.endswith(".gz"):
import gzip
seq_list = gzip.open(filename, "rt").read().splitlines()
else:
seq_list = open(filename).read().splitlines()
return seq_list
def _sliding_window(self, xr):
"""
:type xr: numpy.ndarray
:rtype: numpy.ndarray
"""
# noinspection PyProtectedMember
from numpy.lib.stride_tricks import as_strided
x = numpy.concatenate([self.zpad, xr, self.zpad])
return as_strided(
x,
shape=(x.shape[0] - self.window + 1, 1, self.window, self.num_inputs),
strides=(x.strides[0], x.strides[1] * self.num_inputs) + x.strides
).reshape((xr.shape[0], self.num_inputs * self.window))
def is_cached(self, start, end):
"""
:param int start: like in load_seqs(), sorted seq idx
:param int end: like in load_seqs(), sorted seq idx
:rtype: bool
:returns whether we have the full range (start,end) of sorted seq idx.
"""
if start == end:
return True # Empty.
assert start < end
return False
def get_seq_length(self, seq_idx):
"""
:param int seq_idx:
:rtype: NumbersDict
:returns the len of the input features and the len of the target sequence.
"""
raise NotImplementedError
def get_num_timesteps(self):
"""
:rtype: int
"""
assert self._num_timesteps > 0
return self._num_timesteps
def get_num_codesteps(self):
"""
:rtype: int|list[int]
"""
if self._num_codesteps is None:
return [self.get_num_timesteps()]
return self._num_codesteps
def load_seqs(self, start, end):
"""
Load data sequences, such that self.get_data() & friends can return the data.
:param int start: start sorted seq idx, inclusive
:param int end: end sorted seq idx, exclusive
"""
assert start >= 0
assert start <= end
if self.is_cached(start, end):
return
if self.shuffle_frames_of_nseqs > 0:
# We always load N seqs at once and shuffle all their frames.
start, end = self._get_load_seqs_superset(start, end)
self._load_seqs(start, end)
while start < end:
self._shuffle_frames_in_seqs(start, start + self.shuffle_frames_of_nseqs)
start += self.shuffle_frames_of_nseqs
else:
self._load_seqs(start, end)
def _get_load_seqs_superset(self, start, end):
"""
:type start: int
:type end: int
:returns the superset (start,end) of seqs to be loaded.
For shuffle_frames_of_nseqs > 0, we always load N seqs at once
and shuffle all their frames,
thus start/end will be aligned to self.shuffle_frames_of_nseqs.
"""
assert start <= end
assert start < self.num_seqs
if self.shuffle_frames_of_nseqs > 0:
m = self.shuffle_frames_of_nseqs
start -= start % m
end += (m - (end % m)) % m
return start, end
def _shuffle_frames_in_seqs(self, start, end):
raise OptionalNotImplementedError
def _load_seqs(self, start, end):
"""
Load data sequences.
:param int start: inclusive seq idx start
:param int end: exclusive seq idx end. can be more than num_seqs
If end > num_seqs, will not load them.
"""
raise NotImplementedError
def _get_seq_order_seq_lens_by_idx(self, seq_idx):
"""
:param int seq_idx:
:rtype: int
"""
if not self._seq_order_seq_lens_by_idx:
assert self._seq_order_seq_lens_file
if self._seq_order_seq_lens_file.endswith(".gz"):
import gzip
raw = gzip.GzipFile(self._seq_order_seq_lens_file, "rb").read()
else:
raw = open(self._seq_order_seq_lens_file, "rb").read()
seq_lens = eval(raw)
assert isinstance(seq_lens, dict)
all_tags = self.get_all_tags()
self._seq_order_seq_lens_by_idx = [seq_lens[tag] for tag in all_tags]
return self._seq_order_seq_lens_by_idx[seq_idx]
def get_seq_order_for_epoch(self, epoch, num_seqs, get_seq_len=None):
"""
Returns the order of the given epoch.
This is mostly a static method, except that is depends on the configured type of ordering,
such as 'default' (= as-is), 'sorted' or 'random'. 'sorted' also uses the sequence length.
:param int epoch: for 'random', this determines the random seed
:param int num_seqs:
:param ((int) -> int)|None get_seq_len: function (originalSeqIdx: int) -> int
:return: the order for the given epoch. such that seq_idx -> underlying idx
:rtype: list[int]
"""
partition_epoch = self.partition_epoch or 1
repeat_epoch = self.repeat_epoch or 1
if not epoch:
epoch = 1
full_epoch = epoch
if partition_epoch > 1:
full_epoch = (epoch - 1) // partition_epoch + 1
assert num_seqs > 0
seq_index = list(range(num_seqs)) # type: typing.List[int] # the real seq idx after sorting
if self._seq_order_seq_lens_file:
get_seq_len = self._get_seq_order_seq_lens_by_idx
if self.seq_ordering == 'default':
pass # Keep order as-is.
elif self.seq_ordering.startswith("default_every_n:"):
# This order is useful if you have "initial_state": "keep_over_epoch",
# where num == max_seqs, batch_size = inf, max_seq_len = inf, chunking = None.
_, num = self.seq_ordering.split(":")
num = int(num)
seq_index = numpy.arange(num_seqs // num, dtype="int64").repeat(num)
for i in range(1, num):
seq_index[i::num] += i * (num_seqs // num)
seq_index = list(seq_index)
elif self.seq_ordering == 'reverse':
seq_index = list(reversed(seq_index))
elif self.seq_ordering == 'sorted':
assert get_seq_len
seq_index.sort(key=get_seq_len) # sort by length, starting with shortest
elif self.seq_ordering == "sorted_reverse":
assert get_seq_len
seq_index.sort(key=get_seq_len, reverse=True) # sort by length, in reverse, starting with longest
elif self.seq_ordering.startswith('sort_bin_shuffle'):
# Shuffle seqs, sort by length, and shuffle bins (then shuffle seqs within each bin if sort_bin_shuffle_x2).
assert get_seq_len
tmp = self.seq_ordering.split(':')[1:]
# Keep this deterministic! Use fixed seed.
if len(tmp) <= 1:
nth = 1
else:
nth = int(tmp[1])
rnd_seed = ((full_epoch - 1) // nth + 1) if full_epoch else 1
rnd = Random(rnd_seed + self.random_seed_offset)
rnd.shuffle(seq_index) # Shuffle sequences.
seq_index.sort(key=get_seq_len) # Sort by length, starting with shortest.
if len(tmp) == 0:
bins = 2
else:
if tmp[0].startswith("."): # starting with "." -> approx chunk size (num of seqs in one bin)
bins = max(num_seqs // int(tmp[0][1:]), 2)
else: # the number of bins
bins = int(tmp[0])
bin_ids = list(range(bins))
rnd.shuffle(bin_ids) # Shuffle bins.
out_index = []
for i in bin_ids:
if i == bins - 1:
part = seq_index[i * len(seq_index) // bins:][:]
else:
part = seq_index[i * len(seq_index) // bins:(i + 1) * len(seq_index) // bins][:]
if self.seq_ordering.startswith('sort_bin_shuffle_x2'):
rnd.shuffle(part) # Shuffle within the bin.
out_index += part
seq_index = out_index
elif self.seq_ordering.startswith('laplace'):
assert get_seq_len
tmp = self.seq_ordering.split(':')[1:]
if len(tmp) == 0:
bins = 2
else:
if tmp[0].startswith("."): # starting with "." -> approx chunk size (num of seqs in one bin)
bins = max(num_seqs // int(tmp[0][1:]), 2)
else: # the number of bins
bins = int(tmp[0])
if len(tmp) <= 1:
nth = 1
else:
nth = int(tmp[1])
rnd_seed = ((full_epoch - 1) // nth + 1) if full_epoch else 1
rnd = Random(rnd_seed + self.random_seed_offset)
rnd.shuffle(seq_index)
out_index = []
for i in range(bins):
if i == bins - 1:
part = seq_index[i * len(seq_index) // bins:][:]
else:
part = seq_index[i * len(seq_index) // bins:(i + 1) * len(seq_index) // bins][:]
part.sort(key=get_seq_len, reverse=(i % 2 == 1))
out_index += part
seq_index = out_index
elif self.seq_ordering.startswith('random'):
tmp = self.seq_ordering.split(':')
nth = int(tmp[1]) if len(tmp) > 1 else 1
# Keep this deterministic! Use fixed seed.
rnd_seed = (full_epoch - 1) / nth + 1
rnd = Random(rnd_seed + self.random_seed_offset)
rnd.shuffle(seq_index)
else:
assert False, "invalid batching specified: " + self.seq_ordering
if self.unique_seq_tags:
# Note: This is as generic as possible, but requires that get_all_tags is implemented.
all_seq_tags = self.get_all_tags()
used_seq_tags = set()
seq_index = [
i for i in seq_index
if (all_seq_tags[i] not in used_seq_tags, used_seq_tags.add(all_seq_tags[i]))[0]]
if partition_epoch > 1:
seq_index = self._apply_partition_epoch(seq_index, partition_epoch, epoch)
if repeat_epoch > 1:
seq_index = seq_index * repeat_epoch
if self.seq_tags_filter is not None:
# Note: This is as generic as possible, but requires that get_all_tags is implemented.
assert seq_index
all_seq_tags = self.get_all_tags()
assert len(all_seq_tags) == num_seqs == self.get_total_num_seqs(), "%r vs %r vs %r" % (
len(all_seq_tags), num_seqs, self.get_total_num_seqs())
old_seq_index = seq_index
seq_index = [i for i in seq_index if all_seq_tags[i] in self.seq_tags_filter]
assert seq_index, "%s: empty after applying seq_list_filter_file. Example filter tags: %r, used tags: %r" % (
self, sorted(self.seq_tags_filter)[:3], [all_seq_tags[i] for i in old_seq_index[:3]])
return seq_index
@classmethod
def _apply_partition_epoch(cls, seq_index, partition_epoch, epoch):
"""
:param list[int] seq_index: full list of ordered sequence indices
:param int partition_epoch: number of partitions seq_index should be split into
:param int|None epoch: current epoch
:return: partition of seq_index for current epoch
:rtype: list[int]
"""
num_seqs = len(seq_index)
current_partition = ((epoch or 1) - 1) % partition_epoch
seqs_per_epoch = num_seqs // partition_epoch
partition_sizes = ([seqs_per_epoch + 1] * (num_seqs % partition_epoch) +
[seqs_per_epoch] * (partition_epoch - num_seqs % partition_epoch))
assert sum(partition_sizes) == num_seqs and len(partition_sizes) == partition_epoch
partitions = functools.reduce(lambda a, x: a + [a[-1] + x], partition_sizes, [0]) # cumulative sum
assert len(partitions) == partition_epoch + 1
seq_index = seq_index[partitions[current_partition]:partitions[current_partition + 1]]
assert len(seq_index) == partition_sizes[current_partition]
return seq_index
def _get_random_seed_for_epoch(self, epoch):
"""
:param int|None epoch:
:rtype: int
"""
partition_epoch = self.partition_epoch or 1
full_epoch = epoch or 1
if partition_epoch > 1:
full_epoch = (full_epoch - 1) // partition_epoch + 1
return full_epoch + self.random_seed_offset
def init_seq_order(self, epoch=None, seq_list=None):
"""
:type epoch: int|None
:param list[str] | None seq_list: In case we want to set a predefined order.
:rtype: bool
:returns whether the order changed (True is always safe to return)
This is called when we start a new epoch, or at initialization.
Call this when you reset the seq list.
"""
self.epoch = epoch
self.rnd_seq_drop = Random(self._get_random_seed_for_epoch(epoch=epoch))
return False
def finish_epoch(self):
"""
This would get called at the end of the epoch (currently optional only).
After this, further calls to :func:`get_data` or :func:`load_seqs` are invalid,
until a new call to :func:`init_seq_order` follows.
"""
self.epoch = None
def get_current_seq_order(self):
"""
:return: many datasets use self.get_seq_order_for_epoch. this function would return the current seq order
for the current epoch, after self.init_seq_order was called.
Not all datasets implement this.
:rtype: list[int]
"""
raise OptionalNotImplementedError
def _base_init(self):
self.nbytes = 0
self.zpad = None
# We expect that the following attributes are already set elsewhere, by a derived class.
assert self.num_outputs
if not self.num_inputs:
assert not self.window or self.window in (0, 1) or "data" in self.num_outputs
return
assert self.num_inputs > 0
assert self.window > 0
if int(self.window) % 2 == 0:
self.window += 1
self.nbytes = numpy.array([], dtype=numpy.float32).itemsize * (self.num_inputs * self.window + 1 + 1)
if self.window > 1:
self.zpad = numpy.zeros((int(self.window) // 2, self.num_inputs), dtype=numpy.float32)
def initialize(self):
"""
Does the main initialization before it can be used.
This needs to be called before self.load_seqs() can be used.
"""
self._base_init()
self.init_seq_order()
def get_times(self, sorted_seq_idx):
"""
:param int sorted_seq_idx:
"""
raise OptionalNotImplementedError
def get_data(self, seq_idx, key):
"""
:param int seq_idx: sorted seq idx
:param str key: data-key, e.g. "data" or "classes"
:rtype: numpy.ndarray
:returns features or targets: format 2d (time,feature) (float)
"""
# Fallback implementation for old-style subclasses.
if key == "data":
return self.get_input_data(seq_idx)
else:
return self.get_targets(key, seq_idx)
def get_input_data(self, sorted_seq_idx):
"""
:type sorted_seq_idx: int
:rtype: numpy.ndarray
:returns features: format 2d (time,feature) (float)
"""
raise NotImplementedError
def get_targets(self, target, sorted_seq_idx):
"""
:param str target: data key
:type sorted_seq_idx: int
:rtype: numpy.ndarray
:returns targets: format 1d (time) (int: idx of output-feature)
"""
# For new-style subclasses, which just provide get_data.
return self.get_data(sorted_seq_idx, target)
def get_ctc_targets(self, sorted_seq_idx):
"""
Warning: This is deprecated/obsolete.
:param int sorted_seq_idx:
:rtype: numpy.ndarray|None
"""
return None
def get_data_slice(self, seq_idx, key, start_frame, end_frame):
"""
:param int seq_idx:
:param str key:
:param int start_frame:
:param int end_frame:
:return: x[start_frame:end_frame], with x = get_data(seq_idx, key)
:rtype: numpy.ndarray
"""
if "[sparse:" in key and (start_frame > 0 or end_frame < self.get_seq_length(seq_idx)[key]):
return self._get_data_slice_sparse(seq_idx, key, start_frame, end_frame)
data = self.get_data(seq_idx, key)
return data[start_frame:end_frame]
def _get_data_slice_sparse(self, seq_idx, key, start_frame, end_frame):
key_prefix = key[:key.index("[")]
sparse_info = key[key.index("[") + 1:key.index("]")].split(":")
assert len(sparse_info) == 4
assert tuple(sparse_info[0:3]) == ("sparse", "coo", "2")
s0 = self.get_data(seq_idx, "%s[sparse:coo:2:0]" % key_prefix)
assert s0 is not None
from NativeOp import sparse_splice_offset_numpy
s0_start = sparse_splice_offset_numpy(s0, start_frame)
s0_end = sparse_splice_offset_numpy(s0, end_frame)
if sparse_info[-1] == "0":
return s0[s0_start:s0_end] - start_frame
else:
data = self.get_data(seq_idx, key)
return data[s0_start:s0_end]
def get_tag(self, sorted_seq_idx):
"""
:param int sorted_seq_idx:
:rtype: str
"""
return "seq-%i" % sorted_seq_idx
def get_all_tags(self):
"""
:return: list of all seq tags, of the whole dataset, without partition epoch.
Note that this is not possible with all datasets.
:rtype: list[str]
"""
old_partition_epoch = self.partition_epoch
try:
all_tags = [None] * self.num_seqs # type: typing.List[typing.Union[None,str]]
for seq_idx in range(self.num_seqs):
all_tags[seq_idx] = self.get_tag(seq_idx)
return all_tags
finally:
self.partition_epoch = old_partition_epoch
def get_total_num_seqs(self):
"""
:return: total number of seqs, without partition epoch.
Should be the same as len(self.get_all_tags()).
Note that this is not possible with all datasets.
:rtype: int
"""
if self.partition_epoch == 1:
# Note: self.num_seqs might not always be set, or even be correct...
return self.num_seqs
raise NotImplementedError("%s: get_total_num_seqs with partition epoch %i" % (self, self.partition_epoch))
def have_corpus_seq_idx(self):
"""
:rtype: bool
:return: whether you can call self.get_corpus_seq_idx()
"""
return False
def get_corpus_seq_idx(self, seq_idx):
"""
:param int seq_idx: sorted sequence index from the current epoch, depending on seq_ordering
:return: the sequence index as-is in the original corpus (as if you would have sorting="default").
only defined if self.have_corpus_seq_idx()
:rtype: int
"""
if self.seq_ordering == "default":
return seq_idx
assert self.have_corpus_seq_idx()
raise NotImplemented
def has_ctc_targets(self):
"""
:return: whether we have get_ctc_targets implemented
:rtype: bool
"""
return False
# noinspection PyMethodMayBeStatic
def get_max_ctc_length(self):
"""
:rtype: int
"""
return 0
@classmethod
def generic_complete_frac(cls, seq_idx, num_seqs):
"""
:param int seq_idx: idx
:param int|None num_seqs: None if not available
:return: Returns a fraction (float in [0,1], always > 0) of how far we have advanced
for this seq in the dataset.
This does not have to be exact. This is only for the user.
"""
if num_seqs:
return min(float(seq_idx + 1) / num_seqs, 1.0)
else:
# We don't know. So:
# Some monotonic increasing function in [0,1] which never reaches 1.
import math
return max(1.e-10, 1.0 - math.exp(-seq_idx * 1000))
def get_complete_frac(self, seq_idx):
"""
:param int seq_idx:
:return: Returns a fraction (float in [0,1], always > 0) of how far we have advanced
for this seq in the dataset.
This does not have to be exact. This is only for the user.
:rtype: float
"""
# noinspection PyBroadException
try:
num_seqs = self.num_seqs
except Exception: # num_seqs not always available
# noinspection PyBroadException
try:
num_seqs = self.estimated_num_seqs
except Exception: # also not always available
num_seqs = None # ignore
return self.generic_complete_frac(seq_idx, num_seqs)
@property
def num_seqs(self):
"""
:rtype: int
"""
raise NotImplementedError
@property
def estimated_num_seqs(self):
"""
:return: estimated num seqs. does not have to be exact
:rtype: int|None
"""
# noinspection PyBroadException
try:
return self.num_seqs
except Exception: # might not be available
pass
if self._estimated_num_seqs is not None:
return self._estimated_num_seqs
return None
def get_data_keys(self):
"""
:return: all available data keys (for get_data and all other functions)
:rtype: list[str]
"""
return ["data"] + self.get_target_list()
def get_target_list(self):
"""
:return: subset of :func:`get_data_keys`. target keys are usually not available during inference
:rtype: list[str]
"""
return ["classes"]
def get_data_dim(self, key):
"""
:param str key: e.g. "data" or "classes"
:return: number of classes, no matter if sparse or not
:rtype: int
"""
if key in self.num_outputs:
# num_outputs should have the correct dimension, even for key "data" with self.window > 1.
return self.num_outputs[key][0]
if self.window > 1 and key == "data":
assert self.num_inputs
return self.num_inputs * self.window
return 1 # unknown
def get_data_dtype(self, key):
"""
:param str key: e.g. "data" or "classes"
:return: dtype as str, e.g. "int32" or "float32"
:rtype: str
"""
if self.is_data_sparse(key):
return "int32"
return "float32"
def is_data_sparse(self, key):
"""
:param str key: e.g. "data" or "classes"
:return: whether the data is sparse
:rtype: bool
"""
# Note: We cannot call get_data_dtype, as we would maybe result in infinite recursion.
if key in self.num_outputs:
return self.num_outputs[key][1] <= 1
if key == "data":
return False
return True
def get_data_shape(self, key):
"""
:returns get_data(*, key).shape[1:], i.e. num-frames excluded
:rtype: list[int]
"""
if key in self.num_outputs:
if self.num_outputs[key][1] <= 1:
return []
res_shape = [None] * (self.num_outputs[key][1] - 1) # type: typing.List[typing.Union[None,int]]
if not self.is_data_sparse(key):
res_shape[-1] = self.get_data_dim(key)
return res_shape
if self.is_data_sparse(key):
return []
return [self.get_data_dim(key)]
def have_seqs(self):
"""
:return: whether num_seqs > 0
:rtype: bool
"""
return self.is_less_than_num_seqs(0)
def len_info(self):
"""
:rtype: str
:returns a string to present the user as information about our len.
Depending on our implementation, we can give some more or some less information.
"""
return ", ".join([self.__class__.__name__,
"sequences: %s" % try_run(lambda: self.num_seqs, default="unknown"),
"frames: %s" % try_run(self.get_num_timesteps, default="unknown")])
def is_less_than_num_seqs(self, n):
"""
:type n: int
:rtype: bool
:returns whether n < num_seqs. In case num_seqs is not known in advance, it will wait
until it knows that n is behind the end or that we have the seq.
"""
# We keep this dynamic so that other implementations which don't know the num of seqs
# in advance can handle this somehow.
return n < self.num_seqs
def can_serialize_data(self, key):
"""
:param str key: e.g. "classes"
:rtype: bool
"""
return key in self.labels
def serialize_data(self, key, data):
"""
:param str key: e.g. "classes". self.labels[key] should be set
:param numpy.ndarray data: 1D
:rtype: str
"""
labels = self.labels[key]
if len(labels) < 1000 and all([len(label) == 1 for label in labels]):
# are these actually raw bytes? -> assume utf8
if all([ord(label) <= 255 for label in labels]):
try:
if PY3:
return bytes([ord(labels[c]) for c in data]).decode("utf8")
else:
return b"".join([bytes(labels[c]) for c in data]).decode("utf8")
except UnicodeDecodeError:
pass # pass on to default case
return "".join(map(labels.__getitem__, data))
else:
return " ".join(map(labels.__getitem__, data))
def calculate_priori(self, target="classes"):
"""
:param str target:
:rtype: numpy.ndarray
"""
priori = numpy.zeros((self.num_outputs[target][0],), dtype=numpy.float32)
i = 0
while self.is_less_than_num_seqs(i):
self.load_seqs(i, i + 1)
for t in self.get_targets(target, i):
priori[t] += 1
i += 1
return numpy.array(priori / self.get_num_timesteps(), dtype=numpy.float32)
def iterate_seqs(self, chunk_size=None, chunk_step=None, used_data_keys=None):
"""
Takes chunking into consideration.
:param int|NumbersDict chunk_size:
:param int|NumbersDict chunk_step:
:param set(str)|None used_data_keys:
:return: generator which yields tuples (seq index, seq start, seq end)
:rtype: list[(int,NumbersDict,NumbersDict)]
"""
if chunk_size is None:
chunk_size = self.chunk_size
if chunk_step is None:
chunk_step = self.chunk_step
chunk_size = NumbersDict(chunk_size)
chunk_step = NumbersDict(chunk_step)
chunk_size_orig = chunk_size.copy()
chunk_step_orig = chunk_step.copy()
s = 0
while self.is_less_than_num_seqs(s):
length = self.get_seq_length(s)
if chunk_size == 0:
yield s, NumbersDict.constant_like(0, numbers_dict=length), length
else:
default_key = "data"
if used_data_keys is not None:
length = NumbersDict({k: length[k] for k in used_data_keys})
if default_key not in used_data_keys:
default_key = sorted(used_data_keys)[0]
if chunk_step[default_key] == 0: # allow some keys with zero chunk-step
assert chunk_step.max_value() > 0
default_key = [key for key in sorted(used_data_keys) if chunk_step[key] > 0][0]
if self.chunking_variance > 0:
chunking_variance = 1. - self.rnd_seq_drop.random() * self.chunking_variance
for k in used_data_keys:
chunk_size[k] = max(int(chunk_size_orig[k] * chunking_variance), 1)
chunk_step[k] = max(int(chunk_step_orig[k] * chunking_variance), 1)
# In case there are data keys with different chunk sizes,
# make sure we keep the original ratio.
smallest_key = [
k for k in sorted(used_data_keys, key=lambda key: (chunk_step_orig[key], key))
if chunk_step_orig[k] > 0][0]
for k in used_data_keys:
if chunk_size_orig[k] > chunk_size_orig[smallest_key]:
if chunk_size_orig[k] % chunk_size_orig[smallest_key] == 0:
ratio = chunk_size_orig[k] // chunk_size_orig[smallest_key]
chunk_size[k] = chunk_size[smallest_key] * ratio
chunk_step[k] = chunk_step[smallest_key] * ratio
assert chunk_step[default_key] > 0
t = NumbersDict.constant_like(0, numbers_dict=length)
# There are usually the 'data' (input) and 'classes' (targets) data-keys in `length` but there can be others.
# We expect them all of the same length so that we can do chunking.
# In case that some length is 0 or 1,
# we treat it special and always return the full seq repeated for every chunk.
keys_with_full_seqs = []
for key in length.keys():
if chunk_step[key] == chunk_step[default_key]:
if length[key] == length[default_key]:
continue # ok
if length[key] <= 1: # special case as explained above
keys_with_full_seqs.append(key)
continue
if chunk_step[key] == chunk_step[default_key]:
raise Exception("Chunking with multiple data-keys of different length: %r" % length)
else:
limit = limit_default = 1
if self.min_chunk_size == chunk_size[default_key]:
limit = chunk_size[key]
limit_default = chunk_size[default_key]
nr_of_chunks = (length[key] - limit) // chunk_step[key] + 1
nr_of_chunks_default = (length[default_key] - limit_default) // chunk_step[default_key] + 1
assert nr_of_chunks == nr_of_chunks_default, (
"%s: iterate seqs with chunking: length %r, chunk size/step %r/%r (min %r), key %r (default %r)" % (
self, length, chunk_size, chunk_step, self.min_chunk_size, key, default_key))
while length[default_key] > t[default_key]:
chunk_start = NumbersDict(t)