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_random_dataset.py 3.4 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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.common.dtype as mstype
  16. import mindspore.dataset as ds
  17. from mindspore import log as logger
  18. # just a basic test with parallel random data op
  19. def test_randomdataset_basic1():
  20. logger.info("Test randomdataset basic 1")
  21. schema = ds.Schema()
  22. schema.add_column('image', de_type=mstype.uint8, shape=[2])
  23. schema.add_column('label', de_type=mstype.uint8, shape=[1])
  24. # apply dataset operations
  25. ds1 = ds.RandomDataset(schema=schema, total_rows=50, num_parallel_workers=4)
  26. ds1 = ds1.repeat(4)
  27. num_iter = 0
  28. for data in ds1.create_dict_iterator(): # each data is a dictionary
  29. # in this example, each dictionary has keys "image" and "label"
  30. logger.info("{} image: {}".format(num_iter, data["image"]))
  31. logger.info("{} label: {}".format(num_iter, data["label"]))
  32. num_iter += 1
  33. logger.info("Number of data in ds1: {}".format(num_iter))
  34. assert num_iter == 200
  35. logger.info("Test randomdataset basic 1 complete")
  36. # Another simple test
  37. def test_randomdataset_basic2():
  38. logger.info("Test randomdataset basic 2")
  39. schema = ds.Schema()
  40. schema.add_column('image', de_type=mstype.uint8,
  41. shape=[640, 480, 3]) # 921600 bytes (a bit less than 1 MB per image)
  42. schema.add_column('label', de_type=mstype.uint8, shape=[1])
  43. # Make up 10 rows
  44. ds1 = ds.RandomDataset(schema=schema, total_rows=10, num_parallel_workers=1)
  45. ds1 = ds1.repeat(4)
  46. num_iter = 0
  47. for data in ds1.create_dict_iterator(): # each data is a dictionary
  48. # in this example, each dictionary has keys "image" and "label"
  49. # logger.info(data["image"])
  50. logger.info("printing the label: {}".format(data["label"]))
  51. num_iter += 1
  52. logger.info("Number of data in ds1: {}".format(num_iter))
  53. assert num_iter == 40
  54. logger.info("Test randomdataset basic 2 complete")
  55. # Another simple test
  56. def test_randomdataset_basic3():
  57. logger.info("Test randomdataset basic 3")
  58. # Make up 10 samples, but here even the schema is randomly created
  59. # The columns are named like this "c0", "c1", "c2" etc
  60. # But, we will use a tuple iterator instead of dict iterator so the column names
  61. # are not needed to iterate
  62. ds1 = ds.RandomDataset(total_rows=10, num_parallel_workers=1)
  63. ds1 = ds1.repeat(2)
  64. num_iter = 0
  65. for _ in ds1.create_tuple_iterator():
  66. num_iter += 1
  67. logger.info("Number of data in ds1: {}".format(num_iter))
  68. assert num_iter == 20
  69. logger.info("Test randomdataset basic 3 Complete")
  70. if __name__ == '__main__':
  71. test_randomdataset_basic1()
  72. test_randomdataset_basic2()
  73. test_randomdataset_basic3()