Spaces:
Paused
Paused
File size: 12,278 Bytes
d66c48f |
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 |
# 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.
from cmath import inf
import io
import librosa
import torch
import json
import tqdm
import numpy as np
import logging
import pickle
import os
import time
from torch.utils.data import Dataset
import torch.nn as nn
import torch.nn.functional as F
from multiprocessing import Pool
import concurrent.futures
from pathlib import Path
from transformers import SeamlessM4TFeatureExtractor
from transformers import Wav2Vec2BertModel
os.chdir("./models/tts/debatts")
import sys
sys.path.append("./models/tts/debatts")
from utils.g2p_new.g2p_new import new_g2p
from torch.nn.utils.rnn import pad_sequence
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
class WarningFilter(logging.Filter):
def filter(self, record):
if record.name == "phonemizer" and record.levelno == logging.WARNING:
return False
if record.name == "qcloud_cos.cos_client" and record.levelno == logging.INFO:
return False
if record.name == "jieba" and record.levelno == logging.DEBUG:
return False
return True
filter = WarningFilter()
logging.getLogger("phonemizer").addFilter(filter)
logging.getLogger("qcloud_cos.cos_client").addFilter(filter)
logging.getLogger("jieba").addFilter(filter)
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class T2SDataset(torch.utils.data.Dataset):
def __init__(
self,
cfg=None,
):
self.cfg = cfg
self.meta_info_path = "Debatts-Data Summary Json"
with open(self.meta_info_path, "r") as f:
self.meta_info_data = json.load(f)
self.wav_paths = []
self.prompt0_paths = [] # Add prompt0 paths
self.wav_path_index2duration = []
self.wav_path_index2phonelen = []
self.wav_path_index2spkid = []
self.wav_path_index2phoneid = []
self.index2num_frames = []
self.index2lang = []
self.lang2id = {"en": 1, "zh": 2, "ja": 3, "fr": 4, "ko": 5, "de": 6}
for info in self.meta_info_data:
if info["prompt0_wav_path"] == None:
continue
self.wav_paths.append(info["wav_path"])
self.prompt0_paths.append(info["prompt0_wav_path"]) # Add prompt0 path
self.wav_path_index2duration.append(info["duration"])
self.wav_path_index2phonelen.append(info["phone_count"])
self.wav_path_index2spkid.append(info["speaker_id"])
self.wav_path_index2phoneid.append(info["phone_id"])
self.index2num_frames.append(info["duration"] * 50 + len(info["phone_id"]))
lang_id = self.lang2id[info["language"]]
self.index2lang.append(lang_id)
# self.index2num_frames.append(info["duration"] * self.cfg.preprocess.sample_rate)
self.num_frame_indices = np.array(
sorted(
range(len(self.index2num_frames)),
key=lambda k: self.index2num_frames[k],
)
)
self.processor = SeamlessM4TFeatureExtractor.from_pretrained("./w2v-bert-2")
def new_g2p(self, text, language):
return new_g2p(text, language)
def __len__(self):
return self.wav_paths.__len__()
def get_num_frames(self, index):
return (
self.wav_path_index2duration[index] * 50
+ self.wav_path_index2phonelen[index]
)
def __getitem__(self, idx):
wav_path = self.wav_paths[idx]
speech, sr = librosa.load(wav_path, sr=self.cfg.preprocess.sample_rate)
speech = np.pad(
speech,
(
0,
self.cfg.preprocess.hop_size
- len(speech) % self.cfg.preprocess.hop_size,
),
mode="constant",
)
# resample the speech to 16k for feature extraction
if self.cfg.preprocess.sample_rate != 16000:
speech_16k = librosa.resample(
speech, orig_sr=self.cfg.preprocess.sample_rate, target_sr=16000
)
else:
speech_16k = speech
inputs = self.processor(speech_16k, sampling_rate=16000)
# wav 2 bert convert to useful feature
input_features = inputs["input_features"][0]
attention_mask = inputs["attention_mask"][0]
prompt0_wav_path = self.prompt0_paths[idx] # Get prompt0 path
speech_prompt0, sr_prompt0 = librosa.load(
prompt0_wav_path, sr=self.cfg.preprocess.sample_rate
)
speech_prompt0 = np.pad(
speech_prompt0,
(
0,
self.cfg.preprocess.hop_size
- len(speech_prompt0) % self.cfg.preprocess.hop_size,
),
mode="constant",
)
# resample the speech to 16k for feature extraction
if self.cfg.preprocess.sample_rate != 16000:
speech_16k_prompt0 = librosa.resample(
speech_prompt0, orig_sr=self.cfg.preprocess.sample_rate, target_sr=16000
)
else:
speech_16k_prompt0 = speech_prompt0
inputs_prompt0 = self.processor(speech_16k_prompt0, sampling_rate=16000)
input_features_prompt0 = inputs_prompt0["input_features"][0]
attention_mask_prompt0 = inputs_prompt0["attention_mask"][0]
# get speech mask
speech_frames = len(speech) // self.cfg.preprocess.hop_size
mask = np.ones(speech_frames)
speech_frames_prompt0 = len(speech_prompt0) // self.cfg.preprocess.hop_size
mask_prompt0 = np.ones(speech_frames_prompt0)
del speech, speech_16k, speech_prompt0, speech_16k_prompt0
lang_id = self.index2lang[idx]
phone_id = self.wav_path_index2phoneid[idx]
phone_id = torch.tensor(phone_id, dtype=torch.long)
phone_mask = np.ones(len(phone_id))
single_feature = dict()
spk_id = self.wav_path_index2spkid[idx]
single_feature.update({"spk_id": spk_id})
single_feature.update({"lang_id": lang_id})
single_feature.update({"phone_id": phone_id})
single_feature.update({"phone_mask": phone_mask})
single_feature.update(
{
"input_features": input_features,
"attention_mask": attention_mask,
"mask": mask,
"input_features_prompt0": input_features_prompt0,
"attention_mask_prompt0": attention_mask_prompt0,
"mask_prompt0": mask_prompt0,
}
)
return single_feature
class T2SCollator(object):
def __init__(self, cfg):
self.cfg = cfg
def __call__(self, batch):
packed_batch_features = dict()
for key in batch[0].keys():
if "input_features" in key:
packed_batch_features[key] = pad_sequence(
[
(
utt[key].float()
if isinstance(utt[key], torch.Tensor)
else torch.tensor(utt[key]).float()
)
for utt in batch
],
batch_first=True,
)
if "attention_mask" in key:
packed_batch_features[key] = pad_sequence(
[
(
utt[key].float()
if isinstance(utt[key], torch.Tensor)
else torch.tensor(utt[key]).float()
)
for utt in batch
],
batch_first=True,
)
if "mask" in key:
packed_batch_features[key] = pad_sequence(
[
(
utt[key].long()
if isinstance(utt[key], torch.Tensor)
else torch.tensor(utt[key]).long()
)
for utt in batch
],
batch_first=True,
)
if "semantic_code" in key:
packed_batch_features[key] = pad_sequence(
[
(
utt[key].float()
if isinstance(utt[key], torch.Tensor)
else torch.tensor(utt[key]).float()
)
for utt in batch
],
batch_first=True,
)
if key == "phone_id":
packed_batch_features[key] = pad_sequence(
[utt[key].long() for utt in batch],
batch_first=True,
padding_value=1023, # phone vocab size is 1024
)
if key == "phone_mask":
packed_batch_features[key] = pad_sequence(
[torch.tensor(utt[key]).long() for utt in batch], batch_first=True
)
if key == "lang_id":
packed_batch_features[key] = torch.tensor(
[utt[key] for utt in batch]
).long()
if key == "spk_id":
packed_batch_features[key] = torch.tensor(
[utt[key] for utt in batch]
).long()
if key == "spk_emb_input_features":
packed_batch_features[key] = pad_sequence(
[torch.tensor(utt[key]).float() for utt in batch], batch_first=True
)
if key == "spk_emb_attention_mask":
packed_batch_features[key] = pad_sequence(
[torch.tensor(utt[key]).long() for utt in batch], batch_first=True
)
else:
pass
return packed_batch_features
class DownsampleWithMask(nn.Module):
def __init__(self, downsample_factor=2):
super(DownsampleWithMask, self).__init__()
self.downsample_factor = downsample_factor
def forward(self, x, mask):
# input from numpy.ndarray to torch.Tensor
if isinstance(x, np.ndarray):
x = torch.tensor(x, dtype=torch.float32)
if isinstance(mask, np.ndarray):
mask = torch.tensor(mask, dtype=torch.float32)
# print(f"################## x size original {x.shape}################################")
x = x.float()
x = x.permute(1, 0) # to (feature_dim, timestep)
x = x.unsqueeze(1) # add channel dimension: (timestep, 1, feature_dim)
if x.size(-1) < self.downsample_factor:
raise ValueError("Input size must be larger than downsample factor")
# print(f"################## x size before {x.shape}################################")
x = F.avg_pool1d(x, kernel_size=self.downsample_factor)
x = x.squeeze(
1
) # remove channel dimension: (timestep, feature_dim // downsample_factor)
x = x.long()
x = x.permute(1, 0) # to (feature_dim, timestep)
mask = mask.float() # convert mask to float for pooling
mask = mask.unsqueeze(0).unsqueeze(
0
) # add channel dimension: (timestep, 1, feature_dim)
if mask.size(-1) < self.downsample_factor:
raise ValueError("Mask size must be larger than downsample factor")
mask = F.avg_pool1d(
mask, kernel_size=self.downsample_factor, stride=self.downsample_factor
)
mask = mask.squeeze(0).squeeze(
0
) # remove channel dimension: (timestep, feature_dim // downsample_factor)
mask = (mask >= 0.5).long() # if average > 0.5 --> 1, else 0
return x, mask
|