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_decoding.py 2.7 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 MuLawDecoding 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_decoding():
  23. """
  24. Test mu_law_decoding_op (pipeline).
  25. """
  26. logger.info("Test MuLawDecoding.")
  27. def gen():
  28. data = np.array([[10, 100, 70, 200]])
  29. yield (np.array(data, dtype=np.float32),)
  30. dataset = ds.GeneratorDataset(source=gen, column_names=["multi_dim_data"])
  31. dataset = dataset.map(operations=audio.MuLawDecoding(), input_columns=["multi_dim_data"])
  32. for i in dataset.create_dict_iterator(num_epochs=1, output_numpy=True):
  33. assert i["multi_dim_data"].shape == (1, 4)
  34. expected = np.array([[-0.6459359526634216, -0.009046762250363827, -0.04388953000307083, 0.08788024634122849]])
  35. assert np.array_equal(i["multi_dim_data"], expected)
  36. logger.info("Finish testing MuLawDecoding.")
  37. def test_mu_law_decoding_eager():
  38. """
  39. Test mu_law_decoding_op callable (eager).
  40. """
  41. logger.info("Test MuLawDecoding callable.")
  42. input_t = np.array([70, 170])
  43. output_t = audio.MuLawDecoding(128)(input_t)
  44. assert output_t.shape == (2,)
  45. excepted = np.array([0.00506480922922492, 26.928272247314453])
  46. assert np.array_equal(output_t, excepted)
  47. logger.info("Finish testing MuLawDecoding.")
  48. def test_mu_law_decoding_uncallable():
  49. """
  50. Test mu_law_decoding_op not callable.
  51. """
  52. logger.info("Test MuLawDecoding not callable.")
  53. try:
  54. input_t = np.random.rand(2, 4)
  55. output_t = audio.MuLawDecoding(-3)(input_t)
  56. assert output_t.shape == (2, 4)
  57. except ValueError as e:
  58. assert 'Input quantization_channels is not within the required interval of [1, 2147483647].' in str(e)
  59. logger.info("Finish testing MuLawDecoding.")
  60. if __name__ == "__main__":
  61. test_mu_law_decoding()
  62. test_mu_law_decoding_eager()
  63. test_mu_law_decoding_uncallable()