-
Notifications
You must be signed in to change notification settings - Fork 13
/
data_io.py
executable file
·137 lines (98 loc) · 4.41 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
import kaldi_io
import numpy as np
from optparse import OptionParser
import ConfigParser
def load_dataset(fea_scp,fea_opts,lab_folder,lab_opts,left,right):
fea= { k:m for k,m in kaldi_io.read_mat_ark('ark:copy-feats scp:'+fea_scp+' ark:- |'+fea_opts) }
lab= { k:v for k,v in kaldi_io.read_vec_int_ark('gunzip -c '+lab_folder+'/ali*.gz | '+lab_opts+' '+lab_folder+'/final.mdl ark:- ark:-|') 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")
count=0
end_snt=0
end_index=[]
snt_name=[]
for k in sorted(fea.keys(), key=lambda k: len(fea[k])):
if count==0:
count=1
fea_conc=fea[k]
lab_conc=lab[k]
end_snt=end_snt+fea[k].shape[0]-left
else:
fea_conc=np.concatenate([fea_conc,fea[k]],axis=0)
lab_conc=np.concatenate([lab_conc,lab[k]],axis=0)
end_snt=end_snt+fea[k].shape[0]
end_index.append(end_snt)
snt_name.append(k)
end_index[-1]=end_index[-1]-right
return [snt_name,fea_conc,lab_conc,end_index]
def context_window(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 load_chunk(fea_scp,fea_opts,lab_folder,lab_opts,left,right,shuffle_seed):
# open the file
[data_name,data_set,data_lab,end_index]=load_dataset(fea_scp,fea_opts,lab_folder,lab_opts,left,right)
# Context window
data_set=context_window(data_set,left,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))
# shuffle (only for test data)
if shuffle_seed>0:
np.random.seed(shuffle_seed)
np.random.shuffle(data_set)
return [data_name,data_set,end_index]
def load_counts(class_counts_file):
with open(class_counts_file) as f:
row = f.next().strip().strip('[]').strip()
counts = np.array([ np.float32(v) for v in row.split() ])
return counts
def read_opts():
parser=OptionParser()
parser.add_option("--cfg") # Mandatory
(options,args)=parser.parse_args()
cfg_file=options.cfg
Config = ConfigParser.ConfigParser()
Config.read(cfg_file)
# DATA
options.out_folder=Config.get('data', 'out_folder')
options.tr_fea_scp=Config.get('data', 'tr_fea_scp')
options.tr_fea_opts=Config.get('data', 'tr_fea_opts')
options.tr_lab_folder=Config.get('data', 'tr_lab_folder')
options.tr_lab_opts=Config.get('data', 'tr_lab_opts')
options.dev_fea_scp=Config.get('data', 'dev_fea_scp')
options.dev_fea_opts=Config.get('data', 'dev_fea_opts')
options.dev_lab_folder=Config.get('data', 'dev_lab_folder')
options.dev_lab_opts=Config.get('data', 'dev_lab_opts')
options.te_fea_scp=Config.get('data', 'te_fea_scp')
options.te_fea_opts=Config.get('data', 'te_fea_opts')
options.te_lab_folder=Config.get('data', 'te_lab_folder')
options.te_lab_opts=Config.get('data', 'te_lab_opts')
options.count_file=Config.get('data', 'count_file')
# ARCHITECTURE
options.hidden_dim=Config.get('architecture', 'hidden_dim')
options.N_hid=Config.get('architecture', 'N_hid')
options.drop_rate=Config.get('architecture', 'drop_rate')
options.use_batchnorm=Config.get('architecture', 'use_batchnorm')
options.cw_left=Config.get('architecture', 'cw_left')
options.cw_right=Config.get('architecture', 'cw_right')
options.seed=Config.get('architecture', 'seed')
options.use_cuda=Config.get('architecture', 'use_cuda')
options.N_ep=Config.get('optimization', 'N_ep')
options.lr=Config.get('optimization', 'lr')
options.halving_factor=Config.get('optimization', 'halving_factor')
options.improvement_threshold=Config.get('optimization', 'improvement_threshold')
options.batch_size=Config.get('optimization', 'batch_size')
options.save_gpumem=Config.get('optimization', 'save_gpumem')
return options