forked from rwth-i6/returnn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHDFDataset.py
1327 lines (1165 loc) · 51.7 KB
/
HDFDataset.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
"""
Provides :class:`HDFDataset`.
"""
from __future__ import print_function
import typing
import collections
import gc
import h5py
import numpy
from CachedDataset import CachedDataset
from CachedDataset2 import CachedDataset2
from Dataset import Dataset, DatasetSeq
from Log import log
import Util
# Common attribute names for HDF dataset, which should be used in order to be proceed with HDFDataset class.
attr_seqLengths = 'seqLengths'
attr_inputPattSize = 'inputPattSize'
attr_numLabels = 'numLabels'
attr_times = 'times'
attr_ctcIndexTranscription = 'ctcIndexTranscription'
class HDFDataset(CachedDataset):
"""
Dataset based on HDF files.
This was the main original dataset format of RETURNN.
"""
def __init__(self, files=None, use_cache_manager=False, **kwargs):
"""
:param None|list[str] files:
:param bool use_cache_manager: uses :func:`Util.cf` for files
"""
super(HDFDataset, self).__init__(**kwargs)
assert self.partition_epoch == 1 or self.cache_byte_size_total_limit == 0, \
"To use partition_epoch in HDFDatasets, disable caching by setting cache_byte_size=0"
self._use_cache_manager = use_cache_manager
self.files = [] # type: typing.List[str] # file names
self.h5_files = [] # type: typing.List[h5py.File]
self.file_start = [0]
self.file_seq_start = [] # type: typing.List[numpy.ndarray]
self.data_dtype = {} # type: typing.Dict[str,str]
self.data_sparse = {} # type: typing.Dict[str,bool]
if files:
for fn in files:
self.add_file(fn)
def __del__(self):
for f in self.h5_files:
# noinspection PyBroadException
try:
f.close()
except Exception: # e.g. at shutdown. but does not matter
pass
del self.h5_files[:]
del self.file_seq_start[:]
@staticmethod
def _decode(s):
"""
:param str|bytes|unicode s:
:rtype: str
"""
from Util import unicode, PY3
if not PY3 and isinstance(s, unicode):
s = s.encode("utf8")
assert isinstance(s, str) # Python 2
if not isinstance(s, str): # bytes (Python 3)
s = s.decode("utf-8")
s = s.split('\0')[0]
return s
def add_file(self, filename):
"""
Setups data:
self.file_start
self.file_seq_start
Use load_seqs() to load the actual data.
:type filename: str
"""
if self._use_cache_manager:
filename = Util.cf(filename)
fin = h5py.File(filename, "r")
if 'targets' in fin:
self.labels = {
k: [self._decode(item) for item in fin["targets/labels"][k][...].tolist()]
for k in fin['targets/labels']}
if not self.labels:
labels = [item.split('\0')[0] for item in fin["labels"][...].tolist()] # type: typing.List[str]
self.labels = {'classes': labels}
assert len(self.labels['classes']) == len(labels), (
"expected " + str(len(self.labels['classes'])) + " got " + str(len(labels)))
self.files.append(filename)
self.h5_files.append(fin)
print("parsing file", filename, file=log.v5)
if 'times' in fin:
if self.timestamps is None:
self.timestamps = fin[attr_times][...]
else:
self.timestamps = numpy.concatenate([self.timestamps, fin[attr_times][...]], axis=0)
prev_target_keys = None
if len(self.files) >= 2:
prev_target_keys = self.target_keys
if 'targets' in fin:
self.target_keys = sorted(
set(fin['targets/labels'].keys()) |
set(fin['targets/data'].keys()) |
set(fin['targets/size'].attrs.keys()))
else:
self.target_keys = ['classes']
seq_lengths = fin[attr_seqLengths][...] # shape (num_seqs,num_target_keys + 1)
if len(seq_lengths.shape) == 1:
seq_lengths = numpy.array(zip(*[seq_lengths.tolist() for _ in range(len(self.target_keys)+1)]))
assert seq_lengths.ndim == 2 and seq_lengths.shape[1] == len(self.target_keys) + 1
if prev_target_keys is not None and prev_target_keys != self.target_keys:
print("Warning: %s: loaded prev files %s, which defined target keys %s. Now loaded %s and got target keys %s." % (
self, self.files[:-1], prev_target_keys, filename, self.target_keys), file=log.v2)
# This can happen for multiple reasons. E.g. just different files. Or saved with different RETURNN versions.
# We currently support this by removing all the new additional targets, which only works if the prev targets
# were a subset (so the order in which you load the files matters).
assert all([key in self.target_keys for key in prev_target_keys]) # check if subset
# Filter out the relevant seq lengths
seq_lengths = seq_lengths[:, [0] + [self.target_keys.index(key) + 1 for key in prev_target_keys]]
assert seq_lengths.shape[1] == len(prev_target_keys) + 1
self.target_keys = prev_target_keys
seq_start = numpy.zeros((seq_lengths.shape[0] + 1, seq_lengths.shape[1]), dtype="int64")
numpy.cumsum(seq_lengths, axis=0, dtype="int64", out=seq_start[1:])
self._num_timesteps += numpy.sum(seq_lengths[:, 0])
if self._num_codesteps is None:
self._num_codesteps = [0 for _ in range(1, len(seq_lengths[0]))]
for i in range(1, len(seq_lengths[0])):
self._num_codesteps[i - 1] += numpy.sum(seq_lengths[:, i])
if not self._seq_start:
self._seq_start = [numpy.zeros((seq_lengths.shape[1],), 'int64')]
# May be large, so better delete them early, we don't need them anymore.
del seq_lengths
self.file_seq_start.append(seq_start)
nseqs = len(seq_start) - 1
self._num_seqs += nseqs
self.file_start.append(self.file_start[-1] + nseqs)
if 'maxCTCIndexTranscriptionLength' in fin.attrs:
self.max_ctc_length = max(self.max_ctc_length, fin.attrs['maxCTCIndexTranscriptionLength'])
if len(fin['inputs'].shape) == 1: # sparse
num_inputs = [fin.attrs[attr_inputPattSize], 1]
else:
num_inputs = [fin['inputs'].shape[1], len(fin['inputs'].shape)] # fin.attrs[attr_inputPattSize]
if self.num_inputs == 0:
self.num_inputs = num_inputs[0]
assert self.num_inputs == num_inputs[0], "wrong input dimension in file %s (expected %s got %s)" % (
filename, self.num_inputs, num_inputs[0])
if 'targets/size' in fin:
num_outputs = {}
for k in self.target_keys:
if numpy.isscalar(fin['targets/size'].attrs[k]):
num_outputs[k] = (int(fin['targets/size'].attrs[k]), len(fin['targets/data'][k].shape))
else: # hdf_dump will give directly as tuple
assert fin['targets/size'].attrs[k].shape == (2,)
num_outputs[k] = tuple([int(v) for v in fin['targets/size'].attrs[k]])
else:
num_outputs = {'classes': [int(fin.attrs[attr_numLabels]), 1]}
num_outputs["data"] = num_inputs
if not self.num_outputs:
self.num_outputs = num_outputs
assert self.num_outputs == num_outputs, "wrong dimensions in file %s (expected %s got %s)" % (
filename, self.num_outputs, num_outputs)
if 'ctcIndexTranscription' in fin:
if self.ctc_targets is None:
self.ctc_targets = fin['ctcIndexTranscription'][...]
else:
tmp = fin['ctcIndexTranscription'][...]
pad_width = self.max_ctc_length - tmp.shape[1]
tmp = numpy.pad(tmp, ((0, 0), (0, pad_width)), 'constant', constant_values=-1)
pad_width = self.max_ctc_length - self.ctc_targets.shape[1]
self.ctc_targets = numpy.pad(self.ctc_targets, ((0, 0), (0, pad_width)), 'constant', constant_values=-1)
self.ctc_targets = numpy.concatenate((self.ctc_targets, tmp))
self.num_running_chars = numpy.sum(self.ctc_targets != -1)
if 'targets' in fin:
for name in self.target_keys:
self.data_dtype[str(name)] = str(fin['targets/data'][name].dtype)
self.targets[str(name)] = None
if str(name) not in self.num_outputs:
ndim = len(fin['targets/data'][name].shape)
dim = 1 if ndim == 1 else fin['targets/data'][name].shape[-1]
self.num_outputs[str(name)] = (dim, ndim)
self.data_dtype["data"] = str(fin['inputs'].dtype)
assert len(self.target_keys) == len(self.file_seq_start[0][0]) - 1
def _load_seqs(self, start, end):
"""
Load data sequences.
As a side effect, will modify / fill-up:
self.alloc_intervals
self.targets
self.chars
:param int start: start sorted seq idx
:param int end: end sorted seq idx
"""
assert start < self.num_seqs
assert end <= self.num_seqs
if self.cache_byte_size_total_limit == 0:
# Just don't use the alloc intervals, or any of the other logic. Just load it on the fly when requested.
return
selection = self.insert_alloc_interval(start, end)
assert len(selection) <= end - start, (
"DEBUG: more sequences requested (" + str(len(selection)) + ") as required (" + str(end-start) + ")")
self.preload_set |= set(range(start, end)) - set(selection)
file_info = [[] for _ in range(len(self.files))] # type: typing.List[typing.List[typing.Tuple[int,int]]]
# file_info[i] is (sorted seq idx from selection, real seq idx)
for idc in selection:
if self.sample(idc):
ids = self._seq_index[idc]
file_info[self._get_file_index(ids)].append((idc, ids))
else:
self.preload_set.add(idc)
for i in range(len(self.files)):
if len(file_info[i]) == 0:
continue
if start == 0 or self.cache_byte_size_total_limit > 0: # suppress with disabled cache
print("loading file %d/%d (seq range %i-%i)" % (i+1, len(self.files), start, end), self.files[i], file=log.v4)
fin = self.h5_files[i]
inputs = fin['inputs']
targets = None
if 'targets' in fin:
targets = {k: fin['targets/data/' + k] for k in fin['targets/data']}
for idc, ids in file_info[i]:
s = ids - self.file_start[i]
p = self.file_seq_start[i][s]
q = self.file_seq_start[i][s + 1]
if 'targets' in fin:
for k in fin['targets/data']:
if self.targets[k] is None:
self.targets[k] = numpy.zeros(
(self._num_codesteps[self.target_keys.index(k)],) + targets[k].shape[1:], dtype=self.data_dtype[k]) - 1
ldx = self.target_keys.index(k) + 1
self.targets[k][self.get_seq_start(idc)[ldx]:self.get_seq_start(idc)[ldx] + q[ldx] - p[ldx]] = (
targets[k][p[ldx]:q[ldx]])
self._set_alloc_intervals_data(idc, data=inputs[p[0]:q[0]])
self.preload_set.add(idc)
gc.collect()
def get_data(self, seq_idx, key):
"""
:param int seq_idx:
:param str key:
:rtype: numpy.ndarray
"""
if self.cache_byte_size_total_limit > 0: # Use the cache?
return super(HDFDataset, self).get_data(seq_idx, key)
# Otherwise, directly read it from file now.
real_seq_idx = self._seq_index[seq_idx]
file_idx = self._get_file_index(real_seq_idx)
fin = self.h5_files[file_idx]
real_file_seq_idx = real_seq_idx - self.file_start[file_idx]
start_pos = self.file_seq_start[file_idx][real_file_seq_idx]
end_pos = self.file_seq_start[file_idx][real_file_seq_idx + 1]
if key == "data":
inputs = fin['inputs']
data = inputs[start_pos[0]:end_pos[0]]
if self.window > 1:
data = self._sliding_window(data)
else:
assert 'targets' in fin
targets = fin['targets/data/' + key]
ldx = self.target_keys.index(key) + 1
data = targets[start_pos[ldx]:end_pos[ldx]]
return data
def get_input_data(self, sorted_seq_idx):
"""
:param int sorted_seq_idx:
:rtype: numpy.ndarray
"""
if self.cache_byte_size_total_limit > 0: # Use the cache?
return super(HDFDataset, self).get_input_data(sorted_seq_idx)
return self.get_data(sorted_seq_idx, "data")
def get_targets(self, target, sorted_seq_idx):
"""
:param str target:
:param int sorted_seq_idx:
:rtype: numpy.ndarray
"""
if self.cache_byte_size_total_limit > 0: # Use the cache?
return super(HDFDataset, self).get_targets(target, sorted_seq_idx)
return self.get_data(sorted_seq_idx, target)
def _get_seq_length_by_real_idx(self, real_seq_idx):
"""
:param int real_seq_idx:
:returns length of the sequence with index 'real_seq_idx'. see get_seq_length_nd
:rtype: numpy.ndarray
"""
file_idx = self._get_file_index(real_seq_idx)
real_file_seq_idx = real_seq_idx - self.file_start[file_idx]
start_pos = self.file_seq_start[file_idx][real_file_seq_idx]
end_pos = self.file_seq_start[file_idx][real_file_seq_idx + 1]
return end_pos - start_pos
def _get_tag_by_real_idx(self, real_seq_idx):
file_idx = self._get_file_index(real_seq_idx)
real_file_seq_idx = real_seq_idx - self.file_start[file_idx]
s = self.h5_files[file_idx]["seqTags"][real_file_seq_idx]
s = self._decode(s)
return s
def get_tag(self, sorted_seq_idx):
"""
:param int sorted_seq_idx:
:rtype: str
"""
ids = self._seq_index[self._index_map[sorted_seq_idx]]
return self._get_tag_by_real_idx(ids)
def get_all_tags(self):
"""
:rtype: list[str]
"""
tags = []
for h5_file in self.h5_files:
tags += h5_file["seqTags"][...].tolist()
return list(map(self._decode, tags))
def get_total_num_seqs(self):
"""
:rtype: int
"""
return self._num_seqs
def is_data_sparse(self, key):
"""
:param str key:
:rtype: bool
"""
if self.get_data_dtype(key).startswith("int"):
if key in self.num_outputs:
return self.num_outputs[key][1] <= 1
return False
def get_data_dtype(self, key):
"""
:param str key:
:rtype: str
"""
return self.data_dtype[key]
def len_info(self):
"""
:rtype: str
"""
return ", ".join(["HDF dataset",
"sequences: %i" % self.num_seqs,
"frames: %i" % self.get_num_timesteps()])
def _get_file_index(self, real_seq_idx):
file_index = 0
while file_index < len(self.file_start) - 1 and real_seq_idx >= self.file_start[file_index + 1]:
file_index += 1
return file_index
# ------------------------------------------------------------------------------
class StreamParser(object):
def __init__(self, seq_names, stream):
self.seq_names = seq_names
self.stream = stream
self.num_features = None
self.feature_type = None # 1 for sparse, 2 for dense
self.dtype = None
def get_data(self, seq_name):
raise NotImplementedError()
def get_seq_length(self, seq_name):
raise NotImplementedError()
def get_dtype(self):
return self.dtype
class FeatureSequenceStreamParser(StreamParser):
def __init__(self, *args, **kwargs):
super(FeatureSequenceStreamParser, self).__init__(*args, **kwargs)
for s in self.seq_names:
seq_data = self.stream['data'][s]
assert len(seq_data.shape) == 2
if self.num_features is None:
self.num_features = seq_data.shape[1]
if self.dtype is None:
self.dtype = seq_data.dtype
assert seq_data.shape[1] == self.num_features
assert seq_data.dtype == self.dtype
self.feature_type = 2
def get_data(self, seq_name):
return self.stream['data'][seq_name][...]
def get_seq_length(self, seq_name):
return self.stream['data'][seq_name].shape[0]
class SparseStreamParser(StreamParser):
def __init__(self, *args, **kwargs):
super(SparseStreamParser, self).__init__(*args, **kwargs)
for s in self.seq_names:
seq_data = self.stream['data'][s]
assert len(seq_data.shape) == 1
if self.dtype is None:
self.dtype = seq_data.dtype
assert seq_data.dtype == self.dtype
self.num_features = self.stream['feature_names'].shape[0]
self.feature_type = 1
def get_data(self, seq_name):
return self.stream['data'][seq_name][:]
def get_seq_length(self, seq_name):
return self.stream['data'][seq_name].shape[0]
class SegmentAlignmentStreamParser(StreamParser):
def __init__(self, *args, **kwargs):
super(SegmentAlignmentStreamParser, self).__init__(*args, **kwargs)
for s in self.seq_names:
seq_data = self.stream['data'][s]
if self.dtype is None:
self.dtype = seq_data.dtype
assert seq_data.dtype == self.dtype
assert len(seq_data.shape) == 2
assert seq_data.shape[1] == 2
self.num_features = self.stream['feature_names'].shape[0]
self.feature_type = 1
def get_data(self, seq_name):
# we return flatted two-dimensional data where the 2nd dimension is 2 [class, segment end]
length = self.get_seq_length(seq_name) // 2
segments = self.stream['data'][seq_name][:]
alignment = numpy.zeros((length, 2), dtype=self.dtype)
num_segments = segments.shape[0]
seg_end = 0
for i in range(num_segments):
next_seg_end = seg_end + segments[i, 1]
alignment[seg_end:next_seg_end, 0] = segments[i, 0] # set class
alignment[next_seg_end - 1, 1] = 1 # mark segment end
seg_end = next_seg_end
alignment = alignment.reshape((-1,))
return alignment
def get_seq_length(self, seq_name):
return 2 * sum(self.stream['data'][seq_name][:, 1])
class NextGenHDFDataset(CachedDataset2):
"""
Another separate dataset which uses HDF files to store the data.
"""
parsers = {'feature_sequence': FeatureSequenceStreamParser,
'sparse': SparseStreamParser,
'segment_alignment': SegmentAlignmentStreamParser}
def __init__(self, input_stream_name, files=None, **kwargs):
"""
:param str input_stream_name:
:param None|list[str] files:
"""
super(NextGenHDFDataset, self).__init__(**kwargs)
self.input_stream_name = input_stream_name
self.files = []
self.h5_files = []
self.all_seq_names = []
self.seq_name_to_idx = {}
self.file_indices = []
self.seq_order = []
self.all_parsers = collections.defaultdict(list)
if files:
for fn in files:
self.add_file(fn)
def add_file(self, path):
self.files.append(path)
self.h5_files.append(h5py.File(path))
cur_file = self.h5_files[-1]
assert {'seq_names', 'streams'}.issubset(set(cur_file.keys())), (
"%s does not contain all required datasets/groups" % path)
seqs = list(cur_file['seq_names'])
norm_seqs = [self._normalize_seq_name(s) for s in seqs]
prev_no_seqs = len(self.all_seq_names)
seqs_in_this_file = len(seqs)
self.seq_name_to_idx.update(zip(seqs, range(prev_no_seqs, prev_no_seqs + seqs_in_this_file + 1)))
self.all_seq_names.extend(seqs)
self.file_indices.extend([len(self.files) - 1] * len(seqs))
all_streams = set(cur_file['streams'].keys())
assert self.input_stream_name in all_streams, (
"%s does not contain the input stream %s" % (path, self.input_stream_name))
parsers = {
name: NextGenHDFDataset.parsers[stream.attrs['parser']](norm_seqs, stream)
for name, stream in cur_file['streams'].items()}
for k, v in parsers.items():
self.all_parsers[k].append(v)
if len(self.files) == 1:
self.num_outputs = {name: [parser.num_features, parser.feature_type] for name, parser in parsers.items()}
self.num_inputs = self.num_outputs[self.input_stream_name][0]
else:
num_features = [(name, self.num_outputs[name][0], parser.num_features) for name, parser in parsers.items()]
assert all([nf[1] == nf[2] for nf in num_features]), (
'\n'.join([
"Number of features does not match for parser %s: %d (config) vs. %d (hdf-file)" % nf
for nf in num_features if nf[1] != nf[2]]))
def initialize(self):
total_seqs = len(self.all_seq_names)
self._num_seqs = total_seqs
self._estimated_num_seqs = total_seqs
super(NextGenHDFDataset, self).initialize()
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.
"""
super(NextGenHDFDataset, self).init_seq_order(epoch, seq_list)
if seq_list is not None:
self.seq_order = [self.seq_name_to_idx[s] for s in seq_list]
else:
epoch = epoch or 1
self.seq_order = self.get_seq_order_for_epoch(epoch, len(self.all_seq_names), self._get_seq_length)
def _get_seq_length(self, orig_seq_idx):
"""
:type orig_seq_idx: int
:rtype int
"""
parser = self.all_parsers[self.input_stream_name][self.file_indices[orig_seq_idx]]
return parser.get_seq_length(self._normalize_seq_name(self.all_seq_names[orig_seq_idx]))
def _collect_single_seq(self, seq_idx):
"""
:type seq_idx: int
:rtype: DatasetSeq
"""
if seq_idx >= len(self.seq_order):
return None
real_seq_index = self.seq_order[seq_idx]
file_index = self.file_indices[real_seq_index]
seq_name = self.all_seq_names[real_seq_index]
norm_seq_name = self._normalize_seq_name(seq_name)
targets = {name: parsers[file_index].get_data(norm_seq_name) for name, parsers in self.all_parsers.items()}
features = targets[self.input_stream_name]
return DatasetSeq(seq_idx=seq_idx,
seq_tag=seq_name,
features=features,
targets=targets)
def get_data_dtype(self, key):
if key == 'data':
return self.get_data_dtype(self.input_stream_name)
return self.all_parsers[key][0].get_dtype()
@staticmethod
def _normalize_seq_name(name):
"""
HDF Datasets cannot contain '/' in their name (this would create subgroups), we do not
want this and thus replace it with '\' when asking for data from the parsers
:type name: string
:rtype: string
"""
return name.replace('/', '\\')
class SiameseHDFDataset(CachedDataset2):
"""
SiameseHDFDataset class allows to do sequence sampling for weakly-supervised training.
It accepts data in the format of NextGenHDFDataset and performs sampling of sequence triplets before each epoch.
Triplets are tuples of the format: (anchor seq, random seq with the same label, random seq with a different label)
Here we assume that each dataset from the input .hdf has a single label.
In the config we can access streams by e.g. ["data:features_0"], ["data:features_1"], ["data:features_2"].
Split names depend on stream names in the input data, e.g. "features", "data", "classes", etc.
SiameseHDFDataset method _collect_single_seq(self, seq_idx) returns a DatasetSeq with extended dictionary of targets.
"data:features_0" key stands for features of anchor sequences from the input data.
In NexGenHDFDataset it would correspond to "data:features" or "data".
"data:features_1" is a key, which denote a pair of "data:features_0".
For each anchor sequence SiameseHDFDataset randomly samples a sequence with the same label.
"data:features_2" denotes the third element in a triplet tuple.
For each anchor sequence SiameseHDFDataset randomly samples a sequence with a different label.
Targets are splitted into different streams as well, e.g. "data:classes_0", "data:classes_1", "data:classes_2".
SiameseHDFDataset also supports non-uniform sampling and accepts a path to .npz matrix.
Rows of this matrix should have probabilities for each of the classes to be sampled.
This probability distribution might reflect class similarities.
This dataset might be useful for metric learning,
where we want to learn such representations of input sequences,
that those which belong to the same class are close together,
while those with different labels should have representations far away from each other.
"""
parsers = {'feature_sequence': FeatureSequenceStreamParser,
'sparse': SparseStreamParser,
'segment_alignment': SegmentAlignmentStreamParser}
def __init__(self, input_stream_name, seq_label_stream='words', class_distribution=None, files=None, **kwargs):
"""
:param str input_stream_name: name of a feature stream
:param str seq_label_stream: name of a stream with labels
:param str class_distribution: path to .npz file of size n x n (n is a number of classes),
where each line i contains probs of other classes to be picked in triplets
when sampling a pair for element from class i
:param list[str] files: list of paths to .hdf files
"""
super(SiameseHDFDataset, self).__init__(**kwargs)
self.input_stream_name = input_stream_name
if class_distribution is not None:
self.class_probs = numpy.load(class_distribution)['arr_0']
else:
self.class_probs = None
self.files = []
self.h5_files = []
self.all_seq_names = [] # all_seq_names[(int)seq_index] = (string) sequence_name
self.seq_name_to_idx = {} # (string) sequence_name -> seq_index (int)
self.file_indices = [] # file_indices[(int)seq_index] = file_index => indices of files to which seqs belongs to
self.seq_order = []
self.all_parsers = collections.defaultdict(list)
self.seq_to_target = {} # (string) sequence_name -> (int) class_index
self.target_to_seqs = {} # (int) class_index -> (string) sequence_names
self.curr_epoch_triplets = []
self.targets_stream = seq_label_stream
if files:
for fn in files:
self.add_file(fn)
def add_file(self, path):
"""
register input files and sequences
:param path: path to single .hdf file
"""
self.files.append(path)
self.h5_files.append(h5py.File(path, "r"))
cur_file = self.h5_files[-1]
assert {'seq_names', 'streams'}.issubset(set(cur_file.keys())), (
"%s does not contain all required datasets/groups" % path)
seqs = list(cur_file['seq_names'])
norm_seqs = [self._normalize_seq_name(s) for s in seqs]
prev_no_seqs = len(self.all_seq_names)
seqs_in_this_file = len(seqs)
self.seq_name_to_idx.update(zip(seqs, range(prev_no_seqs, prev_no_seqs + seqs_in_this_file + 1)))
self.all_seq_names.extend(seqs)
self.file_indices.extend([len(self.files) - 1] * len(seqs))
all_streams = set(cur_file['streams'].keys())
assert self.input_stream_name in all_streams, (
"%s does not contain the input stream %s" % (path, self.input_stream_name))
if self.targets_stream is not None:
assert self.targets_stream in all_streams, (
"%s does not contain the input stream %s" % (path, self.targets_stream))
parsers = {
name: SiameseHDFDataset.parsers[stream.attrs['parser']](norm_seqs, stream)
for name, stream in cur_file['streams'].items()} # name - stream name (words, features, orth_features)
for k, v in parsers.items():
self.all_parsers[k].append(v)
if len(self.files) == 1:
self.num_outputs = {name: [parser.num_features, parser.feature_type] for name, parser in parsers.items()}
self.num_inputs = self.num_outputs[self.input_stream_name][0]
else:
num_features = [(name, self.num_outputs[name][0], parser.num_features) for name, parser in parsers.items()]
assert all([nf[1] == nf[2] for nf in num_features]), (
'\n'.join([
"Number of features does not match for parser %s: %d (config) vs. %d (hdf-file)" % nf
for nf in num_features if nf[1] != nf[2]]))
def initialize(self):
"""
initialize target_to_seqs and seq_to_target dicts
"""
self.target_to_seqs = {}
self.seq_to_target = {}
for cur_file in self.h5_files:
sequences = cur_file['streams'][self.targets_stream]['data'] # (string) seq_name -> (int) word_id
for seq_name, value in sequences.items():
seq_targ = int(value.value[0])
if seq_targ in self.target_to_seqs.keys():
self.target_to_seqs[seq_targ].append(seq_name)
else:
self.target_to_seqs[seq_targ] = [seq_name]
self.seq_to_target[seq_name] = seq_targ
super(SiameseHDFDataset, self).initialize()
def init_seq_order(self, epoch=None, seq_list=None):
"""
:param int|None epoch: current epoch id
:param list[str] | None seq_list: In case we want to set a predefined order.
"""
super(SiameseHDFDataset, self).init_seq_order(epoch, seq_list)
if seq_list is not None:
self.seq_order = [self.seq_name_to_idx[s] for s in seq_list]
else:
epoch = epoch or 1
self.seq_order = self.get_seq_order_for_epoch(epoch, len(self.all_seq_names), self._get_seq_length)
# init random seed for siamese triplet sampling
numpy.random.seed()
self._init_triplets()
def _init_triplets(self):
"""
sample triplet for current epoch: (anchor_sample, sample_from_same_class, sample_from_diff_class)
"""
self.curr_epoch_triplets = []
# here we will intialize triplets before each epoch
for seq_idx, real_seq_idx in enumerate(self.seq_order):
seq_name = self.all_seq_names[real_seq_idx]
seq_target = self.seq_to_target[seq_name]
# randomly sample same pair
same_words = self.target_to_seqs[seq_target]
if len(same_words) > 1:
pair_word_idx = numpy.random.randint(0, len(same_words))
# sample again if pair sequence is the same sequence
while same_words[pair_word_idx] == seq_name:
pair_word_idx = numpy.random.randint(0, len(same_words))
pair_seq_name = same_words[pair_word_idx]
real_pair_idx = self.seq_name_to_idx[pair_seq_name]
else:
real_pair_idx = real_seq_idx
# randomly sample third element from another class
rand_target_val = self._sample_diff_class(seq_target)
# sample again if random class is the same class as anchor
while rand_target_val == seq_target:
rand_target_val = self._sample_diff_class(seq_target)
# sample an example from random_target
rand_seq_id = numpy.random.randint(0, len(self.target_to_seqs[rand_target_val]))
rand_seq_name = self.target_to_seqs[rand_target_val][rand_seq_id]
real_third_idx = self.seq_name_to_idx[rand_seq_name]
self.curr_epoch_triplets.append(tuple((real_seq_idx, real_pair_idx, real_third_idx)))
def _sample_diff_class(self, anchor_seq_target):
"""
draw a class from a space of all classes
:param int anchor_seq_target: id of anchor class
:return: int id of a drawn class
"""
if self.class_probs is not None:
distrib = self.class_probs[anchor_seq_target]
classes = list(map(int, list(self.target_to_seqs.keys())))
probs = numpy.array(distrib[classes])
probs /= numpy.sum(probs)
rand_target_val = numpy.random.choice(classes, size=1, p=probs)[0]
else:
random_target = numpy.random.randint(0, len(list(self.target_to_seqs.keys())))
rand_target_val = list(self.target_to_seqs.keys())[random_target]
return rand_target_val
def _collect_single_seq(self, seq_idx):
"""
:param int seq_idx: sequence id
:rtype: DatasetSeq
"""
if seq_idx >= len(self.seq_order):
return None
real_seq_index = self.seq_order[seq_idx]
seq_name = self.all_seq_names[real_seq_index]
curr_triplet = self.curr_epoch_triplets[seq_idx]
targets = {}
for id_, sample in enumerate(curr_triplet):
real_sample_seq_idx = sample
sample_seq_name = self.all_seq_names[real_sample_seq_idx]
sample_seq_file_index = self.file_indices[real_sample_seq_idx]
norm_sample_seq_name = self._normalize_seq_name(sample_seq_name)
for name, parsers in self.all_parsers.items():
targets['%s_%d' % (name, id_)] = parsers[sample_seq_file_index].get_data(norm_sample_seq_name)
targets['%s_all' % self.targets_stream] = numpy.concatenate(
(targets['%s_0' % self.targets_stream],
targets['%s_1' % self.targets_stream],
targets['%s_2' % self.targets_stream]),
axis=0)
features = targets['%s_%d' % (self.input_stream_name, 0)]
return DatasetSeq(seq_idx=seq_idx,
seq_tag=seq_name,
features=features,
targets=targets)
def _get_seq_length(self, orig_seq_idx):
"""
:type orig_seq_idx: int
:rtype int
"""
parser = self.all_parsers[self.input_stream_name][self.file_indices[orig_seq_idx]]
return parser.get_seq_length(self._normalize_seq_name(self.all_seq_names[orig_seq_idx]))
@staticmethod
def _normalize_seq_name(name):
"""
HDF Datasets cannot contain '/' in their name (this would create subgroups), we do not
want this and thus replace it with '\' when asking for data from the parsers
:type name: string
:rtype: string
"""
return name.replace('/', '\\')
def is_data_sparse(self, key):
"""
:param str key: e.g. "features_0" or "orth_features_0" or "words_0"
:return: whether the data is sparse
:rtype: bool
"""
if "features" in key:
return False
return True
def get_data_dim(self, key):
"""
:param str key: e.g. "features_0", "features_1", "classes_0", etc.
:return: number of classes, no matter if sparse or not
:rtype: int
"""
k = "_".join(key.split("_")[:-1]) if "_" in key else key
if k in self.num_outputs:
return self.num_outputs[k][0]
return 1 # unknown
class SimpleHDFWriter:
"""
Intended for a simple interface, to dump data on-the-fly into a HDF file,
which can be read later by :class:`HDFDataset`.
Note that we dump to a temp file first, and only at :func:`close` we move it over to the real destination.
"""
def __init__(self, filename, dim, labels=None, ndim=None, extra_type=None, swmr=False):
"""
:param str filename: Create file, truncate if exists
:param int|None dim:
:param int ndim: counted without batch
:param list[str]|None labels:
:param dict[str,(int,int,str)]|None extra_type: key -> (dim,ndim,dtype)
:param bool swmr: see http://docs.h5py.org/en/stable/swmr.html
"""
from Util import hdf5_strings, unicode
import tempfile
import os
if ndim is None:
if dim is None:
ndim = 1
else:
ndim = 2
self.dim = dim
self.ndim = ndim
self.labels = labels
if labels:
assert len(labels) == dim
self.filename = filename
# By default, we should not override existing data.
# If we want that at some later point, we can introduce an option for it.
assert not os.path.exists(self.filename)
tmp_fd, self.tmp_filename = tempfile.mkstemp(suffix=".hdf")
os.close(tmp_fd)
self._file = h5py.File(self.tmp_filename, "w", libver='latest' if swmr else None)
self._file.attrs['numTimesteps'] = 0 # we will increment this on-the-fly
self._file.attrs['inputPattSize'] = dim or 1
self._file.attrs['numDims'] = 1 # ignored?
self._file.attrs['numLabels'] = dim or 1
self._file.attrs['numSeqs'] = 0 # we will increment this on-the-fly
if labels:
hdf5_strings(self._file, 'labels', labels)
else:
self._file.create_dataset('labels', (0,), dtype="S5") # dtype string length does not matter
self._datasets = {} # type: typing.Dict[str, h5py.Dataset] # key -> data
# seq_length idx represents (seq_idx,data_key_idx),
# where data_key_idx == 0 is for the main input data,
# and otherwise data_key_idx == 1 + sorted(self._prepared_extra).index(data_key).
# data_key_idx must allow for 2 entries by default, as HDFDataset assumes 'classes' by default.
self._seq_lengths = self._file.create_dataset("seqLengths", (0, 2), dtype='i', maxshape=(None, None))
# Note about strings in HDF: http://docs.h5py.org/en/stable/strings.html
# Earlier we used S%i, i.e. fixed-sized strings, with the calculated max string length.
# noinspection PyUnresolvedReferences
dt = h5py.special_dtype(vlen=unicode)
self._seq_tags = self._file.create_dataset('seqTags', (0,), dtype=dt, maxshape=(None,))
self._extra_num_time_steps = {} # type: typing.Dict[str,int] # key -> num-steps
self._prepared_extra = set()
if extra_type:
self._prepare_extra(extra_type)
if swmr:
assert not self._file.swmr_mode # this also checks whether the attribute exists (right version)
self._file.swmr_mode = True
# See comments in test_SimpleHDFWriter_swmr...
raise NotImplementedError("SimpleHDFWriter SWMR is not really finished...")
def __del__(self):
if self._file:
self._file.close()
self._file = None
def _prepare_extra(self, extra_type):
"""
:param dict[str,(int,int,str)] extra_type: key -> (dim,ndim,dtype)
:return: whether we added a new entry
:rtype: bool
"""
added_count = 0
for data_key, (dim, ndim, dtype) in extra_type.items():
assert data_key != "inputs"
if data_key in self._prepared_extra:
return
if not self._prepared_extra:
# For the first time, need to create the groups.
self._file.create_group('targets/data')
self._file.create_group('targets/size')
self._file.create_group("targets/labels")
Util.hdf5_strings(self._file, "targets/labels/%s" % data_key, ["dummy-label"])
if ndim == 0:
ndim = 1 # we will automatically add a dummy-dim
shape = [None] * ndim # type: typing.List[typing.Optional[int]]
if ndim >= 2:
shape[-1] = dim
if dtype == "string":
# noinspection PyUnresolvedReferences
dtype = h5py.special_dtype(vlen=str)
self._datasets[data_key] = self._file['targets/data'].create_dataset(
data_key, shape=[d if d else 0 for d in shape], dtype=dtype, maxshape=shape)
self._file['targets/size'].attrs[data_key] = [dim or 1, ndim]
self._extra_num_time_steps[data_key] = 0
self._prepared_extra.add(data_key)
added_count += 1
if added_count:
assert self._prepared_extra
self._seq_lengths.resize(1 + len(self._prepared_extra), axis=1)
return bool(added_count)
def _insert_h5_inputs(self, raw_data):
"""
Inserts a record into the hdf5-file.
Resizes if necessary.
:param numpy.ndarray raw_data: shape=(time,data) or shape=(time,)
"""
assert raw_data.ndim >= 1
name = "inputs"
if name not in self._datasets:
self._datasets[name] = self._file.create_dataset(
name, raw_data.shape, raw_data.dtype, maxshape=tuple(None for _ in raw_data.shape))
else: