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_phase_vocoder.py 7.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. 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 gen(shape):
  21. np.random.seed(0)
  22. data = np.random.random(shape)
  23. yield (np.array(data, dtype=np.float32),)
  24. def count_unequal_element(data_expected, data_me, rtol, atol):
  25. assert data_expected.shape == data_me.shape
  26. total_count = len(data_expected.flatten())
  27. error = np.abs(data_expected - data_me)
  28. greater = np.greater(error, atol + np.abs(data_expected) * rtol)
  29. loss_count = np.count_nonzero(greater)
  30. assert (loss_count / total_count) < rtol, \
  31. "\ndata_expected_std:{0}\ndata_me_error:{1}\nloss:{2}". \
  32. format(data_expected[greater], data_me[greater], error[greater])
  33. def allclose_nparray(data_expected, data_me, rtol, atol, equal_nan=True):
  34. if np.any(np.isnan(data_expected)):
  35. assert np.allclose(data_me, data_expected, rtol, atol, equal_nan=equal_nan)
  36. elif not np.allclose(data_me, data_expected, rtol, atol, equal_nan=equal_nan):
  37. count_unequal_element(data_expected, data_me, rtol, atol)
  38. def test_phase_vocoder_compare():
  39. """
  40. Feature: PhaseVocoder
  41. Description: mindspore eager mode checking precision
  42. Expectation: the returned result is as expected
  43. """
  44. indata_0 = np.array([[[[0.43189, 2.3049924],
  45. [-0.01202229, 0.9176453],
  46. [-0.6258611, 0.66475236],
  47. [0.13541847, 1.2829605],
  48. [0.9725325, 1.1669061]],
  49. [[-0.35001752, -1.0989336],
  50. [-1.4930767, 0.86829656],
  51. [0.3355314, -0.41216415],
  52. [-1.1828239, 1.0075365],
  53. [-0.19343425, 0.38364533]]]]).astype('float32')
  54. indata_1 = np.array([[[[0.43189, 2.3049924],
  55. [-0.01202229, 0.9176453],
  56. [-0.6258611, 0.66475236],
  57. [0.13541847, 1.2829605],
  58. [0.9725325, 1.1669061]],
  59. [[-0.35001752, -1.0989336],
  60. [-1.4930767, 0.86829656],
  61. [0.3355314, -0.41216415],
  62. [-1.1828239, 1.0075365],
  63. [-0.19343425, 0.38364533]]]]).astype('float64')
  64. rate = 2.
  65. phase_advance_0 = np.array([[0.0000], [3.9270]]).astype('float32')
  66. op_0 = audio.PhaseVocoder(rate, phase_advance_0)
  67. phase_advance_1 = np.array([[0.0000], [3.9270]]).astype('float64')
  68. op_1 = audio.PhaseVocoder(rate, phase_advance_1)
  69. outdata_0 = op_0(indata_0)
  70. outdata_1 = op_1(indata_1)
  71. stand_outdata = np.array([[[[0.43189007, 2.3049924],
  72. [-0.01196056, 0.9129374],
  73. [1.1385509, 1.00558]],
  74. [[-0.35001755, -1.0989336],
  75. [-0.4594292, 0.26718047],
  76. [0.404371, -0.14520557]]]]).astype('float32')
  77. allclose_nparray(outdata_0, stand_outdata, 0.0001, 0.0001)
  78. allclose_nparray(outdata_1, stand_outdata, 0.0001, 0.0001)
  79. def test_phase_vocoder_eager():
  80. """
  81. Feature: PhaseVocoder
  82. Description: mindspore eager mode with normal testcase
  83. Expectation: the returned result is as expected
  84. """
  85. logger.info("test PhaseVocoder op in eager mode")
  86. stft = next(gen([10, 10, 10, 2]))[0]
  87. out_put = audio.PhaseVocoder(1.3, np.random.randn(10, 1).astype('float32'))(stft)
  88. assert out_put.shape == (10, 10, 8, 2)
  89. def test_phase_vocoder_pipeline():
  90. """
  91. Feature: PhaseVocoder
  92. Description: mindspore pipeline mode with normal testcase
  93. Expectation: the returned result is as expected
  94. """
  95. logger.info("test PhaseVocoder op in pipeline mode")
  96. generator = gen([32, 33, 333, 2])
  97. data1 = ds.GeneratorDataset(source=generator, column_names=["input"])
  98. transforms = [audio.PhaseVocoder(0.8, np.random.randn(33, 1).astype('float32'))]
  99. data1 = data1.map(operations=transforms, input_columns=["input"])
  100. for item in data1.create_dict_iterator(num_epochs=1, output_numpy=True):
  101. out_put = item["input"]
  102. assert out_put.shape == (32, 33, 417, 2)
  103. def test_phase_vocoder_invalid_input():
  104. """
  105. Feature: PhaseVocoder
  106. Description: mindspore eager mode with invalid input
  107. Expectation: the returned result is as expected
  108. """
  109. def test_invalid_param(test_name, rate, phase_advance, error, error_msg):
  110. logger.info("Test PhaseVocoder with wrong params: {0}".format(test_name))
  111. with pytest.raises(error) as error_info:
  112. _ = audio.PhaseVocoder(rate, phase_advance)
  113. assert error_msg in str(error_info.value)
  114. def test_invalid_input(test_name, spec, rate, phase_advance, error, error_msg):
  115. logger.info("Test PhaseVocoder with wrong params: {0}".format(test_name))
  116. with pytest.raises(error) as error_info:
  117. _ = audio.PhaseVocoder(rate, phase_advance)(spec)
  118. assert error_msg in str(error_info.value)
  119. test_invalid_param("invalid phase_advance", 2, None, TypeError,
  120. "Argument phase_advance with value None is not of type")
  121. test_invalid_param("invalid phase_advance", 0, np.random.randn(4, 1), ValueError,
  122. "Input rate is not within the required interval of (0, 16777216].")
  123. spec = next(gen([1, 2, 2]))[0]
  124. test_invalid_input("invalid phase_advance", spec, 1.23, np.random.randn(4), RuntimeError,
  125. "PhaseVocoder: invalid parameter, 'phase_advance' should be in shape of <freq, 1>.")
  126. test_invalid_input("invalid phase_advance", spec, 1.1, np.random.randn(4, 4, 1), RuntimeError,
  127. "PhaseVocoder: invalid parameter, 'phase_advance' should be in shape of <freq, 1>.")
  128. test_invalid_input("invalid input tensor", spec, 2, np.random.randn(3, 1), RuntimeError,
  129. "PhaseVocoder: invalid parameter, 'first dimension of 'phase_advance'' should be equal")
  130. input_tensor = np.random.randn(4, 4, 2).astype('float32')
  131. input_phase_advance = np.random.randn(4, 1).astype('float64')
  132. test_invalid_input("invalid input tensor", input_tensor, 2, input_phase_advance, RuntimeError,
  133. "PhaseVocoder: invalid parameter, data type of phase_advance should be equal to data")
  134. if __name__ == "__main__":
  135. test_phase_vocoder_compare()
  136. test_phase_vocoder_eager()
  137. test_phase_vocoder_pipeline()
  138. test_phase_vocoder_invalid_input()