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_compute_deltas.py 5.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 ComputeDeltas 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 BorderType
  24. CHANNEL = 1
  25. FREQ = 20
  26. TIME = 15
  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. """ Precision calculation func """
  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, "\ndata_expected_std:{0}\ndata_me_error:{1}\nloss:{2}".format(
  39. data_expected[greater], data_me[greater], error[greater])
  40. def allclose_nparray(data_expected, data_me, rtol, atol, equal_nan=True):
  41. """ Precision calculation formula """
  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_compute_deltas_eager():
  47. """
  48. Feature: test the basic function in eager mode.
  49. Description: mindspore eager mode normal testcase:compute_deltas op.
  50. Expectation: compile done without error.
  51. """
  52. logger.info("check compute_deltas op output")
  53. ndarr_in = np.array([[[0.08746047, -0.33246294, 0.5240939, 0.6064913, -0.70366],
  54. [1.1420338, 0.50532603, 0.73435473, -0.83435977, -1.0607501],
  55. [-1.4298731, -0.86117035, -0.7773941, -0.60023546, 1.1807907],
  56. [0.4973711, 0.5299286, 0.818514, 0.7559297, -0.3418539],
  57. [-0.2824797, 0.30402678, 0.7848569, -0.4135576, 0.19522846],
  58. [-0.11636204, -0.4780833, 1.2691815, 0.9824286, 0.029275],
  59. [-1.2611166, -1.1957082, 0.26212585, 0.35354254, 0.3609486]]]).astype(np.float32)
  60. out_expect = np.array([[[0.0453, 0.1475, -0.0643, -0.1970, -0.3766],
  61. [-0.1452, -0.4360, -0.5745, -0.4927, -0.3817],
  62. [0.1874, 0.2312, 0.5482, 0.6042, 0.5697],
  63. [0.0675, 0.0838, -0.1452, -0.2904, -0.3419],
  64. [0.2721, 0.0805, 0.0238, -0.0807, -0.0570],
  65. [0.2409, 0.3583, 0.1752, -0.0225, -0.3433],
  66. [0.3112, 0.4753, 0.4793, 0.3212, 0.0205]]]).astype(np.float32)
  67. compute_deltas_op = c_audio.ComputeDeltas()
  68. out_mindspore = compute_deltas_op(ndarr_in)
  69. allclose_nparray(out_mindspore, out_expect, 0.0001, 0.0001)
  70. def test_compute_deltas_pipeline():
  71. """
  72. Feature: test the basic function in pipeline mode.
  73. Description: mindspore pipeline mode normal testcase:compute_deltas op.
  74. Expectation: compile done without error.
  75. """
  76. logger.info("test ComputeDeltas op with default value")
  77. generator = gen([CHANNEL, FREQ, TIME])
  78. data1 = ds.GeneratorDataset(
  79. source=generator, column_names=["multi_dimensional_data"]
  80. )
  81. transforms = [c_audio.ComputeDeltas()]
  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. out_put = item["multi_dimensional_data"]
  85. assert out_put.shape == (CHANNEL, FREQ, TIME)
  86. def test_compute_deltas_invalid_input():
  87. """
  88. Feature: test the validate function with invalid parameters.
  89. Description: mindspore invalid parameters testcase:compute_deltas op.
  90. Expectation: compile done without error.
  91. """
  92. def test_invalid_input(test_name, win_length, pad_mode, error, error_msg):
  93. logger.info("Test ComputeDeltas with bad input: {0}".format(test_name))
  94. with pytest.raises(error) as error_info:
  95. c_audio.ComputeDeltas(win_length=win_length, pad_mode=pad_mode)
  96. assert error_msg in str(error_info.value)
  97. test_invalid_input(
  98. "invalid win_length parameter value", "test", BorderType.EDGE, TypeError,
  99. "Argument win_length with value test is not of type [<class 'int'>], but got <class 'str'>.",
  100. )
  101. test_invalid_input(
  102. "invalid win_length parameter value", 2, BorderType.EDGE, ValueError,
  103. "Input win_length is not within the required interval of [3, 2147483647]",
  104. )
  105. test_invalid_input(
  106. "invalid pad_mode parameter value", 5, 2, TypeError,
  107. "Argument pad_mode with value 2 is not of type [<enum 'BorderType'>], but got <class 'int'>.",
  108. )
  109. if __name__ == "__main__":
  110. test_compute_deltas_eager()
  111. test_compute_deltas_pipeline()
  112. test_compute_deltas_invalid_input()