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_textfileop.py 8.6 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. # Copyright 2020 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. import pytest
  16. import mindspore.dataset as ds
  17. from mindspore import log as logger
  18. from util import config_get_set_num_parallel_workers, config_get_set_seed
  19. DATA_FILE = "../data/dataset/testTextFileDataset/1.txt"
  20. DATA_ALL_FILE = "../data/dataset/testTextFileDataset/*"
  21. def test_textline_dataset_one_file():
  22. data = ds.TextFileDataset(DATA_FILE)
  23. count = 0
  24. for i in data.create_dict_iterator(num_epochs=1, output_numpy=True):
  25. logger.info("{}".format(i["text"]))
  26. count += 1
  27. assert count == 3
  28. def test_textline_dataset_all_file():
  29. data = ds.TextFileDataset(DATA_ALL_FILE)
  30. count = 0
  31. for i in data.create_dict_iterator(num_epochs=1, output_numpy=True):
  32. logger.info("{}".format(i["text"]))
  33. count += 1
  34. assert count == 5
  35. def test_textline_dataset_num_samples_zero():
  36. data = ds.TextFileDataset(DATA_FILE, num_samples=0)
  37. count = 0
  38. for i in data.create_dict_iterator(num_epochs=1, output_numpy=True):
  39. logger.info("{}".format(i["text"]))
  40. count += 1
  41. assert count == 3
  42. def test_textline_dataset_shuffle_false4():
  43. original_num_parallel_workers = config_get_set_num_parallel_workers(4)
  44. original_seed = config_get_set_seed(987)
  45. data = ds.TextFileDataset(DATA_ALL_FILE, shuffle=False)
  46. count = 0
  47. line = ["This is a text file.", "Another file.",
  48. "Be happy every day.", "End of file.", "Good luck to everyone."]
  49. for i in data.create_dict_iterator(num_epochs=1, output_numpy=True):
  50. strs = i["text"].item().decode("utf8")
  51. assert strs == line[count]
  52. count += 1
  53. assert count == 5
  54. # Restore configuration
  55. ds.config.set_num_parallel_workers(original_num_parallel_workers)
  56. ds.config.set_seed(original_seed)
  57. def test_textline_dataset_shuffle_false1():
  58. original_num_parallel_workers = config_get_set_num_parallel_workers(1)
  59. original_seed = config_get_set_seed(987)
  60. data = ds.TextFileDataset(DATA_ALL_FILE, shuffle=False)
  61. count = 0
  62. line = ["This is a text file.", "Be happy every day.", "Good luck to everyone.",
  63. "Another file.", "End of file."]
  64. for i in data.create_dict_iterator(num_epochs=1, output_numpy=True):
  65. strs = i["text"].item().decode("utf8")
  66. assert strs == line[count]
  67. count += 1
  68. assert count == 5
  69. # Restore configuration
  70. ds.config.set_num_parallel_workers(original_num_parallel_workers)
  71. ds.config.set_seed(original_seed)
  72. def test_textline_dataset_shuffle_files4():
  73. original_num_parallel_workers = config_get_set_num_parallel_workers(4)
  74. original_seed = config_get_set_seed(135)
  75. data = ds.TextFileDataset(DATA_ALL_FILE, shuffle=ds.Shuffle.FILES)
  76. count = 0
  77. line = ["This is a text file.", "Another file.",
  78. "Be happy every day.", "End of file.", "Good luck to everyone."]
  79. for i in data.create_dict_iterator(num_epochs=1, output_numpy=True):
  80. strs = i["text"].item().decode("utf8")
  81. assert strs == line[count]
  82. count += 1
  83. assert count == 5
  84. # Restore configuration
  85. ds.config.set_num_parallel_workers(original_num_parallel_workers)
  86. ds.config.set_seed(original_seed)
  87. def test_textline_dataset_shuffle_files1():
  88. original_num_parallel_workers = config_get_set_num_parallel_workers(1)
  89. original_seed = config_get_set_seed(135)
  90. data = ds.TextFileDataset(DATA_ALL_FILE, shuffle=ds.Shuffle.FILES)
  91. count = 0
  92. line = ["This is a text file.", "Be happy every day.", "Good luck to everyone.",
  93. "Another file.", "End of file."]
  94. for i in data.create_dict_iterator(num_epochs=1, output_numpy=True):
  95. strs = i["text"].item().decode("utf8")
  96. assert strs == line[count]
  97. count += 1
  98. assert count == 5
  99. # Restore configuration
  100. ds.config.set_num_parallel_workers(original_num_parallel_workers)
  101. ds.config.set_seed(original_seed)
  102. def test_textline_dataset_shuffle_global4():
  103. original_num_parallel_workers = config_get_set_num_parallel_workers(4)
  104. original_seed = config_get_set_seed(246)
  105. data = ds.TextFileDataset(DATA_ALL_FILE, shuffle=ds.Shuffle.GLOBAL)
  106. count = 0
  107. line = ["Another file.", "Good luck to everyone.", "End of file.",
  108. "This is a text file.", "Be happy every day."]
  109. for i in data.create_dict_iterator(num_epochs=1, output_numpy=True):
  110. strs = i["text"].item().decode("utf8")
  111. assert strs == line[count]
  112. count += 1
  113. assert count == 5
  114. # Restore configuration
  115. ds.config.set_num_parallel_workers(original_num_parallel_workers)
  116. ds.config.set_seed(original_seed)
  117. def test_textline_dataset_shuffle_global1():
  118. original_num_parallel_workers = config_get_set_num_parallel_workers(1)
  119. original_seed = config_get_set_seed(246)
  120. data = ds.TextFileDataset(DATA_ALL_FILE, shuffle=ds.Shuffle.GLOBAL)
  121. count = 0
  122. line = ["Another file.", "Good luck to everyone.", "This is a text file.",
  123. "End of file.", "Be happy every day."]
  124. for i in data.create_dict_iterator(num_epochs=1, output_numpy=True):
  125. strs = i["text"].item().decode("utf8")
  126. assert strs == line[count]
  127. count += 1
  128. assert count == 5
  129. # Restore configuration
  130. ds.config.set_num_parallel_workers(original_num_parallel_workers)
  131. ds.config.set_seed(original_seed)
  132. def test_textline_dataset_num_samples():
  133. data = ds.TextFileDataset(DATA_FILE, num_samples=2)
  134. count = 0
  135. for _ in data.create_dict_iterator(num_epochs=1, output_numpy=True):
  136. count += 1
  137. assert count == 2
  138. def test_textline_dataset_distribution():
  139. data = ds.TextFileDataset(DATA_ALL_FILE, num_shards=2, shard_id=1)
  140. count = 0
  141. for _ in data.create_dict_iterator(num_epochs=1, output_numpy=True):
  142. count += 1
  143. assert count == 3
  144. def test_textline_dataset_repeat():
  145. data = ds.TextFileDataset(DATA_FILE, shuffle=False)
  146. data = data.repeat(3)
  147. count = 0
  148. line = ["This is a text file.", "Be happy every day.", "Good luck to everyone.",
  149. "This is a text file.", "Be happy every day.", "Good luck to everyone.",
  150. "This is a text file.", "Be happy every day.", "Good luck to everyone."]
  151. for i in data.create_dict_iterator(num_epochs=1, output_numpy=True):
  152. strs = i["text"].item().decode("utf8")
  153. assert strs == line[count]
  154. count += 1
  155. assert count == 9
  156. def test_textline_dataset_get_datasetsize():
  157. data = ds.TextFileDataset(DATA_FILE)
  158. size = data.get_dataset_size()
  159. assert size == 3
  160. def test_textline_dataset_to_device():
  161. data = ds.TextFileDataset(DATA_FILE, shuffle=False)
  162. data = data.to_device()
  163. data.send()
  164. def test_textline_dataset_exceptions():
  165. with pytest.raises(ValueError) as error_info:
  166. _ = ds.TextFileDataset(DATA_FILE, num_samples=-1)
  167. assert "Input num_samples is not within the required interval" in str(error_info.value)
  168. with pytest.raises(ValueError) as error_info:
  169. _ = ds.TextFileDataset("does/not/exist/no.txt")
  170. assert "The following patterns did not match any files" in str(error_info.value)
  171. with pytest.raises(ValueError) as error_info:
  172. _ = ds.TextFileDataset("")
  173. assert "The following patterns did not match any files" in str(error_info.value)
  174. if __name__ == "__main__":
  175. test_textline_dataset_one_file()
  176. test_textline_dataset_all_file()
  177. test_textline_dataset_num_samples_zero()
  178. test_textline_dataset_shuffle_false4()
  179. test_textline_dataset_shuffle_false1()
  180. test_textline_dataset_shuffle_files4()
  181. test_textline_dataset_shuffle_files1()
  182. test_textline_dataset_shuffle_global4()
  183. test_textline_dataset_shuffle_global1()
  184. test_textline_dataset_num_samples()
  185. test_textline_dataset_distribution()
  186. test_textline_dataset_repeat()
  187. test_textline_dataset_get_datasetsize()
  188. test_textline_dataset_to_device()
  189. test_textline_dataset_exceptions()