File size: 5,364 Bytes
c983126
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import auraloss
import numpy as np
import pytorch_lightning as pl

from deepafx_st.callbacks.plotting import plot_multi_spectrum
from deepafx_st.metrics import (
    LoudnessError,
    SpectralCentroidError,
    CrestFactorError,
    PESQ,
    MelSpectralDistance,
)


class LogAudioCallback(pl.callbacks.Callback):
    def __init__(self, num_examples=4, peak_normalize=True, sample_rate=22050):
        super().__init__()
        self.num_examples = 4
        self.peak_normalize = peak_normalize

        self.metrics = {
            "PESQ": PESQ(sample_rate),
            "MRSTFT": auraloss.freq.MultiResolutionSTFTLoss(
                fft_sizes=[32, 128, 512, 2048, 8192, 32768],
                hop_sizes=[16, 64, 256, 1024, 4096, 16384],
                win_lengths=[32, 128, 512, 2048, 8192, 32768],
                w_sc=0.0,
                w_phs=0.0,
                w_lin_mag=1.0,
                w_log_mag=1.0,
            ),
            "MSD": MelSpectralDistance(sample_rate),
            "SCE": SpectralCentroidError(sample_rate),
            "CFE": CrestFactorError(),
            "LUFS": LoudnessError(sample_rate),
        }

        self.outputs = []

    def on_validation_batch_end(
        self,
        trainer,
        pl_module,
        outputs,
        batch,
        batch_idx,
        dataloader_idx,
    ):
        """Called when the validation batch ends."""

        if outputs is not None:
            examples = np.min([self.num_examples, outputs["x"].shape[0]])
            self.outputs.append(outputs)

            if batch_idx == 0:
                for n in range(examples):
                    if batch_idx == 0:
                        self.log_audio(
                            outputs,
                            n,
                            pl_module.hparams.sample_rate,
                            pl_module.hparams.val_length,
                            trainer.global_step,
                            trainer.logger,
                        )

    def on_validation_end(self, trainer, pl_module):
        metrics = {
            "PESQ": [],
            "MRSTFT": [],
            "MSD": [],
            "SCE": [],
            "CFE": [],
            "LUFS": [],
        }
        for output in self.outputs:
            for metric_name, metric in self.metrics.items():
                try:
                    val = metric(output["y_hat"], output["y"])
                    metrics[metric_name].append(val)
                except:
                    pass

        # log final mean metrics
        for metric_name, metric in metrics.items():
            val = np.mean(metric)
            trainer.logger.experiment.add_scalar(
                f"metrics/{metric_name}", val, trainer.global_step
            )

        # clear outputs
        self.outputs = []

    def compute_metrics(self, metrics_dict, outputs, batch_idx, global_step):
        # extract audio
        y = outputs["y"][batch_idx, ...].float()
        y_hat = outputs["y_hat"][batch_idx, ...].float()

        # compute all metrics
        for metric_name, metric in self.metrics.items():
            try:
                val = metric(y_hat.view(1, 1, -1), y.view(1, 1, -1))
                metrics_dict[metric_name].append(val)
            except:
                pass

    def log_audio(self, outputs, batch_idx, sample_rate, n_fft, global_step, logger):
        x = outputs["x"][batch_idx, ...].float()
        y = outputs["y"][batch_idx, ...].float()
        y_hat = outputs["y_hat"][batch_idx, ...].float()

        if self.peak_normalize:
            x /= x.abs().max()
            y /= y.abs().max()
            y_hat /= y_hat.abs().max()

        logger.experiment.add_audio(
            f"x/{batch_idx+1}",
            x[0:1, :],
            global_step,
            sample_rate=sample_rate,
        )

        logger.experiment.add_audio(
            f"y/{batch_idx+1}",
            y[0:1, :],
            global_step,
            sample_rate=sample_rate,
        )

        logger.experiment.add_audio(
            f"y_hat/{batch_idx+1}",
            y_hat[0:1, :],
            global_step,
            sample_rate=sample_rate,
        )

        if "y_ref" in outputs:
            y_ref = outputs["y_ref"][batch_idx, ...].float()

            if self.peak_normalize:
                y_ref /= y_ref.abs().max()

            logger.experiment.add_audio(
                f"y_ref/{batch_idx+1}",
                y_ref[0:1, :],
                global_step,
                sample_rate=sample_rate,
            )
        logger.experiment.add_image(
            f"spec/{batch_idx+1}",
            compare_spectra(
                y_hat[0:1, :],
                y[0:1, :],
                x[0:1, :],
                sample_rate=sample_rate,
                n_fft=n_fft,
            ),
            global_step,
        )


def compare_spectra(
    deepafx_y_hat, y, x, baseline_y_hat=None, sample_rate=44100, n_fft=16384
):
    legend = ["Corrupted"]
    signals = [x]
    if baseline_y_hat is not None:
        legend.append("Baseline")
        signals.append(baseline_y_hat)

    legend.append("DeepAFx")
    signals.append(deepafx_y_hat)
    legend.append("Target")
    signals.append(y)

    image = plot_multi_spectrum(
        ys=signals,
        legend=legend,
        sample_rate=sample_rate,
        n_fft=n_fft,
    )

    return image