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_time_stretch.py 5.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. """
  16. Testing TimeStretch 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. CHANNEL_NUM = 2
  24. FREQ = 1025
  25. FRAME_NUM = 300
  26. COMPLEX = 2
  27. def gen(shape):
  28. np.random.seed(0)
  29. data = np.random.random(shape)
  30. yield (np.array(data, dtype=np.float32),)
  31. def count_unequal_element(data_expected, data_me, rtol, atol):
  32. assert data_expected.shape == data_me.shape
  33. total_count = len(data_expected.flatten())
  34. error = np.abs(data_expected - data_me)
  35. greater = np.greater(error, atol + np.abs(data_expected) * rtol)
  36. loss_count = np.count_nonzero(greater)
  37. assert (loss_count / total_count) < rtol, "\ndata_expected_std:{0}\ndata_me_error:{1}\nloss:{2}".format(
  38. data_expected[greater], data_me[greater], error[greater])
  39. def allclose_nparray(data_expected, data_me, rtol, atol, equal_nan=True):
  40. if np.any(np.isnan(data_expected)):
  41. assert np.allclose(data_me, data_expected, rtol, atol, equal_nan=equal_nan)
  42. elif not np.allclose(data_me, data_expected, rtol, atol, equal_nan=equal_nan):
  43. count_unequal_element(data_expected, data_me, rtol, atol)
  44. def test_time_stretch_pipeline():
  45. """
  46. Test TimeStretch op. Pipeline.
  47. """
  48. logger.info("test TimeStretch op")
  49. generator = gen([CHANNEL_NUM, FREQ, FRAME_NUM, COMPLEX])
  50. data1 = ds.GeneratorDataset(source=generator, column_names=["multi_dimensional_data"])
  51. transforms = [c_audio.TimeStretch(512, FREQ, 1.3)]
  52. data1 = data1.map(operations=transforms, input_columns=["multi_dimensional_data"])
  53. for item in data1.create_dict_iterator(num_epochs=1, output_numpy=True):
  54. out_put = item["multi_dimensional_data"]
  55. assert out_put.shape == (CHANNEL_NUM, FREQ, np.ceil(FRAME_NUM / 1.3), COMPLEX)
  56. def test_time_stretch_pipeline_invalid_param():
  57. """
  58. Test TimeStretch op. Set invalid param. Pipeline.
  59. """
  60. logger.info("test TimeStretch op with invalid values")
  61. generator = gen([CHANNEL_NUM, FREQ, FRAME_NUM, COMPLEX])
  62. data1 = ds.GeneratorDataset(source=generator, column_names=["multi_dimensional_data"])
  63. with pytest.raises(ValueError, match=r"Input fixed_rate is not within the required interval of \(0, 16777216\]."):
  64. transforms = [c_audio.TimeStretch(512, FREQ, -1.3)]
  65. data1 = data1.map(operations=transforms, input_columns=["multi_dimensional_data"])
  66. for item in data1.create_dict_iterator(num_epochs=1, output_numpy=True):
  67. out_put = item["multi_dimensional_data"]
  68. assert out_put.shape == (CHANNEL_NUM, FREQ, np.ceil(FRAME_NUM / 1.3), COMPLEX)
  69. def test_time_stretch_eager():
  70. """
  71. Test TimeStretch op. Set param. Eager.
  72. """
  73. logger.info("test TimeStretch op with customized parameter values")
  74. spectrogram = next(gen([CHANNEL_NUM, FREQ, FRAME_NUM, COMPLEX]))[0]
  75. out_put = c_audio.TimeStretch(512, FREQ, 1.3)(spectrogram)
  76. assert out_put.shape == (CHANNEL_NUM, FREQ, np.ceil(FRAME_NUM / 1.3), COMPLEX)
  77. def test_percision_time_stretch_eager():
  78. """
  79. Test TimeStretch op. Compare precision. Eager.
  80. """
  81. logger.info("test TimeStretch op with default values")
  82. spectrogram = np.array([[[[1.0402449369430542, 0.3807601034641266],
  83. [-1.120057225227356, -0.12819576263427734],
  84. [1.4303032159805298, -0.08839055150747299]],
  85. [[1.4198592901229858, 0.6900091767311096],
  86. [-1.8593409061431885, 0.16363371908664703],
  87. [-2.3349387645721436, -1.4366451501846313]]],
  88. [[[-0.7083967328071594, 0.9325454831123352],
  89. [-1.9133838415145874, 0.011225821450352669],
  90. [1.477278232574463, -1.0551637411117554]],
  91. [[-0.6668586134910583, -0.23143270611763],
  92. [-2.4390718936920166, 0.17638640105724335],
  93. [-0.4795735776424408, 0.1345423310995102]]]]).astype(np.float64)
  94. out_expect = np.array([[[[1.0402449369430542, 0.3807601034641266],
  95. [-1.302264928817749, -0.1490504890680313]],
  96. [[1.4198592901229858, 0.6900091767311096],
  97. [-2.382312774658203, 0.2096325159072876]]],
  98. [[[-0.7083966732025146, 0.9325454831123352],
  99. [-1.8545820713043213, 0.010880803689360619]],
  100. [[-0.6668586134910583, -0.23143276572227478],
  101. [-1.2737033367156982, 0.09211209416389465]]]]).astype(np.float64)
  102. out_ms = c_audio.TimeStretch(64, 2, 1.6)(spectrogram)
  103. allclose_nparray(out_ms, out_expect, 0.001, 0.001)
  104. if __name__ == '__main__':
  105. test_time_stretch_pipeline()
  106. test_time_stretch_pipeline_invalid_param()
  107. test_time_stretch_eager()
  108. test_percision_time_stretch_eager()