コード例 #1
0
 def test_InverseSpectrogram(self):
     tensor = common_utils.get_whitenoise(sample_rate=8000)
     spectrogram = common_utils.get_spectrogram(tensor,
                                                n_fft=400,
                                                hop_length=100)
     self._assert_consistency_complex(
         T.InverseSpectrogram(n_fft=400, hop_length=100), spectrogram)
コード例 #2
0
 def test_PSD(self):
     tensor = common_utils.get_whitenoise(sample_rate=8000, n_channels=4)
     spectrogram = common_utils.get_spectrogram(tensor,
                                                n_fft=400,
                                                hop_length=100)
     spectrogram = spectrogram.to(self.device)
     self._assert_consistency_complex(T.PSD(), spectrogram)
コード例 #3
0
    def test_timestretch_non_zero(self, rate, test_pseudo_complex):
        """Verify that ``T.TimeStretch`` does not fail if it's not close to 0

        ``T.TimeStrech`` is not differentiable around 0, so this test checks the differentiability
        for cases where input is not zero.

        As tested above, when spectrogram contains values close to zero, the gradients are unstable
        and gradcheck fails.

        In this test, we generate spectrogram from random signal, then we push the points around
        zero away from the origin.

        This process does not reflect the real use-case, and it is not practical for users, but
        this helps us understand to what degree the function is differentiable and when not.
        """
        n_fft = 16
        transform = T.TimeStretch(n_freq=n_fft // 2 + 1, fixed_rate=rate)
        waveform = get_whitenoise(sample_rate=40, duration=1, n_channels=2)
        spectrogram = get_spectrogram(waveform, n_fft=n_fft, power=None)

        # 1e-3 is too small (on CPU)
        epsilon = 1e-2
        too_close = spectrogram.abs() < epsilon
        spectrogram[too_close] = epsilon * spectrogram[too_close] / spectrogram[too_close].abs()
        if test_pseudo_complex:
            spectrogram = torch.view_as_real(spectrogram)
        self.assert_grad(transform, [spectrogram])
コード例 #4
0
 def test_psd(self):
     transform = T.PSD()
     waveform = get_whitenoise(sample_rate=8000,
                               duration=0.05,
                               n_channels=2)
     spectrogram = get_spectrogram(waveform, n_fft=400)
     self.assert_grad(transform, [spectrogram])
コード例 #5
0
    def test_griffinlim(self, momentum):
        # FFT params
        n_fft = 400
        win_length = n_fft
        hop_length = n_fft // 4
        window = torch.hann_window(win_length, device=self.device)
        power = 1
        # GriffinLim params
        n_iter = 8

        waveform = get_whitenoise(device=self.device, dtype=self.dtype)
        specgram = get_spectrogram(
            waveform, n_fft=n_fft, hop_length=hop_length, power=power,
            win_length=win_length, window=window)

        result = F.griffinlim(
            specgram,
            window=window,
            n_fft=n_fft,
            hop_length=hop_length,
            win_length=win_length,
            power=power,
            n_iter=n_iter,
            momentum=momentum,
            length=waveform.size(1),
            rand_init=False)
        expected = librosa.griffinlim(
            specgram[0].cpu().numpy(),
            n_iter=n_iter,
            hop_length=hop_length,
            momentum=momentum,
            init=None,
            length=waveform.size(1))[None, ...]
        self.assertEqual(result, torch.from_numpy(expected), atol=5e-5, rtol=1e-07)
コード例 #6
0
 def test_InverseSpectrogram_pseudocomplex(self):
     tensor = common_utils.get_whitenoise(sample_rate=8000)
     spectrogram = common_utils.get_spectrogram(tensor,
                                                n_fft=400,
                                                hop_length=100)
     spectrogram = torch.view_as_real(spectrogram)
     self._assert_consistency(
         T.InverseSpectrogram(n_fft=400, hop_length=100), spectrogram)
コード例 #7
0
 def test_power_to_db(self):
     spectrogram = get_spectrogram(get_whitenoise(), n_fft=400,
                                   power=2).to(self.device, self.dtype)
     result = T.AmplitudeToDB('power', 80.).to(self.device,
                                               self.dtype)(spectrogram)[0]
     expected = librosa.core.spectrum.power_to_db(
         spectrogram[0].cpu().numpy())
     self.assertEqual(result, torch.from_numpy(expected))
コード例 #8
0
 def test_PSD_with_mask(self):
     tensor = common_utils.get_whitenoise(sample_rate=8000, n_channels=4)
     spectrogram = common_utils.get_spectrogram(tensor,
                                                n_fft=400,
                                                hop_length=100)
     spectrogram = spectrogram.to(self.device)
     mask = torch.rand(spectrogram.shape[-2:], device=self.device)
     self._assert_consistency_complex(T.PSD(), spectrogram, mask)
コード例 #9
0
 def test_mvdr(self, solution):
     transform = T.MVDR(solution=solution)
     waveform = get_whitenoise(sample_rate=8000,
                               duration=0.05,
                               n_channels=2)
     spectrogram = get_spectrogram(waveform, n_fft=400)
     mask_s = torch.rand(spectrogram.shape[-2:])
     mask_n = torch.rand(spectrogram.shape[-2:])
     self.assert_grad(transform, [spectrogram, mask_s, mask_n])
コード例 #10
0
 def test_melscale(self):
     sample_rate = 8000
     n_fft = 400
     n_mels = n_fft // 2 + 1
     transform = T.MelScale(sample_rate=sample_rate, n_mels=n_mels)
     spec = get_spectrogram(
         get_whitenoise(sample_rate=sample_rate, duration=0.05, n_channels=2),
         n_fft=n_fft, power=1)
     self.assert_grad(transform, [spec])
コード例 #11
0
 def test_inverse_spectrogram(self):
     # create a realistic input:
     waveform = get_whitenoise(sample_rate=8000,
                               duration=0.05,
                               n_channels=2)
     length = waveform.shape[-1]
     spectrogram = get_spectrogram(waveform, n_fft=400)
     # test
     inv_transform = T.InverseSpectrogram(n_fft=400)
     self.assert_grad(inv_transform, [spectrogram, length])
コード例 #12
0
 def test_griffinlim(self, momentum, rand_init):
     n_fft = 400
     power = 1
     n_iter = 3
     spec = get_spectrogram(
         get_whitenoise(sample_rate=8000, duration=0.05, n_channels=2),
         n_fft=n_fft, power=power)
     transform = _DeterministicWrapper(
         T.GriffinLim(n_fft=n_fft, n_iter=n_iter, momentum=momentum, rand_init=rand_init, power=power))
     self.assert_grad(transform, [spec])
コード例 #13
0
 def test_masking(self, masking_transform):
     sample_rate = 8000
     n_fft = 400
     spectrogram = get_spectrogram(get_whitenoise(sample_rate=sample_rate,
                                                  duration=0.05,
                                                  n_channels=2),
                                   n_fft=n_fft,
                                   power=1)
     deterministic_transform = _DeterministicWrapper(masking_transform(400))
     self.assert_grad(deterministic_transform, [spectrogram])
コード例 #14
0
    def test_amplitude_to_DB(self):
        amin = 1e-10
        db_multiplier = 0.0
        top_db = 80.0
        multiplier = 20.0

        spec = get_spectrogram(get_whitenoise(device=self.device, dtype=self.dtype), power=1)
        result = F.amplitude_to_DB(spec, multiplier, amin, db_multiplier, top_db)
        expected = librosa.core.amplitude_to_db(spec[0].cpu().numpy())[None, ...]
        self.assertEqual(result, torch.from_numpy(expected))
コード例 #15
0
    def test_timestretch_zeros_fail(self):
        """Test that ``T.TimeStretch`` fails gradcheck at 0

        This is because ``F.phase_vocoder`` converts data from cartesian to polar coordinate,
        which performs ``atan2(img, real)``, and gradient is not defined at 0.
        """
        n_fft = 16
        transform = T.TimeStretch(n_freq=n_fft // 2 + 1, fixed_rate=0.99)
        waveform = torch.zeros(2, 40)
        spectrogram = get_spectrogram(waveform, n_fft=n_fft, power=None)
        self.assert_grad(transform, [spectrogram])
コード例 #16
0
 def test_MVDR(self, solution, online):
     tensor = common_utils.get_whitenoise(sample_rate=8000, n_channels=4)
     spectrogram = common_utils.get_spectrogram(tensor,
                                                n_fft=400,
                                                hop_length=100)
     spectrogram = spectrogram.to(device=self.device, dtype=torch.cdouble)
     mask_s = torch.rand(spectrogram.shape[-2:], device=self.device)
     mask_n = torch.rand(spectrogram.shape[-2:], device=self.device)
     self._assert_consistency_complex(
         T.MVDR(solution=solution, online=online), spectrogram, mask_s,
         mask_n)
コード例 #17
0
    def test_psd_with_mask(self, multi_mask):
        transform = T.PSD(multi_mask=multi_mask)
        waveform = get_whitenoise(sample_rate=8000,
                                  duration=0.05,
                                  n_channels=2)
        spectrogram = get_spectrogram(waveform, n_fft=400)
        if multi_mask:
            mask = torch.rand(spectrogram.shape[-3:])
        else:
            mask = torch.rand(spectrogram.shape[-2:])

        self.assert_grad(transform, [spectrogram, mask])
コード例 #18
0
    def test_sliding_window_cmn(self, kwargs):
        n_fft = 10
        power = 1
        spec = get_spectrogram(get_whitenoise(sample_rate=200,
                                              duration=0.05,
                                              n_channels=2),
                               n_fft=n_fft,
                               power=power)
        spec_reshaped = spec.transpose(-1, -2)

        transform = T.SlidingWindowCmn(**kwargs)
        self.assert_grad(transform, [spec_reshaped])
コード例 #19
0
    def test_inverse_spectrogram(self):
        def func(tensor):
            length = 400
            n_fft = 400
            hop = 200
            ws = 400
            pad = 0
            window = torch.hann_window(ws, device=tensor.device, dtype=torch.float64)
            normalize = False
            return F.inverse_spectrogram(tensor, length, pad, window, n_fft, hop, ws, normalize)

        waveform = common_utils.get_whitenoise(sample_rate=8000, duration=0.05)
        tensor = common_utils.get_spectrogram(waveform, n_fft=400, hop_length=200)
        self._assert_consistency_complex(func, tensor)
コード例 #20
0
    def test_InverseMelScale(self):
        """Gauge the quality of InverseMelScale transform.

        As InverseMelScale is currently implemented with
        random initialization + iterative optimization,
        it is not practically possible to assert the difference between
        the estimated spectrogram and the original spectrogram as a whole.
        Estimated spectrogram has very huge descrepency locally.
        Thus in this test we gauge what percentage of elements are bellow
        certain tolerance.
        At the moment, the quality of estimated spectrogram is not good.
        When implementation is changed in a way it makes the quality even worse,
        this test will fail.
        """
        n_fft = 400
        power = 1
        n_mels = 64
        sample_rate = 8000

        n_stft = n_fft // 2 + 1

        # Generate reference spectrogram and input mel-scaled spectrogram
        expected = get_spectrogram(get_whitenoise(sample_rate=sample_rate,
                                                  duration=1,
                                                  n_channels=2),
                                   n_fft=n_fft,
                                   power=power).to(self.device, self.dtype)
        input = T.MelScale(n_mels=n_mels,
                           sample_rate=sample_rate).to(self.device,
                                                       self.dtype)(expected)

        # Run transform
        transform = T.InverseMelScale(n_stft,
                                      n_mels=n_mels,
                                      sample_rate=sample_rate).to(
                                          self.device, self.dtype)
        torch.random.manual_seed(0)
        result = transform(input)

        # Compare
        epsilon = 1e-60
        relative_diff = torch.abs((result - expected) / (expected + epsilon))

        for tol in [1e-1, 1e-3, 1e-5, 1e-10]:
            print(f"Ratio of relative diff smaller than {tol:e} is "
                  f"{_get_ratio(relative_diff < tol)}")
        assert _get_ratio(relative_diff < 1e-1) > 0.2
        assert _get_ratio(relative_diff < 1e-3) > 5e-3
        assert _get_ratio(relative_diff < 1e-5) > 1e-5
コード例 #21
0
    def test_masking_iid(self, masking_transform):
        sample_rate = 8000
        n_fft = 400
        specs = [
            get_spectrogram(get_whitenoise(sample_rate=sample_rate,
                                           duration=0.05,
                                           n_channels=2,
                                           seed=i),
                            n_fft=n_fft,
                            power=1) for i in range(3)
        ]

        batch = torch.stack(specs)
        assert batch.ndim == 4
        deterministic_transform = _DeterministicWrapper(
            masking_transform(400, True))
        self.assert_grad(deterministic_transform, [batch])
コード例 #22
0
    def test_TimeStretch(self):
        n_fft = 1025
        n_freq = n_fft // 2 + 1
        hop_length = 512
        fixed_rate = 1.3
        tensor = torch.rand((10, 2, n_freq, 10), dtype=torch.cfloat)
        batch = 10
        num_channels = 2

        waveform = common_utils.get_whitenoise(sample_rate=8000,
                                               n_channels=batch * num_channels)
        tensor = common_utils.get_spectrogram(waveform, n_fft=n_fft)
        tensor = tensor.reshape(batch, num_channels, n_freq, -1)
        self._assert_consistency_complex(
            T.TimeStretch(n_freq=n_freq,
                          hop_length=hop_length,
                          fixed_rate=fixed_rate),
            tensor,
        )
コード例 #23
0
ファイル: transforms_test_impl.py プロジェクト: pytorch/audio
 def test_psd(self, duration, channel, mask, multi_mask):
     """Providing dtype changes the kernel cache dtype"""
     transform = T.PSD(multi_mask)
     waveform = get_whitenoise(sample_rate=8000,
                               duration=duration,
                               n_channels=channel)
     spectrogram = get_spectrogram(waveform,
                                   n_fft=400)  # (channel, freq, time)
     spectrogram = spectrogram.to(torch.cdouble)
     if mask is not None:
         if multi_mask:
             mask = torch.rand(spectrogram.shape[-3:])
         else:
             mask = torch.rand(spectrogram.shape[-2:])
         psd_np = psd_numpy(spectrogram.detach().numpy(),
                            mask.detach().numpy(), multi_mask)
     else:
         psd_np = psd_numpy(spectrogram.detach().numpy(), mask, multi_mask)
     psd = transform(spectrogram, mask)
     self.assertEqual(psd, psd_np, atol=1e-5, rtol=1e-5)