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_libri_tts.py 9.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 LibriTTS 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/testLibriTTSData"
  23. def test_libri_tts_basic():
  24. """
  25. Feature: LibriTTSDataset
  26. Description: test basic usage of LibriTTS
  27. Expectation: the dataset is as expected
  28. """
  29. logger.info("Test LibriTTSDataset Op")
  30. # case 1: test loading fault dataset.
  31. data1 = ds.LibriTTSDataset(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.LibriTTSDataset(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.LibriTTSDataset(DATA_DIR, usage="all", 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.LibriTTSDataset(DATA_DIR, usage="train-clean-100", 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.LibriTTSDataset(DATA_DIR, usage="train-clean-100", 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_libri_tts_distribute_sampler():
  65. """
  66. Feature: LibriTTSDataset
  67. Description: test LibriTTS dataset with DisributeSampler
  68. Expectation: the results are as expected
  69. """
  70. logger.info("Test LibriTTS with sharding")
  71. list1, list2 = [], []
  72. num_shards = 3
  73. shard_id = 0
  74. data1 = ds.LibriTTSDataset(DATA_DIR, usage="all", num_shards=num_shards, shard_id=shard_id)
  75. count = 0
  76. for item1 in data1.create_dict_iterator(output_numpy=True, num_epochs=1):
  77. list1.append(item1["original_text"])
  78. count = count + 1
  79. assert count == 1
  80. num_shards = 3
  81. shard_id = 0
  82. sampler = ds.DistributedSampler(num_shards, shard_id)
  83. data2 = ds.LibriTTSDataset(DATA_DIR, usage="train-clean-100", sampler=sampler)
  84. count = 0
  85. for item2 in data2.create_dict_iterator(output_numpy=True, num_epochs=1):
  86. list2.append(item2["original_text"])
  87. count = count + 1
  88. assert count == 1
  89. def test_libri_tts_exception():
  90. """
  91. Feature: LibriTTSDataset
  92. Description: test error cases for LibriTTSDataset
  93. Expectation: the results are as expected
  94. """
  95. logger.info("Test error cases for LibriTTSDataset")
  96. error_msg_1 = "sampler and shuffle cannot be specified at the same time"
  97. with pytest.raises(RuntimeError, match=error_msg_1):
  98. ds.LibriTTSDataset(DATA_DIR, shuffle=False, sampler=ds.PKSampler(3))
  99. error_msg_2 = "sampler and sharding cannot be specified at the same time"
  100. with pytest.raises(RuntimeError, match=error_msg_2):
  101. ds.LibriTTSDataset(DATA_DIR, sampler=ds.PKSampler(3), num_shards=2, shard_id=0)
  102. error_msg_3 = "num_shards is specified and currently requires shard_id as well"
  103. with pytest.raises(RuntimeError, match=error_msg_3):
  104. ds.LibriTTSDataset(DATA_DIR, num_shards=10)
  105. error_msg_4 = "shard_id is specified but num_shards is not"
  106. with pytest.raises(RuntimeError, match=error_msg_4):
  107. ds.LibriTTSDataset(DATA_DIR, shard_id=0)
  108. error_msg_5 = "Input shard_id is not within the required interval"
  109. with pytest.raises(ValueError, match=error_msg_5):
  110. ds.LibriTTSDataset(DATA_DIR, num_shards=5, shard_id=-1)
  111. with pytest.raises(ValueError, match=error_msg_5):
  112. ds.LibriTTSDataset(DATA_DIR, num_shards=5, shard_id=5)
  113. with pytest.raises(ValueError, match=error_msg_5):
  114. ds.LibriTTSDataset(DATA_DIR, num_shards=2, shard_id=5)
  115. error_msg_6 = "num_parallel_workers exceeds"
  116. with pytest.raises(ValueError, match=error_msg_6):
  117. ds.LibriTTSDataset(DATA_DIR, shuffle=False, num_parallel_workers=0)
  118. with pytest.raises(ValueError, match=error_msg_6):
  119. ds.LibriTTSDataset(DATA_DIR, shuffle=False, num_parallel_workers=256)
  120. with pytest.raises(ValueError, match=error_msg_6):
  121. ds.LibriTTSDataset(DATA_DIR, shuffle=False, num_parallel_workers=-2)
  122. error_msg_7 = "Argument shard_id"
  123. with pytest.raises(TypeError, match=error_msg_7):
  124. ds.LibriTTSDataset(DATA_DIR, num_shards=2, shard_id="0")
  125. def exception_func(item):
  126. raise Exception("Error occur!")
  127. error_msg_8 = "The corresponding data files"
  128. with pytest.raises(RuntimeError, match=error_msg_8):
  129. data = ds.LibriTTSDataset(DATA_DIR)
  130. data = data.map(operations=exception_func, input_columns=["waveform"], num_parallel_workers=1)
  131. for _ in data.create_dict_iterator(output_numpy=True, num_epochs=1):
  132. pass
  133. def test_libri_tts_sequential_sampler():
  134. """
  135. Feature: LibriTTSDataset
  136. Description: test LibriTTSDataset with SequentialSampler
  137. Expectation: the results are as expected
  138. """
  139. logger.info("Test LibriTTSDataset Op with SequentialSampler")
  140. num_samples = 2
  141. sampler = ds.SequentialSampler(num_samples=num_samples)
  142. data1 = ds.LibriTTSDataset(DATA_DIR, usage="train-clean-100", sampler=sampler)
  143. data2 = ds.LibriTTSDataset(DATA_DIR, usage="train-clean-100", shuffle=False, num_samples=num_samples)
  144. list1, list2 = [], []
  145. list_expected = [24000, b'good morning', b'Good morning', 2506, 11267, b'2506_11267_000001_000000',
  146. 24000, b'good afternoon', b'Good afternoon', 2506, 11267, b'2506_11267_000002_000000']
  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. list1.append(item1["sample_rate"])
  151. list2.append(item2["sample_rate"])
  152. list1.append(item1["original_text"])
  153. list2.append(item2["original_text"])
  154. list1.append(item1["normalized_text"])
  155. list2.append(item2["normalized_text"])
  156. list1.append(item1["speaker_id"])
  157. list2.append(item2["speaker_id"])
  158. list1.append(item1["chapter_id"])
  159. list2.append(item2["chapter_id"])
  160. list1.append(item1["utterance_id"])
  161. list2.append(item2["utterance_id"])
  162. num_iter += 1
  163. np.testing.assert_array_equal(list1, list_expected)
  164. np.testing.assert_array_equal(list2, list_expected)
  165. assert num_iter == num_samples
  166. def test_libri_tts_usage():
  167. """
  168. Feature: LibriTTSDataset
  169. Description: test LibriTTSDataset usage
  170. Expectation: the results are as expected
  171. """
  172. logger.info("Test LibriTTSDataset usage")
  173. def test_config(usage, libri_tts_path=None):
  174. libri_tts_path = DATA_DIR if libri_tts_path is None else libri_tts_path
  175. try:
  176. data = ds.LibriTTSDataset(libri_tts_path, usage=usage, shuffle=False)
  177. num_rows = 0
  178. for _ in data.create_dict_iterator(num_epochs=1, output_numpy=True):
  179. num_rows += 1
  180. except (ValueError, TypeError, RuntimeError) as e:
  181. return str(e)
  182. return num_rows
  183. assert test_config("all") == 3
  184. assert test_config("train-clean-100") == 3
  185. assert "Input usage is not within the valid set of ['dev-clean', 'dev-other', 'test-clean', 'test-other', " \
  186. "'train-clean-100', 'train-clean-360', 'train-other-500', 'all']." in test_config("invalid")
  187. assert "Argument usage with value ['list'] is not of type [<class 'str'>]" in test_config(["list"])
  188. all_files_path = None
  189. if all_files_path is not None:
  190. assert test_config("train-clean-100", all_files_path) == 3
  191. assert ds.LibriTTSDataset(all_files_path, usage="train-clean-100").get_dataset_size() == 3
  192. assert test_config("all", all_files_path) == 3
  193. assert ds.LibriTTSDataset(all_files_path, usage="all").get_dataset_size() == 3
  194. if __name__ == '__main__':
  195. test_libri_tts_basic()
  196. test_libri_tts_distribute_sampler()
  197. test_libri_tts_exception()
  198. test_libri_tts_sequential_sampler()
  199. test_libri_tts_usage()