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_sliding_window_cmn.py 7.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. # Copyright 2021 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. import numpy as np
  16. import pytest
  17. import mindspore.dataset as ds
  18. import mindspore.dataset.audio.transforms as audio
  19. from mindspore import log as logger
  20. def count_unequal_element(data_expected, data_me, rtol, atol):
  21. assert data_expected.shape == data_me.shape
  22. total_count = len(data_expected.flatten())
  23. error = np.abs(data_expected - data_me)
  24. greater = np.greater(error, atol + np.abs(data_expected) * rtol)
  25. loss_count = np.count_nonzero(greater)
  26. assert (loss_count / total_count) < rtol, \
  27. "\ndata_expected_std:{0}\ndata_me_error:{1}\nloss:{2}". \
  28. format(data_expected[greater], data_me[greater], error[greater])
  29. def test_sliding_window_cmn_eager():
  30. """
  31. Feature: test the basic function in eager mode.
  32. Description: mindspore eager mode normal testcase:sliding_window_cmn op.
  33. Expectation: compile done without error.
  34. """
  35. # Original waveform
  36. waveform_1 = np.array([[[0.0000, 0.1000, 0.2000],
  37. [0.3000, 0.4000, 0.5000]],
  38. [[0.6000, 0.7000, 0.8000],
  39. [0.9000, 1.0000, 1.1000]]], dtype=np.float64)
  40. # Expect waveform
  41. expect_waveform_1 = np.array([[[-0.1500, -0.1500, -0.1500],
  42. [0.1500, 0.1500, 0.1500]],
  43. [[-0.1500, -0.1500, -0.1500],
  44. [0.1500, 0.1500, 0.1500]]], dtype=np.float64)
  45. sliding_window_cmn_op_1 = audio.SlidingWindowCmn(500, 200, False, False)
  46. # Filtered waveform by sliding_window_cmn
  47. output_1 = sliding_window_cmn_op_1(waveform_1)
  48. count_unequal_element(expect_waveform_1, output_1, 0.0001, 0.0001)
  49. # Original waveform
  50. waveform_2 = np.array([[0.0050, 0.0306, 0.6146, 0.7620, 0.6369],
  51. [0.9525, 0.0362, 0.6721, 0.6867, 0.8466]], dtype=np.float32)
  52. # Expect waveform
  53. expect_waveform_2 = np.array([[-1.0000, -1.0000, -1.0000, 1.0000, -1.0000],
  54. [1.0000, 1.0000, 1.0000, -1.0000, 1.0000]], dtype=np.float32)
  55. sliding_window_cmn_op_2 = audio.SlidingWindowCmn(600, 100, False, True)
  56. # Filtered waveform by sliding_window_cmn
  57. output_2 = sliding_window_cmn_op_2(waveform_2)
  58. count_unequal_element(expect_waveform_2, output_2, 0.0001, 0.0001)
  59. # Original waveform
  60. waveform_3 = np.array([[[0.3764, 0.4168, 0.0635, 0.7082, 0.4596, 0.3457, 0.8438, 0.8860, 0.9151, 0.5746,
  61. 0.6630, 0.0260, 0.2631, 0.7410, 0.5627, 0.6749, 0.7099, 0.1120, 0.4794, 0.2778],
  62. [0.4157, 0.2246, 0.2488, 0.2686, 0.0562, 0.4422, 0.9407, 0.0756, 0.5737, 0.7501,
  63. 0.3122, 0.7982, 0.3034, 0.1880, 0.2298, 0.0961, 0.7439, 0.9947, 0.8156, 0.2907]]],
  64. dtype=np.float64)
  65. # Expect waveform
  66. expect_waveform_3 = np.array([[[-1.0000, 1.0000, -1.0000, 1.0000, 1.0000, -1.0000, -1.0000, 1.0000,
  67. 1.0000, -1.0000, 1.0000, -1.0000, -1.0000, 1.0000, 1.0000, 1.0000,
  68. -1.0000, -1.0000, -1.0000, -1.0000],
  69. [1.0000, -1.0000, 1.0000, -1.0000, -1.0000, 1.0000, 1.0000, -1.0000,
  70. -1.0000, 1.0000, -1.0000, 1.0000, 1.0000, -1.0000, -1.0000, -1.0000,
  71. 1.0000, 1.0000, 1.0000, 1.0000]]], dtype=np.float64)
  72. sliding_window_cmn_op_3 = audio.SlidingWindowCmn(3, 0, True, True)
  73. # Filtered waveform by sliding_window_cmn
  74. output_3 = sliding_window_cmn_op_3(waveform_3)
  75. count_unequal_element(expect_waveform_3, output_3, 0.0001, 0.0001)
  76. def test_sliding_window_cmn_pipeline():
  77. """
  78. Feature: test the basic function in pipeline mode.
  79. Description: mindspore pipeline mode normal testcase:sliding_window_cmn op.
  80. Expectation: compile done without error.
  81. """
  82. # Original waveform
  83. waveform = np.array([[[3.2, 2.1, 1.3], [6.2, 5.3, 6]]], dtype=np.float64)
  84. # Expect waveform
  85. expect_waveform = np.array([[[-1.0000, -1.0000, -1.0000],
  86. [1.0000, 1.0000, 1.0000]]], dtype=np.float64)
  87. dataset = ds.NumpySlicesDataset(waveform, ["audio"], shuffle=False)
  88. sliding_window_cmn_op = audio.SlidingWindowCmn(600, 100, False, True)
  89. # Filtered waveform by sliding_window_cmn
  90. dataset = dataset.map(input_columns=["audio"], operations=sliding_window_cmn_op, num_parallel_workers=8)
  91. i = 0
  92. for item in dataset.create_dict_iterator(num_epochs=1, output_numpy=True):
  93. count_unequal_element(expect_waveform[i, :],
  94. item['audio'], 0.0001, 0.0001)
  95. i += 1
  96. def test_sliding_window_cmn_invalid_input():
  97. """
  98. Feature: test the validate function with invalid parameters.
  99. Description: mindspore invalid parameters testcase:sliding_window_cmn op.
  100. Expectation: compile done without error.
  101. """
  102. def test_invalid_input(test_name, cmn_window, min_cmn_window, center, norm_vars, error, error_msg):
  103. logger.info("Test SlidingWindowCmn with bad input: {0}".format(test_name))
  104. with pytest.raises(error) as error_info:
  105. audio.SlidingWindowCmn(cmn_window, min_cmn_window, center, norm_vars)
  106. assert error_msg in str(error_info.value)
  107. test_invalid_input("invalid cmn_window parameter type as a String", "600", 100, False, False, TypeError,
  108. "Argument cmn_window with value 600 is not of type [<class 'int'>],"
  109. " but got <class 'str'>.")
  110. test_invalid_input("invalid cmn_window parameter value", 441324343243242342345300, 100, False, False, ValueError,
  111. "Input cmn_window is not within the required interval of [0, 2147483647].")
  112. test_invalid_input("invalid min_cmn_window parameter type as a String", 600, "100", False, False, TypeError,
  113. "Argument min_cmn_window with value 100 is not of type [<class 'int'>],"
  114. " but got <class 'str'>.")
  115. test_invalid_input("invalid min_cmn_window parameter value", 600, 441324343243242342345300, False, False,
  116. ValueError, "Input min_cmn_window is not within the required interval of [0, 2147483647].")
  117. test_invalid_input("invalid center parameter type as a String", 600, 100, "False", False, TypeError,
  118. "Argument center with value False is not of type [<class 'bool'>],"
  119. " but got <class 'str'>.")
  120. test_invalid_input("invalid norm_vars parameter type as a String", 600, 100, False, "False", TypeError,
  121. "Argument norm_vars with value False is not of type [<class 'bool'>],"
  122. " but got <class 'str'>.")
  123. if __name__ == '__main__':
  124. test_sliding_window_cmn_eager()
  125. test_sliding_window_cmn_pipeline()
  126. test_sliding_window_cmn_invalid_input()