-
Notifications
You must be signed in to change notification settings - Fork 446
/
data_io.py
1446 lines (1238 loc) · 53.7 KB
/
data_io.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
##########################################################
# pytorch-kaldi v.0.1
# Mirco Ravanelli, Titouan Parcollet
# Mila, University of Montreal
# October 2018
##########################################################
import numpy as np
import sys
from utils import compute_cw_max, dict_fea_lab_arch, is_sequential_dict
import os
import configparser
import re, gzip, struct
def load_dataset(
fea_scp, fea_opts, lab_folder, lab_opts, left, right, max_sequence_length, output_folder, fea_only=False
):
def _input_is_wav_file(fea_scp):
with open(fea_scp, "r") as f:
first_line = f.readline()
ark_file = first_line.split(" ")[1].split(":")[0]
with open(ark_file, "rb") as f:
first_ark_line = f.readline()
return b"RIFF" in first_ark_line
def _input_is_feature_file(fea_scp):
return not _input_is_wav_file(fea_scp)
def _read_features_and_labels_with_kaldi(fea_scp, fea_opts, fea_only, lab_folder, lab_opts, output_folder):
fea = dict()
lab = dict()
if _input_is_feature_file(fea_scp):
kaldi_bin = "copy-feats"
read_function = read_mat_ark
elif _input_is_wav_file(fea_scp):
kaldi_bin = "wav-copy"
read_function = read_vec_flt_ark
fea = {
k: m
for k, m in read_function("ark:" + kaldi_bin + " scp:" + fea_scp + " ark:- |" + fea_opts, output_folder)
}
if not fea_only:
lab = {
k: v
for k, v in read_vec_int_ark(
"gunzip -c " + lab_folder + "/ali*.gz | " + lab_opts + " " + lab_folder + "/final.mdl ark:- ark:-|",
output_folder,
)
if k in fea
} # Note that I'm copying only the aligments of the loaded fea
fea = {
k: v for k, v in fea.items() if k in lab
} # This way I remove all the features without an aligment (see log file in alidir "Did not Succeded")
return fea, lab
def _chunk_features_and_labels(max_sequence_length, fea, lab, fea_only, input_is_wav):
def _append_to_concat_list(fea_chunked, lab_chunked, fea_conc, lab_conc, name):
for j in range(0, len(fea_chunked)):
fea_conc.append(fea_chunked[j])
lab_conc.append(lab_chunked[j])
if len(fea_chunked) > 1:
snt_name.append(name + "_split" + str(j))
else:
snt_name.append(k)
return fea_conc, lab_conc
def _chunk(max_sequence_length, fea, lab, fea_only):
def _chunk_by_input_and_output_chunk_config(chunk_config, fea, lab, fea_only):
"""
If the sequence length is above the threshold, we split it with a minimal length max/4
If max length = 500, then the split will start at 500 + (500/4) = 625.
A seq of length 625 will be splitted in one of 500 and one of 125
"""
chunk_size_fea, chunk_step_fea, chunk_size_lab, chunk_step_lab = (
chunk_config["chunk_size_fea"],
chunk_config["chunk_step_fea"],
chunk_config["chunk_size_lab"],
chunk_config["chunk_step_lab"],
)
fea_chunked = list()
lab_chunked = list()
split_threshold_fea = chunk_size_fea + (chunk_size_fea / 4)
if (len(fea) > chunk_size_fea) and chunk_size_fea > 0:
nr_of_chunks = (len(fea) + chunk_size_fea - 1) // chunk_size_fea
for i in range(nr_of_chunks):
chunk_start_fea = i * chunk_step_fea
if len(fea[chunk_start_fea:]) > split_threshold_fea:
chunk_end_fea = chunk_start_fea + chunk_size_fea
fea_chunk = fea[chunk_start_fea:chunk_end_fea]
if not fea_only:
chunk_start_lab = i * chunk_step_lab
chunk_end_lab = chunk_start_lab + chunk_size_lab
lab_chunk = lab[chunk_start_lab:chunk_end_lab]
else:
lab_chunk = np.zeros((fea_chunk.shape[0],))
fea_chunked.append(fea_chunk)
lab_chunked.append(lab_chunk)
else:
fea_chunk = fea[chunk_start_fea:]
if not fea_only:
chunk_start_lab = i * chunk_step_lab
lab_chunk = lab[chunk_start_lab:]
else:
lab_chunk = np.zeros((fea_chunk.shape[0],))
lab_chunked.append(lab_chunk)
fea_chunked.append(fea_chunk)
break
else:
fea_chunked.append(fea)
if not fea_only:
lab_chunked.append(lab)
else:
lab_chunked.append(np.zeros((fea.shape[0],)))
return fea_chunked, lab_chunked
chunk_config = dict()
if type(max_sequence_length) == dict:
chunk_config["chunk_size_fea"] = max_sequence_length["chunk_size_fea"]
chunk_config["chunk_step_fea"] = max_sequence_length["chunk_step_fea"]
chunk_config["chunk_size_lab"] = max_sequence_length["chunk_size_lab"]
chunk_config["chunk_step_lab"] = max_sequence_length["chunk_step_lab"]
elif type(max_sequence_length) == int:
chunk_config["chunk_size_fea"] = max_sequence_length
chunk_config["chunk_step_fea"] = max_sequence_length
chunk_config["chunk_size_lab"] = max_sequence_length
chunk_config["chunk_step_lab"] = max_sequence_length
else:
raise ValueError("Unknown type of max_sequence_length")
return _chunk_by_input_and_output_chunk_config(chunk_config, fea, lab, fea_only)
snt_name = list()
fea_conc = list()
lab_conc = list()
feature_keys_soted_by_sequence_length = sorted(sorted(fea.keys()), key=lambda k: len(fea[k]))
for k in feature_keys_soted_by_sequence_length:
fea_el = fea[k]
lab_el = None
if not fea_only:
lab_el = lab[k]
fea_chunked, lab_chunked = _chunk(max_sequence_length, fea_el, lab_el, fea_only)
fea_conc, lab_conc = _append_to_concat_list(fea_chunked, lab_chunked, fea_conc, lab_conc, k)
return fea_conc, lab_conc, snt_name
def _concatenate_features_and_labels(fea_conc, lab_conc):
def _sort_chunks_by_length(fea_conc, lab_conc):
fea_zipped = zip(fea_conc, lab_conc)
fea_sorted = sorted(fea_zipped, key=lambda x: x[0].shape[0])
fea_conc, lab_conc = zip(*fea_sorted)
return fea_conc, lab_conc
def _get_end_index_from_list(conc):
end_snt = 0
end_index = list()
for entry in conc:
end_snt = end_snt + entry.shape[0]
end_index.append(end_snt)
return end_index
fea_conc, lab_conc = _sort_chunks_by_length(fea_conc, lab_conc)
end_index_fea = _get_end_index_from_list(fea_conc)
end_index_lab = _get_end_index_from_list(lab_conc)
fea_conc = np.concatenate(fea_conc)
lab_conc = np.concatenate(lab_conc)
return fea_conc, lab_conc, end_index_fea, end_index_lab
def _match_feature_and_label_sequence_lengths(fea, lab, max_sequence_length):
ALLOW_FRAME_DIFF_LARGER_ONE = False
def _adjust_feature_sequence_length(fea, nr_of_fea_for_lab):
nr_of_fea = fea.shape[0]
if nr_of_fea > nr_of_fea_for_lab:
fea_adj = np.take(fea, range(nr_of_fea_for_lab), axis=0)
elif nr_of_fea < nr_of_fea_for_lab:
padding = np.zeros(shape=(nr_of_fea_for_lab - nr_of_fea,) + fea.shape[1:])
fea_adj = np.concatenate([fea, padding], axis=0)
else:
fea_adj = fea
return fea_adj
chunk_size_fea = max_sequence_length["chunk_size_fea"]
chunk_step_fea = max_sequence_length["chunk_step_fea"]
chunk_size_lab = max_sequence_length["chunk_size_lab"]
chunk_step_lab = max_sequence_length["chunk_step_lab"]
window_shift = max_sequence_length["window_shift"]
window_size = max_sequence_length["window_size"]
for k in fea.keys():
nr_of_fea = fea[k].shape[0]
nr_of_lab = lab[k].shape[0]
nr_of_fea_for_lab = (nr_of_lab - 1) * window_shift + window_size
if abs(nr_of_fea - nr_of_fea_for_lab) > window_shift and not ALLOW_FRAME_DIFF_LARGER_ONE:
raise ValueError(
"Nr. of features: "
+ str(nr_of_fea)
+ " does not match nr. of labels: "
+ str(nr_of_lab)
+ " with expected nr. of features: "
+ str(nr_of_fea_for_lab)
)
fea[k] = _adjust_feature_sequence_length(fea[k], nr_of_fea_for_lab)
return fea, lab
fea, lab = _read_features_and_labels_with_kaldi(fea_scp, fea_opts, fea_only, lab_folder, lab_opts, output_folder)
if _input_is_wav_file(fea_scp) and (not fea_only):
fea, lab = _match_feature_and_label_sequence_lengths(fea, lab, max_sequence_length)
fea_chunks, lab_chunks, chunk_names = _chunk_features_and_labels(
max_sequence_length, fea, lab, fea_only, _input_is_wav_file(fea_scp)
)
fea_conc, lab_conc, end_index_fea, end_index_lab = _concatenate_features_and_labels(fea_chunks, lab_chunks)
return [chunk_names, fea_conc, lab_conc, np.asarray(end_index_fea), np.asarray(end_index_lab)]
def context_window_old(fea, left, right):
N_row = fea.shape[0]
N_fea = fea.shape[1]
frames = np.empty((N_row - left - right, N_fea * (left + right + 1)))
for frame_index in range(left, N_row - right):
right_context = fea[frame_index + 1 : frame_index + right + 1].flatten() # right context
left_context = fea[frame_index - left : frame_index].flatten() # left context
current_frame = np.concatenate([left_context, fea[frame_index], right_context])
frames[frame_index - left] = current_frame
return frames
def context_window(fea, left, right):
N_elem = fea.shape[0]
N_fea = fea.shape[1]
fea_conc = np.empty([N_elem, N_fea * (left + right + 1)])
index_fea = 0
for lag in range(-left, right + 1):
fea_conc[:, index_fea : index_fea + fea.shape[1]] = np.roll(fea, -lag, axis=0)
index_fea = index_fea + fea.shape[1]
fea_conc = fea_conc[left : fea_conc.shape[0] - right]
return fea_conc
def load_chunk(
fea_scp, fea_opts, lab_folder, lab_opts, left, right, max_sequence_length, output_folder, fea_only=False
):
# open the file
[data_name, data_set, data_lab, end_index_fea, end_index_lab] = load_dataset(
fea_scp, fea_opts, lab_folder, lab_opts, left, right, max_sequence_length, output_folder, fea_only
)
# TODO: currently end_index_lab is ignored
# Context window
if left != 0 or right != 0:
data_set = context_window(data_set, left, right)
end_index_fea = end_index_fea - left
end_index_fea[-1] = end_index_fea[-1] - right
# mean and variance normalization
data_set = (data_set - np.mean(data_set, axis=0)) / np.std(data_set, axis=0)
# Label processing
data_lab = data_lab - data_lab.min()
if right > 0:
data_lab = data_lab[left:-right]
else:
data_lab = data_lab[left:]
data_set = np.column_stack((data_set, data_lab))
return [data_name, data_set, end_index_fea]
def load_counts(class_counts_file):
with open(class_counts_file) as f:
row = next(f).strip().strip("[]").strip()
counts = np.array([np.float32(v) for v in row.split()])
return counts
def read_lab_fea_refac01(cfg_file, fea_only, shared_list, output_folder):
def _read_chunk_specific_config(cfg_file):
if not (os.path.exists(cfg_file)):
sys.stderr.write("ERROR: The config file %s does not exist!\n" % (cfg_file))
sys.exit(0)
else:
config = configparser.ConfigParser()
config.read(cfg_file)
return config
def _read_from_config(config, fea_only):
def _get_max_seq_length_from_config_str(config_str):
max_seq_length = [int(e) for e in config_str.split(",")]
if len(max_seq_length) == 1:
max_seq_length = max_seq_length[0]
else:
assert len(max_seq_length) == 6
max_seq_length_list = max_seq_length
max_seq_length = dict()
max_seq_length["chunk_size_fea"] = max_seq_length_list[0]
max_seq_length["chunk_step_fea"] = max_seq_length_list[1]
max_seq_length["chunk_size_lab"] = max_seq_length_list[2]
max_seq_length["chunk_step_lab"] = max_seq_length_list[3]
max_seq_length["window_shift"] = max_seq_length_list[4]
max_seq_length["window_size"] = max_seq_length_list[5]
return max_seq_length
to_do = config["exp"]["to_do"]
if to_do == "train":
max_seq_length = _get_max_seq_length_from_config_str(config["batches"]["max_seq_length_train"])
if to_do == "valid":
max_seq_length = _get_max_seq_length_from_config_str(config["batches"]["max_seq_length_valid"])
if to_do == "forward":
max_seq_length = -1 # do to break forward sentences
fea_only = True
fea_dict, lab_dict, arch_dict = dict_fea_lab_arch(config, fea_only)
seq_model = is_sequential_dict(config, arch_dict)
return to_do, max_seq_length, fea_dict, lab_dict, arch_dict, seq_model
def _read_features_and_labels(fea_dict, lab_dict, max_seq_length, fea_only, output_folder):
def _get_fea_config_from_dict(fea_dict_entr):
fea_scp = fea_dict_entr[1]
fea_opts = fea_dict_entr[2]
cw_left = int(fea_dict_entr[3])
cw_right = int(fea_dict_entr[4])
return fea_scp, fea_opts, cw_left, cw_right
def _get_lab_config_from_dict(lab_dict_entr, fea_only):
if fea_only:
lab_folder = None
lab_opts = None
else:
lab_folder = lab_dict_entr[1]
lab_opts = lab_dict_entr[2]
return lab_folder, lab_opts
def _compensate_for_different_context_windows(
data_set_fea,
data_set_lab,
cw_left_max,
cw_left,
cw_right_max,
cw_right,
data_end_index_fea,
data_end_index_lab,
):
data_set_lab = np.take(
data_set_lab,
range(cw_left_max - cw_left, data_set_lab.shape[0] - (cw_right_max - cw_right)),
axis=0,
mode="clip",
)
data_set_fea = np.take(
data_set_fea,
range(cw_left_max - cw_left, data_set_fea.shape[0] - (cw_right_max - cw_right)),
axis=0,
mode="clip",
)
data_end_index_fea = data_end_index_fea - (cw_left_max - cw_left)
data_end_index_lab = data_end_index_lab - (cw_left_max - cw_left)
data_end_index_fea[-1] = data_end_index_fea[-1] - (cw_right_max - cw_right)
data_end_index_lab[-1] = data_end_index_lab[-1] - (cw_right_max - cw_right)
return data_set_lab, data_set_fea, data_end_index_fea, data_end_index_lab
def _update_data(data_set, labs, fea_dict, fea, fea_index, data_set_fea, labs_fea, cnt_fea, cnt_lab):
if cnt_fea == 0 and cnt_lab == 0:
data_set = data_set_fea
labs = labs_fea
fea_dict[fea].append(fea_index)
fea_index = fea_index + data_set_fea.shape[1]
fea_dict[fea].append(fea_index)
fea_dict[fea].append(fea_dict[fea][6] - fea_dict[fea][5])
elif cnt_fea == 0 and (not cnt_lab == 0):
labs = np.column_stack((labs, labs_fea))
elif (not cnt_fea == 0) and cnt_lab == 0:
data_set = np.column_stack((data_set, data_set_fea))
fea_dict[fea].append(fea_index)
fea_index = fea_index + data_set_fea.shape[1]
fea_dict[fea].append(fea_index)
fea_dict[fea].append(fea_dict[fea][6] - fea_dict[fea][5])
return data_set, labs, fea_dict, fea_index
def _check_consistency(
data_name,
data_name_fea,
data_end_index_fea_ini,
data_end_index_fea,
data_end_index_lab_ini,
data_end_index_lab,
):
if not (data_name == data_name_fea):
sys.stderr.write(
'ERROR: different sentence ids are detected for the different features. Plase check again input feature lists"\n'
)
sys.exit(0)
if not (data_end_index_fea_ini == data_end_index_fea).all():
sys.stderr.write('ERROR end_index must be the same for all the sentences"\n')
sys.exit(0)
if not (data_end_index_lab_ini == data_end_index_lab).all():
sys.stderr.write('ERROR end_index must be the same for all the sentences"\n')
sys.exit(0)
def _update_lab_dict(lab_dict, data_set):
cnt_lab = 0
for lab in lab_dict.keys():
lab_dict[lab].append(data_set.shape[1] + cnt_lab)
cnt_lab = cnt_lab + 1
return lab_dict
def _load_chunk_refac01(
fea_scp, fea_opts, lab_folder, lab_opts, left, right, max_sequence_length, output_folder, fea_only=False
):
[data_name, data_set, data_lab, end_index_fea, end_index_lab] = load_dataset(
fea_scp, fea_opts, lab_folder, lab_opts, left, right, max_sequence_length, output_folder, fea_only
)
# TODO: this function will currently only work well if no context window is given or fea and lab have the same time dimensionality
# Context window
if left != 0 or right != 0:
data_set = context_window(data_set, left, right)
end_index_fea = end_index_fea - left
end_index_lab = end_index_lab - left
end_index_fea[-1] = end_index_fea[-1] - right
end_index_lab[-1] = end_index_lab[-1] - right
# mean and variance normalization
data_set = (data_set - np.mean(data_set, axis=0)) / np.std(data_set, axis=0)
# Label processing
data_lab = data_lab - data_lab.min()
if right > 0:
data_lab = data_lab[left:-right]
else:
data_lab = data_lab[left:]
if len(data_set.shape) == 1:
data_set = np.expand_dims(data_set, -1)
return [data_name, data_set, data_lab, end_index_fea, end_index_lab]
cw_left_max, cw_right_max = compute_cw_max(fea_dict)
fea_index = 0
cnt_fea = 0
data_name = None
data_end_index_fea_ini = None
data_end_index_lab_ini = None
data_set = None
labs = None
for fea in fea_dict.keys():
fea_scp, fea_opts, cw_left, cw_right = _get_fea_config_from_dict(fea_dict[fea])
cnt_lab = 0
if fea_only:
lab_dict.update({"lab_name": "none"})
for lab in lab_dict.keys():
lab_folder, lab_opts = _get_lab_config_from_dict(lab_dict[lab], fea_only)
data_name_fea, data_set_fea, data_set_lab, data_end_index_fea, data_end_index_lab = _load_chunk_refac01(
fea_scp, fea_opts, lab_folder, lab_opts, cw_left, cw_right, max_seq_length, output_folder, fea_only
)
if sum([abs(e) for e in [cw_left_max, cw_right_max, cw_left, cw_right]]) != 0:
data_set_lab, data_set_fea, data_end_index_fea, data_end_index_lab = _compensate_for_different_context_windows(
data_set_fea,
data_set_lab,
cw_left_max,
cw_left,
cw_right_max,
cw_right,
data_end_index_fea,
data_end_index_lab,
)
if cnt_fea == 0 and cnt_lab == 0:
data_end_index_fea_ini = data_end_index_fea
data_end_index_lab_ini = data_end_index_lab
data_name = data_name_fea
data_set, labs, fea_dict, fea_index = _update_data(
data_set, labs, fea_dict, fea, fea_index, data_set_fea, data_set_lab, cnt_fea, cnt_lab
)
_check_consistency(
data_name,
data_name_fea,
data_end_index_fea_ini,
data_end_index_fea,
data_end_index_lab_ini,
data_end_index_lab,
)
cnt_lab = cnt_lab + 1
cnt_fea = cnt_fea + 1
if not fea_only:
lab_dict = _update_lab_dict(lab_dict, data_set)
return data_name, data_end_index_fea_ini, data_end_index_lab_ini, fea_dict, lab_dict, data_set, labs
def _reorder_data_set(data_set, labs, seq_model, to_do):
if not (seq_model) and to_do != "forward" and (data_set.shape[0] == labs.shape[0]):
data_set_shape = data_set.shape[1]
data_set_joint = np.column_stack((data_set, labs))
np.random.shuffle(data_set)
data_set = data_set_joint[:, :data_set_shape]
labs = np.squeeze(data_set_joint[:, data_set_shape:], axis=-1)
return data_set, labs
def _append_to_shared_list(
shared_list, data_name, data_end_index_fea, data_end_index_lab, fea_dict, lab_dict, arch_dict, data_set
):
shared_list.append(data_name)
shared_list.append(data_end_index_fea)
shared_list.append(data_end_index_lab)
shared_list.append(fea_dict)
shared_list.append(lab_dict)
shared_list.append(arch_dict)
shared_list.append(data_set)
return shared_list
config = _read_chunk_specific_config(cfg_file)
to_do, max_seq_length, fea_dict, lab_dict, arch_dict, seq_model = _read_from_config(config, fea_only)
data_name, data_end_index_fea, data_end_index_lab, fea_dict, lab_dict, data_set, labs = _read_features_and_labels(
fea_dict, lab_dict, max_seq_length, fea_only, output_folder
)
data_set, labs = _reorder_data_set(data_set, labs, seq_model, to_do)
data_set = {"input": data_set, "ref": labs}
shared_list = _append_to_shared_list(
shared_list, data_name, data_end_index_fea, data_end_index_lab, fea_dict, lab_dict, arch_dict, data_set
)
def read_lab_fea(cfg_file, fea_only, shared_list, output_folder):
# Reading chunk-specific cfg file (first argument-mandatory file)
if not (os.path.exists(cfg_file)):
sys.stderr.write("ERROR: The config file %s does not exist!\n" % (cfg_file))
sys.exit(0)
else:
config = configparser.ConfigParser()
config.read(cfg_file)
# Reading some cfg parameters
to_do = config["exp"]["to_do"]
if to_do == "train":
max_seq_length = int(
config["batches"]["max_seq_length_train"]
) # *(int(info_file[-13:-10])+1) # increasing over the epochs
if to_do == "valid":
max_seq_length = int(config["batches"]["max_seq_length_valid"])
if to_do == "forward":
max_seq_length = -1 # do to break forward sentences
[fea_dict, lab_dict, arch_dict] = dict_fea_lab_arch(config, fea_only)
[cw_left_max, cw_right_max] = compute_cw_max(fea_dict)
fea_index = 0
cnt_fea = 0
for fea in fea_dict.keys():
# reading the features
fea_scp = fea_dict[fea][1]
fea_opts = fea_dict[fea][2]
cw_left = int(fea_dict[fea][3])
cw_right = int(fea_dict[fea][4])
cnt_lab = 0
# Production case, we don't have labels (lab_name = none)
if fea_only:
lab_dict.update({"lab_name": "none"})
for lab in lab_dict.keys():
# Production case, we don't have labels (lab_name = none)
if fea_only:
lab_folder = None
lab_opts = None
else:
lab_folder = lab_dict[lab][1]
lab_opts = lab_dict[lab][2]
[data_name_fea, data_set_fea, data_end_index_fea] = load_chunk(
fea_scp, fea_opts, lab_folder, lab_opts, cw_left, cw_right, max_seq_length, output_folder, fea_only
)
# making the same dimenion for all the features (compensating for different context windows)
labs_fea = data_set_fea[cw_left_max - cw_left : data_set_fea.shape[0] - (cw_right_max - cw_right), -1]
data_set_fea = data_set_fea[cw_left_max - cw_left : data_set_fea.shape[0] - (cw_right_max - cw_right), 0:-1]
data_end_index_fea = data_end_index_fea - (cw_left_max - cw_left)
data_end_index_fea[-1] = data_end_index_fea[-1] - (cw_right_max - cw_right)
if cnt_fea == 0 and cnt_lab == 0:
data_set = data_set_fea
labs = labs_fea
data_end_index = data_end_index_fea
data_end_index = data_end_index_fea
data_name = data_name_fea
fea_dict[fea].append(fea_index)
fea_index = fea_index + data_set_fea.shape[1]
fea_dict[fea].append(fea_index)
fea_dict[fea].append(fea_dict[fea][6] - fea_dict[fea][5])
else:
if cnt_fea == 0:
labs = np.column_stack((labs, labs_fea))
if cnt_lab == 0:
data_set = np.column_stack((data_set, data_set_fea))
fea_dict[fea].append(fea_index)
fea_index = fea_index + data_set_fea.shape[1]
fea_dict[fea].append(fea_index)
fea_dict[fea].append(fea_dict[fea][6] - fea_dict[fea][5])
# Checks if lab_names are the same for all the features
if not (data_name == data_name_fea):
sys.stderr.write(
'ERROR: different sentence ids are detected for the different features. Plase check again input feature lists"\n'
)
sys.exit(0)
# Checks if end indexes are the same for all the features
if not (data_end_index == data_end_index_fea).all():
sys.stderr.write('ERROR end_index must be the same for all the sentences"\n')
sys.exit(0)
cnt_lab = cnt_lab + 1
cnt_fea = cnt_fea + 1
cnt_lab = 0
if not fea_only:
for lab in lab_dict.keys():
lab_dict[lab].append(data_set.shape[1] + cnt_lab)
cnt_lab = cnt_lab + 1
data_set = np.column_stack((data_set, labs))
# check automatically if the model is sequential
seq_model = is_sequential_dict(config, arch_dict)
# Randomize if the model is not sequential
if not (seq_model) and to_do != "forward":
np.random.shuffle(data_set)
# Split dataset in many part. If the dataset is too big, we can have issues to copy it into the shared memory (due to pickle limits)
# N_split=10
# data_set=np.array_split(data_set, N_split)
# Adding all the elements in the shared list
shared_list.append(data_name)
shared_list.append(data_end_index)
shared_list.append(fea_dict)
shared_list.append(lab_dict)
shared_list.append(arch_dict)
shared_list.append(data_set)
# The following libraries are copied from kaldi-io-for-python project (https://github.com/vesis84/kaldi-io-for-python)
# Copyright 2014-2016 Brno University of Technology (author: Karel Vesely)
# Licensed under the Apache License, Version 2.0 (the "License")
#################################################
# Define all custom exceptions,
class UnsupportedDataType(Exception):
pass
class UnknownVectorHeader(Exception):
pass
class UnknownMatrixHeader(Exception):
pass
class BadSampleSize(Exception):
pass
class BadInputFormat(Exception):
pass
class SubprocessFailed(Exception):
pass
#################################################
# Data-type independent helper functions,
def open_or_fd(file, output_folder, mode="rb"):
""" fd = open_or_fd(file)
Open file, gzipped file, pipe, or forward the file-descriptor.
Eventually seeks in the 'file' argument contains ':offset' suffix.
"""
offset = None
try:
# strip 'ark:' prefix from r{x,w}filename (optional),
if re.search("^(ark|scp)(,scp|,b|,t|,n?f|,n?p|,b?o|,n?s|,n?cs)*:", file):
(prefix, file) = file.split(":", 1)
# separate offset from filename (optional),
if re.search(":[0-9]+$", file):
(file, offset) = file.rsplit(":", 1)
# input pipe?
if file[-1] == "|":
fd = popen(file[:-1], output_folder, "rb") # custom,
# output pipe?
elif file[0] == "|":
fd = popen(file[1:], output_folder, "wb") # custom,
# is it gzipped?
elif file.split(".")[-1] == "gz":
fd = gzip.open(file, mode)
# a normal file...
else:
fd = open(file, mode)
except TypeError:
# 'file' is opened file descriptor,
fd = file
# Eventually seek to offset,
if offset != None:
fd.seek(int(offset))
return fd
# based on '/usr/local/lib/python3.4/os.py'
def popen(cmd, output_folder, mode="rb"):
if not isinstance(cmd, str):
raise TypeError("invalid cmd type (%s, expected string)" % type(cmd))
import subprocess, io, threading
# cleanup function for subprocesses,
def cleanup(proc, cmd):
ret = proc.wait()
if ret > 0:
raise SubprocessFailed("cmd %s returned %d !" % (cmd, ret))
return
# text-mode,
if mode == "r":
err = open(output_folder + "/log.log", "a")
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=err)
threading.Thread(target=cleanup, args=(proc, cmd)).start() # clean-up thread,
return io.TextIOWrapper(proc.stdout)
elif mode == "w":
err = open(output_folder + "/log.log", "a")
proc = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stderr=err)
threading.Thread(target=cleanup, args=(proc, cmd)).start() # clean-up thread,
return io.TextIOWrapper(proc.stdin)
# binary,
elif mode == "rb":
err = open(output_folder + "/log.log", "a")
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=err)
threading.Thread(target=cleanup, args=(proc, cmd)).start() # clean-up thread,
return proc.stdout
elif mode == "wb":
err = open(output_folder + "/log.log", "a")
proc = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stderr=err)
threading.Thread(target=cleanup, args=(proc, cmd)).start() # clean-up thread,
return proc.stdin
# sanity,
else:
raise ValueError("invalid mode %s" % mode)
def read_key(fd):
""" [key] = read_key(fd)
Read the utterance-key from the opened ark/stream descriptor 'fd'.
"""
key = ""
while 1:
char = fd.read(1).decode("latin1")
if char == "":
break
if char == " ":
break
key += char
key = key.strip()
if key == "":
return None # end of file,
assert re.match("^\S+$", key) != None # check format (no whitespace!)
return key
#################################################
# Integer vectors (alignments, ...),
def read_ali_ark(file_or_fd, output_folder):
""" Alias to 'read_vec_int_ark()' """
return read_vec_int_ark(file_or_fd, output_folder)
def read_vec_int_ark(file_or_fd, output_folder):
""" generator(key,vec) = read_vec_int_ark(file_or_fd)
Create generator of (key,vector<int>) tuples, which reads from the ark file/stream.
file_or_fd : ark, gzipped ark, pipe or opened file descriptor.
Read ark to a 'dictionary':
d = { u:d for u,d in kaldi_io.read_vec_int_ark(file) }
"""
fd = open_or_fd(file_or_fd, output_folder)
try:
key = read_key(fd)
while key:
ali = read_vec_int(fd, output_folder)
yield key, ali
key = read_key(fd)
finally:
if fd is not file_or_fd:
fd.close()
def read_vec_int(file_or_fd, output_folder):
""" [int-vec] = read_vec_int(file_or_fd)
Read kaldi integer vector, ascii or binary input,
"""
fd = open_or_fd(file_or_fd, output_folder)
binary = fd.read(2).decode()
if binary == "\0B": # binary flag
assert fd.read(1).decode() == "\4"
# int-size
vec_size = np.frombuffer(fd.read(4), dtype="int32", count=1)[0] # vector dim
if vec_size == 0:
return np.array([], dtype="int32")
# Elements from int32 vector are sored in tuples: (sizeof(int32), value),
vec = np.frombuffer(fd.read(vec_size * 5), dtype=[("size", "int8"), ("value", "int32")], count=vec_size)
assert vec[0]["size"] == 4 # int32 size,
ans = vec[:]["value"] # values are in 2nd column,
else: # ascii,
arr = (binary + fd.readline().decode()).strip().split()
try:
arr.remove("[")
arr.remove("]") # optionally
except ValueError:
pass
ans = np.array(arr, dtype=int)
if fd is not file_or_fd:
fd.close() # cleanup
return ans
# Writing,
def write_vec_int(file_or_fd, output_folder, v, key=""):
""" write_vec_int(f, v, key='')
Write a binary kaldi integer vector to filename or stream.
Arguments:
file_or_fd : filename or opened file descriptor for writing,
v : the vector to be stored,
key (optional) : used for writing ark-file, the utterance-id gets written before the vector.
Example of writing single vector:
kaldi_io.write_vec_int(filename, vec)
Example of writing arkfile:
with open(ark_file,'w') as f:
for key,vec in dict.iteritems():
kaldi_io.write_vec_flt(f, vec, key=key)
"""
fd = open_or_fd(file_or_fd, output_folder, mode="wb")
if sys.version_info[0] == 3:
assert fd.mode == "wb"
try:
if key != "":
fd.write((key + " ").encode("latin1")) # ark-files have keys (utterance-id),
fd.write("\0B".encode()) # we write binary!
# dim,
fd.write("\4".encode()) # int32 type,
fd.write(struct.pack(np.dtype("int32").char, v.shape[0]))
# data,
for i in range(len(v)):
fd.write("\4".encode()) # int32 type,
fd.write(struct.pack(np.dtype("int32").char, v[i])) # binary,
finally:
if fd is not file_or_fd:
fd.close()
#################################################
# Float vectors (confidences, ivectors, ...),
# Reading,
def read_vec_flt_scp(file_or_fd, output_folder):
""" generator(key,mat) = read_vec_flt_scp(file_or_fd)
Returns generator of (key,vector) tuples, read according to kaldi scp.
file_or_fd : scp, gzipped scp, pipe or opened file descriptor.
Iterate the scp:
for key,vec in kaldi_io.read_vec_flt_scp(file):
...
Read scp to a 'dictionary':
d = { key:mat for key,mat in kaldi_io.read_mat_scp(file) }
"""
fd = open_or_fd(file_or_fd, output_folder)
try:
for line in fd:
(key, rxfile) = line.decode().split(" ")
vec = read_vec_flt(rxfile, output_folder)
yield key, vec
finally:
if fd is not file_or_fd:
fd.close()
def read_vec_flt_ark(file_or_fd, output_folder):
""" generator(key,vec) = read_vec_flt_ark(file_or_fd)
Create generator of (key,vector<float>) tuples, reading from an ark file/stream.
file_or_fd : ark, gzipped ark, pipe or opened file descriptor.
Read ark to a 'dictionary':
d = { u:d for u,d in kaldi_io.read_vec_flt_ark(file) }
"""
fd = open_or_fd(file_or_fd, output_folder)
try:
key = read_key(fd)
while key:
ali = read_vec_flt(fd, output_folder)
yield key, ali
key = read_key(fd)
finally:
if fd is not file_or_fd:
fd.close()
def read_vec_flt(file_or_fd, output_folder):
""" [flt-vec] = read_vec_flt(file_or_fd)
Read kaldi float vector, ascii or binary input,
"""
fd = open_or_fd(file_or_fd, output_folder)
binary = fd.read(2).decode()
if binary == "\0B": # binary flag
return _read_vec_flt_binary(fd)
elif binary == "RI":
return _read_vec_flt_riff(fd)
else: # ascii,
arr = (binary + fd.readline().decode()).strip().split()
try:
arr.remove("[")
arr.remove("]") # optionally
except ValueError:
pass
ans = np.array(arr, dtype=float)
if fd is not file_or_fd:
fd.close() # cleanup
return ans
def _read_vec_flt_riff(fd):
RIFF_CHUNK_DESCR_HEADER_SIZE = 12
ALREADY_READ_HEADER_BYTES = 2
SUB_CHUNK_HEADER_SIZE = 8
DATA_CHUNK_HEADER_SIZE = 8
def pcm2float(signal, dtype="float32"):
signal = np.asarray(signal)
dtype = np.dtype(dtype)
return signal.astype(dtype) / dtype.type(-np.iinfo(signal.dtype).min)
import struct
header = fd.read(RIFF_CHUNK_DESCR_HEADER_SIZE - ALREADY_READ_HEADER_BYTES)
assert header[:2] == b"FF"
chunk_header = fd.read(SUB_CHUNK_HEADER_SIZE)
subchunk_id, subchunk_size = struct.unpack("<4sI", chunk_header)
aformat, channels, samplerate, byterate, block_align, bps = struct.unpack("HHIIHH", fd.read(subchunk_size))
subchunk2_id, subchunk2_size = struct.unpack("<4sI", fd.read(DATA_CHUNK_HEADER_SIZE))
pcm_data = np.frombuffer(fd.read(subchunk2_size), dtype="int" + str(bps))
return pcm2float(pcm_data)
def _read_vec_flt_binary(fd):
header = fd.read(3).decode()
if header == "FV ":
sample_size = 4 # floats
elif header == "DV ":
sample_size = 8 # doubles
else:
raise UnknownVectorHeader("The header contained '%s'" % header)
assert sample_size > 0
# Dimension,
assert fd.read(1).decode() == "\4"
# int-size
vec_size = np.frombuffer(fd.read(4), dtype="int32", count=1)[0] # vector dim
if vec_size == 0:
return np.array([], dtype="float32")
# Read whole vector,
buf = fd.read(vec_size * sample_size)
if sample_size == 4:
ans = np.frombuffer(buf, dtype="float32")
elif sample_size == 8:
ans = np.frombuffer(buf, dtype="float64")
else:
raise BadSampleSize
return ans
# Writing,
def write_vec_flt(file_or_fd, output_folder, v, key=""):
""" write_vec_flt(f, v, key='')
Write a binary kaldi vector to filename or stream. Supports 32bit and 64bit floats.
Arguments:
file_or_fd : filename or opened file descriptor for writing,
v : the vector to be stored,