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_datasets_cmu_arctic.py 9.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. # Copyright 2022 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 foNtest_resr the specific language governing permissions and
  13. # limitations under the License.
  14. # ==============================================================================
  15. """
  16. Test CMUArctic dataset operators
  17. """
  18. import numpy as np
  19. import pytest
  20. import mindspore.dataset as ds
  21. from mindspore import log as logger
  22. DATA_DIR = "../data/dataset/testCMUArcticData"
  23. def test_cmu_arctic_basic():
  24. """
  25. Feature: CMUArcticDataset
  26. Description: test basic name of CMUArctic
  27. Expectation: the dataset is as expected
  28. """
  29. logger.info("Test CMUArcticDataset Op")
  30. # case 1: test loading fault dataset.
  31. data1 = ds.CMUArcticDataset(DATA_DIR)
  32. num_iter1 = 0
  33. for _ in data1.create_dict_iterator(output_numpy=True, num_epochs=1):
  34. num_iter1 += 1
  35. assert num_iter1 == 3
  36. # case 2: test num_samples.
  37. data2 = ds.CMUArcticDataset(DATA_DIR, num_samples=1)
  38. num_iter2 = 0
  39. for _ in data2.create_dict_iterator(output_numpy=True, num_epochs=1):
  40. num_iter2 += 1
  41. assert num_iter2 == 1
  42. # case 3: test repeat.
  43. data3 = ds.CMUArcticDataset(DATA_DIR, name="aew", num_samples=3)
  44. data3 = data3.repeat(3)
  45. num_iter3 = 0
  46. for _ in data3.create_dict_iterator(output_numpy=True, num_epochs=1):
  47. num_iter3 += 1
  48. assert num_iter3 == 9
  49. # case 4: test batch with drop_remainder=False.
  50. data4 = ds.CMUArcticDataset(DATA_DIR, name="aew", num_samples=3)
  51. assert data4.get_dataset_size() == 3
  52. assert data4.get_batch_size() == 1
  53. data4 = data4.batch(batch_size=2) # drop_remainder is default to be False.
  54. assert data4.get_dataset_size() == 2
  55. assert data4.get_batch_size() == 2
  56. # case 5: test batch with drop_remainder=True.
  57. data5 = ds.CMUArcticDataset(DATA_DIR, name="aew", num_samples=3)
  58. assert data5.get_dataset_size() == 3
  59. assert data5.get_batch_size() == 1
  60. # the rest of incomplete batch will be dropped.
  61. data5 = data5.batch(batch_size=2, drop_remainder=True)
  62. assert data5.get_dataset_size() == 1
  63. assert data5.get_batch_size() == 2
  64. def test_cmu_arctic_distribute_sampler():
  65. """
  66. Feature: CMUArcticDataset
  67. Description: test CMUArctic dataset with DistributedSampler
  68. Expectation: the results are as expected
  69. """
  70. logger.info("Test CMUArctic with sharding")
  71. num_shards = 3
  72. shard_id = 0
  73. data1 = ds.CMUArcticDataset(DATA_DIR, name="aew", num_shards=num_shards, shard_id=shard_id)
  74. count = 0
  75. for _ in data1.create_dict_iterator(output_numpy=True, num_epochs=1):
  76. count = count + 1
  77. assert count == 1
  78. num_shards = 3
  79. shard_id = 0
  80. sampler = ds.DistributedSampler(num_shards, shard_id)
  81. data2 = ds.CMUArcticDataset(DATA_DIR, name="aew", sampler=sampler)
  82. count = 0
  83. for _ in data2.create_dict_iterator(output_numpy=True, num_epochs=1):
  84. count = count + 1
  85. assert count == 1
  86. def test_cmu_arctic_exception():
  87. """
  88. Feature: CMUArcticDataset
  89. Description: test error cases for CMUArcticDataset
  90. Expectation: the results are as expected
  91. """
  92. logger.info("Test error cases for CMUArcticDataset")
  93. error_msg_1 = "sampler and shuffle cannot be specified at the same time"
  94. with pytest.raises(RuntimeError, match=error_msg_1):
  95. ds.CMUArcticDataset(DATA_DIR, shuffle=False, sampler=ds.PKSampler(3))
  96. error_msg_2 = "sampler and sharding cannot be specified at the same time"
  97. with pytest.raises(RuntimeError, match=error_msg_2):
  98. ds.CMUArcticDataset(DATA_DIR, sampler=ds.PKSampler(3), num_shards=2, shard_id=0)
  99. error_msg_3 = "num_shards is specified and currently requires shard_id as well"
  100. with pytest.raises(RuntimeError, match=error_msg_3):
  101. ds.CMUArcticDataset(DATA_DIR, num_shards=10)
  102. error_msg_4 = "shard_id is specified but num_shards is not"
  103. with pytest.raises(RuntimeError, match=error_msg_4):
  104. ds.CMUArcticDataset(DATA_DIR, shard_id=0)
  105. error_msg_5 = "Input shard_id is not within the required interval"
  106. with pytest.raises(ValueError, match=error_msg_5):
  107. ds.CMUArcticDataset(DATA_DIR, num_shards=5, shard_id=-1)
  108. with pytest.raises(ValueError, match=error_msg_5):
  109. ds.CMUArcticDataset(DATA_DIR, num_shards=5, shard_id=5)
  110. with pytest.raises(ValueError, match=error_msg_5):
  111. ds.CMUArcticDataset(DATA_DIR, num_shards=2, shard_id=5)
  112. error_msg_6 = "num_parallel_workers exceeds"
  113. with pytest.raises(ValueError, match=error_msg_6):
  114. ds.CMUArcticDataset(DATA_DIR, shuffle=False, num_parallel_workers=0)
  115. with pytest.raises(ValueError, match=error_msg_6):
  116. ds.CMUArcticDataset(DATA_DIR, shuffle=False, num_parallel_workers=256)
  117. with pytest.raises(ValueError, match=error_msg_6):
  118. ds.CMUArcticDataset(DATA_DIR, shuffle=False, num_parallel_workers=-2)
  119. error_msg_7 = "Argument shard_id"
  120. with pytest.raises(TypeError, match=error_msg_7):
  121. ds.CMUArcticDataset(DATA_DIR, num_shards=2, shard_id="0")
  122. def exception_func(item):
  123. raise Exception("Error occur!")
  124. error_msg_8 = "The corresponding data files"
  125. with pytest.raises(RuntimeError, match=error_msg_8):
  126. data = ds.CMUArcticDataset(DATA_DIR)
  127. data = data.map(operations=exception_func, input_columns=["waveform"], num_parallel_workers=1)
  128. for _ in data.create_dict_iterator(output_numpy=True, num_epochs=1):
  129. pass
  130. def test_cmu_arctic_sequential_sampler():
  131. """
  132. Feature: CMUArcticDataset
  133. Description: test CMUArcticDataset with SequentialSampler
  134. Expectation: the results are as expected
  135. """
  136. logger.info("Test CMUArcticDataset Op with SequentialSampler")
  137. num_samples = 2
  138. sampler = ds.SequentialSampler(num_samples=num_samples)
  139. data1 = ds.CMUArcticDataset(DATA_DIR, name="aew", sampler=sampler)
  140. data2 = ds.CMUArcticDataset(DATA_DIR, name="aew", shuffle=False, num_samples=num_samples)
  141. utterance_id_expected = [b'a0001', b'a0002']
  142. utterance_id_list1, utterance_id_list2 = [], []
  143. sample_rate_expected = [16000, 16000]
  144. sample_rate_list1, sample_rate_list2 = [], []
  145. transcript_expected = [b'Dog.', b'Cat.']
  146. transcript_list1, transcript_list2 = [], []
  147. num_iter = 0
  148. for item1, item2 in zip(data1.create_dict_iterator(output_numpy=True, num_epochs=1),
  149. data2.create_dict_iterator(output_numpy=True, num_epochs=1)):
  150. transcript_list1.append(item1["transcript"])
  151. transcript_list2.append(item2["transcript"])
  152. sample_rate_list1.append(item1["sample_rate"])
  153. sample_rate_list2.append(item2["sample_rate"])
  154. utterance_id_list1.append(item1["utterance_id"])
  155. utterance_id_list2.append(item2["utterance_id"])
  156. num_iter += 1
  157. np.testing.assert_array_equal(transcript_list1, transcript_expected)
  158. np.testing.assert_array_equal(transcript_list2, transcript_expected)
  159. np.testing.assert_array_equal(utterance_id_list1, utterance_id_expected)
  160. np.testing.assert_array_equal(utterance_id_list2, utterance_id_expected)
  161. np.testing.assert_array_equal(sample_rate_list1, sample_rate_expected)
  162. np.testing.assert_array_equal(sample_rate_list2, sample_rate_expected)
  163. assert num_iter == num_samples
  164. def test_cmu_arctic_name():
  165. """
  166. Feature: CMUArcticDataset
  167. Description: test CMUArcticDataset name
  168. Expectation: the results are as expected
  169. """
  170. logger.info("Test CMUArcticDataset name")
  171. def test_config(name, cmu_arctic_path=None):
  172. cmu_arctic_path = DATA_DIR if cmu_arctic_path is None else cmu_arctic_path
  173. try:
  174. data = ds.CMUArcticDataset(cmu_arctic_path, name=name, shuffle=False)
  175. num_rows = 0
  176. for _ in data.create_dict_iterator(num_epochs=1, output_numpy=True):
  177. num_rows += 1
  178. except (ValueError, TypeError, RuntimeError) as e:
  179. return str(e)
  180. return num_rows
  181. assert test_config("aew") == 3
  182. assert "Input name is not within the valid set of ['aew', 'ahw', 'aup', 'awb', 'axb', 'bdl', 'clb', 'eey', "\
  183. "'fem', 'gka', 'jmk', 'ksp', 'ljm', 'lnh', 'rms', 'rxr', 'slp', 'slt']." in test_config("invalid")
  184. assert "Argument name with value ['list'] is not of type [<class 'str'>]" in test_config(["list"])
  185. all_files_path = None
  186. if all_files_path is not None:
  187. assert test_config("aew", all_files_path) == 3
  188. assert ds.cmu_arcticDataset(all_files_path, name="aew").get_dataset_size() == 3
  189. if __name__ == '__main__':
  190. test_cmu_arctic_basic()
  191. test_cmu_arctic_distribute_sampler()
  192. test_cmu_arctic_exception()
  193. test_cmu_arctic_sequential_sampler()
  194. test_cmu_arctic_name()