Spaces:
Runtime error
Runtime error
File size: 15,839 Bytes
ae8e1dd |
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 |
import os
import argparse
import glob
import logging
import numpy as np
import matplotlib.pyplot as plt
import data_loader as loaders
import data_collate as collates
import json
from model import GradTTSXvector, GradTTSWithEmo
import torch
def intersperse(lst, item):
# Adds blank symbol
result = [item] * (len(lst) * 2 + 1)
result[1::2] = lst
return result
def parse_filelist(filelist_path, split_char="|"):
with open(filelist_path, encoding='utf-8') as f:
filepaths_and_text = [line.strip().split(split_char) for line in f]
return filepaths_and_text
def latest_checkpoint_path(dir_path, regex="grad_*.pt"):
f_list = glob.glob(os.path.join(dir_path, regex))
f_list.sort(key=lambda f: int("".join(filter(str.isdigit, f))))
x = f_list[-1]
return x
def load_checkpoint(checkpoint_path, model, optimizer=None):
assert os.path.isfile(checkpoint_path)
checkpoint_dict = torch.load(checkpoint_path, map_location='cpu')
iteration = 1
if 'iteration' in checkpoint_dict.keys():
iteration = checkpoint_dict['iteration']
if 'learning_rate' in checkpoint_dict.keys():
learning_rate = checkpoint_dict['learning_rate']
else:
learning_rate = None
if optimizer is not None and 'optimizer' in checkpoint_dict.keys():
optimizer.load_state_dict(checkpoint_dict['optimizer'])
saved_state_dict = checkpoint_dict['model']
if hasattr(model, 'module'):
state_dict = model.module.state_dict()
else:
state_dict = model.state_dict()
new_state_dict = {}
for k, v in state_dict.items():
try:
new_state_dict[k] = saved_state_dict[k]
except:
logger.info("%s is not in the checkpoint" % k)
print("%s is not in the checkpoint" % k)
new_state_dict[k] = v
if hasattr(model, 'module'):
model.module.load_state_dict(new_state_dict)
else:
model.load_state_dict(new_state_dict)
return model, optimizer, learning_rate, iteration
def load_checkpoint_no_logger(checkpoint_path, model, optimizer=None):
assert os.path.isfile(checkpoint_path)
checkpoint_dict = torch.load(checkpoint_path, map_location='cpu')
iteration = 1
if 'iteration' in checkpoint_dict.keys():
iteration = checkpoint_dict['iteration']
if 'learning_rate' in checkpoint_dict.keys():
learning_rate = checkpoint_dict['learning_rate']
else:
learning_rate = None
if optimizer is not None and 'optimizer' in checkpoint_dict.keys():
optimizer.load_state_dict(checkpoint_dict['optimizer'])
saved_state_dict = checkpoint_dict['model']
if hasattr(model, 'module'):
state_dict = model.module.state_dict()
else:
state_dict = model.state_dict()
new_state_dict = {}
for k, v in state_dict.items():
try:
new_state_dict[k] = saved_state_dict[k]
except:
print("%s is not in the checkpoint" % k)
new_state_dict[k] = v
if hasattr(model, 'module'):
model.module.load_state_dict(new_state_dict)
else:
model.load_state_dict(new_state_dict)
return model, optimizer, learning_rate, iteration
def save_figure_to_numpy(fig):
data = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8, sep='')
data = data.reshape(fig.canvas.get_width_height()[::-1] + (3,))
return data
def plot_tensor(tensor):
plt.style.use('default')
fig, ax = plt.subplots(figsize=(12, 3))
im = ax.imshow(tensor, aspect="auto", origin="lower", interpolation='none')
plt.colorbar(im, ax=ax)
plt.tight_layout()
fig.canvas.draw()
data = save_figure_to_numpy(fig)
plt.close()
return data
def save_plot(tensor, savepath):
plt.style.use('default')
fig, ax = plt.subplots(figsize=(12, 3))
im = ax.imshow(tensor, aspect="auto", origin="lower", interpolation='none')
plt.colorbar(im, ax=ax)
plt.tight_layout()
fig.canvas.draw()
plt.savefig(savepath)
plt.close()
return
def get_correct_class(hps, train=True):
if train:
if hps.xvector and hps.pe:
raise NotImplementedError
elif hps.xvector: # no pitch energy
raise NotImplementedError
loader = loaders.XvectorLoader
collate = collates.XvectorCollate
model = GradTTSXvector
dataset = loader(utts=hps.data.train_utts,
hparams=hps.data,
feats_scp=hps.data.train_feats_scp,
utt2phns=hps.data.train_utt2phns,
phn2id=hps.data.phn2id,
utt2phn_duration=hps.data.train_utt2phn_duration,
spk_xvector_scp=hps.data.train_spk_xvector_scp,
utt2spk_name=hps.data.train_utt2spk)
elif hps.pe:
raise NotImplementedError
else: # no PE, no xvector
loader = loaders.SpkIDLoaderWithEmo
collate = collates.SpkIDCollateWithEmo
model = GradTTSWithEmo
dataset = loader(utts=hps.data.train_utts,
hparams=hps.data,
feats_scp=hps.data.train_feats_scp,
utt2text=hps.data.train_utt2phns,
utt2spk=hps.data.train_utt2spk,
utt2emo=hps.data.train_utt2emo)
else:
if hps.xvector and hps.pe:
raise NotImplementedError
elif hps.xvector:
raise NotImplementedError
loader = loaders.XvectorLoader
collate = collates.XvectorCollate
model = GradTTSXvector
dataset = loader(utts=hps.data.val_utts,
hparams=hps.data,
feats_scp=hps.data.val_feats_scp,
utt2phns=hps.data.val_utt2phns,
phn2id=hps.data.phn2id,
utt2phn_duration=hps.data.val_utt2phn_duration,
spk_xvector_scp=hps.data.val_spk_xvector_scp,
utt2spk_name=hps.data.val_utt2spk)
elif hps.pe:
raise NotImplementedError
else: # no PE, no xvector
loader = loaders.SpkIDLoaderWithEmo
collate = collates.SpkIDCollateWithEmo
model = GradTTSWithEmo
dataset = loader(utts=hps.data.val_utts,
hparams=hps.data,
feats_scp=hps.data.val_feats_scp,
utt2text=hps.data.val_utt2phns,
utt2spk=hps.data.val_utt2spk,
utt2emo=hps.data.val_utt2emo)
return dataset, collate(), model
def get_hparams(init=True):
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--config', type=str, default="./configs/train_grad.json",
help='JSON file for configuration')
parser.add_argument('-m', '--model', type=str, required=True,
help='Model name')
parser.add_argument('-s', '--seed', type=int, default=1234)
parser.add_argument('--not-pretrained', action='store_true', help='if set to true, then train from scratch')
args = parser.parse_args()
model_dir = os.path.join("./logs", args.model)
if not os.path.exists(model_dir):
os.makedirs(model_dir)
config_path = args.config
config_save_path = os.path.join(model_dir, "config.json")
if init:
with open(config_path, "r") as f:
data = f.read()
with open(config_save_path, "w") as f:
f.write(data)
else:
with open(config_save_path, "r") as f:
data = f.read()
config = json.loads(data)
hparams = HParams(**config)
hparams.model_dir = model_dir
hparams.train.seed = args.seed
hparams.not_pretrained = args.not_pretrained
return hparams
class HParams():
def __init__(self, **kwargs):
for k, v in kwargs.items():
if type(v) == dict:
v = HParams(**v)
self[k] = v
def keys(self):
return self.__dict__.keys()
def items(self):
return self.__dict__.items()
def values(self):
return self.__dict__.values()
def __len__(self):
return len(self.__dict__)
def __getitem__(self, key):
return getattr(self, key)
def __setitem__(self, key, value):
return setattr(self, key, value)
def __contains__(self, key):
return key in self.__dict__
def __repr__(self):
return self.__dict__.__repr__()
def get_logger(model_dir, filename="train.log"):
global logger
logger = logging.getLogger(os.path.basename(model_dir))
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter("%(asctime)s\t%(name)s\t%(levelname)s\t%(message)s")
if not os.path.exists(model_dir):
os.makedirs(model_dir)
h = logging.FileHandler(os.path.join(model_dir, filename))
h.setLevel(logging.DEBUG)
h.setFormatter(formatter)
logger.addHandler(h)
return logger
def save_checkpoint(model, optimizer, learning_rate, iteration, checkpoint_path):
logger.info("Saving model and optimizer state at iteration {} to {}".format(
iteration, checkpoint_path))
if hasattr(model, 'module'):
state_dict = model.module.state_dict()
else:
state_dict = model.state_dict()
torch.save({'model': state_dict,
'iteration': iteration,
'optimizer': optimizer.state_dict(),
'learning_rate': learning_rate}, checkpoint_path)
def get_hparams_decode(model_dir=None):
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--config', type=str, default="./configs/train_grad.json",
help='JSON file for configuration')
parser.add_argument('-m', '--model', type=str, default=model_dir,
help='Model name')
parser.add_argument('-s', '--seed', type=int, default=1234)
parser.add_argument('-t', "--timesteps", type=int, default=10, help='how many timesteps to perform reverse diffusion')
parser.add_argument("--stoc", action='store_true', default=False, help="Whether to add stochastic term into decoding")
parser.add_argument("-g", "--guidance", type=float, default=3, help='classifier guidance')
parser.add_argument('-n', '--noise', type=float, default=1.5, help='to multiply sigma')
parser.add_argument('-f', '--file', type=str, required=True, help='path to a file with texts to synthesize')
parser.add_argument('-r', '--generated_path', type=str, required=True, help='path to save wav files')
args = parser.parse_args()
model_dir = os.path.join("./logs", args.model)
if not os.path.exists(model_dir):
os.makedirs(model_dir)
config_path = args.config
config_save_path = os.path.join(model_dir, "config.json") # NOTE: which config to load
with open(config_path, "r") as f:
data = f.read()
config = json.loads(data)
hparams = HParams(**config)
hparams.model_dir = model_dir
hparams.train.seed = args.seed
return hparams, args
def get_hparams_decode_two_mixture(model_dir=None):
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--config', type=str, default="./configs/train_grad.json",
help='JSON file for configuration')
parser.add_argument('-m', '--model', type=str, required=False, default='/raid/adal_abilbekov/training_emodiff/Emo_diff/logs/logs_train',
help='Model name')
parser.add_argument('-s', '--seed', type=int, default=1234)
parser.add_argument('--dataset', choices=['train', 'val'], default='val', type=str, help='which dataset to use')
parser.add_argument('--use-control-spk', action='store_true', help='whether to use GT spk or other spk')
parser.add_argument('--control-spk-id', default=None, type=int, help='if use control spk, then which spk')
parser.add_argument("--use-control-emo", action='store_true')
parser.add_argument("--control-emo-id1", type=int)
parser.add_argument("--control-emo-id2", type=int)
parser.add_argument("--emo1-weight", type=float, default=0.5)
parser.add_argument('--control-spk-name', default=None, type=str, help='if use control spk, then which spk')
parser.add_argument("--max-utt-num", default=100, type=int, help='maximum utts number to decode')
parser.add_argument("--specify-utt-name", default=None, type=str, help='if specified, only decodes for that utt')
parser.add_argument('-t', "--timesteps", type=int, default=10, help='how many timesteps to perform reverse diffusion')
parser.add_argument("--stoc", action='store_true', default=False, help="Whether to add stochastic term into decoding")
parser.add_argument("-g", "--guidance", type=float, default=3, help='classifier guidance')
parser.add_argument('-n', '--noise', type=float, default=1.5, help='to multiply sigma')
parser.add_argument('--text', type=str, default=None, help="given text file")
args = parser.parse_args()
model_dir = os.path.join("./logs", args.model)
if not os.path.exists(model_dir):
os.makedirs(model_dir)
config_path = args.config
config_save_path = os.path.join(model_dir, "config.json") # NOTE: which config to load
with open(config_path, "r") as f:
data = f.read()
config = json.loads(data)
hparams = HParams(**config)
hparams.model_dir = model_dir
hparams.train.seed = args.seed
if args.use_control_spk:
if hparams.xvector:
assert args.control_spk_name is not None
else:
assert args.control_spk_id is not None
return hparams, args
def get_hparams_classifier_objective():
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--config', type=str, default="./configs/base.json",
help='JSON file for configuration')
parser.add_argument('-m', '--model', type=str, required=True,
help='Model name')
parser.add_argument('-s', '--seed', type=int, default=1234)
parser.add_argument('--dataset', choices=['train', 'val'], default='val', type=str, help='which dataset to use')
parser.add_argument('--use-control-spk', action='store_true', help='whether to use GT spk or other spk')
parser.add_argument('--control-spk-id', default=None, type=int, help='if use control spk, then which spk')
parser.add_argument("--use-control-emo", action='store_true')
parser.add_argument("--max-utt-num", default=100, type=int, help='maximum utts number to decode')
parser.add_argument("--specify-utt-name", default=None, type=str, help='if specified, only decodes for that utt')
parser.add_argument('--text', type=str, default=None, help="given text file")
parser.add_argument("--feat", type=str, default=None, help='given feats.scp after CMVN')
parser.add_argument("--dur", type=str, default=None, help='Force durations')
args = parser.parse_args()
model_dir = os.path.join("./logs", args.model)
if not os.path.exists(model_dir):
os.makedirs(model_dir)
config_path = args.config
config_save_path = os.path.join(model_dir, "config.json") # NOTE: which config to load
with open(config_path, "r") as f:
data = f.read()
config = json.loads(data)
hparams = HParams(**config)
hparams.model_dir = model_dir
hparams.train.seed = args.seed
if args.use_control_spk:
if hparams.xvector:
assert args.control_spk_name is not None
else:
assert args.control_spk_id is not None
return hparams, args
|