Spaces:
Running
on
Zero
Running
on
Zero
File size: 8,090 Bytes
78e32cc |
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 |
###
# Author: Kai Li
# Date: 2022-05-26 18:09:54
# Email: [email protected]
# LastEditTime: 2024-01-24 00:00:28
###
import gc
from omegaconf import OmegaConf
import torch
import pytorch_lightning as pl
from torch.optim.lr_scheduler import ReduceLROnPlateau
from collections.abc import MutableMapping
from omegaconf import ListConfig
def flatten_dict(d, parent_key="", sep="_"):
"""Flattens a dictionary into a single-level dictionary while preserving
parent keys. Taken from
`SO <https://stackoverflow.com/questions/6027558/flatten-nested-dictionaries-compressing-keys>`_
Args:
d (MutableMapping): Dictionary to be flattened.
parent_key (str): String to use as a prefix to all subsequent keys.
sep (str): String to use as a separator between two key levels.
Returns:
dict: Single-level dictionary, flattened.
"""
items = []
for k, v in d.items():
new_key = parent_key + sep + k if parent_key else k
if isinstance(v, MutableMapping):
items.extend(flatten_dict(v, new_key, sep=sep).items())
else:
items.append((new_key, v))
return dict(items)
class AudioLightningModule(pl.LightningModule):
def __init__(
self,
model=None,
discriminator=None,
optimizer=None,
loss_func=None,
metrics=None,
scheduler=None,
):
super().__init__()
self.audio_model = model
self.discriminator = discriminator
self.optimizer = list(optimizer)
self.loss_func = loss_func
self.metrics = metrics
self.scheduler = list(scheduler)
# Save lightning"s AttributeDict under self.hparams
self.default_monitor = "val_loss"
# self.print(self.audio_model)
self.validation_step_outputs = []
self.test_step_outputs = []
self.automatic_optimization = False
def forward(self, wav):
"""Applies forward pass of the model.
Returns:
:class:`torch.Tensor`
"""
return self.audio_model(wav)
def training_step(self, batch, batch_nb):
ori_data, codec_data = batch
optimizer_g, optimizer_d = self.optimizers()
# multiple schedulers
scheduler_g, scheduler_d = self.lr_schedulers()
# train discriminator
optimizer_g.zero_grad()
output = self(codec_data)
optimizer_d.zero_grad()
est_outputs, _ = self.discriminator(output.detach(), sample_rate=44100)
target_outputs, _ = self.discriminator(ori_data, sample_rate=44100)
loss_d = self.loss_func["d"](target_outputs, est_outputs)
self.manual_backward(loss_d)
self.clip_gradients(optimizer_d, gradient_clip_val=5, gradient_clip_algorithm="norm")
optimizer_d.step()
# train generator
est_outputs, est_feature_maps = self.discriminator(output, sample_rate=44100)
_, targets_feature_maps = self.discriminator(ori_data, sample_rate=44100)
loss_g = self.loss_func["g"](est_outputs, est_feature_maps, targets_feature_maps, output, ori_data)
self.manual_backward(loss_g)
self.clip_gradients(optimizer_g, gradient_clip_val=5, gradient_clip_algorithm="norm")
optimizer_g.step()
# print(loss)
if self.trainer.is_last_batch:
scheduler_g.step()
scheduler_d.step()
self.log(
"train_loss_d",
loss_d,
on_epoch=True,
prog_bar=True,
sync_dist=True,
logger=True,
)
self.log(
"train_loss_g",
loss_g,
on_epoch=True,
prog_bar=True,
sync_dist=True,
logger=True,
)
def validation_step(self, batch, batch_nb):
# cal val loss
ori_data, codec_data = batch
# print(mixtures.shape)
est_sources = self(codec_data)
loss = self.metrics(est_sources, ori_data)
self.log(
"val_loss",
loss,
on_epoch=True,
prog_bar=True,
sync_dist=True,
logger=True,
)
self.validation_step_outputs.append(loss)
return {"val_loss": loss}
def on_validation_epoch_end(self):
# val
avg_loss = torch.stack(self.validation_step_outputs).mean()
val_loss = torch.mean(self.all_gather(avg_loss))
self.log(
"lr",
self.optimizer[0].param_groups[0]["lr"],
on_epoch=True,
prog_bar=True,
sync_dist=True,
)
self.logger.experiment.log(
{"learning_rate": self.optimizer[0].param_groups[0]["lr"], "epoch": self.current_epoch}
)
self.logger.experiment.log(
{"val_pit_sisnr": -val_loss, "epoch": self.current_epoch}
)
self.validation_step_outputs.clear() # free memory
torch.cuda.empty_cache()
def test_step(self, batch, batch_nb):
mixtures, targets = batch
est_sources = self(mixtures)
loss = self.metrics(est_sources, targets)
self.log(
"test_loss",
loss,
on_epoch=True,
prog_bar=True,
sync_dist=True,
logger=True,
)
self.test_step_outputs.append(loss)
return {"test_loss": loss}
def on_test_epoch_end(self):
# val
avg_loss = torch.stack(self.test_step_outputs).mean()
test_loss = torch.mean(self.all_gather(avg_loss))
self.log(
"lr",
self.optimizer.param_groups[0]["lr"],
on_epoch=True,
prog_bar=True,
sync_dist=True,
)
self.logger.experiment.log(
{"learning_rate": self.optimizer.param_groups[0]["lr"], "epoch": self.current_epoch}
)
self.logger.experiment.log(
{"test_pit_sisnr": -test_loss, "epoch": self.current_epoch}
)
self.test_step_outputs.clear()
def configure_optimizers(self):
"""Initialize optimizers, batch-wise and epoch-wise schedulers."""
if self.scheduler is None:
return self.optimizer
if not isinstance(self.scheduler, (list, tuple)):
self.scheduler = [self.scheduler] # support multiple schedulers
if not isinstance(self.optimizer, (list, tuple)):
self.optimizer = [self.optimizer] # support multiple schedulers
epoch_schedulers = []
for sched in self.scheduler:
if not isinstance(sched, dict):
if isinstance(sched, ReduceLROnPlateau):
sched = {"scheduler": sched, "monitor": self.default_monitor}
epoch_schedulers.append(sched)
else:
sched.setdefault("monitor", self.default_monitor)
sched.setdefault("frequency", 1)
# Backward compat
if sched["interval"] == "batch":
sched["interval"] = "step"
assert sched["interval"] in [
"epoch",
"step",
], "Scheduler interval should be either step or epoch"
epoch_schedulers.append(sched)
return self.optimizer, epoch_schedulers
@staticmethod
def config_to_hparams(dic):
"""Sanitizes the config dict to be handled correctly by torch
SummaryWriter. It flatten the config dict, converts ``None`` to
``"None"`` and any list and tuple into torch.Tensors.
Args:
dic (dict): Dictionary to be transformed.
Returns:
dict: Transformed dictionary.
"""
dic = flatten_dict(dic)
for k, v in dic.items():
if v is None:
dic[k] = str(v)
elif isinstance(v, (list, tuple)):
dic[k] = torch.tensor(v)
return dic
|