Spaces:
Running
Running
File size: 14,344 Bytes
b4ad1cc 8a6f9a8 b4ad1cc 8a6f9a8 b4ad1cc |
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 |
# SPDX-FileCopyrightText: Copyright (c) 2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: MIT
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
import torch
from torch import nn
from common import ConvNorm, Invertible1x1Conv
from common import AffineTransformationLayer, SplineTransformationLayer
from common import ConvLSTMLinear
from transformer import FFTransformer
from autoregressive_flow import AR_Step, AR_Back_Step
def get_attribute_prediction_model(config):
name = config["name"]
hparams = config["hparams"]
if name == "dap":
model = DAP(**hparams)
elif name == "bgap":
model = BGAP(**hparams)
elif name == "agap":
model = AGAP(**hparams)
else:
raise Exception("{} model is not supported".format(name))
return model
class AttributeProcessing:
def __init__(self, take_log_of_input=False):
super(AttributeProcessing).__init__()
self.take_log_of_input = take_log_of_input
def normalize(self, x):
if self.take_log_of_input:
x = torch.log(x + 1)
return x
def denormalize(self, x):
if self.take_log_of_input:
x = torch.exp(x) - 1
return x
class BottleneckLayerLayer(nn.Module):
def __init__(
self,
in_dim,
reduction_factor,
norm="weightnorm",
non_linearity="relu",
kernel_size=3,
use_partial_padding=False,
):
super(BottleneckLayerLayer, self).__init__()
self.reduction_factor = reduction_factor
reduced_dim = int(in_dim / reduction_factor)
self.out_dim = reduced_dim
if self.reduction_factor > 1:
fn = ConvNorm(
in_dim,
reduced_dim,
kernel_size=kernel_size,
use_weight_norm=(norm == "weightnorm"),
)
if norm == "instancenorm":
fn = nn.Sequential(fn, nn.InstanceNorm1d(reduced_dim, affine=True))
self.projection_fn = fn
self.non_linearity = nn.ReLU()
if non_linearity == "leakyrelu":
self.non_linearity = nn.LeakyReLU()
def forward(self, x):
if self.reduction_factor > 1:
x = self.projection_fn(x)
x = self.non_linearity(x)
return x
class DAP(nn.Module):
def __init__(
self,
n_speaker_dim,
bottleneck_hparams,
take_log_of_input,
arch_hparams,
use_transformer=False,
):
super(DAP, self).__init__()
self.attribute_processing = AttributeProcessing(take_log_of_input)
self.bottleneck_layer = BottleneckLayerLayer(**bottleneck_hparams)
arch_hparams["in_dim"] = self.bottleneck_layer.out_dim + n_speaker_dim
if use_transformer:
self.feat_pred_fn = FFTransformer(**arch_hparams)
else:
self.feat_pred_fn = ConvLSTMLinear(**arch_hparams)
def forward(self, txt_enc, spk_emb, x, lens):
if x is not None:
x = self.attribute_processing.normalize(x)
txt_enc = self.bottleneck_layer(txt_enc)
spk_emb_expanded = spk_emb[..., None].expand(-1, -1, txt_enc.shape[2])
context = torch.cat((txt_enc, spk_emb_expanded), 1)
x_hat = self.feat_pred_fn(context, lens)
outputs = {"x_hat": x_hat, "x": x}
return outputs
def infer(self, z, txt_enc, spk_emb, lens=None):
x_hat = self.forward(txt_enc, spk_emb, x=None, lens=lens)["x_hat"]
x_hat = self.attribute_processing.denormalize(x_hat)
return x_hat
class BGAP(torch.nn.Module):
def __init__(
self,
n_in_dim,
n_speaker_dim,
bottleneck_hparams,
n_flows,
n_group_size,
n_layers,
with_dilation,
kernel_size,
scaling_fn,
take_log_of_input=False,
n_channels=1024,
use_quadratic=False,
n_bins=8,
n_spline_steps=2,
):
super(BGAP, self).__init__()
# assert(n_group_size % 2 == 0)
self.n_flows = n_flows
self.n_group_size = n_group_size
self.transforms = torch.nn.ModuleList()
self.convinv = torch.nn.ModuleList()
self.n_speaker_dim = n_speaker_dim
self.scaling_fn = scaling_fn
self.attribute_processing = AttributeProcessing(take_log_of_input)
self.n_spline_steps = n_spline_steps
self.bottleneck_layer = BottleneckLayerLayer(**bottleneck_hparams)
n_txt_reduced_dim = self.bottleneck_layer.out_dim
context_dim = n_txt_reduced_dim * n_group_size + n_speaker_dim
if self.n_group_size > 1:
self.unfold_params = {
"kernel_size": (n_group_size, 1),
"stride": n_group_size,
"padding": 0,
"dilation": 1,
}
self.unfold = nn.Unfold(**self.unfold_params)
for k in range(n_flows):
self.convinv.append(Invertible1x1Conv(n_in_dim * n_group_size))
if k >= n_flows - self.n_spline_steps:
left = -3
right = 3
top = 3
bottom = -3
self.transforms.append(
SplineTransformationLayer(
n_in_dim * n_group_size,
context_dim,
n_layers,
with_dilation=with_dilation,
kernel_size=kernel_size,
scaling_fn=scaling_fn,
n_channels=n_channels,
top=top,
bottom=bottom,
left=left,
right=right,
use_quadratic=use_quadratic,
n_bins=n_bins,
)
)
else:
self.transforms.append(
AffineTransformationLayer(
n_in_dim * n_group_size,
context_dim,
n_layers,
with_dilation=with_dilation,
kernel_size=kernel_size,
scaling_fn=scaling_fn,
affine_model="simple_conv",
n_channels=n_channels,
)
)
def fold(self, data):
"""Inverse of the self.unfold(data.unsqueeze(-1)) operation used for
the grouping or "squeeze" operation on input
Args:
data: B x C x T tensor of temporal data
"""
output_size = (data.shape[2] * self.n_group_size, 1)
data = nn.functional.fold(
data, output_size=output_size, **self.unfold_params
).squeeze(-1)
return data
def preprocess_context(self, txt_emb, speaker_vecs, std_scale=None):
if self.n_group_size > 1:
txt_emb = self.unfold(txt_emb[..., None])
speaker_vecs = speaker_vecs[..., None].expand(-1, -1, txt_emb.shape[2])
context = torch.cat((txt_emb, speaker_vecs), 1)
return context
def forward(self, txt_enc, spk_emb, x, lens):
"""x<tensor>: duration or pitch or energy average"""
assert txt_enc.size(2) >= x.size(1)
if len(x.shape) == 2:
# add channel dimension
x = x[:, None]
txt_enc = self.bottleneck_layer(txt_enc)
# lens including padded values
lens_grouped = (lens // self.n_group_size).long()
context = self.preprocess_context(txt_enc, spk_emb)
x = self.unfold(x[..., None])
log_s_list, log_det_W_list = [], []
for k in range(self.n_flows):
x, log_s = self.transforms[k](x, context, seq_lens=lens_grouped)
x, log_det_W = self.convinv[k](x)
log_det_W_list.append(log_det_W)
log_s_list.append(log_s)
# prepare outputs
outputs = {"z": x, "log_det_W_list": log_det_W_list, "log_s_list": log_s_list}
return outputs
def infer(self, z, txt_enc, spk_emb, seq_lens):
txt_enc = self.bottleneck_layer(txt_enc)
context = self.preprocess_context(txt_enc, spk_emb)
lens_grouped = (seq_lens // self.n_group_size).long()
z = self.unfold(z[..., None])
for k in reversed(range(self.n_flows)):
z = self.convinv[k](z, inverse=True)
z = self.transforms[k].forward(
z, context, inverse=True, seq_lens=lens_grouped
)
# z mapped to input domain
x_hat = self.fold(z)
# pad on the way out
return x_hat
class AGAP(torch.nn.Module):
def __init__(
self,
n_in_dim,
n_speaker_dim,
n_flows,
n_hidden,
n_lstm_layers,
bottleneck_hparams,
scaling_fn="exp",
take_log_of_input=False,
p_dropout=0.0,
setup="",
spline_flow_params=None,
n_group_size=1,
):
super(AGAP, self).__init__()
self.flows = torch.nn.ModuleList()
self.n_group_size = n_group_size
self.n_speaker_dim = n_speaker_dim
self.attribute_processing = AttributeProcessing(take_log_of_input)
self.n_in_dim = n_in_dim
self.bottleneck_layer = BottleneckLayerLayer(**bottleneck_hparams)
n_txt_reduced_dim = self.bottleneck_layer.out_dim
if self.n_group_size > 1:
self.unfold_params = {
"kernel_size": (n_group_size, 1),
"stride": n_group_size,
"padding": 0,
"dilation": 1,
}
self.unfold = nn.Unfold(**self.unfold_params)
if spline_flow_params is not None:
spline_flow_params["n_in_channels"] *= self.n_group_size
for i in range(n_flows):
if i % 2 == 0:
self.flows.append(
AR_Step(
n_in_dim * n_group_size,
n_speaker_dim,
n_txt_reduced_dim * n_group_size,
n_hidden,
n_lstm_layers,
scaling_fn,
spline_flow_params,
)
)
else:
self.flows.append(
AR_Back_Step(
n_in_dim * n_group_size,
n_speaker_dim,
n_txt_reduced_dim * n_group_size,
n_hidden,
n_lstm_layers,
scaling_fn,
spline_flow_params,
)
)
def fold(self, data):
"""Inverse of the self.unfold(data.unsqueeze(-1)) operation used for
the grouping or "squeeze" operation on input
Args:
data: B x C x T tensor of temporal data
"""
output_size = (data.shape[2] * self.n_group_size, 1)
data = nn.functional.fold(
data, output_size=output_size, **self.unfold_params
).squeeze(-1)
return data
def preprocess_context(self, txt_emb, speaker_vecs):
if self.n_group_size > 1:
txt_emb = self.unfold(txt_emb[..., None])
speaker_vecs = speaker_vecs[..., None].expand(-1, -1, txt_emb.shape[2])
context = torch.cat((txt_emb, speaker_vecs), 1)
return context
def forward(self, txt_emb, spk_emb, x, lens):
"""x<tensor>: duration or pitch or energy average"""
x = x[:, None] if len(x.shape) == 2 else x # add channel dimension
if self.n_group_size > 1:
x = self.unfold(x[..., None])
x = x.permute(2, 0, 1) # permute to time, batch, dims
x = self.attribute_processing.normalize(x)
txt_emb = self.bottleneck_layer(txt_emb)
context = self.preprocess_context(txt_emb, spk_emb)
context = context.permute(2, 0, 1) # permute to time, batch, dims
lens_groupped = (lens / self.n_group_size).long()
log_s_list = []
for i, flow in enumerate(self.flows):
x, log_s = flow(x, context, lens_groupped)
log_s_list.append(log_s)
x = x.permute(1, 2, 0) # x mapped to z
log_s_list = [log_s_elt.permute(1, 2, 0) for log_s_elt in log_s_list]
outputs = {"z": x, "log_s_list": log_s_list, "log_det_W_list": []}
return outputs
def infer(self, z, txt_emb, spk_emb, seq_lens=None):
if self.n_group_size > 1:
n_frames = z.shape[2]
z = self.unfold(z[..., None])
z = z.permute(2, 0, 1) # permute to time, batch, dims
txt_emb = self.bottleneck_layer(txt_emb)
context = self.preprocess_context(txt_emb, spk_emb)
context = context.permute(2, 0, 1) # permute to time, batch, dims
for i, flow in enumerate(reversed(self.flows)):
z = flow.infer(z, context)
x_hat = z.permute(1, 2, 0)
if self.n_group_size > 1:
x_hat = self.fold(x_hat)
if n_frames > x_hat.shape[2]:
m = nn.ReflectionPad1d((0, n_frames - x_hat.shape[2]))
x_hat = m(x_hat)
x_hat = self.attribute_processing.denormalize(x_hat)
return x_hat
|