Spaces:
Runtime error
Runtime error
File size: 11,268 Bytes
2b7bf83 |
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 |
# -*- coding: utf-8 -*-
# Copyright 2019 Tomoki Hayashi
# MIT License (https://opensource.org/licenses/MIT)
"""Dataset modules."""
import logging
import os
from multiprocessing import Manager
import numpy as np
from torch.utils.data import Dataset
from parallel_wavegan.utils import find_files
from parallel_wavegan.utils import read_hdf5
class AudioMelDataset(Dataset):
"""PyTorch compatible audio and mel dataset."""
def __init__(
self,
root_dir,
audio_query="*.h5",
mel_query="*.h5",
audio_load_fn=lambda x: read_hdf5(x, "wave"),
mel_load_fn=lambda x: read_hdf5(x, "feats"),
audio_length_threshold=None,
mel_length_threshold=None,
return_utt_id=False,
allow_cache=False,
):
"""Initialize dataset.
Args:
root_dir (str): Root directory including dumped files.
audio_query (str): Query to find audio files in root_dir.
mel_query (str): Query to find feature files in root_dir.
audio_load_fn (func): Function to load audio file.
mel_load_fn (func): Function to load feature file.
audio_length_threshold (int): Threshold to remove short audio files.
mel_length_threshold (int): Threshold to remove short feature files.
return_utt_id (bool): Whether to return the utterance id with arrays.
allow_cache (bool): Whether to allow cache of the loaded files.
"""
# find all of audio and mel files
audio_files = sorted(find_files(root_dir, audio_query))
mel_files = sorted(find_files(root_dir, mel_query))
# filter by threshold
if audio_length_threshold is not None:
audio_lengths = [audio_load_fn(f).shape[0] for f in audio_files]
idxs = [
idx
for idx in range(len(audio_files))
if audio_lengths[idx] > audio_length_threshold
]
if len(audio_files) != len(idxs):
logging.warning(
f"Some files are filtered by audio length threshold "
f"({len(audio_files)} -> {len(idxs)})."
)
audio_files = [audio_files[idx] for idx in idxs]
mel_files = [mel_files[idx] for idx in idxs]
if mel_length_threshold is not None:
mel_lengths = [mel_load_fn(f).shape[0] for f in mel_files]
idxs = [
idx
for idx in range(len(mel_files))
if mel_lengths[idx] > mel_length_threshold
]
if len(mel_files) != len(idxs):
logging.warning(
f"Some files are filtered by mel length threshold "
f"({len(mel_files)} -> {len(idxs)})."
)
audio_files = [audio_files[idx] for idx in idxs]
mel_files = [mel_files[idx] for idx in idxs]
# assert the number of files
assert len(audio_files) != 0, f"Not found any audio files in ${root_dir}."
assert len(audio_files) == len(
mel_files
), f"Number of audio and mel files are different ({len(audio_files)} vs {len(mel_files)})."
self.audio_files = audio_files
self.audio_load_fn = audio_load_fn
self.mel_load_fn = mel_load_fn
self.mel_files = mel_files
if ".npy" in audio_query:
self.utt_ids = [
os.path.basename(f).replace("-wave.npy", "") for f in audio_files
]
else:
self.utt_ids = [
os.path.splitext(os.path.basename(f))[0] for f in audio_files
]
self.return_utt_id = return_utt_id
self.allow_cache = allow_cache
if allow_cache:
# NOTE(kan-bayashi): Manager is need to share memory in dataloader with num_workers > 0
self.manager = Manager()
self.caches = self.manager.list()
self.caches += [() for _ in range(len(audio_files))]
def __getitem__(self, idx):
"""Get specified idx items.
Args:
idx (int): Index of the item.
Returns:
str: Utterance id (only in return_utt_id = True).
ndarray: Audio signal (T,).
ndarray: Feature (T', C).
"""
if self.allow_cache and len(self.caches[idx]) != 0:
return self.caches[idx]
utt_id = self.utt_ids[idx]
audio = self.audio_load_fn(self.audio_files[idx])
mel = self.mel_load_fn(self.mel_files[idx])
if self.return_utt_id:
items = utt_id, audio, mel
else:
items = audio, mel
if self.allow_cache:
self.caches[idx] = items
return items
def __len__(self):
"""Return dataset length.
Returns:
int: The length of dataset.
"""
return len(self.audio_files)
class AudioDataset(Dataset):
"""PyTorch compatible audio dataset."""
def __init__(
self,
root_dir,
audio_query="*-wave.npy",
audio_length_threshold=None,
audio_load_fn=np.load,
return_utt_id=False,
allow_cache=False,
):
"""Initialize dataset.
Args:
root_dir (str): Root directory including dumped files.
audio_query (str): Query to find audio files in root_dir.
audio_load_fn (func): Function to load audio file.
audio_length_threshold (int): Threshold to remove short audio files.
return_utt_id (bool): Whether to return the utterance id with arrays.
allow_cache (bool): Whether to allow cache of the loaded files.
"""
# find all of audio and mel files
audio_files = sorted(find_files(root_dir, audio_query))
# filter by threshold
if audio_length_threshold is not None:
audio_lengths = [audio_load_fn(f).shape[0] for f in audio_files]
idxs = [
idx
for idx in range(len(audio_files))
if audio_lengths[idx] > audio_length_threshold
]
if len(audio_files) != len(idxs):
logging.waning(
f"some files are filtered by audio length threshold "
f"({len(audio_files)} -> {len(idxs)})."
)
audio_files = [audio_files[idx] for idx in idxs]
# assert the number of files
assert len(audio_files) != 0, f"Not found any audio files in ${root_dir}."
self.audio_files = audio_files
self.audio_load_fn = audio_load_fn
self.return_utt_id = return_utt_id
if ".npy" in audio_query:
self.utt_ids = [
os.path.basename(f).replace("-wave.npy", "") for f in audio_files
]
else:
self.utt_ids = [
os.path.splitext(os.path.basename(f))[0] for f in audio_files
]
self.allow_cache = allow_cache
if allow_cache:
# NOTE(kan-bayashi): Manager is need to share memory in dataloader with num_workers > 0
self.manager = Manager()
self.caches = self.manager.list()
self.caches += [() for _ in range(len(audio_files))]
def __getitem__(self, idx):
"""Get specified idx items.
Args:
idx (int): Index of the item.
Returns:
str: Utterance id (only in return_utt_id = True).
ndarray: Audio (T,).
"""
if self.allow_cache and len(self.caches[idx]) != 0:
return self.caches[idx]
utt_id = self.utt_ids[idx]
audio = self.audio_load_fn(self.audio_files[idx])
if self.return_utt_id:
items = utt_id, audio
else:
items = audio
if self.allow_cache:
self.caches[idx] = items
return items
def __len__(self):
"""Return dataset length.
Returns:
int: The length of dataset.
"""
return len(self.audio_files)
class MelDataset(Dataset):
"""PyTorch compatible mel dataset."""
def __init__(
self,
root_dir,
mel_query="*-feats.npy",
mel_length_threshold=None,
mel_load_fn=np.load,
return_utt_id=False,
allow_cache=False,
):
"""Initialize dataset.
Args:
root_dir (str): Root directory including dumped files.
mel_query (str): Query to find feature files in root_dir.
mel_load_fn (func): Function to load feature file.
mel_length_threshold (int): Threshold to remove short feature files.
return_utt_id (bool): Whether to return the utterance id with arrays.
allow_cache (bool): Whether to allow cache of the loaded files.
"""
# find all of the mel files
mel_files = sorted(find_files(root_dir, mel_query))
# filter by threshold
if mel_length_threshold is not None:
mel_lengths = [mel_load_fn(f).shape[0] for f in mel_files]
idxs = [
idx
for idx in range(len(mel_files))
if mel_lengths[idx] > mel_length_threshold
]
if len(mel_files) != len(idxs):
logging.warning(
f"Some files are filtered by mel length threshold "
f"({len(mel_files)} -> {len(idxs)})."
)
mel_files = [mel_files[idx] for idx in idxs]
# assert the number of files
assert len(mel_files) != 0, f"Not found any mel files in ${root_dir}."
self.mel_files = mel_files
self.mel_load_fn = mel_load_fn
self.utt_ids = [os.path.splitext(os.path.basename(f))[0] for f in mel_files]
if ".npy" in mel_query:
self.utt_ids = [
os.path.basename(f).replace("-feats.npy", "") for f in mel_files
]
else:
self.utt_ids = [os.path.splitext(os.path.basename(f))[0] for f in mel_files]
self.return_utt_id = return_utt_id
self.allow_cache = allow_cache
if allow_cache:
# NOTE(kan-bayashi): Manager is need to share memory in dataloader with num_workers > 0
self.manager = Manager()
self.caches = self.manager.list()
self.caches += [() for _ in range(len(mel_files))]
def __getitem__(self, idx):
"""Get specified idx items.
Args:
idx (int): Index of the item.
Returns:
str: Utterance id (only in return_utt_id = True).
ndarray: Feature (T', C).
"""
if self.allow_cache and len(self.caches[idx]) != 0:
return self.caches[idx]
utt_id = self.utt_ids[idx]
mel = self.mel_load_fn(self.mel_files[idx])
if self.return_utt_id:
items = utt_id, mel
else:
items = mel
if self.allow_cache:
self.caches[idx] = items
return items
def __len__(self):
"""Return dataset length.
Returns:
int: The length of dataset.
"""
return len(self.mel_files)
|