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_split.py 12 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. # test5trainimgs.json contains 5 images whose un-decoded shape is [83554, 54214, 65512, 54214, 64631]
  18. # the label of each image is [0,0,0,1,1] each image can be uniquely identified
  19. # via the following lookup table (dict){(83554, 0): 0, (54214, 0): 1, (54214, 1): 2, (65512, 0): 3, (64631, 1): 4}
  20. manifest_file = "../data/dataset/testManifestData/test5trainimgs.json"
  21. manifest_map = {(172876, 0): 0, (54214, 0): 1, (54214, 1): 2, (173673, 0): 3, (64631, 1): 4}
  22. def split_with_invalid_inputs(d):
  23. with pytest.raises(ValueError) as info:
  24. s1, s2 = d.split([])
  25. assert "sizes cannot be empty" in str(info.value)
  26. with pytest.raises(ValueError) as info:
  27. s1, s2 = d.split([5, 0.6])
  28. assert "sizes should be list of int or list of float" in str(info.value)
  29. with pytest.raises(ValueError) as info:
  30. s1, s2 = d.split([-1, 6])
  31. assert "there should be no negative numbers" in str(info.value)
  32. with pytest.raises(RuntimeError) as info:
  33. s1, s2 = d.split([3, 1])
  34. assert "sum of split sizes 4 is not equal to dataset size 5" in str(info.value)
  35. with pytest.raises(RuntimeError) as info:
  36. s1, s2 = d.split([5, 1])
  37. assert "sum of split sizes 6 is not equal to dataset size 5" in str(info.value)
  38. with pytest.raises(RuntimeError) as info:
  39. s1, s2 = d.split([0.15, 0.15, 0.15, 0.15, 0.15, 0.25])
  40. assert "sum of calculated split sizes 6 is not equal to dataset size 5" in str(info.value)
  41. with pytest.raises(ValueError) as info:
  42. s1, s2 = d.split([-0.5, 0.5])
  43. assert "there should be no numbers outside the range [0, 1]" in str(info.value)
  44. with pytest.raises(ValueError) as info:
  45. s1, s2 = d.split([1.5, 0.5])
  46. assert "there should be no numbers outside the range [0, 1]" in str(info.value)
  47. with pytest.raises(ValueError) as info:
  48. s1, s2 = d.split([0.5, 0.6])
  49. assert "percentages do not sum up to 1" in str(info.value)
  50. with pytest.raises(ValueError) as info:
  51. s1, s2 = d.split([0.3, 0.6])
  52. assert "percentages do not sum up to 1" in str(info.value)
  53. with pytest.raises(RuntimeError) as info:
  54. s1, s2 = d.split([0.05, 0.95])
  55. assert "percentage 0.05 is too small" in str(info.value)
  56. def test_unmappable_invalid_input():
  57. text_file_dataset_path = "../data/dataset/testTextFileDataset/*"
  58. d = ds.TextFileDataset(text_file_dataset_path)
  59. split_with_invalid_inputs(d)
  60. d = ds.TextFileDataset(text_file_dataset_path, num_shards=2, shard_id=0)
  61. with pytest.raises(RuntimeError) as info:
  62. s1, s2 = d.split([4, 1])
  63. assert "dataset should not be sharded before split" in str(info.value)
  64. def test_unmappable_split():
  65. text_file_dataset_path = "../data/dataset/testTextFileDataset/*"
  66. text_file_data = ["This is a text file.", "Another file.", "Be happy every day.",
  67. "End of file.", "Good luck to everyone."]
  68. ds.config.set_num_parallel_workers(4)
  69. d = ds.TextFileDataset(text_file_dataset_path, shuffle=False)
  70. s1, s2 = d.split([4, 1], randomize=False)
  71. s1_output = []
  72. for item in s1.create_dict_iterator():
  73. s1_output.append(item["text"].item().decode("utf8"))
  74. s2_output = []
  75. for item in s2.create_dict_iterator():
  76. s2_output.append(item["text"].item().decode("utf8"))
  77. assert s1_output == text_file_data[0:4]
  78. assert s2_output == text_file_data[4:]
  79. # exact percentages
  80. s1, s2 = d.split([0.8, 0.2], randomize=False)
  81. s1_output = []
  82. for item in s1.create_dict_iterator():
  83. s1_output.append(item["text"].item().decode("utf8"))
  84. s2_output = []
  85. for item in s2.create_dict_iterator():
  86. s2_output.append(item["text"].item().decode("utf8"))
  87. assert s1_output == text_file_data[0:4]
  88. assert s2_output == text_file_data[4:]
  89. # fuzzy percentages
  90. s1, s2 = d.split([0.33, 0.67], randomize=False)
  91. s1_output = []
  92. for item in s1.create_dict_iterator():
  93. s1_output.append(item["text"].item().decode("utf8"))
  94. s2_output = []
  95. for item in s2.create_dict_iterator():
  96. s2_output.append(item["text"].item().decode("utf8"))
  97. assert s1_output == text_file_data[0:2]
  98. assert s2_output == text_file_data[2:]
  99. def test_mappable_invalid_input():
  100. d = ds.ManifestDataset(manifest_file)
  101. split_with_invalid_inputs(d)
  102. d = ds.ManifestDataset(manifest_file, num_shards=2, shard_id=0)
  103. with pytest.raises(RuntimeError) as info:
  104. s1, s2 = d.split([4, 1])
  105. assert "dataset should not be sharded before split" in str(info.value)
  106. def test_mappable_split_general():
  107. d = ds.ManifestDataset(manifest_file, shuffle=False)
  108. d = d.take(5)
  109. # absolute rows
  110. s1, s2 = d.split([4, 1], randomize=False)
  111. s1_output = []
  112. for item in s1.create_dict_iterator():
  113. s1_output.append(manifest_map[(item["image"].shape[0], item["label"].item())])
  114. s2_output = []
  115. for item in s2.create_dict_iterator():
  116. s2_output.append(manifest_map[(item["image"].shape[0], item["label"].item())])
  117. assert s1_output == [0, 1, 2, 3]
  118. assert s2_output == [4]
  119. # exact percentages
  120. s1, s2 = d.split([0.8, 0.2], randomize=False)
  121. s1_output = []
  122. for item in s1.create_dict_iterator():
  123. s1_output.append(manifest_map[(item["image"].shape[0], item["label"].item())])
  124. s2_output = []
  125. for item in s2.create_dict_iterator():
  126. s2_output.append(manifest_map[(item["image"].shape[0], item["label"].item())])
  127. assert s1_output == [0, 1, 2, 3]
  128. assert s2_output == [4]
  129. # fuzzy percentages
  130. s1, s2 = d.split([0.33, 0.67], randomize=False)
  131. s1_output = []
  132. for item in s1.create_dict_iterator():
  133. s1_output.append(manifest_map[(item["image"].shape[0], item["label"].item())])
  134. s2_output = []
  135. for item in s2.create_dict_iterator():
  136. s2_output.append(manifest_map[(item["image"].shape[0], item["label"].item())])
  137. assert s1_output == [0, 1]
  138. assert s2_output == [2, 3, 4]
  139. def test_mappable_split_optimized():
  140. d = ds.ManifestDataset(manifest_file, shuffle=False)
  141. # absolute rows
  142. s1, s2 = d.split([4, 1], randomize=False)
  143. s1_output = []
  144. for item in s1.create_dict_iterator():
  145. s1_output.append(manifest_map[(item["image"].shape[0], item["label"].item())])
  146. s2_output = []
  147. for item in s2.create_dict_iterator():
  148. s2_output.append(manifest_map[(item["image"].shape[0], item["label"].item())])
  149. assert s1_output == [0, 1, 2, 3]
  150. assert s2_output == [4]
  151. # exact percentages
  152. s1, s2 = d.split([0.8, 0.2], randomize=False)
  153. s1_output = []
  154. for item in s1.create_dict_iterator():
  155. s1_output.append(manifest_map[(item["image"].shape[0], item["label"].item())])
  156. s2_output = []
  157. for item in s2.create_dict_iterator():
  158. s2_output.append(manifest_map[(item["image"].shape[0], item["label"].item())])
  159. assert s1_output == [0, 1, 2, 3]
  160. assert s2_output == [4]
  161. # fuzzy percentages
  162. s1, s2 = d.split([0.33, 0.67], randomize=False)
  163. s1_output = []
  164. for item in s1.create_dict_iterator():
  165. s1_output.append(manifest_map[(item["image"].shape[0], item["label"].item())])
  166. s2_output = []
  167. for item in s2.create_dict_iterator():
  168. s2_output.append(manifest_map[(item["image"].shape[0], item["label"].item())])
  169. assert s1_output == [0, 1]
  170. assert s2_output == [2, 3, 4]
  171. def test_mappable_randomize_deterministic():
  172. # set arbitrary seed for shard after split
  173. # the labels outputted by ManifestDataset for seed 53 is [0, 1, 3, 4]
  174. ds.config.set_seed(53)
  175. d = ds.ManifestDataset(manifest_file, shuffle=False)
  176. s1, s2 = d.split([0.8, 0.2])
  177. for _ in range(10):
  178. s1_output = []
  179. for item in s1.create_dict_iterator():
  180. s1_output.append(manifest_map[(item["image"].shape[0], item["label"].item())])
  181. s2_output = []
  182. for item in s2.create_dict_iterator():
  183. s2_output.append(manifest_map[(item["image"].shape[0], item["label"].item())])
  184. # note no overlap
  185. assert s1_output == [0, 1, 3, 4]
  186. assert s2_output == [2]
  187. def test_mappable_randomize_repeatable():
  188. # set arbitrary seed for shard after split
  189. # the labels outputted by ManifestDataset for seed 53 is [0, 1, 3, 4]
  190. ds.config.set_seed(53)
  191. d = ds.ManifestDataset(manifest_file, shuffle=False)
  192. s1, s2 = d.split([0.8, 0.2])
  193. num_epochs = 5
  194. s1 = s1.repeat(num_epochs)
  195. s2 = s2.repeat(num_epochs)
  196. s1_output = []
  197. for item in s1.create_dict_iterator():
  198. s1_output.append(manifest_map[(item["image"].shape[0], item["label"].item())])
  199. s2_output = []
  200. for item in s2.create_dict_iterator():
  201. s2_output.append(manifest_map[(item["image"].shape[0], item["label"].item())])
  202. # note no overlap
  203. assert s1_output == [0, 1, 3, 4] * num_epochs
  204. assert s2_output == [2] * num_epochs
  205. def test_mappable_sharding():
  206. # set arbitrary seed for repeatability for shard after split
  207. # the labels outputted by ManifestDataset for seed 53 is [0, 1, 3, 4]
  208. ds.config.set_seed(53)
  209. num_epochs = 5
  210. first_split_num_rows = 4
  211. d = ds.ManifestDataset(manifest_file, shuffle=False)
  212. s1, s2 = d.split([first_split_num_rows, 1])
  213. distributed_sampler = ds.DistributedSampler(2, 0)
  214. s1.use_sampler(distributed_sampler)
  215. s1 = s1.repeat(num_epochs)
  216. # testing sharding, second dataset to simulate another instance
  217. d2 = ds.ManifestDataset(manifest_file, shuffle=False)
  218. d2s1, d2s2 = d2.split([first_split_num_rows, 1])
  219. distributed_sampler = ds.DistributedSampler(2, 1)
  220. d2s1.use_sampler(distributed_sampler)
  221. d2s1 = d2s1.repeat(num_epochs)
  222. # shard 0
  223. s1_output = []
  224. for item in s1.create_dict_iterator():
  225. s1_output.append(manifest_map[(item["image"].shape[0], item["label"].item())])
  226. # shard 1
  227. d2s1_output = []
  228. for item in d2s1.create_dict_iterator():
  229. d2s1_output.append(manifest_map[(item["image"].shape[0], item["label"].item())])
  230. rows_per_shard_per_epoch = 2
  231. assert len(s1_output) == rows_per_shard_per_epoch * num_epochs
  232. assert len(d2s1_output) == rows_per_shard_per_epoch * num_epochs
  233. # verify each epoch that
  234. # 1. shards contain no common elements
  235. # 2. the data was split the same way, and that the union of shards equal the split
  236. correct_sorted_split_result = [0, 1, 3, 4]
  237. for i in range(num_epochs):
  238. combined_data = []
  239. for j in range(rows_per_shard_per_epoch):
  240. combined_data.append(s1_output[i * rows_per_shard_per_epoch + j])
  241. combined_data.append(d2s1_output[i * rows_per_shard_per_epoch + j])
  242. assert sorted(combined_data) == correct_sorted_split_result
  243. # test other split
  244. s2_output = []
  245. for item in s2.create_dict_iterator():
  246. s2_output.append(manifest_map[(item["image"].shape[0], item["label"].item())])
  247. d2s2_output = []
  248. for item in d2s2.create_dict_iterator():
  249. d2s2_output.append(manifest_map[(item["image"].shape[0], item["label"].item())])
  250. assert s2_output == [2]
  251. assert d2s2_output == [2]
  252. if __name__ == '__main__':
  253. test_unmappable_invalid_input()
  254. test_unmappable_split()
  255. test_mappable_invalid_input()
  256. test_mappable_split_general()
  257. test_mappable_split_optimized()
  258. test_mappable_randomize_deterministic()
  259. test_mappable_randomize_repeatable()
  260. test_mappable_sharding()