File size: 20,729 Bytes
9b9e0ee |
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 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 |
from email.policy import strict
import torch
import os
import pytorch_lightning as pl
import torch.nn.functional as F
from contextlib import contextmanager
import numpy as np
from qa_mdt.audioldm_train.modules.diffusionmodules.ema import *
from torch.optim.lr_scheduler import LambdaLR
from qa_mdt.audioldm_train.modules.diffusionmodules.model import Encoder, Decoder
from qa_mdt.audioldm_train.modules.diffusionmodules.distributions import (
DiagonalGaussianDistribution,
)
import wandb
from qa_mdt.audioldm_train.utilities.model_util import instantiate_from_config
import soundfile as sf
from qa_mdt.audioldm_train.utilities.model_util import get_vocoder
from qa_mdt.audioldm_train.utilities.tools import synth_one_sample
import itertools
class AutoencoderKL(pl.LightningModule):
def __init__(
self,
ddconfig=None,
lossconfig=None,
batchsize=None,
embed_dim=None,
time_shuffle=1,
subband=1,
sampling_rate=16000,
ckpt_path=None,
reload_from_ckpt=None,
ignore_keys=[],
image_key="fbank",
colorize_nlabels=None,
monitor=None,
base_learning_rate=1e-5,
):
super().__init__()
self.automatic_optimization = False
assert (
"mel_bins" in ddconfig.keys()
), "mel_bins is not specified in the Autoencoder config"
num_mel = ddconfig["mel_bins"]
self.image_key = image_key
self.sampling_rate = sampling_rate
self.encoder = Encoder(**ddconfig)
self.decoder = Decoder(**ddconfig)
self.loss = instantiate_from_config(lossconfig)
self.subband = int(subband)
if self.subband > 1:
print("Use subband decomposition %s" % self.subband)
assert ddconfig["double_z"]
self.quant_conv = torch.nn.Conv2d(2 * ddconfig["z_channels"], 2 * embed_dim, 1)
self.post_quant_conv = torch.nn.Conv2d(embed_dim, ddconfig["z_channels"], 1)
if self.image_key == "fbank":
self.vocoder = get_vocoder(None, "cpu", num_mel)
self.embed_dim = embed_dim
if colorize_nlabels is not None:
assert type(colorize_nlabels) == int
self.register_buffer("colorize", torch.randn(3, colorize_nlabels, 1, 1))
if monitor is not None:
self.monitor = monitor
if ckpt_path is not None:
self.init_from_ckpt(ckpt_path, ignore_keys=ignore_keys)
self.learning_rate = float(base_learning_rate)
print("Initial learning rate %s" % self.learning_rate)
self.time_shuffle = time_shuffle
self.reload_from_ckpt = reload_from_ckpt
self.reloaded = False
self.mean, self.std = None, None
self.feature_cache = None
self.flag_first_run = True
self.train_step = 0
self.logger_save_dir = None
self.logger_exp_name = None
self.logger_exp_group_name = None
if not self.reloaded and self.reload_from_ckpt is not None:
# import pdb
# pdb.set_trace()
print("--> Reload weight of autoencoder from %s" % self.reload_from_ckpt)
checkpoint = torch.load(self.reload_from_ckpt)
load_todo_keys = {}
pretrained_state_dict = checkpoint["state_dict"]
current_state_dict = self.state_dict()
for key in current_state_dict:
if (
key in pretrained_state_dict.keys()
and pretrained_state_dict[key].size()
== current_state_dict[key].size()
):
load_todo_keys[key] = pretrained_state_dict[key]
else:
print("Key %s mismatch during loading, seems fine" % key)
self.load_state_dict(load_todo_keys, strict=False)
self.reloaded = True
else:
print("Train from scratch")
def get_log_dir(self):
return os.path.join(
self.logger_save_dir, self.logger_exp_group_name, self.logger_exp_name
)
def set_log_dir(self, save_dir, exp_group_name, exp_name):
self.logger_save_dir = save_dir
self.logger_exp_name = exp_name
self.logger_exp_group_name = exp_group_name
def init_from_ckpt(self, path, ignore_keys=list()):
sd = torch.load(path, map_location="cpu")["state_dict"]
keys = list(sd.keys())
for k in keys:
for ik in ignore_keys:
if k.startswith(ik):
print("Deleting key {} from state_dict.".format(k))
del sd[k]
self.load_state_dict(sd, strict=False)
print(f"Restored from {path}")
def encode(self, x):
# x = self.time_shuffle_operation(x)
x = self.freq_split_subband(x)
h = self.encoder(x)
moments = self.quant_conv(h)
posterior = DiagonalGaussianDistribution(moments)
return posterior
def decode(self, z):
z = self.post_quant_conv(z)
dec = self.decoder(z)
# bs, ch, shuffled_timesteps, fbins = dec.size()
# dec = self.time_unshuffle_operation(dec, bs, int(ch*shuffled_timesteps), fbins)
dec = self.freq_merge_subband(dec)
return dec
def decode_to_waveform(self, dec):
from qa_mdt.audioldm_train.utilities.model_util import vocoder_infer
if self.image_key == "fbank":
dec = dec.squeeze(1).permute(0, 2, 1)
wav_reconstruction = vocoder_infer(dec, self.vocoder)
elif self.image_key == "stft":
dec = dec.squeeze(1).permute(0, 2, 1)
wav_reconstruction = self.wave_decoder(dec)
return wav_reconstruction
def visualize_latent(self, input):
import matplotlib.pyplot as plt
# for i in range(10):
# zero_input = torch.zeros_like(input) - 11.59
# zero_input[:,:,i * 16: i * 16 + 16,:16] += 13.59
# posterior = self.encode(zero_input)
# latent = posterior.sample()
# avg_latent = torch.mean(latent, dim=1)[0]
# plt.imshow(avg_latent.cpu().detach().numpy().T)
# plt.savefig("%s.png" % i)
# plt.close()
np.save("input.npy", input.cpu().detach().numpy())
# zero_input = torch.zeros_like(input) - 11.59
time_input = input.clone()
time_input[:, :, :, :32] *= 0
time_input[:, :, :, :32] -= 11.59
np.save("time_input.npy", time_input.cpu().detach().numpy())
posterior = self.encode(time_input)
latent = posterior.sample()
np.save("time_latent.npy", latent.cpu().detach().numpy())
avg_latent = torch.mean(latent, dim=1)
for i in range(avg_latent.size(0)):
plt.imshow(avg_latent[i].cpu().detach().numpy().T)
plt.savefig("freq_%s.png" % i)
plt.close()
freq_input = input.clone()
freq_input[:, :, :512, :] *= 0
freq_input[:, :, :512, :] -= 11.59
np.save("freq_input.npy", freq_input.cpu().detach().numpy())
posterior = self.encode(freq_input)
latent = posterior.sample()
np.save("freq_latent.npy", latent.cpu().detach().numpy())
avg_latent = torch.mean(latent, dim=1)
for i in range(avg_latent.size(0)):
plt.imshow(avg_latent[i].cpu().detach().numpy().T)
plt.savefig("time_%s.png" % i)
plt.close()
def forward(self, input, sample_posterior=True):
posterior = self.encode(input)
if sample_posterior:
z = posterior.sample()
else:
z = posterior.mode()
if self.flag_first_run:
print("Latent size: ", z.size())
self.flag_first_run = False
dec = self.decode(z)
return dec, posterior
def get_input(self, batch):
fname, text, label_indices, waveform, stft, fbank = (
batch["fname"],
batch["text"],
batch["label_vector"],
batch["waveform"],
batch["stft"],
batch["log_mel_spec"],
)
# if(self.time_shuffle != 1):
# if(fbank.size(1) % self.time_shuffle != 0):
# pad_len = self.time_shuffle - (fbank.size(1) % self.time_shuffle)
# fbank = torch.nn.functional.pad(fbank, (0,0,0,pad_len))
ret = {}
ret["fbank"], ret["stft"], ret["fname"], ret["waveform"] = (
fbank.unsqueeze(1),
stft.unsqueeze(1),
fname,
waveform.unsqueeze(1),
)
return ret
# def time_shuffle_operation(self, fbank):
# if(self.time_shuffle == 1):
# return fbank
# shuffled_fbank = []
# for i in range(self.time_shuffle):
# shuffled_fbank.append(fbank[:,:, i::self.time_shuffle,:])
# return torch.cat(shuffled_fbank, dim=1)
# def time_unshuffle_operation(self, shuffled_fbank, bs, timesteps, fbins):
# if(self.time_shuffle == 1):
# return shuffled_fbank
# buffer = torch.zeros((bs, 1, timesteps, fbins)).to(shuffled_fbank.device)
# for i in range(self.time_shuffle):
# buffer[:,0,i::self.time_shuffle,:] = shuffled_fbank[:,i,:,:]
# return buffer
def freq_split_subband(self, fbank):
if self.subband == 1 or self.image_key != "stft":
return fbank
bs, ch, tstep, fbins = fbank.size()
assert fbank.size(-1) % self.subband == 0
assert ch == 1
return (
fbank.squeeze(1)
.reshape(bs, tstep, self.subband, fbins // self.subband)
.permute(0, 2, 1, 3)
)
def freq_merge_subband(self, subband_fbank):
if self.subband == 1 or self.image_key != "stft":
return subband_fbank
assert subband_fbank.size(1) == self.subband # Channel dimension
bs, sub_ch, tstep, fbins = subband_fbank.size()
return subband_fbank.permute(0, 2, 1, 3).reshape(bs, tstep, -1).unsqueeze(1)
def training_step(self, batch, batch_idx):
g_opt, d_opt = self.optimizers()
inputs_dict = self.get_input(batch)
inputs = inputs_dict[self.image_key]
waveform = inputs_dict["waveform"]
if batch_idx % 5000 == 0 and self.local_rank == 0:
print("Log train image")
self.log_images(inputs, waveform=waveform)
reconstructions, posterior = self(inputs)
if self.image_key == "stft":
rec_waveform = self.decode_to_waveform(reconstructions)
else:
rec_waveform = None
# train the discriminator
# If working on waveform, inputs is STFT, reconstructions are the waveform
# If working on the melspec, inputs is melspec, reconstruction are also mel spec
discloss, log_dict_disc = self.loss(
inputs=inputs,
reconstructions=reconstructions,
posteriors=posterior,
waveform=waveform,
rec_waveform=rec_waveform,
optimizer_idx=1,
global_step=self.global_step,
last_layer=self.get_last_layer(),
split="train",
)
self.log(
"discloss",
discloss,
prog_bar=True,
logger=True,
on_step=True,
on_epoch=True,
)
self.log_dict(
log_dict_disc, prog_bar=False, logger=True, on_step=True, on_epoch=False
)
d_opt.zero_grad()
self.manual_backward(discloss)
d_opt.step()
self.log(
"train_step",
self.train_step,
prog_bar=False,
logger=False,
on_step=True,
on_epoch=False,
)
self.log(
"global_step",
float(self.global_step),
prog_bar=True,
logger=True,
on_step=True,
on_epoch=False,
)
aeloss, log_dict_ae = self.loss(
inputs=inputs,
reconstructions=reconstructions,
posteriors=posterior,
waveform=waveform,
rec_waveform=rec_waveform,
optimizer_idx=0,
global_step=self.global_step,
last_layer=self.get_last_layer(),
split="train",
)
self.log(
"aeloss",
aeloss,
prog_bar=True,
logger=True,
on_step=True,
on_epoch=False,
)
self.log(
"posterior_std",
torch.mean(posterior.var),
prog_bar=True,
logger=True,
on_step=True,
on_epoch=False,
)
self.log_dict(
log_dict_ae, prog_bar=True, logger=True, on_step=True, on_epoch=False
)
self.train_step += 1
g_opt.zero_grad()
self.manual_backward(aeloss)
g_opt.step()
def validation_step(self, batch, batch_idx):
inputs_dict = self.get_input(batch)
inputs = inputs_dict[self.image_key]
waveform = inputs_dict["waveform"]
if batch_idx <= 3:
print("Log val image")
self.log_images(inputs, train=False, waveform=waveform)
reconstructions, posterior = self(inputs)
if self.image_key == "stft":
rec_waveform = self.decode_to_waveform(reconstructions)
else:
rec_waveform = None
aeloss, log_dict_ae = self.loss(
inputs=inputs,
reconstructions=reconstructions,
posteriors=posterior,
waveform=waveform,
rec_waveform=rec_waveform,
optimizer_idx=0,
global_step=self.global_step,
last_layer=self.get_last_layer(),
split="val",
)
discloss, log_dict_disc = self.loss(
inputs=inputs,
reconstructions=reconstructions,
posteriors=posterior,
waveform=waveform,
rec_waveform=rec_waveform,
optimizer_idx=1,
global_step=self.global_step,
last_layer=self.get_last_layer(),
split="val",
)
self.log_dict(log_dict_ae)
self.log_dict(log_dict_disc)
return self.log_dict
def test_step(self, batch, batch_idx):
inputs_dict = self.get_input(batch)
inputs = inputs_dict[self.image_key]
waveform = inputs_dict["waveform"]
fnames = inputs_dict["fname"]
reconstructions, posterior = self(inputs)
save_path = os.path.join(
self.get_log_dir(), "autoencoder_result_audiocaps", str(self.global_step)
)
if self.image_key == "stft":
wav_prediction = self.decode_to_waveform(reconstructions)
wav_original = waveform
self.save_wave(
wav_prediction, fnames, os.path.join(save_path, "stft_wav_prediction")
)
else:
wav_vocoder_gt, wav_prediction = synth_one_sample(
inputs.squeeze(1),
reconstructions.squeeze(1),
labels="validation",
vocoder=self.vocoder,
)
self.save_wave(
wav_vocoder_gt, fnames, os.path.join(save_path, "fbank_vocoder_gt_wave")
)
self.save_wave(
wav_prediction, fnames, os.path.join(save_path, "fbank_wav_prediction")
)
def save_wave(self, batch_wav, fname, save_dir):
os.makedirs(save_dir, exist_ok=True)
for wav, name in zip(batch_wav, fname):
name = os.path.basename(name)
sf.write(os.path.join(save_dir, name), wav, samplerate=self.sampling_rate)
def configure_optimizers(self):
lr = self.learning_rate
params = (
list(self.encoder.parameters())
+ list(self.decoder.parameters())
+ list(self.quant_conv.parameters())
+ list(self.post_quant_conv.parameters())
)
if self.image_key == "stft":
params += list(self.wave_decoder.parameters())
opt_ae = torch.optim.Adam(params, lr=lr, betas=(0.5, 0.9))
if self.image_key == "fbank":
disc_params = self.loss.discriminator.parameters()
elif self.image_key == "stft":
disc_params = itertools.chain(
self.loss.msd.parameters(), self.loss.mpd.parameters()
)
opt_disc = torch.optim.Adam(disc_params, lr=lr, betas=(0.5, 0.9))
return [opt_ae, opt_disc], []
def get_last_layer(self):
return self.decoder.conv_out.weight
@torch.no_grad()
def log_images(self, batch, train=True, only_inputs=False, waveform=None, **kwargs):
log = dict()
x = batch.to(self.device)
if not only_inputs:
xrec, posterior = self(x)
log["samples"] = self.decode(posterior.sample())
log["reconstructions"] = xrec
log["inputs"] = x
wavs = self._log_img(log, train=train, index=0, waveform=waveform)
return wavs
def _log_img(self, log, train=True, index=0, waveform=None):
images_input = self.tensor2numpy(log["inputs"][index, 0]).T
images_reconstruct = self.tensor2numpy(log["reconstructions"][index, 0]).T
images_samples = self.tensor2numpy(log["samples"][index, 0]).T
if train:
name = "train"
else:
name = "val"
if self.logger is not None:
self.logger.log_image(
"img_%s" % name,
[images_input, images_reconstruct, images_samples],
caption=["input", "reconstruct", "samples"],
)
inputs, reconstructions, samples = (
log["inputs"],
log["reconstructions"],
log["samples"],
)
if self.image_key == "fbank":
wav_original, wav_prediction = synth_one_sample(
inputs[index],
reconstructions[index],
labels="validation",
vocoder=self.vocoder,
)
wav_original, wav_samples = synth_one_sample(
inputs[index], samples[index], labels="validation", vocoder=self.vocoder
)
wav_original, wav_samples, wav_prediction = (
wav_original[0],
wav_samples[0],
wav_prediction[0],
)
elif self.image_key == "stft":
wav_prediction = (
self.decode_to_waveform(reconstructions)[index, 0]
.cpu()
.detach()
.numpy()
)
wav_samples = (
self.decode_to_waveform(samples)[index, 0].cpu().detach().numpy()
)
wav_original = waveform[index, 0].cpu().detach().numpy()
if self.logger is not None:
self.logger.experiment.log(
{
"original_%s"
% name: wandb.Audio(
wav_original, caption="original", sample_rate=self.sampling_rate
),
"reconstruct_%s"
% name: wandb.Audio(
wav_prediction,
caption="reconstruct",
sample_rate=self.sampling_rate,
),
"samples_%s"
% name: wandb.Audio(
wav_samples, caption="samples", sample_rate=self.sampling_rate
),
}
)
return wav_original, wav_prediction, wav_samples
def tensor2numpy(self, tensor):
return tensor.cpu().detach().numpy()
def to_rgb(self, x):
assert self.image_key == "segmentation"
if not hasattr(self, "colorize"):
self.register_buffer("colorize", torch.randn(3, x.shape[1], 1, 1).to(x))
x = F.conv2d(x, weight=self.colorize)
x = 2.0 * (x - x.min()) / (x.max() - x.min()) - 1.0
return x
class IdentityFirstStage(torch.nn.Module):
def __init__(self, *args, vq_interface=False, **kwargs):
self.vq_interface = vq_interface # TODO: Should be true by default but check to not break older stuff
super().__init__()
def encode(self, x, *args, **kwargs):
return x
def decode(self, x, *args, **kwargs):
return x
def quantize(self, x, *args, **kwargs):
if self.vq_interface:
return x, None, [None, None, None]
return x
def forward(self, x, *args, **kwargs):
return x
|