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

test_mel_scale.py 8.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. # Copyright 2022 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ==============================================================================
  15. """
  16. Testing MelScale op in DE
  17. """
  18. import numpy as np
  19. import pytest
  20. import mindspore.dataset as ds
  21. import mindspore.dataset.audio.transforms as c_audio
  22. from mindspore import log as logger
  23. from mindspore.dataset.audio.utils import MelType, NormType
  24. CHANNEL = 1
  25. FREQ = 20
  26. TIME = 15
  27. DEFAULT_N_MELS = 128
  28. def gen(shape, dtype=np.float32):
  29. np.random.seed(0)
  30. data = np.random.random(shape)
  31. yield (np.array(data, dtype=dtype),)
  32. def count_unequal_element(data_expected, data_me, rtol, atol):
  33. assert data_expected.shape == data_me.shape
  34. total_count = len(data_expected.flatten())
  35. error = np.abs(data_expected - data_me)
  36. greater = np.greater(error, atol + np.abs(data_expected) * rtol)
  37. loss_count = np.count_nonzero(greater)
  38. assert (loss_count / total_count) < rtol, \
  39. "\ndata_expected_std:{0}\ndata_me_error:{1}\nloss:{2}". \
  40. format(data_expected[greater], data_me[greater], error[greater])
  41. def allclose_nparray(data_expected, data_me, rtol, atol, equal_nan=True):
  42. if np.any(np.isnan(data_expected)):
  43. assert np.allclose(data_me, data_expected, rtol, atol, equal_nan=equal_nan)
  44. elif not np.allclose(data_me, data_expected, rtol, atol, equal_nan=equal_nan):
  45. count_unequal_element(data_expected, data_me, rtol, atol)
  46. def test_mel_scale_pipeline():
  47. """
  48. Feature: MelScale
  49. Description: test MelScale cpp op in pipeline
  50. Expectation: equal results from Mindspore and benchmark
  51. """
  52. in_data = np.array([[[[-0.34207549691200256, -2.0971477031707764, -0.9462487101554871],
  53. [1.2536851167678833, -1.3225716352462769, -0.06942684203386307],
  54. [-0.4859708547592163, -0.4990693926811218, 0.2322249710559845],
  55. [-0.7589328289031982, -2.218672513961792, -0.8374152779579163]],
  56. [[1.0313602685928345, -1.5596215724945068, 0.46823829412460327],
  57. [0.14756731688976288, 0.35987502336502075, -1.3228634595870972],
  58. [-0.7677955627441406, -0.059919968247413635, 0.7958201766014099],
  59. [-0.6194286942481995, -0.5878928899765015, 0.3874965310096741]]]]).astype(np.float32)
  60. out_expect = np.array([[[-0.24386560916900635, -5.417530059814453, -1.4391992092132568],
  61. [-0.08942853659391403, -0.7199308276176453, -0.18166661262512207]],
  62. [[-0.0856514573097229, -1.6701887845993042, 0.25840121507644653],
  63. [-0.12264516949653625, -0.1773705929517746, 0.07029043138027191]]]).astype(np.float32)
  64. dataset = ds.NumpySlicesDataset(in_data, column_names=["multi_dimensional_data"], shuffle=False)
  65. transforms = [c_audio.MelScale(n_mels=2, sample_rate=10, f_min=-50, f_max=100, n_stft=4)]
  66. dataset = dataset.map(operations=transforms, input_columns=["multi_dimensional_data"])
  67. for item in dataset.create_dict_iterator(num_epochs=1, output_numpy=True):
  68. out_put = item["multi_dimensional_data"]
  69. assert out_put.shape == (2, 2, 3)
  70. allclose_nparray(out_put, out_expect, 0.001, 0.001)
  71. def test_mel_scale_pipeline_invalid_param():
  72. """
  73. Feature: MelScale
  74. Description: test MelScale with invalid input parameters
  75. Expectation: throw ValueError or TypeError
  76. """
  77. logger.info("test MelScale op with default values")
  78. generator = gen([CHANNEL, FREQ, TIME])
  79. data1 = ds.GeneratorDataset(source=generator, column_names=["multi_dimensional_data"])
  80. with pytest.raises(ValueError, match="MelScale: f_max should be greater than f_min."):
  81. transforms = [c_audio.MelScale(n_mels=128, sample_rate=16200, f_min=1000, f_max=1000)]
  82. data1 = data1.map(operations=transforms, input_columns=["multi_dimensional_data"])
  83. for item in data1.create_dict_iterator(num_epochs=1, output_numpy=True):
  84. _ = item["multi_dimensional_data"]
  85. with pytest.raises(ValueError, match=r"Input n_mels is not within the required interval of \[1, 2147483647\]."):
  86. transforms = [c_audio.MelScale(n_mels=-1, sample_rate=16200, f_min=10, f_max=1000)]
  87. data1 = data1.map(operations=transforms, input_columns=["multi_dimensional_data"])
  88. with pytest.raises(ValueError,
  89. match=r"Input sample_rate is not within the required interval of \[1, 2147483647\]."):
  90. transforms = [c_audio.MelScale(n_mels=128, sample_rate=0, f_min=10, f_max=1000)]
  91. data1 = data1.map(operations=transforms, input_columns=["multi_dimensional_data"])
  92. with pytest.raises(ValueError, match=r"Input f_max is not within the required interval of \(0, 16777216\]."):
  93. transforms = [c_audio.MelScale(n_mels=128, sample_rate=16200, f_min=10, f_max=-10)]
  94. data1 = data1.map(operations=transforms, input_columns=["multi_dimensional_data"])
  95. with pytest.raises(TypeError, match=r"Argument norm with value slaney is not of type \[<enum 'NormType'>\], " +
  96. "but got <class 'str'>."):
  97. transforms = [c_audio.MelScale(n_mels=128, sample_rate=16200, f_min=10,
  98. f_max=1000, norm="slaney", mel_type=MelType.SLANEY)]
  99. data1 = data1.map(operations=transforms, input_columns=["multi_dimensional_data"])
  100. with pytest.raises(TypeError, match=r"Argument mel_type with value SLANEY is not of type \[<enum 'MelType'>\], " +
  101. "but got <class 'str'>."):
  102. transforms = [c_audio.MelScale(n_mels=128, sample_rate=16200, f_min=10, f_max=1000,
  103. norm=NormType.NONE, mel_type="SLANEY")]
  104. data1 = data1.map(operations=transforms, input_columns=["multi_dimensional_data"])
  105. def test_mel_scale_eager():
  106. """
  107. Feature: MelScale
  108. Description: test MelScale cpp op with eage mode
  109. Expectation: equal results from Mindspore and benchmark
  110. """
  111. spectrogram = np.array([[[-0.7010437250137329, 1.1184569597244263, -1.4936821460723877],
  112. [0.4603022038936615, -0.556514322757721, 0.8629537224769592]],
  113. [[0.41759368777275085, 1.0594186782836914, -0.07423319667577744],
  114. [0.47624683380126953, -0.33720797300338745, 2.0135815143585205]],
  115. [[-0.6765501499176025, 0.8924005031585693, 1.0404413938522339],
  116. [-0.5578446984291077, -0.349029004573822, 0.0370720773935318]]])
  117. spectrogram = spectrogram.astype(np.float32)
  118. out_ms = c_audio.MelScale(n_mels=2, sample_rate=10, f_min=-50, f_max=100, n_stft=2)(spectrogram)
  119. out_expect = np.array([[[-0.27036190032958984, 0.579207181930542, -0.6739760637283325],
  120. [0.029620330780744553, -0.017264455556869507, 0.043247632682323456]],
  121. [[0.7849390506744385, 0.706536054611206, 1.6048823595046997],
  122. [0.10890152305364609, 0.01567467674612999, 0.33446595072746277]],
  123. [[-1.0940029621124268, 0.5411258339881897, 1.000023603439331],
  124. [-0.14039191603660583, 0.002245672047138214, 0.07748986035585403]]]).astype(np.float32)
  125. allclose_nparray(out_ms, out_expect, 0.001, 0.001)
  126. assert out_ms.shape == (3, 2, 3)
  127. if __name__ == "__main__":
  128. test_mel_scale_pipeline()
  129. test_mel_scale_pipeline_invalid_param()
  130. test_mel_scale_eager()