File size: 15,358 Bytes
c968fc3 |
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 |
# Copyright (c) 2024 Amphion.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
import random
import torch
import torchaudio
from torch.nn.utils.rnn import pad_sequence
from utils.data_utils import *
from models.base.base_dataset import (
BaseOfflineCollator,
BaseOfflineDataset,
BaseTestDataset,
BaseTestCollator,
)
from text import text_to_sequence
class JetsDataset(BaseOfflineDataset):
def __init__(self, cfg, dataset, is_valid=False):
BaseOfflineDataset.__init__(self, cfg, dataset, is_valid=is_valid)
self.batch_size = cfg.train.batch_size
cfg = cfg.preprocess
# utt2duration
self.utt2duration_path = {}
for utt_info in self.metadata:
dataset = utt_info["Dataset"]
uid = utt_info["Uid"]
utt = "{}_{}".format(dataset, uid)
self.utt2duration_path[utt] = os.path.join(
cfg.processed_dir,
dataset,
cfg.duration_dir,
uid + ".npy",
)
self.utt2dur = self.read_duration()
if cfg.use_frame_energy:
self.frame_utt2energy, self.energy_statistic = load_energy(
self.metadata,
cfg.processed_dir,
cfg.energy_dir,
use_log_scale=cfg.use_log_scale_energy,
utt2spk=self.preprocess.utt2spk if cfg.use_spkid else None,
return_norm=True,
)
elif cfg.use_phone_energy:
self.phone_utt2energy, self.energy_statistic = load_energy(
self.metadata,
cfg.processed_dir,
cfg.phone_energy_dir,
use_log_scale=cfg.use_log_scale_energy,
utt2spk=self.utt2spk if cfg.use_spkid else None,
return_norm=True,
)
if cfg.use_frame_pitch:
self.frame_utt2pitch, self.pitch_statistic = load_energy(
self.metadata,
cfg.processed_dir,
cfg.pitch_dir,
use_log_scale=cfg.energy_extract_mode,
utt2spk=self.utt2spk if cfg.use_spkid else None,
return_norm=True,
)
elif cfg.use_phone_pitch:
self.phone_utt2pitch, self.pitch_statistic = load_energy(
self.metadata,
cfg.processed_dir,
cfg.phone_pitch_dir,
use_log_scale=cfg.use_log_scale_pitch,
utt2spk=self.utt2spk if cfg.use_spkid else None,
return_norm=True,
)
# utt2lab
self.utt2lab_path = {}
for utt_info in self.metadata:
dataset = utt_info["Dataset"]
uid = utt_info["Uid"]
utt = "{}_{}".format(dataset, uid)
self.utt2lab_path[utt] = os.path.join(
cfg.processed_dir,
dataset,
cfg.lab_dir,
uid + ".txt",
)
self.speaker_map = {}
if os.path.exists(os.path.join(cfg.processed_dir, "spk2id.json")):
with open(
os.path.exists(os.path.join(cfg.processed_dir, "spk2id.json"))
) as f:
self.speaker_map = json.load(f)
self.metadata = self.check_metadata()
if cfg.use_audios:
self.utt2audio_path = {}
for utt_info in self.metadata:
dataset = utt_info["Dataset"]
uid = utt_info["Uid"]
utt = "{}_{}".format(dataset, uid)
if cfg.extract_audio:
self.utt2audio_path[utt] = os.path.join(
cfg.processed_dir,
dataset,
cfg.audio_dir,
uid + ".wav",
)
else:
self.utt2audio_path[utt] = utt_info["Path"]
def __getitem__(self, index):
single_feature = BaseOfflineDataset.__getitem__(self, index)
utt_info = self.metadata[index]
dataset = utt_info["Dataset"]
uid = utt_info["Uid"]
utt = "{}_{}".format(dataset, uid)
duration = self.utt2dur[utt]
# text
f = open(self.utt2lab_path[utt], "r")
phones = f.readlines()[0].strip()
f.close()
# todo: add cleaner(chenxi)
phones_ids = np.array(text_to_sequence(phones, ["english_cleaners"]))
text_len = len(phones_ids)
if self.cfg.preprocess.use_frame_pitch:
pitch = self.frame_utt2pitch[utt]
elif self.cfg.preprocess.use_phone_pitch:
pitch = self.phone_utt2pitch[utt]
if self.cfg.preprocess.use_frame_energy:
energy = self.frame_utt2energy[utt]
elif self.cfg.preprocess.use_phone_energy:
energy = self.phone_utt2energy[utt]
# speaker
if len(self.speaker_map) > 0:
speaker_id = self.speaker_map[utt_info["Singer"]]
else:
speaker_id = 0
single_feature.update(
{
"durations": duration,
"texts": phones_ids,
"spk_id": speaker_id,
"text_len": text_len,
"pitch": pitch,
"energy": energy,
"uid": uid,
}
)
if self.cfg.preprocess.use_audios:
audio, sr = torchaudio.load(self.utt2audio_path[utt])
audio = audio.cpu().numpy().squeeze()
single_feature["audio"] = audio
single_feature["audio_len"] = audio.shape[0]
return self.clip_if_too_long(single_feature)
def read_duration(self):
# read duration
utt2dur = {}
for index in range(len(self.metadata)):
utt_info = self.metadata[index]
dataset = utt_info["Dataset"]
uid = utt_info["Uid"]
utt = "{}_{}".format(dataset, uid)
if not os.path.exists(self.utt2mel_path[utt]) or not os.path.exists(
self.utt2duration_path[utt]
):
continue
mel = np.load(self.utt2mel_path[utt]).transpose(1, 0)
duration = np.load(self.utt2duration_path[utt])
assert mel.shape[0] == sum(
duration
), f"{utt}: mismatch length between mel {mel.shape[0]} and sum(duration) {sum(duration)}"
utt2dur[utt] = duration
return utt2dur
def __len__(self):
return len(self.metadata)
def random_select(self, feature_seq_len, max_seq_len, ending_ts=2812):
"""
ending_ts: to avoid invalid whisper features for over 30s audios
2812 = 30 * 24000 // 256
"""
ts = max(feature_seq_len - max_seq_len, 0)
ts = min(ts, ending_ts - max_seq_len)
start = random.randint(0, ts)
end = start + max_seq_len
return start, end
def clip_if_too_long(self, sample, max_seq_len=1000):
"""
sample :
{
'spk_id': (1,),
'target_len': int
'mel': (seq_len, dim),
'frame_pitch': (seq_len,)
'frame_energy': (seq_len,)
'content_vector_feat': (seq_len, dim)
}
"""
if sample["target_len"] <= max_seq_len:
return sample
start, end = self.random_select(sample["target_len"], max_seq_len)
sample["target_len"] = end - start
for k in sample.keys():
if k not in ["spk_id", "target_len"]:
sample[k] = sample[k][start:end]
return sample
def check_metadata(self):
new_metadata = []
for utt_info in self.metadata:
dataset = utt_info["Dataset"]
uid = utt_info["Uid"]
utt = "{}_{}".format(dataset, uid)
if not os.path.exists(self.utt2duration_path[utt]) or not os.path.exists(
self.utt2mel_path[utt]
):
continue
else:
new_metadata.append(utt_info)
return new_metadata
class JetsCollator(BaseOfflineCollator):
"""Zero-pads model inputs and targets based on number of frames per step"""
def __init__(self, cfg):
BaseOfflineCollator.__init__(self, cfg)
self.sort = cfg.train.sort_sample
self.batch_size = cfg.train.batch_size
self.drop_last = cfg.train.drop_last
def __call__(self, batch):
# mel: [b, T, n_mels]
# frame_pitch, frame_energy: [1, T]
# target_len: [1]
# spk_id: [b, 1]
# mask: [b, T, 1]
packed_batch_features = dict()
for key in batch[0].keys():
if key == "target_len":
packed_batch_features["target_len"] = torch.LongTensor(
[b["target_len"] for b in batch]
)
masks = [
torch.ones((b["target_len"], 1), dtype=torch.long) for b in batch
]
packed_batch_features["mask"] = pad_sequence(
masks, batch_first=True, padding_value=0
)
elif key == "text_len":
packed_batch_features["text_len"] = torch.LongTensor(
[b["text_len"] for b in batch]
)
masks = [
torch.ones((b["text_len"], 1), dtype=torch.long) for b in batch
]
packed_batch_features["text_mask"] = pad_sequence(
masks, batch_first=True, padding_value=0
)
elif key == "spk_id":
packed_batch_features["spk_id"] = torch.LongTensor(
[b["spk_id"] for b in batch]
)
elif key == "uid":
packed_batch_features[key] = [b["uid"] for b in batch]
elif key == "audio_len":
packed_batch_features["audio_len"] = torch.LongTensor(
[b["audio_len"] for b in batch]
)
else:
values = [torch.from_numpy(b[key]) for b in batch]
packed_batch_features[key] = pad_sequence(
values, batch_first=True, padding_value=0
)
return packed_batch_features
class JetsTestDataset(BaseTestDataset):
def __init__(self, args, cfg, infer_type=None):
datasets = cfg.dataset
cfg = cfg.preprocess
is_bigdata = False
assert len(datasets) >= 1
if len(datasets) > 1:
datasets.sort()
bigdata_version = "_".join(datasets)
processed_data_dir = os.path.join(cfg.processed_dir, bigdata_version)
is_bigdata = True
else:
processed_data_dir = os.path.join(cfg.processed_dir, args.dataset)
if args.test_list_file:
self.metafile_path = args.test_list_file
self.metadata = self.get_metadata()
else:
assert args.testing_set
source_metafile_path = os.path.join(
cfg.processed_dir,
args.dataset,
"{}.json".format(args.testing_set),
)
with open(source_metafile_path, "r") as f:
self.metadata = json.load(f)
self.cfg = cfg
self.datasets = datasets
self.data_root = processed_data_dir
self.is_bigdata = is_bigdata
self.source_dataset = args.dataset
######### Load source acoustic features #########
if cfg.use_spkid:
spk2id_path = os.path.join(self.data_root, cfg.spk2id)
utt2sp_path = os.path.join(self.data_root, cfg.utt2spk)
self.spk2id, self.utt2spk = get_spk_map(spk2id_path, utt2sp_path, datasets)
# utt2lab
self.utt2lab_path = {}
for utt_info in self.metadata:
dataset = utt_info["Dataset"]
uid = utt_info["Uid"]
utt = "{}_{}".format(dataset, uid)
self.utt2lab_path[utt] = os.path.join(
cfg.processed_dir,
dataset,
cfg.lab_dir,
uid + ".txt",
)
self.speaker_map = {}
if os.path.exists(os.path.join(cfg.processed_dir, "spk2id.json")):
with open(
os.path.exists(os.path.join(cfg.processed_dir, "spk2id.json"))
) as f:
self.speaker_map = json.load(f)
def __getitem__(self, index):
single_feature = {}
utt_info = self.metadata[index]
dataset = utt_info["Dataset"]
uid = utt_info["Uid"]
utt = "{}_{}".format(dataset, uid)
# text
f = open(self.utt2lab_path[utt], "r")
phones = f.readlines()[0].strip()
f.close()
phones_ids = np.array(text_to_sequence(phones, self.cfg.text_cleaners))
text_len = len(phones_ids)
# speaker
if len(self.speaker_map) > 0:
speaker_id = self.speaker_map[utt_info["Singer"]]
else:
speaker_id = 0
single_feature.update(
{
"texts": phones_ids,
"spk_id": speaker_id,
"text_len": text_len,
}
)
return single_feature
def __len__(self):
return len(self.metadata)
def get_metadata(self):
with open(self.metafile_path, "r", encoding="utf-8") as f:
metadata = json.load(f)
return metadata
class JetsTestCollator(BaseTestCollator):
"""Zero-pads model inputs and targets based on number of frames per step"""
def __init__(self, cfg):
self.cfg = cfg
def __call__(self, batch):
packed_batch_features = dict()
# mel: [b, T, n_mels]
# frame_pitch, frame_energy: [1, T]
# target_len: [1]
# spk_id: [b, 1]
# mask: [b, T, 1]
for key in batch[0].keys():
if key == "target_len":
packed_batch_features["target_len"] = torch.LongTensor(
[b["target_len"] for b in batch]
)
masks = [
torch.ones((b["target_len"], 1), dtype=torch.long) for b in batch
]
packed_batch_features["mask"] = pad_sequence(
masks, batch_first=True, padding_value=0
)
elif key == "text_len":
packed_batch_features["text_len"] = torch.LongTensor(
[b["text_len"] for b in batch]
)
masks = [
torch.ones((b["text_len"], 1), dtype=torch.long) for b in batch
]
packed_batch_features["text_mask"] = pad_sequence(
masks, batch_first=True, padding_value=0
)
elif key == "spk_id":
packed_batch_features["spk_id"] = torch.LongTensor(
[b["spk_id"] for b in batch]
)
else:
values = [torch.from_numpy(b[key]) for b in batch]
packed_batch_features[key] = pad_sequence(
values, batch_first=True, padding_value=0
)
return packed_batch_features
|