cdminix commited on
Commit
ed267f2
·
1 Parent(s): 07ddc18

add train, validation, even and odd splits

Browse files
Files changed (1) hide show
  1. libriheavy.py +53 -15
libriheavy.py CHANGED
@@ -24,6 +24,8 @@ _CITATION = """\
24
  }
25
  """
26
 
 
 
27
  class LibriheavyConfig(datasets.BuilderConfig):
28
  """BuilderConfig for Libriheavy."""
29
 
@@ -69,46 +71,82 @@ class Libriheavy(datasets.GeneratorBasedBuilder):
69
  def _split_generators(self, dl_manager):
70
  """Returns SplitGenerators."""
71
  # first, we load speaker_list.json
72
- speaker_list = "medium_data/speaker_list.json"
73
  speaker_list = dl_manager.download_and_extract(speaker_list)
74
  with open(speaker_list, "r") as f:
75
  speaker_list = json.load(f)
76
  # now we load the individual speaker metadata
77
  speaker_metadata = {}
78
  for speaker_id, metadata_path in speaker_list.items():
79
- metadata_path = f"medium_data/{metadata_path}"
80
  metadata_path = dl_manager.download_and_extract(metadata_path)
81
  with open(metadata_path, "r") as f:
82
  speaker_metadata[speaker_id] = json.load(f)
83
  speaker_chunks = []
 
 
84
  for speaker_id, metadata in speaker_metadata.items():
85
  for chunk_id, chunk in metadata["chunks"].items():
86
- speaker_chunks.append(
87
- {
88
- "speaker_id": speaker_id,
89
- "id": f"{speaker_id}_{chunk_id}",
90
- "audio": dl_manager.download(f"medium_data/{chunk['npz'].replace('.gz', '')}"),
91
- "text": dl_manager.download(f"medium_data/{chunk['json']}"),
92
- }
93
- )
 
 
 
94
  # shuffle the chunks
95
  np.random.seed(42)
96
  np.random.shuffle(speaker_chunks)
97
  return [
98
  datasets.SplitGenerator(
99
- name=datasets.Split.TRAIN,
100
- gen_kwargs={"speaker_chunks": speaker_chunks},
101
- )
 
 
 
 
 
 
 
 
 
 
 
 
102
  ]
103
 
104
- def _generate_examples(self, speaker_chunks):
105
  """Yields examples."""
106
  for chunk in speaker_chunks:
107
  npz = dict(np.load(chunk["audio"], allow_pickle=True))
108
  utterances = npz.keys()
109
  with gzip.open(chunk["text"], "rt") as f:
110
  text = json.load(f)
111
- for utterance_id, utterance in text.items():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112
  result = {
113
  "id": chunk["speaker_id"] + "_" + utterance_id,
114
  "speaker_id": chunk["speaker_id"],
 
24
  }
25
  """
26
 
27
+ PATH = "./medium_data"
28
+
29
  class LibriheavyConfig(datasets.BuilderConfig):
30
  """BuilderConfig for Libriheavy."""
31
 
 
71
  def _split_generators(self, dl_manager):
72
  """Returns SplitGenerators."""
73
  # first, we load speaker_list.json
74
+ speaker_list = f"{PATH}/speaker_list.json"
75
  speaker_list = dl_manager.download_and_extract(speaker_list)
76
  with open(speaker_list, "r") as f:
77
  speaker_list = json.load(f)
78
  # now we load the individual speaker metadata
79
  speaker_metadata = {}
80
  for speaker_id, metadata_path in speaker_list.items():
81
+ metadata_path = f"{PATH}/{speaker_id}/{metadata_path}"
82
  metadata_path = dl_manager.download_and_extract(metadata_path)
83
  with open(metadata_path, "r") as f:
84
  speaker_metadata[speaker_id] = json.load(f)
85
  speaker_chunks = []
86
+ even_speaker_chunks = []
87
+ odd_speaker_chunks = []
88
  for speaker_id, metadata in speaker_metadata.items():
89
  for chunk_id, chunk in metadata["chunks"].items():
90
+ chunk_dict = {
91
+ "speaker_id": speaker_id,
92
+ "id": f"{speaker_id}_{chunk_id}",
93
+ "audio": dl_manager.download(f"{PATH}/{speaker_id}/{chunk['npz'].replace('.gz', '')}"),
94
+ "text": dl_manager.download(f"{PATH}/{speaker_id}/{chunk['json']}"),
95
+ }
96
+ speaker_chunks.append(chunk_dict)
97
+ if int(chunk_id) % 2 == 0:
98
+ even_speaker_chunks.append(chunk_dict)
99
+ else:
100
+ odd_speaker_chunks.append(chunk_dict)
101
  # shuffle the chunks
102
  np.random.seed(42)
103
  np.random.shuffle(speaker_chunks)
104
  return [
105
  datasets.SplitGenerator(
106
+ name="train",
107
+ gen_kwargs={"speaker_chunks": speaker_chunks, "split": "train"}
108
+ ),
109
+ datasets.SplitGenerator(
110
+ name="validation",
111
+ gen_kwargs={"speaker_chunks": speaker_chunks, "split": "validation"}
112
+ ),
113
+ datasets.SplitGenerator(
114
+ name="even",
115
+ gen_kwargs={"speaker_chunks": even_speaker_chunks, "split": "even"}
116
+ ),
117
+ datasets.SplitGenerator(
118
+ name="odd",
119
+ gen_kwargs={"speaker_chunks": odd_speaker_chunks, "split": "odd"}
120
+ ),
121
  ]
122
 
123
+ def _generate_examples(self, speaker_chunks, split):
124
  """Yields examples."""
125
  for chunk in speaker_chunks:
126
  npz = dict(np.load(chunk["audio"], allow_pickle=True))
127
  utterances = npz.keys()
128
  with gzip.open(chunk["text"], "rt") as f:
129
  text = json.load(f)
130
+ if split in ["train", "even", "odd"]:
131
+ for utterance_id, utterance in text.items():
132
+ # skip the last utterance
133
+ if utterance_id == sorted(list(text.keys()))[-1]:
134
+ continue
135
+ result = {
136
+ "id": chunk["speaker_id"] + "_" + utterance_id,
137
+ "speaker_id": chunk["speaker_id"],
138
+ "audio": chunk["audio"],
139
+ "text": chunk["text"],
140
+ "word_segments": [
141
+ {"start": segment[0], "end": segment[1], "word": segment[2]} for segment in utterance["word_segments"]
142
+ ],
143
+ "mel_spectrogram": npz[str(utterance_id)].item()["mel"][0][0],
144
+ }
145
+ yield chunk["speaker_id"] + "_" + utterance_id, result
146
+ else:
147
+ # only use the last utterance
148
+ utterance_id = sorted(list(text.keys()))[-1]
149
+ utterance = text[utterance_id]
150
  result = {
151
  "id": chunk["speaker_id"] + "_" + utterance_id,
152
  "speaker_id": chunk["speaker_id"],