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_mu_law_encoding.py 3.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 MuLawEncoding op in DE.
  17. """
  18. import numpy as np
  19. import mindspore.dataset as ds
  20. import mindspore.dataset.audio.transforms as audio
  21. from mindspore import log as logger
  22. def test_mu_law_encoding():
  23. """
  24. Feature: MuLawEncoding
  25. Description: test MuLawEncoding in pipeline mode
  26. Expectation: the data is processed successfully
  27. """
  28. logger.info("Test MuLawEncoding.")
  29. def gen():
  30. data = np.array([[0.1, 0.2, 0.3, 0.4]])
  31. yield (np.array(data, dtype=np.float32),)
  32. dataset = ds.GeneratorDataset(source=gen, column_names=["multi_dim_data"])
  33. dataset = dataset.map(operations=audio.MuLawEncoding(), input_columns=["multi_dim_data"])
  34. for i in dataset.create_dict_iterator(num_epochs=1, output_numpy=True):
  35. assert i["multi_dim_data"].shape == (1, 4)
  36. expected = np.array([[203, 218, 228, 234]])
  37. assert np.array_equal(i["multi_dim_data"], expected)
  38. logger.info("Finish testing MuLawEncoding.")
  39. def test_mu_law_encoding_eager():
  40. """
  41. Feature: MuLawEncoding
  42. Description: test MuLawEncoding in eager mode
  43. Expectation: the data is processed successfully
  44. """
  45. logger.info("Test MuLawEncoding callable.")
  46. input_t = np.array([[0.1, 0.2, 0.3, 0.4]])
  47. output_t = audio.MuLawEncoding(128)(input_t)
  48. assert output_t.shape == (1, 4)
  49. expected = np.array([[98, 106, 111, 115]])
  50. assert np.array_equal(output_t, expected)
  51. logger.info("Finish testing MuLawEncoding.")
  52. def test_mu_law_encoding_uncallable():
  53. """
  54. Feature: MuLawEncoding
  55. Description: test param check of MuLawEncoding
  56. Expectation: throw correct error and message
  57. """
  58. logger.info("Test MuLawEncoding not callable.")
  59. try:
  60. input_t = np.random.rand(2, 4)
  61. output_t = audio.MuLawEncoding(-3)(input_t)
  62. assert output_t.shape == (2, 4)
  63. except ValueError as e:
  64. assert 'Input quantization_channels is not within the required interval of [1, 2147483647].' in str(e)
  65. logger.info("Finish testing MuLawEncoding.")
  66. def test_mu_law_encoding_and_decoding():
  67. """
  68. Feature: MuLawEncoding and MuLawDecoding
  69. Description: test MuLawEncoding and MuLawDecoding in eager mode
  70. Expectation: the data is processed successfully
  71. """
  72. logger.info("Test MuLawEncoding and MuLawDecoding callable.")
  73. input_t = np.array([[98, 106, 111, 115]])
  74. output_decoding = audio.MuLawDecoding(128)(input_t)
  75. output_encoding = audio.MuLawEncoding(128)(output_decoding)
  76. assert np.array_equal(input_t, output_encoding)
  77. logger.info("Finish testing MuLawEncoding and MuLawDecoding callable.")
  78. if __name__ == "__main__":
  79. test_mu_law_encoding()
  80. test_mu_law_encoding_eager()
  81. test_mu_law_encoding_uncallable()
  82. test_mu_law_encoding_and_decoding()