You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.3 KiB

2 years ago
  1. #!/usr/bin/env python
  2. # coding: utf8
  3. """ Unit testing for Separator class. """
  4. __email__ = 'spleeter@deezer.com'
  5. __author__ = 'Deezer Research'
  6. __license__ = 'MIT License'
  7. from os import makedirs
  8. from os.path import join
  9. from tempfile import TemporaryDirectory
  10. import pytest
  11. import numpy as np
  12. from spleeter.__main__ import evaluate
  13. from spleeter.audio.adapter import AudioAdapter
  14. BACKENDS = ['tensorflow', 'librosa']
  15. TEST_CONFIGURATIONS = {el: el for el in BACKENDS}
  16. res_4stems = {
  17. 'vocals': {
  18. 'SDR': 3.25e-05,
  19. 'SAR': -11.153575,
  20. 'SIR': -1.3849,
  21. 'ISR': 2.75e-05
  22. },
  23. 'drums': {
  24. 'SDR': -0.079505,
  25. 'SAR': -15.7073575,
  26. 'SIR': -4.972755,
  27. 'ISR': 0.0013575
  28. },
  29. 'bass': {
  30. 'SDR': 2.5e-06,
  31. 'SAR': -10.3520575,
  32. 'SIR': -4.272325,
  33. 'ISR': 2.5e-06
  34. },
  35. 'other': {
  36. 'SDR': -1.359175,
  37. 'SAR': -14.7076775,
  38. 'SIR': -4.761505,
  39. 'ISR': -0.01528
  40. }
  41. }
  42. def generate_fake_eval_dataset(path):
  43. """
  44. generate fake evaluation dataset
  45. """
  46. aa = AudioAdapter.default()
  47. n_songs = 2
  48. fs = 44100
  49. duration = 3
  50. n_channels = 2
  51. rng = np.random.RandomState(seed=0)
  52. for song in range(n_songs):
  53. song_path = join(path, 'test', f'song{song}')
  54. makedirs(song_path, exist_ok=True)
  55. for instr in ['mixture', 'vocals', 'bass', 'drums', 'other']:
  56. filename = join(song_path, f'{instr}.wav')
  57. data = rng.rand(duration*fs, n_channels)-0.5
  58. aa.save(filename, data, fs)
  59. @pytest.mark.parametrize('backend', TEST_CONFIGURATIONS)
  60. def test_evaluate(backend):
  61. with TemporaryDirectory() as dataset:
  62. with TemporaryDirectory() as evaluation:
  63. generate_fake_eval_dataset(dataset)
  64. metrics = evaluate(
  65. adapter='spleeter.audio.ffmpeg.FFMPEGProcessAudioAdapter',
  66. output_path=evaluation,
  67. stft_backend=backend,
  68. params_filename='spleeter:4stems',
  69. mus_dir=dataset,
  70. mwf=False,
  71. verbose=False)
  72. for instrument, metric in metrics.items():
  73. for m, value in metric.items():
  74. assert np.allclose(
  75. np.median(value),
  76. res_4stems[instrument][m],
  77. atol=1e-3)