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_complex_norm.py 2.6 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 ComplexNorm op in DE.
  17. """
  18. import numpy as np
  19. from numpy import random
  20. import mindspore.dataset as ds
  21. import mindspore.dataset.audio.transforms as audio
  22. from mindspore import log as logger
  23. def test_complex_norm():
  24. """
  25. Test complex_norm (pipeline).
  26. """
  27. logger.info("Test ComplexNorm.")
  28. def gen():
  29. data = np.array([[1.0, 1.0], [2.0, 3.0], [4.0, 4.0]])
  30. yield (np.array(data, dtype=np.float32),)
  31. dataset = ds.GeneratorDataset(source=gen, column_names=["multi_dim_data"])
  32. dataset = dataset.map(operations=audio.ComplexNorm(2), input_columns=["multi_dim_data"])
  33. for i in dataset.create_dict_iterator(num_epochs=1, output_numpy=True):
  34. assert i["multi_dim_data"].shape == (3,)
  35. expected = np.array([2., 13., 32.])
  36. assert np.array_equal(i["multi_dim_data"], expected)
  37. logger.info("Finish testing ComplexNorm.")
  38. def test_complex_norm_eager():
  39. """
  40. Test complex_norm callable (eager).
  41. """
  42. logger.info("Test ComplexNorm callable.")
  43. input_t = np.array([[1.0, 1.0], [2.0, 3.0], [4.0, 4.0]])
  44. output_t = audio.ComplexNorm()(input_t)
  45. assert output_t.shape == (3,)
  46. expected = np.array([1.4142135623730951, 3.605551275463989, 5.656854249492381])
  47. assert np.array_equal(output_t, expected)
  48. logger.info("Finish testing ComplexNorm.")
  49. def test_complex_norm_uncallable():
  50. """
  51. Test complex_norm_op not callable.
  52. """
  53. logger.info("Test ComplexNorm not callable.")
  54. try:
  55. input_t = random.rand(2, 4, 3, 2)
  56. output_t = audio.ComplexNorm(-3.)(input_t)
  57. assert output_t.shape == (2, 4, 3)
  58. except ValueError as e:
  59. assert 'Input power is not within the required interval of [0, 16777216].' in str(e)
  60. logger.info("Finish testing ComplexNorm.")
  61. if __name__ == "__main__":
  62. test_complex_norm()
  63. test_complex_norm_eager()
  64. test_complex_norm_uncallable()