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_sampler.py 4.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. # 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. def test_sequential_sampler(print_res=False):
  21. manifest_file = "../data/dataset/testManifestData/test5trainimgs.json"
  22. map = {(172876, 0): 0, (54214, 0): 1, (54214, 1): 2, (173673, 0): 3, (64631, 1): 4}
  23. def test_config(num_samples, num_repeats=None):
  24. sampler = ds.SequentialSampler()
  25. data1 = ds.ManifestDataset(manifest_file, num_samples=num_samples, sampler=sampler)
  26. if num_repeats is not None:
  27. data1 = data1.repeat(num_repeats)
  28. res = []
  29. for item in data1.create_dict_iterator():
  30. logger.info("item[image].shape[0]: {}, item[label].item(): {}"
  31. .format(item["image"].shape[0], item["label"].item()))
  32. res.append(map[(item["image"].shape[0], item["label"].item())])
  33. if print_res:
  34. logger.info("image.shapes and labels: {}".format(res))
  35. return res
  36. assert test_config(num_samples=3, num_repeats=None) == [0, 1, 2]
  37. assert test_config(num_samples=None, num_repeats=2) == [0, 1, 2, 3, 4] * 2
  38. assert test_config(num_samples=4, num_repeats=2) == [0, 1, 2, 3] * 2
  39. def test_random_sampler(print_res=False):
  40. manifest_file = "../data/dataset/testManifestData/test5trainimgs.json"
  41. map = {(172876, 0): 0, (54214, 0): 1, (54214, 1): 2, (173673, 0): 3, (64631, 1): 4}
  42. def test_config(replacement, num_samples, num_repeats):
  43. sampler = ds.RandomSampler(replacement=replacement, num_samples=num_samples)
  44. data1 = ds.ManifestDataset(manifest_file, sampler=sampler)
  45. data1 = data1.repeat(num_repeats)
  46. res = []
  47. for item in data1.create_dict_iterator():
  48. res.append(map[(item["image"].shape[0], item["label"].item())])
  49. if print_res:
  50. logger.info("image.shapes and labels: {}".format(res))
  51. return res
  52. # this tests that each epoch COULD return different samples than the previous epoch
  53. assert len(set(test_config(replacement=False, num_samples=2, num_repeats=6))) > 2
  54. # the following two tests test replacement works
  55. ordered_res = [0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4]
  56. assert sorted(test_config(replacement=False, num_samples=None, num_repeats=4)) == ordered_res
  57. assert sorted(test_config(replacement=True, num_samples=None, num_repeats=4)) != ordered_res
  58. def test_random_sampler_multi_iter(print_res=False):
  59. manifest_file = "../data/dataset/testManifestData/test5trainimgs.json"
  60. map = {(172876, 0): 0, (54214, 0): 1, (54214, 1): 2, (173673, 0): 3, (64631, 1): 4}
  61. def test_config(replacement, num_samples, num_repeats, validate):
  62. sampler = ds.RandomSampler(replacement=replacement, num_samples=num_samples)
  63. data1 = ds.ManifestDataset(manifest_file, sampler=sampler)
  64. while num_repeats > 0:
  65. res = []
  66. for item in data1.create_dict_iterator():
  67. res.append(map[(item["image"].shape[0], item["label"].item())])
  68. if print_res:
  69. logger.info("image.shapes and labels: {}".format(res))
  70. if validate != sorted(res):
  71. break
  72. num_repeats -= 1
  73. assert num_repeats > 0
  74. test_config(replacement=True, num_samples=5, num_repeats=5, validate=[0, 1, 2, 3, 4, 5])
  75. if __name__ == '__main__':
  76. test_sequential_sampler(True)
  77. test_random_sampler(True)
  78. test_random_sampler_multi_iter(True)