-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_hdf_dataset.py
More file actions
349 lines (317 loc) · 13.9 KB
/
Copy pathcreate_hdf_dataset.py
File metadata and controls
349 lines (317 loc) · 13.9 KB
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
import argparse
import os
import h5py
import numpy
from dictionary import (
arabic_dictionary,
harakat,
harakat_index_dict,
buckwalter_dictionary,
harakat_buckwalter,
arabic_index_dict,
)
def hdf5_strings(handle, name, data):
"""
:param h5py.File handle:
:param str name:
:param numpy.ndarray data:
"""
# noinspection PyBroadException
try:
s = max([len(d) for d in data])
dset = handle.create_dataset(name, (len(data),), dtype="S" + str(s))
dset[...] = data
except Exception:
# noinspection PyUnresolvedReferences
dt = h5py.special_dtype(vlen="unicode")
del handle[name]
dset = handle.create_dataset(name, (len(data),), dtype=dt)
dset[...] = data
class SimpleHDFWriter:
def __init__(self, filename, dim, labels=None, ndim=None):
"""
:param str filename:
:param int|None dim:
:param int ndim: counted without batch
:param numpy.ndarray|List[str]|None labels:
"""
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._file = h5py.File(filename, "w")
self._file.attrs["numTimesteps"] = 0 # we will increment this on-the-fly
self._other_num_time_steps = 0
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")
self._datasets = {} # type: dict[str, h5py.Dataset]
self._tags = [] # type: list[str]
self._seq_lengths = self._file.create_dataset("seqLengths", (0, 2), dtype="i", maxshape=(None, 2))
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:
old_shape = self._datasets[name].shape
self._datasets[name].resize((old_shape[0] + raw_data.shape[0],) + old_shape[1:])
# append raw data to dataset
self._datasets[name][self._file.attrs["numTimesteps"] :] = raw_data
self._file.attrs["numTimesteps"] += raw_data.shape[0]
self._file.attrs["numSeqs"] += 1
def _insert_h5_other(self, data_key, raw_data, dtype=None, add_time_dim=False, dim=None):
"""
:param str data_key:
:param numpy.ndarray|int|float|list[int] raw_data: shape=(time,data) or shape=(time,) or shape=()...
:param str dtype:
:param bool add_time_dim:
:param int|None dim:
"""
if isinstance(raw_data, (int, float, list)):
raw_data = numpy.array(raw_data)
assert isinstance(raw_data, numpy.ndarray)
if add_time_dim:
raw_data = raw_data[None, :]
assert raw_data.ndim > 0 and raw_data.shape[0] > 0
if dtype:
raw_data = raw_data.astype(dtype)
if dim is None:
if raw_data.ndim > 1:
dim = raw_data.shape[-1]
else:
dim = 1 # dummy
assert data_key != "inputs"
name = data_key
# Keep consistent with _insert_h5_inputs.
if name not in self._datasets:
if "targets/data" not in self._file:
self._file.create_group("targets/data")
if "targets/size" not in self._file:
self._file.create_group("targets/size")
if "targets/labels" not in self._file:
self._file.create_group("targets/labels")
hdf5_strings(self._file, "targets/labels/%s" % data_key, ["dummy-label"])
self._datasets[name] = self._file["targets/data"].create_dataset(
data_key,
raw_data.shape,
raw_data.dtype,
maxshape=tuple(None for _ in raw_data.shape),
)
self._file["targets/size"].attrs[data_key] = [
dim,
raw_data.ndim,
] # (dim, ndim)
else:
old_shape = self._datasets[name].shape
self._datasets[name].resize(
(old_shape[0] + raw_data.shape[0],)
+ tuple(max(old, new) for old, new in zip(old_shape[1:], raw_data.shape[1:]))
)
assert (
self._file.attrs["numSeqs"] > 0 and self._seq_lengths.shape[0] > 0
) # assume _insert_h5_inputs called before
if self._seq_lengths[self._file.attrs["numSeqs"] - 1, 1]:
assert self._seq_lengths[self._file.attrs["numSeqs"] - 1, 1] == raw_data.shape[0]
else:
self._seq_lengths[self._file.attrs["numSeqs"] - 1, 1] = raw_data.shape[0]
self._other_num_time_steps += raw_data.shape[0]
offset = self._other_num_time_steps - raw_data.shape[0]
hdf_data = self._datasets[name]
hdf_data[offset:] = raw_data
def insert_batch(self, inputs, seq_len, seq_tag, extra=None):
"""
:param numpy.ndarray inputs: shape=(n_batch,time,data) (or (n_batch,time), or (n_batch,time1,time2), ...)
:param list[int]|dict[int,list[int]|numpy.ndarray] seq_len: sequence lengths (per axis, excluding batch axis)
:param list[str|bytes] seq_tag: sequence tags of length n_batch
:param dict[str,numpy.ndarray]|None extra:
"""
n_batch = len(seq_tag)
assert n_batch == inputs.shape[0]
assert inputs.ndim == self.ndim + 1 # one more for the batch-dim
if not isinstance(seq_len, dict):
seq_len = {0: seq_len}
assert isinstance(seq_len, dict)
assert all(
[isinstance(key, int) and isinstance(value, (list, numpy.ndarray)) for (key, value) in seq_len.items()]
)
ndim_with_seq_len = self.ndim - (1 if self.dim else 0)
assert all([0 <= key < ndim_with_seq_len for key in seq_len.keys()]) or ndim_with_seq_len == 0
assert len(seq_len) == ndim_with_seq_len
assert all([n_batch == len(value) for (key, value) in seq_len.items()])
assert all([max(value) == inputs.shape[key + 1] for (key, value) in seq_len.items()])
if self.dim:
assert self.dim == inputs.shape[-1]
if extra:
assert all([n_batch == value.shape[0] for value in extra.values()])
seqlen_offset = self._seq_lengths.shape[0]
self._seq_lengths.resize(seqlen_offset + n_batch, axis=0)
for i in range(n_batch):
self._tags.append(seq_tag[i])
# Note: Currently, our HDFDataset does not support to have multiple axes with dynamic length.
# Thus, we flatten all together, and calculate the flattened seq len.
# (Ignore this if there is only a single time dimension.)
flat_seq_len = numpy.prod([seq_len[axis][i] for axis in range(ndim_with_seq_len)])
assert flat_seq_len > 0
flat_shape = [flat_seq_len]
if self.dim:
flat_shape.append(self.dim)
self._seq_lengths[seqlen_offset + i, 0] = flat_seq_len
data = inputs[i]
data = data[tuple([slice(None, seq_len[axis][i]) for axis in range(ndim_with_seq_len)])]
data = numpy.reshape(data, flat_shape)
self._insert_h5_inputs(data)
if len(seq_len) > 1:
# Note: Because we have flattened multiple axes with dynamic len into a single one,
# we want to store the individual axes lengths. We store those in a separate data entry "sizes".
# Note: We could add a dummy time-dim for this "sizes", and then have a feature-dim = number of axes.
# However, we keep it consistent to how we handled it in our 2D MDLSTM experiments.
self._insert_h5_other(
"sizes",
[seq_len[axis][i] for axis in range(ndim_with_seq_len)],
add_time_dim=False,
dtype="int32",
)
if extra:
for key, value in extra.items():
self._insert_h5_other(key, value[i])
def close(self):
max_tag_len = max([len(d) for d in self._tags]) if self._tags else 0
self._file.create_dataset("seqTags", shape=(len(self._tags),), dtype="S%i" % (max_tag_len + 1))
for i, tag in enumerate(self._tags):
self._file["seqTags"][i] = numpy.array(tag, dtype="S%i" % (max_tag_len + 1))
self._file.close()
def main():
parser = argparse.ArgumentParser(description="create hdf files to be used in Returnn")
parser.add_argument("training_text", help="path to the arabic diacritized text (with diacritics included)")
parser.add_argument("hdf_source_letter_dataset", help="path to the source (letter) hdf dataset to be created")
parser.add_argument(
"hdf_source_diacritic_dataset",
help="path to the source (diacritics) hdf dataset to be created",
)
parser.add_argument("hdf_target_dataset", help="path to the target hdf dataset to be created")
parser.add_argument(
"--masking_factor",
default=1.0,
help="percent to randomly mask diacritics. " "1.0 means to remove 100% of diacritics, 0.5 means 50%, so on.",
)
parser.add_argument(
"--max_char_seq",
default=1500,
help="maximum length in terms of the number of characters.",
)
args = parser.parse_args()
masking_factor = float(args.masking_factor)
max_char_seq = int(args.max_char_seq)
hdf_source_letter_writer = SimpleHDFWriter(args.hdf_source_letter_dataset, dim=None)
hdf_source_diacritic_writer = SimpleHDFWriter(args.hdf_source_diacritic_dataset, dim=None)
hdf_target_writer = SimpleHDFWriter(args.hdf_target_dataset, dim=None)
filtered_segments = open("filtered_segments", "w")
sequence_name = os.path.splitext(os.path.basename(args.hdf_source_letter_dataset))[0]
source_data = open(args.training_text, "rt")
inverse_buckwalter_dict = {}
for i, char in enumerate(buckwalter_dictionary):
inverse_buckwalter_dict[i] = char
inverse_harakat_dict = {0: "_"}
for i, char in enumerate(harakat_buckwalter):
inverse_harakat_dict[i + 1] = char
buckwalter_transform_dict = {}
for arab, buck in zip(arabic_dictionary, buckwalter_dictionary):
buckwalter_transform_dict[arab] = buck
for i, source_line in enumerate(source_data):
source_indices = []
target_indices = []
print(i)
for char in source_line.strip():
if char in harakat:
if len(target_indices) == 0:
# sentence start with a harakat, skip
continue
if target_indices[-1] != 0:
# double harakat with shadda in front
target_indices[-1] = len(harakat) + harakat_index_dict[char]
else:
target_indices[-1] = harakat_index_dict[char]
else:
source_indices.append(arabic_index_dict.get(char, 0))
target_indices.append(0)
# randomly select a percent of a list and set it to zero
source_diac_indices = target_indices.copy()
before_masking = [p for p, e in enumerate(source_diac_indices) if e != 0]
# Due to the mask size
if masking_factor == 1.0:
source_diac_indices = [0] * len(source_diac_indices)
elif masking_factor == 0.0:
pass
else:
if len(before_masking):
mask = numpy.random.choice(
range(0, len(before_masking)),
replace=False,
size=int(float(len(before_masking) - 1) * masking_factor),
)
for idx in mask:
source_diac_indices[before_masking[idx]] = 0
assert len(source_indices) == (len(target_indices) and len(source_diac_indices))
# here, we filter long sequence due to memory issues, in particular for self-attention models
if len(source_indices) <= max_char_seq:
hdf_source_letter_writer.insert_batch(
numpy.asarray(
[
source_indices,
],
dtype="int32",
),
[len(source_indices)],
seq_tag=["%s_%s_%i" % (sequence_name, str(args.masking_factor), i)],
)
hdf_target_writer.insert_batch(
numpy.asarray(
[
target_indices,
],
dtype="int32",
),
[len(target_indices)],
seq_tag=["%s_%s_%i" % (sequence_name, str(args.masking_factor), i)],
)
hdf_source_diacritic_writer.insert_batch(
numpy.asarray(
[
source_diac_indices,
],
dtype="int32",
),
[len(source_diac_indices)],
seq_tag=["%s_%s_%i" % (sequence_name, str(args.masking_factor), i)],
)
else:
filtered_segments.write("%s_%s_%i" % (sequence_name, str(args.masking_factor), i) + "\n")
hdf_source_letter_writer.close()
hdf_target_writer.close()
hdf_source_diacritic_writer.close()
filtered_segments.close()
if __name__ == "__main__":
# execute only if run as a script
main()