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_contrast.py 4.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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, "\ndata_expected_std:{0}\ndata_me_error:{1}\nloss:{2}".format(
  27. data_expected[greater], data_me[greater], error[greater])
  28. def test_func_contrast_eager():
  29. """ mindspore eager mode normal testcase:contrast op"""
  30. # Original waveform
  31. waveform = np.array([[1, 2], [3, 4]], dtype=np.float32)
  32. # Expect waveform
  33. expect_waveform = np.array([[1., -8.742277e-08],
  34. [-1., 1.748455e-07]],
  35. dtype=np.float32)
  36. contrast_op = audio.Contrast(75.0)
  37. # Filtered waveform by contrast
  38. output = contrast_op(waveform)
  39. count_unequal_element(expect_waveform, output, 0.0001, 0.0001)
  40. def test_func_contrast_pipeline():
  41. """ mindspore pipeline mode normal testcase:contrast op"""
  42. # Original waveform
  43. waveform = np.array([[0.4941969, 0.53911686, 0.4846254], [0.10841596, 0.029320478, 0.52353495],
  44. [0.23657, 0.087965, 0.43579]], dtype=np.float64)
  45. # Expect waveform
  46. expect_waveform = np.array([[7.032282948493957520e-01, 7.328570485115051270e-01, 6.967759728431701660e-01],
  47. [2.311619222164154053e-01, 6.433061510324478149e-02, 7.226532697677612305e-01],
  48. [4.539981484413146973e-01, 1.895205676555633545e-01, 6.622338891029357910e-01]],
  49. dtype=np.float64)
  50. dataset = ds.NumpySlicesDataset(waveform, ["audio"], shuffle=False)
  51. contrast_op = audio.Contrast()
  52. # Filtered waveform by contrast
  53. dataset = dataset.map(input_columns=["audio"], operations=contrast_op, num_parallel_workers=8)
  54. i = 0
  55. for item in dataset.create_dict_iterator(num_epochs=1, output_numpy=True):
  56. count_unequal_element(expect_waveform[i, :], item['audio'], 0.0001, 0.0001)
  57. i += 1
  58. def test_contrast_invalid_input():
  59. def test_invalid_input(test_name, enhancement_amount, error, error_msg):
  60. logger.info("Test Contrast with bad input: {0}".format(test_name))
  61. with pytest.raises(error) as error_info:
  62. audio.Contrast(enhancement_amount)
  63. assert error_msg in str(error_info.value)
  64. test_invalid_input("invalid enhancement_amount parameter type as a String", "75.0", TypeError,
  65. "Argument enhancement_amount with value 75.0 is not of type [<class 'float'>, <class 'int'>],"
  66. + " but got <class 'str'>.")
  67. test_invalid_input("invalid enhancement_amount parameter value", -1, ValueError,
  68. "Input enhancement_amount is not within the required interval of [0, 100].")
  69. test_invalid_input("invalid enhancement_amount parameter value", 101, ValueError,
  70. "Input enhancement_amount is not within the required interval of [0, 100].")
  71. if __name__ == "__main__":
  72. test_func_contrast_eager()
  73. test_func_contrast_pipeline()
  74. test_contrast_invalid_input()