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_sharding.py 7.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. # Copyright 2019 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. def test_imagefolder_shardings(print_res=False):
  18. image_folder_dir = "../data/dataset/testPK/data"
  19. def sharding_config(num_shards, shard_id, num_samples, shuffle, class_index, repeat_cnt=1):
  20. data1 = ds.ImageFolderDatasetV2(image_folder_dir, num_samples=num_samples, num_shards=num_shards,
  21. shard_id=shard_id,
  22. shuffle=shuffle, class_indexing=class_index, decode=True)
  23. data1 = data1.repeat(repeat_cnt)
  24. res = []
  25. for item in data1.create_dict_iterator(): # each data is a dictionary
  26. res.append(item["label"].item())
  27. if (print_res):
  28. logger.info("labels of dataset: {}".format(res))
  29. return res
  30. # total 44 rows in dataset
  31. assert (sharding_config(4, 0, 5, False, dict()) == [0, 0, 0, 1, 1]) # 5 rows
  32. assert (sharding_config(4, 0, 12, False, dict()) == [0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3]) # 11 rows
  33. assert (sharding_config(4, 3, None, False, dict()) == [0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3]) # 11 rows
  34. # total 22 in dataset rows because of class indexing which takes only 2 folders
  35. assert (len(sharding_config(4, 0, None, True, {"class1": 111, "class2": 999})) == 6)
  36. assert (len(sharding_config(4, 2, 3, True, {"class1": 111, "class2": 999})) == 3)
  37. # test with repeat
  38. assert (sharding_config(4, 0, 12, False, dict(), 3) == [0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3] * 3)
  39. assert (sharding_config(4, 0, 5, False, dict(), 5) == [0, 0, 0, 1, 1] * 5)
  40. assert (len(sharding_config(5, 1, None, True, {"class1": 111, "class2": 999}, 4)) == 20)
  41. def test_manifest_shardings(print_res=False):
  42. manifest_file = "../data/dataset/testManifestData/test5trainimgs.json"
  43. def sharding_config(num_shards, shard_id, num_samples, shuffle, repeat_cnt=1):
  44. data1 = ds.ManifestDataset(manifest_file, num_samples=num_samples, num_shards=num_shards, shard_id=shard_id,
  45. shuffle=shuffle, decode=True)
  46. data1 = data1.repeat(repeat_cnt)
  47. res = []
  48. for item in data1.create_dict_iterator(): # each data is a dictionary
  49. res.append(item["label"].item())
  50. if print_res:
  51. logger.info("labels of dataset: {}".format(res))
  52. return res
  53. # 5 train images in total
  54. sharding_config(2, 0, None, False)
  55. assert (sharding_config(2, 0, None, False) == [0, 1, 1])
  56. assert (sharding_config(2, 1, None, False) == [0, 0, 0])
  57. assert (sharding_config(2, 0, 2, False) == [0, 1])
  58. assert (sharding_config(2, 1, 2, False) == [0, 0])
  59. # with repeat
  60. assert (sharding_config(2, 1, None, False, 3) == [0, 0, 0] * 3)
  61. assert (sharding_config(2, 0, 2, False, 5) == [0, 1] * 5)
  62. def test_voc_shardings(print_res=False):
  63. voc_dir = "../data/dataset/testVOC2012"
  64. def sharding_config(num_shards, shard_id, num_samples, shuffle, repeat_cnt=1):
  65. sampler = ds.DistributedSampler(num_shards, shard_id, shuffle=shuffle)
  66. data1 = ds.VOCDataset(voc_dir, decode=True, sampler=sampler, num_samples=num_samples)
  67. data1 = data1.repeat(repeat_cnt)
  68. res = []
  69. for item in data1.create_dict_iterator(): # each data is a dictionary
  70. res.append(item["image"].shape[0])
  71. if print_res:
  72. logger.info("labels of dataset: {}".format(res))
  73. return res
  74. # 10 images in total, always decode to get the shape
  75. # first dim of all 10 images [2268,2268,2268,2268,642,607,561,596,612,2268]
  76. # 3 shard_workers, 0th worker will get 0-th, 3nd, 6th and 9th image
  77. assert (sharding_config(3, 0, None, False, 2) == [2268, 2268, 561, 2268] * 2)
  78. # 3 shard_workers, 1st worker will get 1-st, 4nd, 7th and 0th image, the last one goes back bc of rounding up
  79. assert (sharding_config(3, 1, 5, False, 3) == [2268, 642, 596, 2268] * 3)
  80. # 3 shard_workers, 2nd worker will get 2nd, 5th, 8th and 11th (which is 1st)
  81. # then takes the first 2 bc num_samples = 2
  82. assert (sharding_config(3, 2, 2, False, 4) == [2268, 607] * 4)
  83. # test that each epoch, each shard_worker returns a different sample
  84. assert (len(sharding_config(2, 0, None, True, 1)) == 5)
  85. assert (len(set(sharding_config(11, 0, None, True, 10))) > 1)
  86. def test_cifar10_shardings(print_res=False):
  87. cifar10_dir = "../data/dataset/testCifar10Data"
  88. def sharding_config(num_shards, shard_id, num_samples, shuffle, repeat_cnt=1):
  89. data1 = ds.Cifar10Dataset(cifar10_dir, num_shards=num_shards, shard_id=shard_id, num_samples=num_samples,
  90. shuffle=shuffle)
  91. data1 = data1.repeat(repeat_cnt)
  92. res = []
  93. for item in data1.create_dict_iterator(): # each data is a dictionary
  94. res.append(item["label"].item())
  95. if print_res:
  96. logger.info("labels of dataset: {}".format(res))
  97. return res
  98. # 60000 rows in total. CIFAR reads everything in memory which would make each test case very slow
  99. # therefore, only 2 test cases for now.
  100. assert (sharding_config(10000, 9999, 7, False, 1) == [9])
  101. assert (sharding_config(10000, 0, 4, False, 3) == [0, 0, 0])
  102. def test_cifar100_shardings(print_res=False):
  103. cifar100_dir = "../data/dataset/testCifar100Data"
  104. def sharding_config(num_shards, shard_id, num_samples, shuffle, repeat_cnt=1):
  105. data1 = ds.Cifar100Dataset(cifar100_dir, num_shards=num_shards, shard_id=shard_id, num_samples=num_samples,
  106. shuffle=shuffle)
  107. data1 = data1.repeat(repeat_cnt)
  108. res = []
  109. for item in data1.create_dict_iterator(): # each data is a dictionary
  110. res.append(item["coarse_label"].item())
  111. if print_res:
  112. logger.info("labels of dataset: {}".format(res))
  113. return res
  114. # 10000 rows in total in test.bin CIFAR100 file
  115. assert (sharding_config(1000, 999, 7, False, 2) == [1, 18, 10, 17, 5, 0, 15] * 2)
  116. assert (sharding_config(1000, 0, None, False) == [10, 16, 2, 11, 10, 17, 11, 14, 13, 3])
  117. def test_mnist_shardings(print_res=False):
  118. mnist_dir = "../data/dataset/testMnistData"
  119. def sharding_config(num_shards, shard_id, num_samples, shuffle, repeat_cnt=1):
  120. data1 = ds.MnistDataset(mnist_dir, num_shards=num_shards, shard_id=shard_id, num_samples=num_samples,
  121. shuffle=shuffle)
  122. data1 = data1.repeat(repeat_cnt)
  123. res = []
  124. for item in data1.create_dict_iterator(): # each data is a dictionary
  125. res.append(item["label"].item())
  126. if print_res:
  127. logger.info("labels of dataset: {}".format(res))
  128. return res
  129. # 70K rows in total , divide across 10K hosts, each host has 7 images
  130. assert sharding_config(10000, 0, num_samples=5, shuffle=False, repeat_cnt=3) == [0, 0, 0]
  131. assert sharding_config(10000, 9999, num_samples=None, shuffle=False, repeat_cnt=1) == [9]
  132. if __name__ == '__main__':
  133. test_imagefolder_shardings(True)
  134. test_manifest_shardings(True)
  135. test_voc_shardings(True)
  136. test_cifar10_shardings(True)
  137. test_cifar100_shardings(True)
  138. test_mnist_shardings(True)