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 3.2 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 mindspore.dataset as ds
  16. from mindspore import log as logger
  17. DATA_FILE = "../data/dataset/testTextFileDataset/1.txt"
  18. DATA_ALL_FILE = "../data/dataset/testTextFileDataset/*"
  19. def test_textline_dataset_one_file():
  20. data = ds.TextFileDataset(DATA_FILE)
  21. count = 0
  22. for i in data.create_dict_iterator():
  23. logger.info("{}".format(i["text"]))
  24. count += 1
  25. assert (count == 3)
  26. def test_textline_dataset_all_file():
  27. data = ds.TextFileDataset(DATA_ALL_FILE)
  28. count = 0
  29. for i in data.create_dict_iterator():
  30. logger.info("{}".format(i["text"]))
  31. count += 1
  32. assert (count == 5)
  33. def test_textline_dataset_totext():
  34. ds.config.set_num_parallel_workers(4)
  35. data = ds.TextFileDataset(DATA_ALL_FILE, shuffle=False)
  36. count = 0
  37. line = ["This is a text file.", "Another file.",
  38. "Be happy every day.", "End of file.", "Good luck to everyone."]
  39. for i in data.create_dict_iterator():
  40. str = i["text"].item().decode("utf8")
  41. assert (str == line[count])
  42. count += 1
  43. assert (count == 5)
  44. def test_textline_dataset_num_samples():
  45. data = ds.TextFileDataset(DATA_FILE, num_samples=2)
  46. count = 0
  47. for i in data.create_dict_iterator():
  48. count += 1
  49. assert (count == 2)
  50. def test_textline_dataset_distribution():
  51. data = ds.TextFileDataset(DATA_ALL_FILE, num_shards=2, shard_id=1)
  52. count = 0
  53. for i in data.create_dict_iterator():
  54. count += 1
  55. assert (count == 3)
  56. def test_textline_dataset_repeat():
  57. data = ds.TextFileDataset(DATA_FILE, shuffle=False)
  58. data = data.repeat(3)
  59. count = 0
  60. line = ["This is a text file.", "Be happy every day.", "Good luck to everyone.",
  61. "This is a text file.", "Be happy every day.", "Good luck to everyone.",
  62. "This is a text file.", "Be happy every day.", "Good luck to everyone."]
  63. for i in data.create_dict_iterator():
  64. str = i["text"].item().decode("utf8")
  65. assert (str == line[count])
  66. count += 1
  67. assert (count == 9)
  68. def test_textline_dataset_get_datasetsize():
  69. data = ds.TextFileDataset(DATA_FILE)
  70. size = data.get_dataset_size()
  71. assert (size == 3)
  72. if __name__ == "__main__":
  73. test_textline_dataset_one_file()
  74. test_textline_dataset_all_file()
  75. test_textline_dataset_totext()
  76. test_textline_dataset_num_samples()
  77. test_textline_dataset_distribution()
  78. test_textline_dataset_repeat()
  79. test_textline_dataset_get_datasetsize()